Implement a REST API method to restore an old version of a file: a POST request on...
authorpastith <devnull@localhost>
Thu, 12 Mar 2009 11:15:17 +0000 (11:15 +0000)
committerpastith <devnull@localhost>
Thu, 12 Mar 2009 11:15:17 +0000 (11:15 +0000)
gss/src/gr/ebs/gss/server/ejb/ExternalAPI.java
gss/src/gr/ebs/gss/server/ejb/ExternalAPIBean.java
gss/src/gr/ebs/gss/server/rest/FilesHandler.java

index 99b6f8b..fc29366 100644 (file)
@@ -1077,19 +1077,19 @@ public interface ExternalAPI {
                        throws ObjectNotFoundException, InsufficientPermissionsException;
 
        /**
-        * Restore file version identified by bodyId
+        * Restore the file contents to the specified version.
         *
         * @param userId the ID of the user
         * @param fileId the ID of the file
-        * @param bodyId the ID of the body
-        *
-        * @throws ObjectNotFoundException
-        * @throws InsufficientPermissionsException
-        * @throws GSSIOException
-        * @throws QuotaExceededException
-        *
+        * @param version the version number of the desired file contents
+        * @throws ObjectNotFoundException if the user or file was not
+        *                      found, with     the exception message mentioning the precise problem
+        * @throws InsufficientPermissionsException if the user does not have the
+        *          appropriate privileges
+        * @throws QuotaExceededException if the user quota limit would be exceeded
+        * @throws GSSIOException if there was an error while accessing the file contents
         */
-       public void restoreVersion(Long userId, Long fileId, Long bodyId)
+       public void restoreVersion(Long userId, Long fileId, int version)
                        throws ObjectNotFoundException, InsufficientPermissionsException,  GSSIOException, QuotaExceededException;
 
        /**
index 2f337c4..9b8b67d 100644 (file)
@@ -1913,22 +1913,17 @@ public class ExternalAPIBean implements ExternalAPI, ExternalAPIRemote {
 
        }
 
-       /* (non-Javadoc)
-        * @see gr.ebs.gss.server.ejb.ExternalAPI#restoreVersion(java.lang.Long, java.lang.Long, java.lang.Long)
-        */
        @Override
-       public void restoreVersion(Long userId, Long fileId, Long bodyId) throws ObjectNotFoundException, InsufficientPermissionsException,  GSSIOException, QuotaExceededException {
+       public void restoreVersion(Long userId, Long fileId, int version) throws ObjectNotFoundException, InsufficientPermissionsException,  GSSIOException, QuotaExceededException {
                if (userId == null)
                        throw new ObjectNotFoundException("No user specified");
                if (fileId == null)
                        throw new ObjectNotFoundException("No file specified");
-               if (bodyId == null)
-                       throw new ObjectNotFoundException("No body specified");
                User user = dao.getEntityById(User.class, userId);
                FileHeader header = dao.getEntityById(FileHeader.class, fileId);
                if(!header.hasWritePermission(user))
                        throw new InsufficientPermissionsException("You don't have the necessary permissions");
-               FileBody body = dao.getEntityById(FileBody.class, bodyId);
+               FileBody body = dao.getFileVersion(fileId, version);
                final File fileContents = new File(body.getStoredFilePath());
 
                try {
index ae303be..1fc4e7e 100644 (file)
@@ -112,6 +112,11 @@ public class FilesHandler extends RequestHandler {
        private static final String PROGRESS_PARAMETER = "progress";
 
        /**
+        * The request parameter name for restoring a previous version of a file.
+        */
+       private static final String RESTORE_VERSION_PARAMETER = "restoreVersion";
+
+       /**
         * The logger.
         */
        private static Log logger = LogFactory.getLog(FilesHandler.class);
@@ -557,6 +562,7 @@ public class FilesHandler extends RequestHandler {
        boolean hasRestoreParam = req.getParameterMap().containsKey(RESOURCE_RESTORE_PARAMETER);
        String copyTo = req.getParameter(RESOURCE_COPY_PARAMETER);
        String moveTo = req.getParameter(RESOURCE_MOVE_PARAMETER);
+       String restoreVersion = req.getParameter(RESTORE_VERSION_PARAMETER);
 
        if (newName != null)
                        createFolder(req, resp, path, newName);
@@ -570,11 +576,59 @@ public class FilesHandler extends RequestHandler {
                        copyResource(req, resp, path, copyTo);
                else if (moveTo != null)
                        moveResource(req, resp, path, moveTo);
+               else if (restoreVersion != null)
+                       restoreVersion(req, resp, path, restoreVersion);
                else
                        resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
        }
 
        /**
+        * Restores a previous version for a file.
+        *
+        * @param req the HTTP request
+        * @param resp the HTTP response
+        * @param path the resource path
+        * @param version the version number to restore
+        * @throws IOException if an I/O error occurs
+        */
+       private void restoreVersion(HttpServletRequest req, HttpServletResponse resp, String path, String version) throws IOException {
+               User user = getUser(req);
+               User owner = getOwner(req);
+               Object resource = null;
+               try {
+                       resource = getService().getResourceAtPath(owner.getId(), path, true);
+               } catch (ObjectNotFoundException e) {
+                       resp.sendError(HttpServletResponse.SC_NOT_FOUND, path);
+                       return;
+               } catch (RpcException e) {
+                       resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, path);
+                       return;
+               }
+               if (resource instanceof FolderDTO) {
+                       resp.sendError(HttpServletResponse.SC_CONFLICT);
+                       return;
+               }
+
+               try {
+                       FileHeaderDTO file = (FileHeaderDTO) resource;
+                       int oldVersion = Integer.parseInt(version);
+                       getService().restoreVersion(user.getId(), file.getId(), oldVersion);
+               } catch (InsufficientPermissionsException e) {
+                       resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
+               } catch (ObjectNotFoundException e) {
+                       resp.sendError(HttpServletResponse.SC_NOT_FOUND, e.getMessage());
+               } catch (RpcException e) {
+                       resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, path);
+               } catch (GSSIOException e) {
+                       resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
+               } catch (QuotaExceededException e) {
+                       resp.sendError(HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE, e.getMessage());
+               } catch (NumberFormatException e) {
+                       resp.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
+               }
+       }
+
+       /**
         * A method for handling multipart POST requests for uploading
         * files from browser-based JavaScript clients.
         *