Search in sources :

Example 1 with HttpRequestMethodNotSupportedException

use of org.springframework.web.HttpRequestMethodNotSupportedException in project spring-framework by spring-projects.

the class RequestMappingInfoHandlerMappingTests method getHandlerRequestMethodNotAllowed.

@Test
public void getHandlerRequestMethodNotAllowed() throws Exception {
    try {
        MockHttpServletRequest request = new MockHttpServletRequest("POST", "/bar");
        this.handlerMapping.getHandler(request);
        fail("HttpRequestMethodNotSupportedException expected");
    } catch (HttpRequestMethodNotSupportedException ex) {
        assertArrayEquals("Invalid supported methods", new String[] { "GET", "HEAD" }, ex.getSupportedMethods());
    }
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) HttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException) Test(org.junit.Test)

Example 2 with HttpRequestMethodNotSupportedException

use of org.springframework.web.HttpRequestMethodNotSupportedException in project CILManagement-Server by LiuinStein.

the class GlobalExceptionResolver method resolveException.

/**
 * Resolve the exception form controller
 *
 * @param request   http request
 * @param response  http response
 * @param o         the executed handler, or null if none chosen at the time of the exception (for example, if multipart resolution failed)
 * @param exception the exception that threw from controller
 * @return a new ModelAndView
 * @implNote https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/HandlerExceptionResolver.html
 */
@NotNull
@Override
public ModelAndView resolveException(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, @Nullable Object o, @NotNull Exception exception) {
    RestfulResult result = new RestfulResult(1, exception.getMessage(), new HashMap<>());
    response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
    if (exception instanceof SimpleException) {
        result.setCode(((SimpleException) exception).getCode());
    }
    if (exception instanceof SimpleHttpException) {
        response.setStatus(((SimpleHttpException) exception).getHttpStatusToReturn().value());
    }
    if (exception instanceof HttpRequestMethodNotSupportedException) {
        result.setCode(405);
        response.setStatus(HttpStatus.METHOD_NOT_ALLOWED.value());
    }
    if (exception instanceof ValidationException || exception instanceof ServletRequestBindingException || exception instanceof IllegalArgumentException) {
        response.setStatus(HttpStatus.BAD_REQUEST.value());
    }
    if (exception instanceof DataAccessException) {
        result.setMessage("database access error");
    }
    try {
        if ("application/xml".equals(request.getHeader("Accept"))) {
            response.setHeader("Content-Type", "application/xml;charset=UTF-8");
            response.getWriter().print(result.toXmlString());
        } else {
            response.setHeader("Content-Type", "application/json;charset=UTF-8");
            response.getWriter().print(result.toJsonString());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return new ModelAndView();
}
Also used : ServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException) ValidationException(com.shaoqunliu.validation.ValidationException) RestfulResult(cn.opencil.vo.RestfulResult) ModelAndView(org.springframework.web.servlet.ModelAndView) IOException(java.io.IOException) HttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException) DataAccessException(org.springframework.dao.DataAccessException) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with HttpRequestMethodNotSupportedException

use of org.springframework.web.HttpRequestMethodNotSupportedException in project spring-security-oauth by spring-projects.

the class DefaultWebResponseExceptionTranslator method translate.

@Override
public ResponseEntity<OAuth2Exception> translate(Exception e) throws Exception {
    // Try to extract a SpringSecurityException from the stacktrace
    Throwable[] causeChain = throwableAnalyzer.determineCauseChain(e);
    Exception ase = (OAuth2Exception) throwableAnalyzer.getFirstThrowableOfType(OAuth2Exception.class, causeChain);
    if (ase != null) {
        return handleOAuth2Exception((OAuth2Exception) ase);
    }
    ase = (AuthenticationException) throwableAnalyzer.getFirstThrowableOfType(AuthenticationException.class, causeChain);
    if (ase != null) {
        return handleOAuth2Exception(new UnauthorizedException(e.getMessage(), e));
    }
    ase = (AccessDeniedException) throwableAnalyzer.getFirstThrowableOfType(AccessDeniedException.class, causeChain);
    if (ase instanceof AccessDeniedException) {
        return handleOAuth2Exception(new ForbiddenException(ase.getMessage(), ase));
    }
    ase = (HttpRequestMethodNotSupportedException) throwableAnalyzer.getFirstThrowableOfType(HttpRequestMethodNotSupportedException.class, causeChain);
    if (ase instanceof HttpRequestMethodNotSupportedException) {
        return handleOAuth2Exception(new MethodNotAllowed(ase.getMessage(), ase));
    }
    return handleOAuth2Exception(new ServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), e));
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) OAuth2Exception(org.springframework.security.oauth2.common.exceptions.OAuth2Exception) HttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException) HttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException) IOException(java.io.IOException) AuthenticationException(org.springframework.security.core.AuthenticationException) AccessDeniedException(org.springframework.security.access.AccessDeniedException) InsufficientScopeException(org.springframework.security.oauth2.common.exceptions.InsufficientScopeException) OAuth2Exception(org.springframework.security.oauth2.common.exceptions.OAuth2Exception)

