Search in sources :

Example 6 with HttpMethod

use of org.springframework.http.HttpMethod in project spring-framework by spring-projects.

the class DefaultCorsProcessor method handleInternal.

/**
	 * Handle the given request.
	 */
protected boolean handleInternal(ServerWebExchange exchange, CorsConfiguration config, boolean preFlightRequest) {
    ServerHttpRequest request = exchange.getRequest();
    ServerHttpResponse response = exchange.getResponse();
    String requestOrigin = request.getHeaders().getOrigin();
    String allowOrigin = checkOrigin(config, requestOrigin);
    HttpMethod requestMethod = getMethodToUse(request, preFlightRequest);
    List<HttpMethod> allowMethods = checkMethods(config, requestMethod);
    List<String> requestHeaders = getHeadersToUse(request, preFlightRequest);
    List<String> allowHeaders = checkHeaders(config, requestHeaders);
    if (allowOrigin == null || allowMethods == null || (preFlightRequest && allowHeaders == null)) {
        rejectRequest(response);
        return false;
    }
    HttpHeaders responseHeaders = response.getHeaders();
    responseHeaders.setAccessControlAllowOrigin(allowOrigin);
    responseHeaders.add(HttpHeaders.VARY, HttpHeaders.ORIGIN);
    if (preFlightRequest) {
        responseHeaders.setAccessControlAllowMethods(allowMethods);
    }
    if (preFlightRequest && !allowHeaders.isEmpty()) {
        responseHeaders.setAccessControlAllowHeaders(allowHeaders);
    }
    if (!CollectionUtils.isEmpty(config.getExposedHeaders())) {
        responseHeaders.setAccessControlExposeHeaders(config.getExposedHeaders());
    }
    if (Boolean.TRUE.equals(config.getAllowCredentials())) {
        responseHeaders.setAccessControlAllowCredentials(true);
    }
    if (preFlightRequest && config.getMaxAge() != null) {
        responseHeaders.setAccessControlMaxAge(config.getMaxAge());
    }
    return true;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) HttpMethod(org.springframework.http.HttpMethod)

Example 7 with HttpMethod

use of org.springframework.http.HttpMethod in project spring-framework by spring-projects.

the class InterceptingClientHttpRequestFactoryTests method changeMethod.

@Test
public void changeMethod() throws Exception {
    final HttpMethod changedMethod = HttpMethod.POST;
    ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() {

        @Override
        public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
            return execution.execute(new HttpRequestWrapper(request) {

                @Override
                public HttpMethod getMethod() {
                    return changedMethod;
                }
            }, body);
        }
    };
    requestFactoryMock = new RequestFactoryMock() {

        @Override
        public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
            assertEquals(changedMethod, httpMethod);
            return super.createRequest(uri, httpMethod);
        }
    };
    requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor));
    ClientHttpRequest request = requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET);
    request.execute();
}
Also used : HttpRequest(org.springframework.http.HttpRequest) HttpRequestWrapper(org.springframework.http.client.support.HttpRequestWrapper) IOException(java.io.IOException) URI(java.net.URI) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Example 8 with HttpMethod

use of org.springframework.http.HttpMethod in project spring-framework by spring-projects.

the class AsyncRestTemplateIntegrationTests method optionsForAllow.

@Test
public void optionsForAllow() throws Exception {
    Future<Set<HttpMethod>> allowedFuture = template.optionsForAllow(new URI(baseUrl + "/get"));
    Set<HttpMethod> allowed = allowedFuture.get();
    assertEquals("Invalid response", EnumSet.of(HttpMethod.GET, HttpMethod.OPTIONS, HttpMethod.HEAD, HttpMethod.TRACE), allowed);
}
Also used : EnumSet(java.util.EnumSet) Set(java.util.Set) URI(java.net.URI) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Example 9 with HttpMethod

use of org.springframework.http.HttpMethod in project spring-framework by spring-projects.

the class HttpPutFormContentFilterTests method wrapPutAndPatchOnly.

