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

The following examples show how to use org.springframework.http.HttpMethod#resolve() . These examples are extracted from open source projects. 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
/**
 * 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
@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
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 4
private HttpMethod resolveHttpMethod(String sHttpMethod) {
    if (StringUtils.isBlank(sHttpMethod)) {
        return null;
    }

    return HttpMethod.resolve(sHttpMethod.toUpperCase());
}
 
Example 5
@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 6
@Override
public HttpMethod getMethod() {
	return HttpMethod.resolve(this.httpRequest.getMethod());
}
 
Example 7
@Override
public HttpMethod getMethod() {
	return HttpMethod.resolve(this.servletRequest.getMethod());
}
 
Example 8
@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 9
@Override
public HttpMethod getMethod() {
	return HttpMethod.resolve(this.connection.getRequestMethod());
}
 
Example 10
@Override
public HttpMethod getMethod() {
	HttpMethod method = HttpMethod.resolve(this.jettyRequest.getMethod());
	Assert.state(method != null, "Method must not be null");
	return method;
}
 
Example 11
/**
 * 请求是否不需要进行权限拦截
 *
 * @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 12
@Override
public HttpMethod getMethod() {
	return HttpMethod.resolve(this.httpRequest.getMethod());
}
 
Example 13
@Override
public HttpMethod getRequestMethod() {
	return HttpMethod.resolve(getRequest().getMethod());
}
 
Example 14
/**
 * 请求是否不需要进行权限拦截
 *
 * @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 15
@Override
public HttpMethod getMethod() {
	return HttpMethod.resolve(this.httpRequest.getMethod());
}
 
Example 16
public HttpMethod getMethod() {
	return HttpMethod.resolve(this.connection.getRequestMethod());
}
 
Example 17
@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 18
@Override
public HttpMethod getRequestMethod() {
	return HttpMethod.resolve(getRequest().getMethod());
}
 
Example 19
@Override
public HttpMethod getMethod() {
	return HttpMethod.resolve(this.httpRequest.getMethod());
}
 
Example 20
Source Project: spring-analysis-note   File: ServerRequest.java    License: 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());
}