List of usage examples for javax.servlet.http HttpServletResponse SC_METHOD_NOT_ALLOWED
int SC_METHOD_NOT_ALLOWED
To view the source code for javax.servlet.http HttpServletResponse SC_METHOD_NOT_ALLOWED.
Click Source Link
Request-Line
is not allowed for the resource identified by the Request-URI
. From source file:org.gss_project.gss.server.rest.FilesHandler.java
/** * Move the resource in the specified path to the trash bin. * * @param req the HTTP request//from w ww .ja va 2s . co m * @param resp the HTTP response * @param path the path of the resource * @throws IOException if an input/output error occurs */ private void trashResource(HttpServletRequest req, HttpServletResponse resp, String path) throws IOException { final User user = getUser(req); User owner = getOwner(req); Object resource = null; try { resource = getService().getResourceAtPath(owner.getId(), path, true); } catch (ObjectNotFoundException e) { resp.sendError(HttpServletResponse.SC_NOT_FOUND, path); return; } catch (RpcException e) { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, path); return; } try { if (resource instanceof Folder) { final Folder folderLocal = (Folder) resource; new TransactionHelper<Void>().tryExecute(new Callable<Void>() { @Override public Void call() throws Exception { getService().moveFolderToTrash(user.getId(), folderLocal.getId()); return null; } }); } else { final FileHeader fileLocal = (FileHeader) resource; new TransactionHelper<Void>().tryExecute(new Callable<Void>() { @Override public Void call() throws Exception { getService().moveFileToTrash(user.getId(), fileLocal.getId()); return null; } }); } } catch (InsufficientPermissionsException e) { resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED); } catch (ObjectNotFoundException e) { resp.sendError(HttpServletResponse.SC_NOT_FOUND, e.getMessage()); } catch (RpcException e) { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, path); } catch (Exception e) { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, path); } }
From source file:org.gss_project.gss.server.rest.FilesHandler.java
/** * Restore the resource in the specified path from the trash bin. * * @param req the HTTP request/* w ww.ja va 2 s . c o m*/ * @param resp the HTTP response * @param path the path of the resource * @throws IOException if an input/output error occurs */ private void restoreResource(HttpServletRequest req, HttpServletResponse resp, String path) throws IOException { final User user = getUser(req); User owner = getOwner(req); Object resource = null; try { resource = getService().getResourceAtPath(owner.getId(), path, false); } catch (ObjectNotFoundException e) { resp.sendError(HttpServletResponse.SC_NOT_FOUND, path); return; } catch (RpcException e) { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, path); return; } try { if (resource instanceof Folder) { final Folder folderLocal = (Folder) resource; new TransactionHelper<Void>().tryExecute(new Callable<Void>() { @Override public Void call() throws Exception { getService().removeFolderFromTrash(user.getId(), folderLocal.getId()); return null; } }); } else { final FileHeader fileLocal = (FileHeader) resource; new TransactionHelper<Void>().tryExecute(new Callable<Void>() { @Override public Void call() throws Exception { getService().removeFileFromTrash(user.getId(), fileLocal.getId()); return null; } }); } } catch (InsufficientPermissionsException e) { resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED); } catch (ObjectNotFoundException e) { resp.sendError(HttpServletResponse.SC_NOT_FOUND, e.getMessage()); } catch (RpcException e) { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, path); } catch (Exception e) { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, path); } }
From source file:org.gss_project.gss.server.rest.FilesHandler.java
/** * Update the resource in the specified path. * * @param req the HTTP request/*from w w w .j a v a 2 s . c o m*/ * @param resp the HTTP response * @param path the path of the resource * @throws IOException if an input/output error occurs */ private void updateResource(HttpServletRequest req, HttpServletResponse resp, String path) throws IOException { final User user = getUser(req); User owner = getOwner(req); Object resource = null; try { resource = getService().getResourceAtPath(owner.getId(), path, false); } catch (ObjectNotFoundException e) { resp.sendError(HttpServletResponse.SC_NOT_FOUND, path); return; } catch (RpcException e) { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, path); return; } StringBuffer input = new StringBuffer(); JSONObject json = null; if (req.getContentType() != null && req.getContentType().startsWith("application/x-www-form-urlencoded")) input.append(req.getParameter(RESOURCE_UPDATE_PARAMETER)); else { // Assume application/json BufferedReader reader = new BufferedReader(new InputStreamReader(req.getInputStream(), "UTF-8")); String line = null; while ((line = reader.readLine()) != null) input.append(line); reader.close(); } try { json = new JSONObject(input.toString()); if (logger.isDebugEnabled()) logger.debug("JSON update: " + json); if (resource instanceof Folder) { final Folder folderLocal = (Folder) resource; String name = json.optString("name"); if (!isValidResourceName(name)) { resp.sendError(HttpServletResponse.SC_BAD_REQUEST); return; } JSONArray permissions = json.optJSONArray("permissions"); Set<Permission> perms = null; if (permissions != null) perms = parsePermissions(user, permissions); Boolean readForAll = null; if (json.opt("readForAll") != null) readForAll = json.optBoolean("readForAll"); if (!name.isEmpty() || permissions != null || readForAll != null) { final String fName = name.isEmpty() ? null : name; final Boolean freadForAll = readForAll; final Set<Permission> fPerms = perms; Folder folderUpdated = new TransactionHelper<Folder>().tryExecute(new Callable<Folder>() { @Override public Folder call() throws Exception { return getService().updateFolder(user.getId(), folderLocal.getId(), fName, freadForAll, fPerms); } }); resp.getWriter().println(getNewUrl(req, folderUpdated)); } } else { final FileHeader fileLocal = (FileHeader) resource; String name = null; if (json.opt("name") != null) name = json.optString("name"); if (name != null) if (!isValidResourceName(name)) { resp.sendError(HttpServletResponse.SC_BAD_REQUEST); return; } Long modificationDate = null; if (json.optLong("modificationDate") != 0) modificationDate = json.optLong("modificationDate"); Boolean versioned = null; if (json.opt("versioned") != null) versioned = json.getBoolean("versioned"); JSONArray tagset = json.optJSONArray("tags"); String tags = null; StringBuffer t = new StringBuffer(); if (tagset != null) { for (int i = 0; i < tagset.length(); i++) t.append(tagset.getString(i) + ','); tags = t.toString(); } JSONArray permissions = json.optJSONArray("permissions"); Set<Permission> perms = null; if (permissions != null) perms = parsePermissions(user, permissions); Boolean readForAll = null; if (json.opt("readForAll") != null) readForAll = json.optBoolean("readForAll"); if (name != null || tags != null || modificationDate != null || versioned != null || perms != null || readForAll != null) { final String fName = name; final String fTags = tags; final Date mDate = modificationDate != null ? new Date(modificationDate) : null; final Boolean fVersioned = versioned; final Boolean fReadForAll = readForAll; final Set<Permission> fPerms = perms; new TransactionHelper<Object>().tryExecute(new Callable<Object>() { @Override public Object call() throws Exception { getService().updateFile(user.getId(), fileLocal.getId(), fName, fTags, mDate, fVersioned, fReadForAll, fPerms); return null; } }); } } } catch (JSONException e) { resp.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage()); } catch (InsufficientPermissionsException e) { resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED); } catch (ObjectNotFoundException e) { resp.sendError(HttpServletResponse.SC_NOT_FOUND, e.getMessage()); } catch (DuplicateNameException e) { resp.sendError(HttpServletResponse.SC_CONFLICT, e.getMessage()); } catch (RpcException e) { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, path); } catch (Exception e) { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, path); return; } }
From source file:org.gss_project.gss.server.rest.FilesHandler.java
/** * Creates a new folder with the specified name under the folder in the provided path. * * @param req the HTTP request/*from w ww. j a v a 2 s . co m*/ * @param resp the HTTP response * @param path the parent folder path * @param folderName the name of the new folder * @throws IOException if an input/output error occurs */ private void createFolder(HttpServletRequest req, HttpServletResponse resp, String path, final String folderName) throws IOException { if (logger.isDebugEnabled()) logger.debug("Creating folder " + folderName + " in '" + path); final User user = getUser(req); User owner = getOwner(req); boolean exists = true; try { getService().getResourceAtPath(owner.getId(), path + folderName, false); } catch (ObjectNotFoundException e) { exists = false; } catch (RpcException e) { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, path + folderName); return; } if (exists) { resp.addHeader("Allow", METHOD_GET + ", " + METHOD_DELETE + ", " + METHOD_HEAD); resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED); return; } Object parent; try { parent = getService().getResourceAtPath(owner.getId(), path, true); } catch (ObjectNotFoundException e) { resp.sendError(HttpServletResponse.SC_CONFLICT); return; } catch (RpcException e) { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, path + folderName); return; } try { if (parent instanceof Folder) { final Folder folderLocal = (Folder) parent; Folder newFolder = new TransactionHelper<Folder>().tryExecute(new Callable<Folder>() { @Override public Folder call() throws Exception { return getService().createFolder(user.getId(), folderLocal.getId(), folderName); } }); String newResource = getApiRoot() + newFolder.getURI(); resp.setHeader("Location", newResource); resp.setContentType("text/plain"); PrintWriter out = resp.getWriter(); out.println(newResource); } else { resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED); return; } } catch (DuplicateNameException e) { resp.sendError(HttpServletResponse.SC_CONFLICT); return; } catch (InsufficientPermissionsException e) { resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED); return; } catch (ObjectNotFoundException e) { resp.sendError(HttpServletResponse.SC_CONFLICT); return; } catch (RpcException e) { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, path + folderName); return; } catch (Exception e) { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, path); return; } resp.setStatus(HttpServletResponse.SC_CREATED); }
From source file:org.gss_project.gss.server.rest.FilesHandler.java
/** * @param req//from w ww . j ava 2 s .com * @param resp * @throws IOException * @throws FileNotFoundException */ void putResource(HttpServletRequest req, HttpServletResponse resp) throws IOException, FileNotFoundException { String path = getInnerPath(req, PATH_FILES); try { path = URLDecoder.decode(path, "UTF-8"); } catch (IllegalArgumentException e) { resp.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage()); return; } if (logger.isDebugEnabled()) logger.debug("Updating resource: " + path); final User user = getUser(req); User owner = getOwner(req); boolean exists = true; Object resource = null; FileHeader fileLocal = null; try { resource = getService().getResourceAtPath(owner.getId(), path, false); } catch (ObjectNotFoundException e) { exists = false; } catch (RpcException e) { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, path); return; } if (exists) if (resource instanceof FileHeader) fileLocal = (FileHeader) resource; else { resp.sendError(HttpServletResponse.SC_CONFLICT, path + " is a folder"); return; } boolean result = true; // Temporary content file used to support partial PUT. File contentFile = null; Range range = parseContentRange(req, resp); InputStream resourceInputStream = null; // Append data specified in ranges to existing content for this // resource - create a temporary file on the local filesystem to // perform this operation. // Assume just one range is specified for now if (range != null) { try { contentFile = executePartialPut(req, range, path); } catch (RpcException e) { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, path); return; } catch (ObjectNotFoundException e) { resp.sendError(HttpServletResponse.SC_CONFLICT); return; } catch (InsufficientPermissionsException e) { resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED); return; } resourceInputStream = new FileInputStream(contentFile); } else resourceInputStream = req.getInputStream(); try { Folder folderLocal = null; Object parent = getService().getResourceAtPath(owner.getId(), getParentPath(path), true); if (!(parent instanceof Folder)) { resp.sendError(HttpServletResponse.SC_CONFLICT); return; } folderLocal = (Folder) parent; final String name = getLastElement(path); final String mimeType = context.getMimeType(name); File uploadedFile = null; try { uploadedFile = getService().uploadFile(resourceInputStream, user.getId()); } catch (IOException ex) { throw new GSSIOException(ex, false); } FileHeader fileTemp = null; final File uploadedf = uploadedFile; final Folder parentf = folderLocal; final FileHeader f = fileLocal; if (exists) fileTemp = new TransactionHelper<FileHeader>().tryExecute(new Callable<FileHeader>() { @Override public FileHeader call() throws Exception { return getService().updateFileContents(user.getId(), f.getId(), mimeType, uploadedf.getCanonicalFile().length(), uploadedf.getAbsolutePath()); } }); else fileTemp = new TransactionHelper<FileHeader>().tryExecute(new Callable<FileHeader>() { @Override public FileHeader call() throws Exception { return getService().createFile(user.getId(), parentf.getId(), name, mimeType, uploadedf.getCanonicalFile().length(), uploadedf.getAbsolutePath()); } }); updateAccounting(owner, new Date(), fileTemp.getCurrentBody().getFileSize()); getService().removeFileUploadProgress(user.getId(), fileTemp.getName()); } catch (ObjectNotFoundException e) { result = false; } catch (RpcException e) { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, path); return; } catch (IOException e) { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, path); return; } catch (GSSIOException e) { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, path); return; } catch (DuplicateNameException e) { resp.sendError(HttpServletResponse.SC_CONFLICT); return; } catch (InsufficientPermissionsException e) { resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED); return; } catch (QuotaExceededException e) { resp.sendError(HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE, e.getMessage()); return; } catch (Exception e) { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, path); return; } if (result) { if (exists) resp.setStatus(HttpServletResponse.SC_NO_CONTENT); else resp.setStatus(HttpServletResponse.SC_CREATED); } else resp.sendError(HttpServletResponse.SC_CONFLICT); }
From source file:org.gss_project.gss.server.rest.FilesHandler.java
/** * Delete a resource./*w ww . ja v a 2s .com*/ * * @param req The servlet request we are processing * @param resp The servlet response we are processing * @throws IOException if the response cannot be sent */ void deleteResource(HttpServletRequest req, HttpServletResponse resp) throws IOException { String path = getInnerPath(req, PATH_FILES); if (logger.isDebugEnabled()) logger.debug("Deleting resource '" + path); path = URLDecoder.decode(path, "UTF-8"); final User user = getUser(req); User owner = getOwner(req); boolean exists = true; Object object = null; try { object = getService().getResourceAtPath(owner.getId(), path, false); } catch (ObjectNotFoundException e) { exists = false; } catch (RpcException e) { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; } if (!exists) { resp.sendError(HttpServletResponse.SC_NOT_FOUND); return; } Folder folderLocal = null; FileHeader fileLocal = null; if (object instanceof Folder) folderLocal = (Folder) object; else fileLocal = (FileHeader) object; if (fileLocal != null) try { final FileHeader f = fileLocal; new TransactionHelper<Void>().tryExecute(new Callable<Void>() { @Override public Void call() throws Exception { getService().deleteFile(user.getId(), f.getId()); return null; } }); } catch (InsufficientPermissionsException e) { resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED); return; } catch (ObjectNotFoundException e) { // Although we had already found the object, it was // probably deleted from another thread. resp.sendError(HttpServletResponse.SC_NOT_FOUND); return; } catch (RpcException e) { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; } catch (Exception e) { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; } else if (folderLocal != null) try { final Folder fo = folderLocal; new TransactionHelper<Void>().tryExecute(new Callable<Void>() { @Override public Void call() throws Exception { getService().deleteFolder(user.getId(), fo.getId()); return null; } }); } catch (InsufficientPermissionsException e) { resp.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED); return; } catch (ObjectNotFoundException e) { resp.setStatus(HttpServletResponse.SC_NOT_FOUND); return; } catch (RpcException e) { resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; } catch (Exception e) { resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; } resp.setStatus(HttpServletResponse.SC_NO_CONTENT); return; }
From source file:edu.slu.action.ObjectAction.java
/** * Public facing servlet to delete a given annotation. *///from w ww . j a va 2 s.c o m public void deleteObject() throws IOException, ServletException, Exception { System.out.println("Delete object"); if (null != processRequestBody(request, true) && methodApproval(request, "delete")) { BasicDBObject query = new BasicDBObject(); BasicDBObject originalObject; //processRequestBody will always return a stringified JSON object here, even if the ID provided was a string in the body. JSONObject received = JSONObject.fromObject(content); JSONObject safe_received; JSONObject updatedWithFlag = new JSONObject(); BasicDBObject updatedObjectWithDeletedFlag; if (received.containsKey("@id")) { query.append("@id", received.getString("@id")); BasicDBObject mongo_obj = (BasicDBObject) mongoDBService .findOneByExample(Constant.COLLECTION_ANNOTATION, query); safe_received = JSONObject.fromObject(mongo_obj); //We can trust this is the object as it exists in mongo boolean alreadyDeleted = checkIfDeleted(safe_received); boolean permission = false; boolean isReleased = false; boolean passedAllChecks = false; if (alreadyDeleted) { writeErrorResponse("Object for delete is already deleted.", HttpServletResponse.SC_METHOD_NOT_ALLOWED); } else { isReleased = checkIfReleased(safe_received); if (isReleased) { writeErrorResponse("This object is in a released state and cannot be deleted.", HttpServletResponse.SC_METHOD_NOT_ALLOWED); } else { String origObjGenerator = safe_received.getJSONObject("__rerum").getString("generatedBy"); boolean isGenerator = (origObjGenerator.equals(generatorID)); if (isGenerator) { passedAllChecks = true; } else { writeErrorResponse( "Only the agent that created this object can delete it. Agent= " + generatorID, HttpServletResponse.SC_UNAUTHORIZED); } } } if (passedAllChecks) { //If all checks have passed. If not, we want to make sure their writeErrorReponse() don't stack. originalObject = (BasicDBObject) JSON.parse(safe_received.toString()); //The original object out of mongo for persistance //Found the @id in the object, but does it exist in RERUM? if (null != originalObject) { String preserveID = safe_received.getString("@id"); JSONObject deletedFlag = new JSONObject(); //The __deleted flag is a JSONObject deletedFlag.element("object", originalObject); deletedFlag.element("deletor", generatorID); deletedFlag.element("time", System.currentTimeMillis()); updatedWithFlag.element("@id", preserveID); updatedWithFlag.element("__deleted", deletedFlag); //We want everything wrapped in deleted except the @id. Object forMongo = JSON.parse(updatedWithFlag.toString()); //JSONObject cannot be converted to BasicDBObject updatedObjectWithDeletedFlag = (BasicDBObject) forMongo; boolean treeHealed = healHistoryTree(JSONObject.fromObject(originalObject)); if (treeHealed) { mongoDBService.update(Constant.COLLECTION_ANNOTATION, originalObject, updatedObjectWithDeletedFlag); response.setStatus(HttpServletResponse.SC_NO_CONTENT); System.out.println("Object deleted:" + preserveID); } else { //@cubap @theHabes FIXME By default, objects that don't have the history property will fail to this line. writeErrorResponse( "We could not update the history tree correctly. The delete failed.", HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } } else { writeErrorResponse( "The '@id' string provided for DELETE could not be found in RERUM: " + safe_received.getString("@id") + ". \n DELETE failed.", HttpServletResponse.SC_NOT_FOUND); } } } else { writeErrorResponse("Object for delete did not contain an '@id'. Could not delete.", HttpServletResponse.SC_BAD_REQUEST); } } }