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:net.ymate.platform.mvc.web.filter.RequestMethodFilter.java

public IView doFilter(RequestMeta meta, String params) throws Exception {
    HttpRequestMeta _httpMeta = (HttpRequestMeta) meta;
    boolean _flag = !WebMVC.getConfig().isRestfulModel()
            && !_httpMeta.allowHttpMethod(HttpMethod.valueOf(WebContext.getRequest().getMethod()));
    if (!_flag) {
        String _pValue = null;/*  ww w  .  ja va2  s. c o m*/
        for (String _pName : _httpMeta.getAllowHttpHeaders().keySet()) {
            if (_pName.startsWith("!")) {
                _pValue = WebContext.getRequest().getHeader(_pName.substring(1));
                if (/* StringUtils.isBlank(_pValue) || */StringUtils.equalsIgnoreCase(_pValue,
                        _httpMeta.getAllowHttpHeaders().get(_pName))) {
                    _flag = true;
                    break;
                }
            } else {
                _pValue = WebContext.getRequest().getHeader(_pName);
                if (/* StringUtils.isBlank(_pValue) || */!StringUtils.equalsIgnoreCase(_pValue,
                        _httpMeta.getAllowHttpHeaders().get(_pName))) {
                    _flag = true;
                    break;
                }
            }
        }
    }
    if (!_flag) {
        String _pValue = null;
        for (String _pName : _httpMeta.getAllowHttpParams().keySet()) {
            if (_pName.startsWith("!")) {
                _pValue = WebContext.getRequest().getParameter(_pName.substring(1));
                if (/* StringUtils.isBlank(_pValue) || */StringUtils.equalsIgnoreCase(_pValue,
                        _httpMeta.getAllowHttpParams().get(_pName))) {
                    _flag = true;
                    break;
                }
            } else {
                _pValue = WebContext.getRequest().getParameter(_pName);
                if (/* StringUtils.isBlank(_pValue) || */!StringUtils.equalsIgnoreCase(_pValue,
                        _httpMeta.getAllowHttpParams().get(_pName))) {
                    _flag = true;
                    break;
                }
            }
        }
    }
    return _flag ? new HttpStatusView(HttpServletResponse.SC_METHOD_NOT_ALLOWED) : null;
}

From source file:thinwire.render.web.WebServlet.java

public void service(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    String method = request.getMethod();

    if (method.equals("GET")) {
        String resource = request.getParameter("_twr_");

        if (resource == null) {
            handleStart(request, response);
        } else {/*  w ww  . j a va 2s.  co  m*/
            handleResource(request, response, resource);
        }
    } else if (method.equals("POST")) {
        String action = request.getParameter("_twa_");

        if (action == null) {
            handlePostEvent(request, response);
        } else if (action.equals("upload")) {
            handleUserUpload(request, response);
        }
    } else {
        response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
    }
}

From source file:com.jaspersoft.jasperserver.rest.RESTAbstractService.java

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServiceException {
    log.error("Method GET is not supported for this object type - request params: Path: " + req.getPathInfo());
    restUtils.setStatusAndBody(HttpServletResponse.SC_METHOD_NOT_ALLOWED, resp,
            "Method not supported for this object type");
}

From source file:org.springframework.flex.servlet.MessageBrokerHandlerAdapter.java

/**
 * /*from   w ww  .jav  a  2 s. c  o m*/
 * {@inheritDoc}
 */
public ModelAndView handle(HttpServletRequest req, HttpServletResponse res, Object handler) throws Exception {
    MessageBroker broker = (MessageBroker) handler;

    try {
        // Update thread locals
        broker.initThreadLocals();

        // Set this first so it is in place for the session creation event.
        FlexContext.setThreadLocalObjects(null, null, broker, req, res, this.servletConfig);

        Object providerToCheck = broker.getFlexSessionManager().getFlexSessionProvider(HttpFlexSession.class);
        Assert.isInstanceOf(HttpFlexSessionProvider.class, providerToCheck,
                "MessageBrokerHandlerAdapter requires an instance of " + HttpFlexSessionProvider.class.getName()
                        + " to have been registered with the MessageBroker.");
        HttpFlexSessionProvider provider = (HttpFlexSessionProvider) providerToCheck;
        provider.getOrCreateSession(req);

        String contextPath = req.getContextPath();
        String pathInfo = req.getPathInfo();
        String endpointPath = req.getServletPath();
        if (pathInfo != null) {
            endpointPath = endpointPath + pathInfo;
        }
        Endpoint endpoint = null;
        try {
            endpoint = broker.getEndpoint(endpointPath, contextPath);
        } catch (MessageException me) {
            if (logger.isErrorEnabled()) {
                logger.error("Received invalid request for endpoint path '" + endpointPath + "'.");
            }

            if (!res.isCommitted()) {
                res.sendError(HttpServletResponse.SC_NOT_FOUND);
            }

            return null;
        }

        try {
            if (logger.isInfoEnabled()) {
                logger.info("Channel endpoint " + endpoint.getId() + " received request.");
            }
            endpoint.service(req, res);
        } catch (UnsupportedOperationException ue) {
            if (logger.isErrorEnabled()) {
                logger.error("Channel endpoint " + endpoint.getId()
                        + " received request for an unsupported operation.", ue);
            }

            if (!res.isCommitted()) {
                res.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
            }
        }
    } finally {
        FlexContext.clearThreadLocalObjects();
        SerializationContext.clearThreadLocalObjects();
    }

    return null;
}

From source file:org.openmrs.module.webservices.rest.web.v1_0.controller.BaseRestController.java

