* @return <code>true</code> if the operation was successful, and <code>false</code> otherwise.
*/
private boolean performCopyMove(HttpServletRequest request, HttpServletResponse response, JSONObject requestObject, IFileStore toCreate, boolean isCopy, int options) throws ServletException, CoreException {
String locationString = requestObject.optString(ProtocolConstants.KEY_LOCATION, null);
if (locationString == null) {
statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "Copy or move request must specify source location", null));
return false;
}
try {
IFileStore source = resolveSourceLocation(request, locationString);
if (source == null) {
statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, NLS.bind("Source does not exist: ", locationString), null));
return false;
} else if (source.isParentOf(toCreate)) {
statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "The destination cannot be a descendent of the source location", null));
return false;
}
boolean allowOverwrite = (options & CREATE_NO_OVERWRITE) == 0;
int efsOptions = allowOverwrite ? EFS.OVERWRITE : EFS.NONE;
try {
if (isCopy) {
source.copy(toCreate, efsOptions, null);
} else {
source.move(toCreate, efsOptions, null);
// Path format is /file/workspaceId/projectId/[location to folder]
Path path = new Path(locationString);
if (path.segmentCount() == 3 && path.segment(0).equals("file")) {
// The folder is a project, remove the metadata
OrionConfiguration.getMetaStore().deleteProject(path.segment(1), source.getName());
}
}
} catch (CoreException e) {
if (!source.fetchInfo(EFS.NONE, null).exists()) {
statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, NLS.bind("Source does not exist: ", locationString), e));
return false;
}
if (e.getStatus().getCode() == EFS.ERROR_EXISTS) {
statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_PRECONDITION_FAILED, "A file or folder with the same name already exists at this location", null));
return false;
}
//just rethrow if we can't do something more specific
throw e;
}
} catch (URISyntaxException e) {
statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, NLS.bind("Bad source location in request: ", locationString), e));
return false;
}
return true;
}