X-Git-Url: https://code.grnet.gr/git/pithos/blobdiff_plain/d74c027051076c7ee3b10588db3169dca1b9b8f8..872e1390a7d5a4dc283ae72686e0166c7c1bfd24:/src/gr/ebs/gss/server/rest/FilesHandler.java diff --git a/src/gr/ebs/gss/server/rest/FilesHandler.java b/src/gr/ebs/gss/server/rest/FilesHandler.java index 9a60841..8e841c9 100644 --- a/src/gr/ebs/gss/server/rest/FilesHandler.java +++ b/src/gr/ebs/gss/server/rest/FilesHandler.java @@ -339,10 +339,13 @@ public class FilesHandler extends RequestHandler { // satisfied. Doing this for folders would require recursive checking // for all of their children, which in turn would defy the purpose of // the optimization. - if (folder == null) + if (folder == null){ // Checking If headers. if (!checkIfHeaders(req, resp, file, oldBody)) return; + } + else if(!checkIfModifiedSince(req, resp, folder)) + return; // Find content type. String contentType = null; @@ -447,9 +450,9 @@ public class FilesHandler extends RequestHandler { try { if(file != null) if (needsContentDisposition(req)) - resp.setHeader("Content-Disposition","attachment; filename*=UTF-8''"+URLEncoder.encode(file.getName(),"UTF-8")); + resp.setHeader("Content-Disposition","attachment; filename*=UTF-8''"+getDispositionFilename(file)); else - resp.setHeader("Content-Disposition","inline; filename*=UTF-8''"+URLEncoder.encode(file.getName(),"UTF-8")); + resp.setHeader("Content-Disposition","inline; filename*=UTF-8''"+getDispositionFilename(file)); if (ostream != null) copy(file, renderResult, ostream, req, oldBody); else @@ -544,6 +547,14 @@ public class FilesHandler extends RequestHandler { } /** + * Return the filename of the specified file properly formatted for + * including in the Content-Disposition header. + */ + private String getDispositionFilename(FileHeaderDTO file) throws UnsupportedEncodingException { + return URLEncoder.encode(file.getName(),"UTF-8").replaceAll("\\+", "%20"); + } + + /** * Determines whether the user agent needs the Content-Disposition * header to be set, in order to properly download a file. * @@ -936,41 +947,6 @@ public class FilesHandler extends RequestHandler { } } - private String getBackupFilename(FileHeaderDTO file, String filename){ - List deletedFiles = new ArrayList(); - try{ - deletedFiles = getService().getDeletedFiles(file.getOwner().getId()); - } - catch(ObjectNotFoundException e){ - - } catch (RpcException e) { - - } - List filesInSameFolder = new ArrayList(); - for(FileHeaderDTO deleted : deletedFiles) - if(deleted.getFolder().getId().equals(file.getFolder().getId())) - filesInSameFolder.add(deleted); - int i=1; - String filenameToCheck = filename; - for(FileHeaderDTO same : filesInSameFolder) - if(same.getName().startsWith(filename)){ - String toCheck=same.getName().substring(filename.length(),same.getName().length()); - if(toCheck.startsWith(" ")){ - int test =-1; - try{ - test = Integer.valueOf(toCheck.replace(" ","")); - } - catch(NumberFormatException e){ - //do nothing since string is not a number - } - if(test>=i) - i = test+1; - } - } - - return filename+" "+i; - } - /** * Move the resource in the specified path to the specified destination. * @@ -1342,41 +1318,28 @@ public class FilesHandler extends RequestHandler { if (resource instanceof FolderDTO) { final FolderDTO folder = (FolderDTO) resource; String name = json.optString("name"); - if (!name.isEmpty()){ + if (!name.isEmpty()) try { name = URLDecoder.decode(name, "UTF-8"); } catch (IllegalArgumentException e) { resp.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage()); return; } - final String fName = name; + JSONArray permissions = json.optJSONArray("permissions"); + Set perms = null; + if (permissions != null) + perms = parsePermissions(user, permissions); + if (!name.isEmpty() || permissions != null) { + final String fName = name.isEmpty()? null: name; + final Set fPerms = perms; FolderDTO folderUpdated = new TransactionHelper().tryExecute(new Callable() { @Override public FolderDTO call() throws Exception { - return getService().modifyFolder(user.getId(), folder.getId(), fName); - } - - }); - String parentUrl =URLDecoder.decode(getContextPath(req, true),"UTF-8"); - String fpath = URLDecoder.decode(req.getPathInfo(), "UTF-8"); - parentUrl = parentUrl.replaceAll(fpath, ""); - if(!parentUrl.endsWith("/")) - parentUrl = parentUrl+"/"; - parentUrl = parentUrl+folderUpdated.getOwner().getUsername()+PATH_FILES+folderUpdated.getPath(); - resp.getWriter().println(parentUrl); - } - - JSONArray permissions = json.optJSONArray("permissions"); - if (permissions != null) { - final Set perms = parsePermissions(user, permissions); - new TransactionHelper().tryExecute(new Callable() { - @Override - public Object call() throws Exception { - getService().setFolderPermissions(user.getId(), folder.getId(), perms); - return null; + return getService().updateFolder(user.getId(), folder.getId(), fName, fPerms); } }); + resp.getWriter().println(getNewUrl(req, folderUpdated)); } } else { final FileHeaderDTO file = (FileHeaderDTO) resource; @@ -1442,6 +1405,20 @@ public class FilesHandler extends RequestHandler { } /** + * Returns the new URL of an updated folder. + */ + private String getNewUrl(HttpServletRequest req, FolderDTO folder) throws UnsupportedEncodingException { + String parentUrl =URLDecoder.decode(getContextPath(req, true),"UTF-8"); + String fpath = URLDecoder.decode(req.getPathInfo(), "UTF-8"); + if (parentUrl.indexOf(fpath) != -1) + parentUrl = parentUrl.substring(0, parentUrl.indexOf(fpath)); + if(!parentUrl.endsWith("/")) + parentUrl = parentUrl+"/"; + parentUrl = parentUrl+folder.getOwner().getUsername()+PATH_FILES+folder.getPath(); + return parentUrl; + } + + /** * Helper method to convert a JSON array of permissions into a set of * PermissionDTO objects. * @@ -2040,8 +2017,6 @@ public class FilesHandler extends RequestHandler { private long fileSize = -100; - private long tenKBRead = -1; - private Long userId; private String filename;