Java Code Examples for org.springframework.http.HttpMethod#resolve()

The following examples show how to use org.springframework.http.HttpMethod#resolve() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CorsConfiguration.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Check the HTTP request method (or the method from the
 * {@code Access-Control-Request-Method} header on a pre-flight request)
 * against the configured allowed methods.
 * @param requestMethod the HTTP request method to check
 * @return the list of HTTP methods to list in the response of a pre-flight
 * request, or {@code null} if the supplied {@code requestMethod} is not allowed
 */
public List<HttpMethod> checkHttpMethod(HttpMethod requestMethod) {
	if (requestMethod == null) {
		return null;
	}
	List<String> allowedMethods =
			(this.allowedMethods != null ? this.allowedMethods : new ArrayList<String>());
	if (allowedMethods.contains(ALL)) {
		return Collections.singletonList(requestMethod);
	}
	if (allowedMethods.isEmpty()) {
		allowedMethods.add(HttpMethod.GET.name());
	}
	List<HttpMethod> result = new ArrayList<HttpMethod>(allowedMethods.size());
	boolean allowed = false;
	for (String method : allowedMethods) {
		if (requestMethod.matches(method)) {
			allowed = true;
		}
		HttpMethod resolved = HttpMethod.resolve(method);
		if (resolved != null) {
			result.add(resolved);
		}
	}
	return (allowed ? result : null);
}
 
Example 2
Source File: NoCacheInterceptor.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public String intercept( ActionInvocation invocation )
    throws Exception
{
    HttpServletRequest request = ServletActionContext.getRequest();
    HttpServletResponse response = ServletActionContext.getResponse();
    
    String header = response.getHeader( ContextUtils.HEADER_CACHE_CONTROL );
    boolean headerSet = header != null && !header.trim().isEmpty();
    
    if ( !headerSet && HttpMethod.GET == HttpMethod.resolve( request.getMethod() ) )
    {
        ContextUtils.setNoStore( response );
    }
            
    return invocation.invoke();
}
 
Example 3
Source File: RequestMethodsRequestCondition.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Nullable
private RequestMethodsRequestCondition matchRequestMethod(String httpMethodValue) {
	HttpMethod httpMethod = HttpMethod.resolve(httpMethodValue);
	if (httpMethod != null) {
		for (RequestMethod method : getMethods()) {
			if (httpMethod.matches(method.name())) {
				return new RequestMethodsRequestCondition(method);
			}
		}
		if (httpMethod == HttpMethod.HEAD && getMethods().contains(RequestMethod.GET)) {
			return GET_CONDITION;
		}
	}
	return null;
}
 
Example 4
Source File: DynamicRouteItemInfoHolder.java    From wecube-platform with Apache License 2.0 5 votes vote down vote up
private HttpMethod resolveHttpMethod(String sHttpMethod) {
    if (StringUtils.isBlank(sHttpMethod)) {
        return null;
    }

    return HttpMethod.resolve(sHttpMethod.toUpperCase());
}
 
Example 5
Source File: HiddenHttpMethodFilter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private ServerWebExchange mapExchange(ServerWebExchange exchange, String methodParamValue) {
	HttpMethod httpMethod = HttpMethod.resolve(methodParamValue.toUpperCase(Locale.ENGLISH));
	Assert.notNull(httpMethod, () -> "HttpMethod '" + methodParamValue + "' not supported");
	if (ALLOWED_METHODS.contains(httpMethod)) {
		return exchange.mutate().request(builder -> builder.method(httpMethod)).build();
	}
	else {
		return exchange;
	}
}
 
Example 6
Source File: AbstractMultipartHttpServletRequest.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public HttpMethod getRequestMethod() {
	return HttpMethod.resolve(getRequest().getMethod());
}
 
Example 7
Source File: HttpComponentsClientHttpRequest.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public HttpMethod getMethod() {
	return HttpMethod.resolve(this.httpRequest.getMethod());
}
 
Example 8
Source File: AbstractMultipartHttpServletRequest.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public HttpMethod getRequestMethod() {
	return HttpMethod.resolve(getRequest().getMethod());
}
 
