/** * Computes the name of the resource to be created by a POST operation. Returns an empty string if * the name was not specified. */ private String computeName(HttpServletRequest request, JSONObject requestObject) { // get the slug first String name = request.getHeader(ProtocolConstants.HEADER_SLUG); // If the requestObject has a name then it must be used due to UTF-8 issues with names Bug // 376671 if (requestObject.has("Name")) { try { name = requestObject.getString("Name"); } catch (JSONException e) { } } // next comes the source location for a copy/move if (name == null || name.length() == 0) { String location = requestObject.optString(ProtocolConstants.KEY_LOCATION); int lastSlash = location.lastIndexOf('/'); if (lastSlash >= 0) name = location.substring(lastSlash + 1); } // finally use the name attribute from the request body if (name == null || name.length() == 0) name = requestObject.optString(ProtocolConstants.KEY_NAME); return name; }
/** * Performs the actual modification corresponding to a POST request. All preconditions are assumed * to be satisfied. * * @return <code>true</code> if the operation was successful, and <code>false</code> otherwise. */ private boolean performPost( HttpServletRequest request, HttpServletResponse response, JSONObject requestObject, IFileStore toCreate, int options) throws CoreException, IOException, ServletException { boolean isCopy = (options & CREATE_COPY) != 0; boolean isMove = (options & CREATE_MOVE) != 0; if (isCopy || isMove) return performCopyMove(request, response, requestObject, toCreate, isCopy, options); if (requestObject.optBoolean(ProtocolConstants.KEY_DIRECTORY)) toCreate.mkdir(EFS.NONE, null); else toCreate.openOutputStream(EFS.NONE, null).close(); return true; }
private void encodeChildren(IFileStore dir, URI location, JSONObject result, int depth) throws CoreException { if (depth <= 0) return; JSONArray children = new JSONArray(); IFileStore[] childStores = dir.childStores(EFS.NONE, null); for (IFileStore childStore : childStores) { IFileInfo childInfo = childStore.fetchInfo(); String name = childInfo.getName(); if (childInfo.isDirectory()) name += "/"; // $NON-NLS-1$ URI childLocation = URIUtil.append(location, name); JSONObject childResult = ServletFileStoreHandler.toJSON(childStore, childInfo, childLocation); if (childInfo.isDirectory()) encodeChildren(childStore, childLocation, childResult, depth - 1); children.put(childResult); } try { result.put(ProtocolConstants.KEY_CHILDREN, children); } catch (JSONException e) { // cannot happen throw new RuntimeException(e); } }
/** * Perform a copy or move as specified by the request. * * @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; } 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); } catch (CoreException e) { if (!source.fetchInfo().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; }