@Test
public void wrapPutAndPatchOnly() throws Exception {
    request.setContent("".getBytes("ISO-8859-1"));
    for (HttpMethod method : HttpMethod.values()) {
        request.setMethod(method.name());
        filterChain = new MockFilterChain();
        filter.doFilter(request, response, filterChain);
        if (method.equals(HttpMethod.PUT) || method.equals(HttpMethod.PATCH)) {
            assertNotSame("Should wrap HTTP method " + method, request, filterChain.getRequest());
        } else {
            assertSame("Should not wrap for HTTP method " + method, request, filterChain.getRequest());
        }
    }
}
Also used : MockFilterChain(org.springframework.mock.web.test.MockFilterChain) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Example 10 with HttpMethod

use of org.springframework.http.HttpMethod in project spring-framework by spring-projects.

the class RequestMappingInfoHandlerMapping method handleNoMatch.

/**
	 * Iterate all RequestMappingInfos once again, look if any match by URL at
	 * least and raise exceptions accordingly.
	 * @throws MethodNotAllowedException for matches by URL but not by HTTP method
	 * @throws UnsupportedMediaTypeStatusException if there are matches by URL
	 * and HTTP method but not by consumable media types
	 * @throws NotAcceptableStatusException if there are matches by URL and HTTP
	 * method but not by producible media types
	 * @throws ServerWebInputException if there are matches by URL and HTTP
	 * method but not by query parameter conditions
	 */
@Override
protected HandlerMethod handleNoMatch(Set<RequestMappingInfo> infos, String lookupPath, ServerWebExchange exchange) throws Exception {
    PartialMatchHelper helper = new PartialMatchHelper(infos, exchange);
    if (helper.isEmpty()) {
        return null;
    }
    ServerHttpRequest request = exchange.getRequest();
    if (helper.hasMethodsMismatch()) {
        HttpMethod httpMethod = request.getMethod();
        Set<String> methods = helper.getAllowedMethods();
        if (HttpMethod.OPTIONS.matches(httpMethod.name())) {
            HttpOptionsHandler handler = new HttpOptionsHandler(methods);
            return new HandlerMethod(handler, HTTP_OPTIONS_HANDLE_METHOD);
        }
        throw new MethodNotAllowedException(httpMethod.name(), methods);
    }
    if (helper.hasConsumesMismatch()) {
        Set<MediaType> mediaTypes = helper.getConsumableMediaTypes();
        MediaType contentType;
        try {
            contentType = request.getHeaders().getContentType();
        } catch (InvalidMediaTypeException ex) {
            throw new UnsupportedMediaTypeStatusException(ex.getMessage());
        }
        throw new UnsupportedMediaTypeStatusException(contentType, new ArrayList<>(mediaTypes));
    }
    if (helper.hasProducesMismatch()) {
        Set<MediaType> mediaTypes = helper.getProducibleMediaTypes();
        throw new NotAcceptableStatusException(new ArrayList<>(mediaTypes));
    }
    if (helper.hasParamsMismatch()) {
        throw new ServerWebInputException("Unsatisfied query parameter conditions: " + helper.getParamConditions() + ", actual parameters: " + request.getQueryParams());
    }
    return null;
}
Also used : NotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException) ServerWebInputException(org.springframework.web.server.ServerWebInputException) MethodNotAllowedException(org.springframework.web.server.MethodNotAllowedException) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) HandlerMethod(org.springframework.web.method.HandlerMethod) UnsupportedMediaTypeStatusException(org.springframework.web.server.UnsupportedMediaTypeStatusException) MediaType(org.springframework.http.MediaType) HttpMethod(org.springframework.http.HttpMethod) InvalidMediaTypeException(org.springframework.http.InvalidMediaTypeException)

Aggregations

HttpMethod (org.springframework.http.HttpMethod)37 Test (org.junit.Test)19 URI (java.net.URI)16 IOException (java.io.IOException)11 HttpHeaders (org.springframework.http.HttpHeaders)8 ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)7 ClientHttpRequestFactory (org.springframework.http.client.ClientHttpRequestFactory)7 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)4 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)4 HttpRequest (org.springframework.http.HttpRequest)3 AccessTokenRequest (org.springframework.security.oauth2.client.token.AccessTokenRequest)3 DefaultAccessTokenRequest (org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest)3 EnumSet (java.util.EnumSet)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Set (java.util.Set)2 TreeSet (java.util.TreeSet)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)2