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;
 }
 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;
 }
 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;
 }
 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);
   }
 }