Search in sources :

Example 31 with HttpMethod

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

the class HiddenHttpMethodFilter method mapExchange.

private ServerWebExchange mapExchange(ServerWebExchange exchange, String methodParamValue) {
    HttpMethod httpMethod = HttpMethod.resolve(methodParamValue.toUpperCase(Locale.ENGLISH));
    Assert.notNull(httpMethod, () -> "HttpMethod '" + methodParamValue + "' not supported");
    return exchange.mutate().request(builder -> builder.method(httpMethod)).build();
}
Also used : WebFilter(org.springframework.web.server.WebFilter) Locale(java.util.Locale) HttpMethod(org.springframework.http.HttpMethod) Mono(reactor.core.publisher.Mono) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) ServerWebExchange(org.springframework.web.server.ServerWebExchange) WebFilterChain(org.springframework.web.server.WebFilterChain) HttpMethod(org.springframework.http.HttpMethod)

Example 32 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(ServerHttpRequest request, ServerHttpResponse response, CorsConfiguration config, boolean preFlightRequest) throws IOException {
    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());
    }
    response.flush();
    return true;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpMethod(org.springframework.http.HttpMethod)

Example 33 with HttpMethod

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

the class InterceptingClientHttpRequestFactoryTests method changeURI.

@Test
public void changeURI() throws Exception {
    final URI changedUri = new URI("http://example.com/2");
    ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() {

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

                @Override
                public URI getURI() {
                    return changedUri;
                }
            }, body);
        }
    };
    requestFactoryMock = new RequestFactoryMock() {

        @Override
        public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
            assertEquals(changedUri, uri);
            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 34 with HttpMethod

use of org.springframework.http.HttpMethod in project nikita-noark5-core by HiOA-ABI.

the class AfterApplicationStartup method afterApplicationStarts.

/**
     * afterApplicationStarts, go through list of endpoints and make a list of endpoints and
     * the HTTP methods they support.
     *
     */
public void afterApplicationStarts() {
    for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMapping.getHandlerMethods().entrySet()) {
        RequestMappingInfo requestMappingInfo = entry.getKey();
        // Assuming there is always a non-null value
        String servletPaths = requestMappingInfo.getPatternsCondition().toString();
        // servletPath starts with "[" and ends with "]". Removing them if they are there
        if (true == servletPaths.startsWith("[")) {
            servletPaths = servletPaths.substring(1);
        }
        if (true == servletPaths.endsWith("]")) {
            servletPaths = servletPaths.substring(0, servletPaths.length() - 1);
        }
        String[] servletPathList = servletPaths.split("\\s+");
        for (String servletPath : servletPathList) {
            if (servletPath != null && false == servletPath.contains("|")) {
                // This is done to be consist on a lookup
                if (false == servletPath.endsWith("/")) {
                    servletPath += SLASH;
                }
                Set<RequestMethod> httpMethodRequests = requestMappingInfo.getMethodsCondition().getMethods();
                if (null != httpMethodRequests && null != servletPath) {
                    // RequestMethod and HTTPMethod are different types, have to convert them here
                    Set<HttpMethod> httpMethods = new TreeSet<>();
                    for (RequestMethod requestMethod : httpMethodRequests) {
                        if (requestMethod.equals(requestMethod.GET)) {
                            httpMethods.add(HttpMethod.GET);
                        } else if (requestMethod.equals(requestMethod.DELETE)) {
                            httpMethods.add(HttpMethod.DELETE);
                        } else if (requestMethod.equals(requestMethod.OPTIONS)) {
                            httpMethods.add(HttpMethod.OPTIONS);
                        } else if (requestMethod.equals(requestMethod.HEAD)) {
                            httpMethods.add(HttpMethod.HEAD);
                        } else if (requestMethod.equals(requestMethod.PATCH)) {
                            httpMethods.add(HttpMethod.PATCH);
                        } else if (requestMethod.equals(requestMethod.POST)) {
                            httpMethods.add(HttpMethod.POST);
                        } else if (requestMethod.equals(requestMethod.PUT)) {
                            httpMethods.add(HttpMethod.PUT);
                        } else if (requestMethod.equals(requestMethod.TRACE)) {
                            httpMethods.add(HttpMethod.TRACE);
                        }
                    }
                    out.println("Adding " + servletPath + " methods " + httpMethods);
                    CommonUtils.WebUtils.addRequestToMethodMap(servletPath, httpMethods);
                } else {
                    logger.warn("Missing HTTP Methods for the following servletPath [" + servletPath + "]");
                }
            }
        }
    }
}
Also used : RequestMappingInfo(org.springframework.web.servlet.mvc.method.RequestMappingInfo) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) TreeSet(java.util.TreeSet) Map(java.util.Map) HandlerMethod(org.springframework.web.method.HandlerMethod) HttpMethod(org.springframework.http.HttpMethod)

Example 35 with HttpMethod

use of org.springframework.http.HttpMethod in project nikita-noark5-core by HiOA-ABI.

the class SimpleCORSFilter method doFilter.

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;
    // Make sure this is a request for something that exists. If it's not just pass it on in the filter
    HttpMethod[] allowMethods = CommonUtils.WebUtils.getMethodsForRequest(request.getServletPath());
    if (allowMethods != null) {
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", Arrays.toString(allowMethods));
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, remember-me, Authorization, Origin");
        response.setHeader("Access-Control-Expose-Headers", "Allow, ETAG");
    }
    chain.doFilter(req, res);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpMethod(org.springframework.http.HttpMethod)

Aggregations

HttpMethod (org.springframework.http.HttpMethod)35 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 AccessTokenRequest (org.springframework.security.oauth2.client.token.AccessTokenRequest)4 DefaultAccessTokenRequest (org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest)4 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)4 HttpRequest (org.springframework.http.HttpRequest)3 EnumSet (java.util.EnumSet)2 HashMap (java.util.HashMap)2 Set (java.util.Set)2 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)2 ResolvableType (org.springframework.core.ResolvableType)2 InvalidMediaTypeException (org.springframework.http.InvalidMediaTypeException)2 MediaType (org.springframework.http.MediaType)2 HttpRequestWrapper (org.springframework.http.client.support.HttpRequestWrapper)2