Example 9
Source File: ResourceAuthorizationFilter.java    From nifi-registry with Apache License 2.0 4 votes vote down vote up
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

    HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
    HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;

    boolean authorizationCheckIsRequired = false;
    String resourcePath = null;
    RequestAction action = null;

    // Only require authorization if the NiFi Registry is running securely.
    if (servletRequest.isSecure()) {

        // Only require authorization for resources for which this filter has been configured
        resourcePath = httpServletRequest.getServletPath();
        if (resourcePath != null) {
            final ResourceType resourceType = ResourceType.mapFullResourcePathToResourceType(resourcePath);
            final HttpMethodAuthorizationRules authorizationRules = resourceTypeAuthorizationRules.get(resourceType);
            if (authorizationRules != null) {
                final String httpMethodStr = httpServletRequest.getMethod().toUpperCase();
                HttpMethod httpMethod = HttpMethod.resolve(httpMethodStr);

                // Only require authorization for HTTP methods included in this resource type's rule set
                if (httpMethod != null && authorizationRules.requiresAuthorization(httpMethod)) {
                    authorizationCheckIsRequired = true;
                    action = authorizationRules.mapHttpMethodToAction(httpMethod);
                }
            }
        }
    }

    if (!authorizationCheckIsRequired) {
        forwardRequestWithoutAuthorizationCheck(httpServletRequest, httpServletResponse, filterChain);
        return;
    }

    // Perform authorization check
    try {
        authorizeAccess(resourcePath, action);
        successfulAuthorization(httpServletRequest, httpServletResponse, filterChain);
    } catch (Exception e) {
        logger.debug("Exception occurred while performing authorization check.", e);
        failedAuthorization(httpServletRequest, httpServletResponse, filterChain, e);
    }
}
 
Example 10
Source File: SimpleStreamingClientHttpRequest.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public HttpMethod getMethod() {
	return HttpMethod.resolve(this.connection.getRequestMethod());
}
 
Example 11
Source File: HttpComponentsClientHttpRequest.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public HttpMethod getMethod() {
	return HttpMethod.resolve(this.httpRequest.getMethod());
}
 
Example 12
Source File: JwtAuthenticationFilter.java    From spring-boot-demo with MIT License 4 votes vote down vote up
/**
 * 请求是否不需要进行权限拦截
 *
 * @param request 当前请求
 * @return true - 忽略,false - 不忽略
 */
private boolean checkIgnores(HttpServletRequest request) {
    String method = request.getMethod();

    HttpMethod httpMethod = HttpMethod.resolve(method);
    if (ObjectUtil.isNull(httpMethod)) {
        httpMethod = HttpMethod.GET;
    }

    Set<String> ignores = Sets.newHashSet();

    switch (httpMethod) {
        case GET:
            ignores.addAll(customConfig.getIgnores()
                    .getGet());
            break;
        case PUT:
            ignores.addAll(customConfig.getIgnores()
                    .getPut());
            break;
        case HEAD:
            ignores.addAll(customConfig.getIgnores()
                    .getHead());
            break;
        case POST:
            ignores.addAll(customConfig.getIgnores()
                    .getPost());
            break;
        case PATCH:
            ignores.addAll(customConfig.getIgnores()
                    .getPatch());
            break;
        case TRACE:
            ignores.addAll(customConfig.getIgnores()
                    .getTrace());
            break;
        case DELETE:
            ignores.addAll(customConfig.getIgnores()
                    .getDelete());
            break;
        case OPTIONS:
            ignores.addAll(customConfig.getIgnores()
                    .getOptions());
            break;
        default:
            break;
    }

    ignores.addAll(customConfig.getIgnores()
            .getPattern());

    if (CollUtil.isNotEmpty(ignores)) {
        for (String ignore : ignores) {
            AntPathRequestMatcher matcher = new AntPathRequestMatcher(ignore, method);
            if (matcher.matches(request)) {
                return true;
            }
        }
    }

    return false;
}
 
Example 13
Source File: HttpComponentsStreamingClientHttpRequest.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public HttpMethod getMethod() {
	return HttpMethod.resolve(this.httpRequest.getMethod());
}
 
Example 14
Source File: HttpComponentsAsyncClientHttpRequest.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public HttpMethod getMethod() {
	return HttpMethod.resolve(this.httpRequest.getMethod());
}
 
Example 15
Source File: JwtAuthenticationFilter.java    From spring-boot-demo with MIT License 4 votes vote down vote up
/**
 * 请求是否不需要进行权限拦截
 *
 * @param request 当前请求
 * @return true - 忽略,false - 不忽略
 */
