Example usage for javax.servlet.http HttpServletResponse SC_METHOD_NOT_ALLOWED

List of usage examples for javax.servlet.http HttpServletResponse SC_METHOD_NOT_ALLOWED

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse SC_METHOD_NOT_ALLOWED.

Prototype

int SC_METHOD_NOT_ALLOWED

To view the source code for javax.servlet.http HttpServletResponse SC_METHOD_NOT_ALLOWED.

Click Source Link

Document

Status code (405) indicating that the method specified in the Request-Line is not allowed for the resource identified by the Request-URI.

Usage

From source file:org.gss_project.gss.server.rest.TagsHandler.java

/**
  * Serve the tags defined by the user.//from  w  ww .  jav  a  2s.  c o  m
  *
  * @param req The servlet request we are processing
  * @param resp The servlet response we are processing
  * @throws IOException if an input/output error occurs
 */
void serveTags(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    String path = getInnerPath(req, PATH_TAGS);
    if (path.equals(""))
        path = "/";

    if (path.equals("/")) {
        Set<String> tags;
        try {
            User user = getUser(req);
            User owner = getOwner(req);
            if (!owner.equals(user)) {
                resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
                return;
            }
            tags = getService().getUserTags(user.getId());
        } catch (ObjectNotFoundException e) {
            logger.error("User not found", e);
            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        } catch (RpcException e) {
            logger.error("", e);
            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        }
        JSONArray json = new JSONArray();
        for (String tag : tags)
            json.put(tag);

        sendJson(req, resp, json.toString());
    } else {
        resp.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
}

From source file:com.thinkberg.webdav.MkColHandler.java

public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    BufferedReader bufferedReader = request.getReader();
    String line = bufferedReader.readLine();
    if (line != null) {
        response.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
        return;/*from  w  ww  .ja  v  a  2  s  .c  o  m*/
    }

    FileObject object = VFSBackend.resolveFile(request.getPathInfo());

    try {
        if (!LockManager.getInstance().evaluateCondition(object, getIf(request)).result) {
            response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
            return;
        }
    } catch (LockException e) {
        response.sendError(SC_LOCKED);
        return;
    } catch (ParseException e) {
        response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
        return;
    }

    if (object.exists()) {
        response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
        return;
    }

    if (!object.getParent().exists() || !FileType.FOLDER.equals(object.getParent().getType())) {
        response.sendError(HttpServletResponse.SC_CONFLICT);
        return;
    }

    try {
        object.createFolder();
        response.setStatus(HttpServletResponse.SC_CREATED);
    } catch (FileSystemException e) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
    }
}

From source file:eu.morfeoproject.fast.catalogue.services.GenericServlet.java

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */// w w  w.  j  a va2s  .  co  m
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
}

From source file:com.enonic.cms.server.service.servlet.CmsDispatcherServlet.java

protected void doService(HttpServletRequest req, HttpServletResponse res) throws Exception {

    HttpMethod requestMethod = HttpMethod.valueOf(req.getMethod());

    if (!ALLOWED_HTTP_METHODS.contains(requestMethod)) {
        res.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
        return;/* w ww .  j  a  v a 2s  . c  o m*/
    }

    startContextIfNeeded();
    if (upgradeIsNeeded(res)) {
        return;
    }

    ServletRequestAccessor.setRequest(req);
    // resolve and set original url if not set
    if (req.getAttribute(Attribute.ORIGINAL_URL) == null) {
        final String originalUrl = OriginalUrlResolver.get().resolveOriginalUrl(req);
        req.setAttribute(Attribute.ORIGINAL_URL, originalUrl);
    }

    if (LOG.isDebugEnabled()) {
        StringBuffer msg = new StringBuffer();
        msg.append("Dispatching request to URL: " + req.getRequestURL());
        LOG.debug(msg.toString());
    }

    super.doService(req, res);
}

From source file:org.eclipse.rap.addons.fileupload.internal.FileUploadServiceHandler.java