Example 4 with HttpRequestMethodNotSupportedException

use of org.springframework.web.HttpRequestMethodNotSupportedException in project spring-integration by spring-projects.

the class HttpInboundChannelAdapterParserTests method getRequestNotAllowed.

@Test
public void getRequestNotAllowed() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");
    request.setParameter("foo", "bar");
    request.setRequestURI("/postOnly");
    try {
        this.integrationRequestMappingHandlerMapping.getHandler(request);
    } catch (HttpRequestMethodNotSupportedException e) {
        assertEquals("GET", e.getMethod());
        assertArrayEquals(new String[] { "POST" }, e.getSupportedMethods());
    }
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException) Test(org.junit.Test)

Example 5 with HttpRequestMethodNotSupportedException

use of org.springframework.web.HttpRequestMethodNotSupportedException in project thingsboard by thingsboard.

the class DefaultRestMsgHandler method process.

@Override
public void process(PluginContext ctx, PluginRestMsg msg) {
    try {
        log.debug("[{}] Processing REST msg: {}", ctx.getPluginId(), msg);
        HttpMethod method = msg.getRequest().getMethod();
        switch(method) {
            case GET:
                handleHttpGetRequest(ctx, msg);
                break;
            case POST:
                handleHttpPostRequest(ctx, msg);
                break;
            case DELETE:
                handleHttpDeleteRequest(ctx, msg);
                break;
            default:
                msg.getResponseHolder().setErrorResult(new HttpRequestMethodNotSupportedException(method.name()));
        }
        log.debug("[{}] Processed REST msg.", ctx.getPluginId());
    } catch (Exception e) {
        log.warn("[{}] Exception during REST msg processing: {}", ctx.getPluginId(), e.getMessage(), e);
        msg.getResponseHolder().setErrorResult(e);
    }
}
Also used : HttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException) HttpMethod(org.springframework.http.HttpMethod) HttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException) ServletException(javax.servlet.ServletException)

Aggregations

HttpRequestMethodNotSupportedException (org.springframework.web.HttpRequestMethodNotSupportedException)15 Test (org.junit.jupiter.api.Test)5 ModelAndView (org.springframework.web.servlet.ModelAndView)4 IOException (java.io.IOException)3 Test (org.junit.Test)2 TypeMismatchException (org.springframework.beans.TypeMismatchException)2 BindException (org.springframework.validation.BindException)2 HttpMediaTypeNotAcceptableException (org.springframework.web.HttpMediaTypeNotAcceptableException)2 HttpMediaTypeNotSupportedException (org.springframework.web.HttpMediaTypeNotSupportedException)2 MissingServletRequestParameterException (org.springframework.web.bind.MissingServletRequestParameterException)2 ServletRequestBindingException (org.springframework.web.bind.ServletRequestBindingException)2 RestfulResult (cn.opencil.vo.RestfulResult)1 AccessDeniedException (com.baidu.dsp.common.exception.AccessDeniedException)1 DocumentNotFoundException (com.baidu.dsp.common.exception.DocumentNotFoundException)1 FieldException (com.baidu.dsp.common.exception.FieldException)1 GlobalExceptionAware (com.baidu.dsp.common.exception.base.GlobalExceptionAware)1 RequestMapping (com.revolsys.ui.web.annotation.RequestMapping)1 ValidationException (com.shaoqunliu.validation.ValidationException)1 ServletException (jakarta.servlet.ServletException)1 Method (java.lang.reflect.Method)1