Search in sources :

Example 6 with HttpRequestMethodNotSupportedException

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

the class MvcRequestMatcherTests method matchesGetMatchableHandlerMappingThrows.

@Test
public void matchesGetMatchableHandlerMappingThrows() throws Exception {
    given(this.introspector.getMatchableHandlerMapping(this.request)).willThrow(new HttpRequestMethodNotSupportedException(this.request.getMethod()));
    assertThat(this.matcher.matches(this.request)).isTrue();
}
Also used : HttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException) Test(org.junit.jupiter.api.Test)

Example 7 with HttpRequestMethodNotSupportedException

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

the class RequestMappingInfoHandlerMapping method handleNoMatch.

/**
 * Iterate all RequestMappingInfo's once again, look if any match by URL at
 * least and raise exceptions according to what doesn't match.
 * @throws HttpRequestMethodNotSupportedException if there are matches by URL
 * but not by HTTP method
 * @throws HttpMediaTypeNotAcceptableException if there are matches by URL
 * but not by consumable/producible media types
 */
@Override
protected HandlerMethod handleNoMatch(Set<RequestMappingInfo> infos, String lookupPath, HttpServletRequest request) throws ServletException {
    PartialMatchHelper helper = new PartialMatchHelper(infos, request);
    if (helper.isEmpty()) {
        return null;
    }
    if (helper.hasMethodsMismatch()) {
        Set<String> methods = helper.getAllowedMethods();
        if (HttpMethod.OPTIONS.matches(request.getMethod())) {
            Set<MediaType> mediaTypes = helper.getConsumablePatchMediaTypes();
            HttpOptionsHandler handler = new HttpOptionsHandler(methods, mediaTypes);
            return new HandlerMethod(handler, HTTP_OPTIONS_HANDLE_METHOD);
        }
        throw new HttpRequestMethodNotSupportedException(request.getMethod(), methods);
    }
    if (helper.hasConsumesMismatch()) {
        Set<MediaType> mediaTypes = helper.getConsumableMediaTypes();
        MediaType contentType = null;
        if (StringUtils.hasLength(request.getContentType())) {
            try {
                contentType = MediaType.parseMediaType(request.getContentType());
            } catch (InvalidMediaTypeException ex) {
                throw new HttpMediaTypeNotSupportedException(ex.getMessage());
            }
        }
        throw new HttpMediaTypeNotSupportedException(contentType, new ArrayList<>(mediaTypes));
    }
    if (helper.hasProducesMismatch()) {
        Set<MediaType> mediaTypes = helper.getProducibleMediaTypes();
        throw new HttpMediaTypeNotAcceptableException(new ArrayList<>(mediaTypes));
    }
    if (helper.hasParamsMismatch()) {
        List<String[]> conditions = helper.getParamConditions();
        throw new UnsatisfiedServletRequestParameterException(conditions, request.getParameterMap());
    }
    return null;
}
Also used : UnsatisfiedServletRequestParameterException(org.springframework.web.bind.UnsatisfiedServletRequestParameterException) HttpMediaTypeNotAcceptableException(org.springframework.web.HttpMediaTypeNotAcceptableException) HttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException) HandlerMethod(org.springframework.web.method.HandlerMethod) HttpMediaTypeNotSupportedException(org.springframework.web.HttpMediaTypeNotSupportedException) MediaType(org.springframework.http.MediaType) InvalidMediaTypeException(org.springframework.http.InvalidMediaTypeException)

Example 8 with HttpRequestMethodNotSupportedException

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

the class CompositeHandlerExceptionResolverTests method resolverShouldDelegateToOtherResolversInContext.

@Test
void resolverShouldDelegateToOtherResolversInContext() {
    load(TestConfiguration.class);
    CompositeHandlerExceptionResolver resolver = (CompositeHandlerExceptionResolver) this.context.getBean(DispatcherServlet.HANDLER_EXCEPTION_RESOLVER_BEAN_NAME);
    ModelAndView resolved = resolver.resolveException(this.request, this.response, null, new HttpRequestMethodNotSupportedException("POST"));
    assertThat(resolved.getViewName()).isEqualTo("test-view");
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) HttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException) Test(org.junit.jupiter.api.Test)

Example 9 with HttpRequestMethodNotSupportedException

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

the class MainFilterChain method doFilter.

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;
    try {
        super.doFilter(request, response, chain);
    } catch (RequestRejectedException e) {
        REQUEST_REJECTED_EXCEPTION_HANDLER.handle(request, response, e.getMessage(), HttpStatus.BAD_REQUEST);
    } catch (HttpRequestMethodNotSupportedException e) {
        REQUEST_REJECTED_EXCEPTION_HANDLER.handle(request, response, e.getMessage(), HttpStatus.METHOD_NOT_ALLOWED);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestRejectedException(org.springframework.security.web.firewall.RequestRejectedException) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException)

Example 10 with HttpRequestMethodNotSupportedException

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

the class ClientCredentialsTokenEndpointFilter method attemptAuthentication.

@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
    if (allowOnlyPost && !"POST".equalsIgnoreCase(request.getMethod())) {
        throw new HttpRequestMethodNotSupportedException(request.getMethod(), new String[] { "POST" });
    }
    String clientId = request.getParameter("client_id");
    String clientSecret = request.getParameter("client_secret");
    // If the request is already authenticated we can assume that this
    // filter is not needed
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null && authentication.isAuthenticated()) {
        return authentication;
    }
    if (clientId == null) {
        throw new BadCredentialsException("No client credentials presented");
    }
    if (clientSecret == null) {
        clientSecret = "";
    }
    clientId = clientId.trim();
    UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(clientId, clientSecret);
    return this.getAuthenticationManager().authenticate(authRequest);
}
Also used : Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) HttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException)

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