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;
/**
}
- /* (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 {
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);
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);
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.
*