public void service(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    // TODO [rst] Revise: does this double security make it any more secure?
    // Ignore requests to this service handler without a valid session for security reasons
    boolean hasSession = request.getSession(false) != null;
    if (hasSession) {
        String token = request.getParameter(PARAMETER_TOKEN);
        FileUploadHandler registeredHandler = FileUploadHandlerStore.getInstance().getHandler(token);
        if (registeredHandler == null) {
            String message = "Invalid or missing token";
            response.sendError(HttpServletResponse.SC_FORBIDDEN, message);
        } else if (!"POST".equals(request.getMethod().toUpperCase())) {
            String message = "Only POST requests allowed";
            response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, message);
        } else if (!ServletFileUpload.isMultipartContent(request)) {
            String message = "Content must be in multipart type";
            response.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, message);
        } else {//from  w  w w .j a va2  s .c om
            FileUploadProcessor processor = new FileUploadProcessor(registeredHandler);
            processor.handleFileUpload(request, response);
        }
    }
}

From source file:org.eclipse.rap.fileupload.internal.FileUploadServiceHandler.java

@Override
public void service(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    // TODO [rst] Revise: does this double security make it any more secure?
    // Ignore requests to this service handler without a valid session for security reasons
    boolean hasSession = request.getSession(false) != null;
    if (hasSession) {
        String token = request.getParameter(PARAMETER_TOKEN);
        FileUploadHandler registeredHandler = FileUploadHandlerStore.getInstance().getHandler(token);
        if (registeredHandler == null) {
            String message = "Invalid or missing token";
            response.sendError(HttpServletResponse.SC_FORBIDDEN, message);
        } else if (!"POST".equals(request.getMethod().toUpperCase())) {
            String message = "Only POST requests allowed";
            response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, message);
        } else if (!ServletFileUpload.isMultipartContent(request)) {
            String message = "Content must be in multipart type";
            response.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, message);
        } else {// w  ww.java  2s  . c o m
            FileUploadProcessor processor = new FileUploadProcessor(registeredHandler);
            processor.handleFileUpload(request, response);
        }
    }
}

From source file:org.efaps.webdav4vfs.handler.MkColHandler.java

@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    BufferedReader bufferedReader = request.getReader();
    String line = bufferedReader.readLine();
    if (line != null) {
        response.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
        return;/*from   w  w w  . ja va2 s. com*/
    }

    FileObject object = VFSBackend.resolveFile(request.getPathInfo());

    try {
        if (!LockManager.getInstance().evaluateCondition(object, getIf(request)).result) {
            response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
            return;
        }
    } catch (LockException e) {
        response.sendError(SC_LOCKED);
        return;
    } catch (ParseException e) {
        response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
        return;
    }

    if (object.exists()) {
        response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
        return;
    }

    if (!object.getParent().exists() || !FileType.FOLDER.equals(object.getParent().getType())) {
        response.sendError(HttpServletResponse.SC_CONFLICT);
        return;
    }

    try {
        object.createFolder();
        response.setStatus(HttpServletResponse.SC_CREATED);
    } catch (FileSystemException e) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
    }
}

From source file:cf.spring.VarzHandlerMapping.java

public VarzHandlerMapping(final VarzAggregator aggregator, final HttpBasicAuthenticator authenticator,
        int order) {
    setOrder(order);/*from www . j av a 2  s  .c  o m*/
    registerHandler("/varz", new HttpRequestHandler() {
        @Override
        public void handleRequest(HttpServletRequest httpServletRequest,
                HttpServletResponse httpServletResponse) throws ServletException, IOException {
            if (httpServletRequest.getMethod().equalsIgnoreCase("GET")) {
                if (authenticator.authenticate(httpServletRequest, httpServletResponse)) {
                    httpServletResponse.setContentType("application/json;charset=utf-8");
                    final ObjectNode varz = aggregator.aggregateVarz();
                    mapper.writeValue(httpServletResponse.getOutputStream(), varz);
                }
            } else {
                httpServletResponse.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
            }
        }
    });
}

