public static Result storeFile() throws Throwable {
MultipartFormData body = request().body().asMultipartFormData();
if (body==null) return badRequest("missing data: is the body multipart/form-data? Check if it contains boundaries too! " );
//FilePart file = body.getFile(FILE_FIELD_NAME);
List<FilePart> files = body.getFiles();
FilePart file = null;
if (!files.isEmpty()) file = files.get(0);
String ret="";
if (file!=null){
Map<String, String[]> data=body.asFormUrlEncoded();
String[] datas=data.get(DATA_FIELD_NAME);
String[] acl=data.get(ACL_FIELD_NAME);
/*extract attachedData */
String dataJson=null;
if (datas!=null && datas.length>0){
dataJson = datas[0];
}else dataJson="{}";
/*extract acl*/
/*the acl json must have the following format:
* {
* "read" : {
* "users":[],
* "roles":[]
* }
* "update" : .......
* }
*/
String aclJsonString=null;
if (acl!=null && datas.length>0){
aclJsonString = acl[0];
ObjectMapper mapper = new ObjectMapper();
JsonNode aclJson=null;
try{
aclJson = mapper.readTree(aclJsonString);
}catch(JsonProcessingException e){
return status(CustomHttpCode.ACL_JSON_FIELD_MALFORMED.getBbCode(),"The 'acl' field is malformed");
}
/*check if the roles and users are valid*/
Iterator<Entry<String, JsonNode>> it = aclJson.fields();
while (it.hasNext()){
//check for permission read/update/delete/all
Entry<String, JsonNode> next = it.next();
if (!PermissionsHelper.permissionsFromString.containsKey(next.getKey())){
return status(CustomHttpCode.ACL_PERMISSION_UNKNOWN.getBbCode(),"The key '"+next.getKey()+"' is invalid. Valid ones are 'read','update','delete','all'");
}
//check for users/roles
Iterator<Entry<String, JsonNode>> it2 = next.getValue().fields();
while (it2.hasNext()){
Entry<String, JsonNode> next2 = it2.next();
if (!next2.getKey().equals("users") && !next2.getKey().equals("roles")) {
return status(CustomHttpCode.ACL_USER_OR_ROLE_KEY_UNKNOWN.getBbCode(),"The key '"+next2.getKey()+"' is invalid. Valid ones are 'users' or 'roles'");
}
//check for the existance of users/roles
JsonNode arrNode = next2.getValue();
if (arrNode.isArray()) {
for (final JsonNode objNode : arrNode) {
//checks the existance users and/or roles
if (next2.getKey().equals("users") && !UserService.exists(objNode.asText())) return status(CustomHttpCode.ACL_USER_DOES_NOT_EXIST.getBbCode(),"The user " + objNode.asText() + " does not exists");
if (next2.getKey().equals("roles") && !RoleService.exists(objNode.asText())) return status(CustomHttpCode.ACL_ROLE_DOES_NOT_EXIST.getBbCode(),"The role " + objNode.asText() + " does not exists");
}
}else return status(CustomHttpCode.JSON_VALUE_MUST_BE_ARRAY.getBbCode(),"The '"+next2.getKey()+"' value must be an array");
}
}
}else aclJsonString="{}";
java.io.File fileContent=file.getFile();
String fileName = file.getFilename();
/*String contentType = file.getContentType();
if (contentType==null || contentType.isEmpty() || contentType.equalsIgnoreCase("application/octet-stream")){ //try to guess the content type
InputStream is = new BufferedInputStream(new FileInputStream(fileContent));
contentType = URLConnection.guessContentTypeFromStream(is);
if (contentType==null || contentType.isEmpty()) contentType="application/octet-stream";