예제 #1
0
 private boolean handlePost(
     HttpServletRequest request, HttpServletResponse response, IFileStore dir)
     throws JSONException, CoreException, ServletException, IOException {
   // setup and precondition checks
   JSONObject requestObject = OrionServlet.readJSONRequest(request);
   String name = computeName(request, requestObject);
   if (name.length() == 0)
     return statusHandler.handleRequest(
         request,
         response,
         new ServerStatus(
             IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "File name not specified.", null));
   int options = getCreateOptions(request);
   IFileStore toCreate = dir.getChild(name);
   boolean destinationExists = toCreate.fetchInfo().exists();
   if (!validateOptions(request, response, toCreate, destinationExists, options)) return true;
   // perform the operation
   if (performPost(request, response, requestObject, toCreate, options)) {
     // write the response
     URI location = URIUtil.append(getURI(request), name);
     JSONObject result = ServletFileStoreHandler.toJSON(toCreate, toCreate.fetchInfo(), location);
     OrionServlet.writeJSONResponse(request, response, result);
     response.setHeader(ProtocolConstants.HEADER_LOCATION, location.toString());
     // response code should indicate if a new resource was actually created or not
     response.setStatus(
         destinationExists ? HttpServletResponse.SC_OK : HttpServletResponse.SC_CREATED);
   }
   return true;
 }
예제 #2
0
 /**
  * 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;
 }
예제 #3
0
 private boolean handlePut(
     HttpServletRequest request, HttpServletResponse response, IFileStore dir)
     throws JSONException, IOException, CoreException {
   IFileInfo info = ServletFileStoreHandler.fromJSON(request);
   dir.putInfo(info, EFS.NONE, null);
   return true;
 }
예제 #4
0
 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);
   }
 }
예제 #5
0
 private boolean handleGet(
     HttpServletRequest request, HttpServletResponse response, IFileStore dir)
     throws IOException, CoreException {
   URI location = getURI(request);
   JSONObject result = ServletFileStoreHandler.toJSON(dir, dir.fetchInfo(), location);
   String depthString = request.getParameter(ProtocolConstants.PARM_DEPTH);
   int depth = 0;
   if (depthString != null) {
     try {
       depth = Integer.parseInt(depthString);
     } catch (NumberFormatException e) {
       // ignore
     }
   }
   encodeChildren(dir, location, result, depth);
   OrionServlet.writeJSONResponse(request, response, result);
   return true;
 }
예제 #6
0
 private boolean handleDelete(
     HttpServletRequest request, HttpServletResponse response, IFileStore dir)
     throws JSONException, CoreException, ServletException, IOException {
   dir.delete(EFS.NONE, null);
   return true;
 }
예제 #7
0
 /**
  * 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;
 }