From source file:com.adobe.acs.commons.oak.impl.EnsureOakIndexServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) {

    try {//from  w  w  w  . j a  v a2 s.co  m

        response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
        response.getWriter().println("<pre>");
        response.getWriter().println();
        response.getWriter().println();
        response.getWriter().println("HTTP method GET is not supported by this URL");
        response.getWriter().println("Use HTTP POST to access this end-point");
        response.getWriter().println("--------------------------------------------");
        response.getWriter().println("HTTP POST /system/console/ensure-oak-index");
        response.getWriter().println(" Parameters");
        response.getWriter().println("   * force = true | false (optional; when blank defaults to false)");
        response.getWriter()
                .println("   * path = /abs/path/to/ensure/definition (optional; when blank indicates all)");
        response.getWriter().println();
        response.getWriter().println();
        response.getWriter().println(
                "Example: curl --user admin:admin --data \"force=true\" https://localhost:4502/system/console/ensure-oak-index");
        response.getWriter().println("</pre>");

    } catch (IOException e) {
        log.warn("Caught IOException while handling doGet() in the Ensure Oak Index Servlet.", e);
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
}

From source file:org.gss_project.gss.server.rest.OthersHandler.java

/**
  * Serve the 'others' namespace for the user.
  *//w w  w  . j  a  v a  2  s.  c  o m
  * @param req The servlet request we are processing
  * @param resp The servlet response we are processing
  * @throws IOException if an input/output error occurs
 */
void serveOthers(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    String parentUrl = getContextPath(req, true);
    String path = getInnerPath(req, PATH_OTHERS);
    if (path.equals(""))
        path = "/";

    User user = getUser(req);
    User owner = getOwner(req);
    if (!owner.equals(user)) {
        resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
        return;
    }
    if (path.equals("/"))
        try {
            // Request to retrieve the other users who have shared resources to this user
            JSONArray json = new JSONArray();

            List<User> others = getService().getUsersSharingFoldersForUser(owner.getId());
            for (User u : others) {
                JSONObject j = new JSONObject();
                j.put("username", u.getUsername()).put("uri", parentUrl + u.getUsername());
                json.put(j);
            }

            sendJson(req, resp, json.toString());
        } catch (ObjectNotFoundException e) {
            logger.error("User not found", e);
            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        } catch (RpcException e) {
            logger.error("", e);
            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        } catch (JSONException e) {
            logger.error("", e);
            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        }
    else
        try {
            // Request to retrieve the files and folders shared by the other user
            // Chop any trailing slash
            path = path.endsWith("/") ? path.substring(0, path.length() - 1) : path;
            // Chop any leading slash
            path = path.startsWith("/") ? path.substring(1) : path;

            User other = getService().findUser(path);
            JSONObject json = new JSONObject();

            List<JSONObject> subfolders = new ArrayList<JSONObject>();
            List<Folder> folders = getService().getSharedRootFolders(other.getId(), owner.getId());
            for (Folder f : folders) {
                JSONObject j = new JSONObject();
                j.put("name", f.getName()).put("uri", getApiRoot() + f.getURI()).put("shared", f.getShared());
                subfolders.add(j);
            }
            json.put("folders", subfolders);

            List<JSONObject> files = new ArrayList<JSONObject>();
            List<FileHeader> fileHeaders = getService().getSharedFiles(other.getId(), owner.getId());
            for (FileHeader f : fileHeaders) {
                JSONObject j = new JSONObject();
                j.put("name", f.getName()).put("owner", f.getOwner().getUsername())
                        .put("deleted", f.isDeleted()).put("version", f.getCurrentBody().getVersion())
                        .put("size", f.getCurrentBody().getFileSize())
                        .put("content", f.getCurrentBody().getMimeType())
                        .put("creationDate", f.getAuditInfo().getCreationDate().getTime())
                        .put("modificationDate", f.getAuditInfo().getModificationDate().getTime())
                        .put("path", f.getFolder().getPath()).put("shared", f.getShared())
                        .put("versioned", f.isVersioned()).put("uri", getApiRoot() + f.getURI());
                files.add(j);
            }
            json.put("files", files);

            sendJson(req, resp, json.toString());
        } catch (ObjectNotFoundException e) {
            logger.error("User not found", e);
            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        } catch (RpcException e) {
            logger.error("", e);
            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        } catch (JSONException e) {
            logger.error("", e);
            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        }

    resp.setHeader("Expires", "-1");
}