private boolean checkIgnores(HttpServletRequest request) {
    String method = request.getMethod();

    HttpMethod httpMethod = HttpMethod.resolve(method);
    if (ObjectUtil.isNull(httpMethod)) {
        httpMethod = HttpMethod.GET;
    }

    Set<String> ignores = Sets.newHashSet();

    switch (httpMethod) {
        case GET:
            ignores.addAll(customConfig.getIgnores()
                    .getGet());
            break;
        case PUT:
            ignores.addAll(customConfig.getIgnores()
                    .getPut());
            break;
        case HEAD:
            ignores.addAll(customConfig.getIgnores()
                    .getHead());
            break;
        case POST:
            ignores.addAll(customConfig.getIgnores()
                    .getPost());
            break;
        case PATCH:
            ignores.addAll(customConfig.getIgnores()
                    .getPatch());
            break;
        case TRACE:
            ignores.addAll(customConfig.getIgnores()
                    .getTrace());
            break;
        case DELETE:
            ignores.addAll(customConfig.getIgnores()
                    .getDelete());
            break;
        case OPTIONS:
            ignores.addAll(customConfig.getIgnores()
                    .getOptions());
            break;
        default:
            break;
    }

    ignores.addAll(customConfig.getIgnores()
            .getPattern());

    if (CollUtil.isNotEmpty(ignores)) {
        for (String ignore : ignores) {
            AntPathRequestMatcher matcher = new AntPathRequestMatcher(ignore, method);
            if (matcher.matches(request)) {
                return true;
            }
        }
    }

    return false;
}
 
Example 16
Source File: JettyClientHttpRequest.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public HttpMethod getMethod() {
	HttpMethod method = HttpMethod.resolve(this.jettyRequest.getMethod());
	Assert.state(method != null, "Method must not be null");
	return method;
}
 
Example 17
Source File: SimpleStreamingAsyncClientHttpRequest.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public HttpMethod getMethod() {
	return HttpMethod.resolve(this.connection.getRequestMethod());
}
 
Example 18
Source File: ServletRequestMethodArgumentResolver.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
		NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {

	Class<?> paramType = parameter.getParameterType();
	if (WebRequest.class.isAssignableFrom(paramType)) {
		if (!paramType.isInstance(webRequest)) {
			throw new IllegalStateException(
					"Current request is not of type [" + paramType.getName() + "]: " + webRequest);
		}
		return webRequest;
	}

	HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
	if (ServletRequest.class.isAssignableFrom(paramType) || MultipartRequest.class.isAssignableFrom(paramType)) {
		Object nativeRequest = webRequest.getNativeRequest(paramType);
		if (nativeRequest == null) {
			throw new IllegalStateException(
					"Current request is not of type [" + paramType.getName() + "]: " + request);
		}
		return nativeRequest;
	}
	else if (HttpSession.class.isAssignableFrom(paramType)) {
		HttpSession session = request.getSession();
		if (session != null && !paramType.isInstance(session)) {
			throw new IllegalStateException(
					"Current session is not of type [" + paramType.getName() + "]: " + session);
		}
		return session;
	}
	else if (InputStream.class.isAssignableFrom(paramType)) {
		InputStream inputStream = request.getInputStream();
		if (inputStream != null && !paramType.isInstance(inputStream)) {
			throw new IllegalStateException(
					"Request input stream is not of type [" + paramType.getName() + "]: " + inputStream);
		}
		return inputStream;
	}
	else if (Reader.class.isAssignableFrom(paramType)) {
		Reader reader = request.getReader();
		if (reader != null && !paramType.isInstance(reader)) {
			throw new IllegalStateException(
					"Request body reader is not of type [" + paramType.getName() + "]: " + reader);
		}
		return reader;
	}
	else if (Principal.class.isAssignableFrom(paramType)) {
		Principal userPrincipal = request.getUserPrincipal();
		if (userPrincipal != null && !paramType.isInstance(userPrincipal)) {
			throw new IllegalStateException(
					"Current user principal is not of type [" + paramType.getName() + "]: " + userPrincipal);
		}
		return userPrincipal;
	}
	else if (HttpMethod.class == paramType) {
		return HttpMethod.resolve(request.getMethod());
	}
	else if (Locale.class == paramType) {
		return RequestContextUtils.getLocale(request);
	}
	else if (TimeZone.class == paramType) {
		TimeZone timeZone = RequestContextUtils.getTimeZone(request);
		return (timeZone != null ? timeZone : TimeZone.getDefault());
	}
	else if ("java.time.ZoneId".equals(paramType.getName())) {
		return ZoneIdResolver.resolveZoneId(request);
	}
	else {
		// Should never happen...
		throw new UnsupportedOperationException(
				"Unknown parameter type [" + paramType.getName() + "] in " + parameter.getMethod());
	}
}
 
Example 19
Source File: ServletServerHttpRequest.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public HttpMethod getMethod() {
	return HttpMethod.resolve(this.servletRequest.getMethod());
}
 
Example 20
Source File: ServerRequest.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Get the HTTP method.
 * @return the HTTP method as an HttpMethod enum value, or {@code null}
 * if not resolvable (e.g. in case of a non-standard HTTP method)
 */
@Nullable
default HttpMethod method() {
	return HttpMethod.resolve(methodName());
}