Exemplo n.º 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;
 }
Exemplo n.º 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;
 }
Exemplo n.º 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;
 }
Exemplo n.º 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);
   }
 }
Exemplo n.º 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;
 }
Exemplo n.º 6
0
 private boolean handleDelete(
     HttpServletRequest request, HttpServletResponse response, IFileStore dir)
     throws JSONException, CoreException, ServletException, IOException {
   dir.delete(EFS.NONE, null);
   return true;
 }
Exemplo n.º 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;
 }
Exemplo n.º 8
0
  @Override
  public void create(InputStream content, int updateFlags, IProgressMonitor monitor)
      throws CoreException {
    final boolean monitorNull = monitor == null;
    monitor = Policy.monitorFor(monitor);
    try {
      String message =
          monitorNull ? "" : NLS.bind(Messages.resources_creating, getFullPath()); // $NON-NLS-1$
      monitor.beginTask(message, Policy.totalWork);
      checkValidPath(path, FILE, true);
      final ISchedulingRule rule = workspace.getRuleFactory().createRule(this);
      try {
        workspace.prepareOperation(rule, monitor);
        checkDoesNotExist();
        Container parent = (Container) getParent();
        ResourceInfo info = parent.getResourceInfo(false, false);
        parent.checkAccessible(getFlags(info));
        checkValidGroupContainer(parent, false, false);

        workspace.beginOperation(true);
        IFileStore store = getStore();
        IFileInfo localInfo = store.fetchInfo();
        if (BitMask.isSet(updateFlags, IResource.FORCE)) {
          if (!Workspace.caseSensitive) {
            if (localInfo.exists()) {
              String name = getLocalManager().getLocalName(store);
              if (name == null || localInfo.getName().equals(name)) {
                delete(true, null);
              } else {
                // The file system is not case sensitive and there is already a file
                // under this location.
                message =
                    NLS.bind(
                        Messages.resources_existsLocalDifferentCase,
                        new Path(store.toString()).removeLastSegments(1).append(name).toOSString());
                throw new ResourceException(
                    IResourceStatus.CASE_VARIANT_EXISTS, getFullPath(), message, null);
              }
            }
          }
        } else {
          if (localInfo.exists()) {
            // return an appropriate error message for case variant collisions
            if (!Workspace.caseSensitive) {
              String name = getLocalManager().getLocalName(store);
              if (name != null && !localInfo.getName().equals(name)) {
                message =
                    NLS.bind(
                        Messages.resources_existsLocalDifferentCase,
                        new Path(store.toString()).removeLastSegments(1).append(name).toOSString());
                throw new ResourceException(
                    IResourceStatus.CASE_VARIANT_EXISTS, getFullPath(), message, null);
              }
            }
            message = NLS.bind(Messages.resources_fileExists, store.toString());
            throw new ResourceException(
                IResourceStatus.FAILED_WRITE_LOCAL, getFullPath(), message, null);
          }
        }
        monitor.worked(Policy.opWork * 40 / 100);

        info = workspace.createResource(this, updateFlags);
        boolean local = content != null;
        if (local) {
          try {
            internalSetContents(
                content,
                localInfo,
                updateFlags,
                false,
                Policy.subMonitorFor(monitor, Policy.opWork * 60 / 100));
          } catch (CoreException e) {
            // a problem happened creating the file on disk, so delete from the workspace and disk
            workspace.deleteResource(this);
            store.delete(EFS.NONE, null);
            throw e; // rethrow
          } catch (OperationCanceledException e) {
            // the operation of setting contents has been canceled, so delete the file from the
            // workspace and disk
            workspace.deleteResource(this);
            store.delete(EFS.NONE, null);
            throw e;
          }
        }
        internalSetLocal(local, DEPTH_ZERO);
        if (!local) getResourceInfo(true, true).clearModificationStamp();
      } catch (OperationCanceledException e) {
        workspace.getWorkManager().operationCanceled();
        throw e;
      } finally {
        workspace.endOperation(rule, true, Policy.subMonitorFor(monitor, Policy.endOpWork));
      }
    } finally {
      monitor.done();
      FileUtil.safeClose(content);
    }
  }