org.springframework.http.HttpMethod#resolve ( )源码实例Demo

下面列出了org.springframework.http.HttpMethod#resolve ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: spring4-understanding   文件: CorsConfiguration.java
/**
 * 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);
}
 
源代码2 项目: dhis2-core   文件: NoCacheInterceptor.java
@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();
}
 
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;
	}
}
 
private HttpMethod resolveHttpMethod(String sHttpMethod) {
    if (StringUtils.isBlank(sHttpMethod)) {
        return null;
    }

    return HttpMethod.resolve(sHttpMethod.toUpperCase());
}
 
@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;
}
 
@Override
public HttpMethod getMethod() {
	return HttpMethod.resolve(this.httpRequest.getMethod());
}
 
源代码7 项目: lams   文件: ServletServerHttpRequest.java
@Override
public HttpMethod getMethod() {
	return HttpMethod.resolve(this.servletRequest.getMethod());
}
 
@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());
	}
}
 
@Override
public HttpMethod getMethod() {
	return HttpMethod.resolve(this.connection.getRequestMethod());
}
 
@Override
public HttpMethod getMethod() {
	HttpMethod method = HttpMethod.resolve(this.jettyRequest.getMethod());
	Assert.state(method != null, "Method must not be null");
	return method;
}
 
源代码11 项目: spring-boot-demo   文件: JwtAuthenticationFilter.java
/**
 * 请求是否不需要进行权限拦截
 *
 * @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;
}
 
@Override
public HttpMethod getMethod() {
	return HttpMethod.resolve(this.httpRequest.getMethod());
}
 
@Override
public HttpMethod getRequestMethod() {
	return HttpMethod.resolve(getRequest().getMethod());
}
 
源代码14 项目: spring-boot-demo   文件: JwtAuthenticationFilter.java
/**
 * 请求是否不需要进行权限拦截
 *
 * @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;
}
 
@Override
public HttpMethod getMethod() {
	return HttpMethod.resolve(this.httpRequest.getMethod());
}
 
public HttpMethod getMethod() {
	return HttpMethod.resolve(this.connection.getRequestMethod());
}
 
@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);
    }
}
 
源代码18 项目: lams   文件: AbstractMultipartHttpServletRequest.java
@Override
public HttpMethod getRequestMethod() {
	return HttpMethod.resolve(getRequest().getMethod());
}
 
源代码19 项目: lams   文件: HttpComponentsClientHttpRequest.java
@Override
public HttpMethod getMethod() {
	return HttpMethod.resolve(this.httpRequest.getMethod());
}
 
源代码20 项目: spring-analysis-note   文件: ServerRequest.java
/**
 * 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());
}