@ExceptionHandler(Exception.class)
@ResponseBody//from   w  w  w .j a  v  a  2  s.  c  o  m
public SimpleObject handleException(Exception ex, HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    log.error(ex.getMessage(), ex);
    int errorCode = DEFAULT_ERROR_CODE;
    String errorDetail = DEFAULT_ERROR_DETAIL;
    ResponseStatus ann = ex.getClass().getAnnotation(ResponseStatus.class);
    if (ann != null) {
        errorCode = ann.value().value();
        if (StringUtils.isNotEmpty(ann.reason())) {
            errorDetail = ann.reason();
        }

    } else if (RestUtil.hasCause(ex, APIAuthenticationException.class)) {
        return apiAuthenticationExceptionHandler(ex, request, response);
    } else if (ex.getClass() == HttpRequestMethodNotSupportedException.class) {
        errorCode = HttpServletResponse.SC_METHOD_NOT_ALLOWED;
    }
    response.setStatus(errorCode);
    return RestUtil.wrapErrorResponse(ex, errorDetail);
}

From source file:org.bibsonomy.webapp.controller.ajax.DiscussionItemAjaxController.java

@Override
public View workOn(final DiscussionItemAjaxCommand<D> command) {
    final RequestWrapperContext context = command.getContext();
    if (!context.isUserLoggedIn()) {
        throw new AccessDeniedException();
    }/* ww w  .j  a  va2s. c om*/

    if (!context.isValidCkey()) {
        errors.reject("error.field.valid.ckey");
    }

    final String hash = command.getHash();

    /*
     * resource hash must be specified
     */
    if (!present(hash)) {
        errors.rejectValue("hash", "error.field.valid.hash");
        return returnErrorView();
    }

    final String username = command.getContext().getLoginUser().getName();

    /*
     * don't call the validator
     */
    if (HttpMethod.DELETE.equals(this.requestLogic.getHttpMethod())) {
        this.logic.deleteDiscussionItem(username, hash, command.getDiscussionItem().getHash());
        return Views.AJAX_JSON;
    }

    /*
     * validate the command (including discussionItem)
     */
    ValidationUtils.invokeValidator(this.getValidator(), command, this.errors);

    /*
     * if validation failed return to the ajax error view
     */
    if (this.errors.hasErrors()) {
        return returnErrorView();
    }

    final D discussionItem = command.getDiscussionItem();

    /*
     * init groups from grouping command
     */
    GroupingCommandUtils.initGroups(command, discussionItem.getGroups());

    try {
        switch (this.requestLogic.getHttpMethod()) {
        case POST:
            this.logic.createDiscussionItem(hash, username, discussionItem);
            break;
        case PUT:
            this.logic.updateDiscussionItem(username, hash, discussionItem);
            break;
        default:
            this.responseLogic.setHttpStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
        }
    } catch (final ValidationException ex) {
        log.warn("couldn't complete controller", ex);
        return returnErrorView();
    }

    /*
     * add hash as response
     */
    final JSONObject result = new JSONObject();
    result.put("hash", discussionItem.getHash());
    command.setResponseString(result.toString());
    return Views.AJAX_JSON;
}

From source file:org.sc.probro.servlets.TextQueryServlet.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    handleException(response,//  w ww .ja  va2s  .  c om
            new BrokerException(HttpServletResponse.SC_METHOD_NOT_ALLOWED, String.format("POST not allowed.")));
}

From source file:com.jaspersoft.jasperserver.rest.RESTAbstractService.java

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServiceException {
    log.error("Method POST is not supported for this object type - request params: Path: " + req.getPathInfo());
    restUtils.setStatusAndBody(HttpServletResponse.SC_METHOD_NOT_ALLOWED, resp,
            "Method not supported for this object type");
}

From source file:net.sf.j2ep.ProxyFilter.java

/**
 * Implementation of a reverse-proxy. All request go through here. This is
 * the main class where are handling starts.
 * /* w  w w . j  a v  a  2s  . co  m*/
 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
 *      javax.servlet.ServletResponse, javax.servlet.FilterChain)
 */
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
        throws IOException, ServletException {

    HttpServletResponse httpResponse = (HttpServletResponse) response;
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    //httpRequest.setCharacterEncoding("UTF-8");
    //httpResponse.setCharacterEncoding("UTF-8");
    Server server = (Server) httpRequest.getAttribute("proxyServer");
    if (server == null) {
        server = serverChain.evaluate(httpRequest);
    }

    if (server == null) {
        filterChain.doFilter(request, response);
    } else {
        String uri = server.getRule().process(getURI(httpRequest));
        String url = request.getScheme() + "://" + server.getDomainName() + server.getPath() + uri;
        log.debug("Connecting to " + url);

        ResponseHandler responseHandler = null;

        try {
            httpRequest = server.preExecute(httpRequest);
            responseHandler = executeRequest(httpRequest, url);
            httpResponse = server.postExecute(httpResponse);

            responseHandler.process(httpResponse);
        } catch (HttpException e) {
            log.error("Problem while connecting to server", e);
            httpResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            server.setConnectionExceptionRecieved(e);
        } catch (UnknownHostException e) {
            log.error("Could not connection to the host specified", e);
            httpResponse.setStatus(HttpServletResponse.SC_GATEWAY_TIMEOUT);
            server.setConnectionExceptionRecieved(e);
        } catch (IOException e) {
            log.error("Problem probably with the input being send, either with a Header or the Stream", e);
            httpResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        } catch (MethodNotAllowedException e) {
            log.error("Incoming method could not be handled", e);
            httpResponse.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
            httpResponse.setHeader("Allow", e.getAllowedMethods());
        } finally {
            if (responseHandler != null) {
                responseHandler.close();
            }
        }
    }
}