Use exponential backoff when updating the password or last login time in WebDAV.
[pithos] / src / gr / ebs / gss / server / rest / FilesHandler.java
index 9a60841..8e841c9 100644 (file)
@@ -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<FileHeaderDTO> deletedFiles = new ArrayList<FileHeaderDTO>();
-               try{
-                       deletedFiles = getService().getDeletedFiles(file.getOwner().getId());
-               }
-               catch(ObjectNotFoundException e){
-
-               } catch (RpcException e) {
-
-               }
-               List<FileHeaderDTO> filesInSameFolder = new ArrayList<FileHeaderDTO>();
-               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<PermissionDTO> perms = null;
+                               if (permissions != null)
+                                       perms = parsePermissions(user, permissions);
+                               if (!name.isEmpty() || permissions != null) {
+                                       final String fName = name.isEmpty()? null: name;
+                                       final Set<PermissionDTO> fPerms = perms;
                                        FolderDTO folderUpdated = new TransactionHelper<FolderDTO>().tryExecute(new Callable<FolderDTO>() {
                                                @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<PermissionDTO> perms = parsePermissions(user, permissions);
-                                       new TransactionHelper<Object>().tryExecute(new Callable<Object>() {
-                                               @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;