@Test public void getMatchingConditionWithEmptyConditions() throws Exception { RequestMethodsRequestCondition condition = new RequestMethodsRequestCondition(); for (RequestMethod method : RequestMethod.values()) { if (method != OPTIONS) { ServerWebExchange exchange = getExchange(method.name()); assertNotNull(condition.getMatchingCondition(exchange)); } } testNoMatch(condition, OPTIONS); }
@Override public boolean supportsParameter(MethodParameter parameter) { if (parameter.getGenericParameterType() != java.lang.String.class) { return false; } RequestMapping requestMapping = parameter.getMethod().getAnnotation(RequestMapping.class); if (requestMapping != null) { RequestMethod[] rms = requestMapping.method(); if (rms != null) { for (RequestMethod rm : rms) { if (rm.name().equals(RequestMethod.GET.toString())) { return true; } } } } return false; }
/** * Extract {@link org.springframework.web.bind.annotation.RequestMapping}'s list of {@link RequestMethod}s into an * array of {@link String}s. * * @param type * @param method * @return */ @Override public Collection<HttpMethod> getRequestMethod(Class<?> type, Method method) { Assert.notNull(type, "Type must not be null!"); Assert.notNull(method, "Method must not be null!"); Annotation mergedAnnotation = findMergedAnnotation(method, annotationType); Object value = getValue(mergedAnnotation, "method"); RequestMethod[] requestMethods = (RequestMethod[]) value; List<HttpMethod> requestMethodNames = new ArrayList<>(); for (RequestMethod requestMethod : requestMethods) { requestMethodNames.add(HttpMethod.valueOf(requestMethod.toString())); } return requestMethodNames; }
public RequestMethod[] getRequestMethods() { RequestMethod[] requestMethods = new RequestMethod[this.methods.length]; for (int i = 0; i < this.methods.length; i++) { requestMethods[i] = RequestMethod.valueOf(this.methods[i].name()); } return requestMethods; }
@Override public void execute(RequestMappingContext context) { RequestMethod currentHttpMethod = (RequestMethod) context.get("currentHttpMethod"); HandlerMethod handlerMethod = context.getHandlerMethod(); String requestMethod = currentHttpMethod.toString(); ApiOperation apiOperationAnnotation = handlerMethod.getMethodAnnotation(ApiOperation.class); if (apiOperationAnnotation != null && StringUtils.hasText(apiOperationAnnotation.httpMethod())) { String apiMethod = apiOperationAnnotation.httpMethod(); try { RequestMethod.valueOf(apiMethod); requestMethod = apiMethod; } catch (IllegalArgumentException e) { log.error("Invalid http method: " + apiMethod + "Valid ones are [" + RequestMethod.values() + "]", e); } } context.put("httpRequestMethod", requestMethod); } }
@Override public void apply(OperationContext context) { Optional<ApiOperation> apiOperationAnnotation = context.findAnnotation(ApiOperation.class); if (apiOperationAnnotation.isPresent() && StringUtils.hasText(apiOperationAnnotation.get().httpMethod())) { String apiMethod = apiOperationAnnotation.get().httpMethod(); try { RequestMethod.valueOf(apiMethod); context.operationBuilder().method(HttpMethod.valueOf(apiMethod)); } catch (IllegalArgumentException e) { log.error("Invalid http method: " + apiMethod + "Valid ones are [" + RequestMethod.values() + "]", e); } } }
private void apiHandle ( PermissionResourceForm form , PermissionResource resource ) { if ( Objects.equals( resource.getResourceType().getValue() , ResourceType.API.getValue() ) ) { final Set< String > methods = Stream.of( RequestMethod.values() ) .map( RequestMethod::name ) .collect( Collectors.toSet() ); for ( String method : form.getResourceApiUriMethods() ) { AssertUtils.isTrue( ! methods.contains( method ) , "操作失败,resourceApiUriMethods格式不正确" ); } resource.setResourceApiUriMethods( form.getResourceApiUriMethods() .parallelStream() .collect( Collectors.joining( "," ) ) ); // 接口类型处理 // + 接口类型权限资源, 这两个字段不能为空 -> resourceApiUri resourceApiUriMethods AssertUtils.isTrue( StringUtils.isBlank( resource.getResourceApiUri() ) , "api类型权限资源,resourceApiUri字段不能为空" ); AssertUtils.isTrue( StringUtils.isBlank( resource.getResourceApiUriMethods() ) , "api类型权限资源,resourceApiUriMethods字段不能为空" ); } }
private RequestMappingInfo createRequestMappingInfo(WebOperation operation) { WebOperationRequestPredicate predicate = operation.getRequestPredicate(); PatternsRequestCondition patterns = patternsRequestConditionForPattern( predicate.getPath()); RequestMethodsRequestCondition methods = new RequestMethodsRequestCondition( RequestMethod.valueOf(predicate.getHttpMethod().name())); ConsumesRequestCondition consumes = new ConsumesRequestCondition( StringUtils.toStringArray(predicate.getConsumes())); ProducesRequestCondition produces = new ProducesRequestCondition( StringUtils.toStringArray(predicate.getProduces())); return new RequestMappingInfo(null, patterns, methods, null, null, consumes, produces, null); }
@Override public void apply(OperationContext context) { HandlerMethod handlerMethod = context.getHandlerMethod(); ApiOperation apiOperationAnnotation = handlerMethod.getMethodAnnotation(ApiOperation.class); if (apiOperationAnnotation != null && StringUtils.hasText(apiOperationAnnotation.httpMethod())) { String apiMethod = apiOperationAnnotation.httpMethod(); try { RequestMethod.valueOf(apiMethod); context.operationBuilder().method(apiMethod); } catch (IllegalArgumentException e) { log.error("Invalid http method: " + apiMethod + "Valid ones are [" + RequestMethod.values() + "]", e); } } }
/** * Return declared HTTP methods. */ public Set<HttpMethod> getAllowedMethods() { return this.partialMatches.stream(). flatMap(m -> m.getInfo().getMethodsCondition().getMethods().stream()). map(requestMethod -> HttpMethod.resolve(requestMethod.name())). collect(Collectors.toSet()); }
@Test public void getMatchingConditionWithEmptyConditions() { RequestMethodsRequestCondition condition = new RequestMethodsRequestCondition(); for (RequestMethod method : RequestMethod.values()) { if (method != OPTIONS) { HttpServletRequest request = new MockHttpServletRequest(method.name(), ""); assertNotNull(condition.getMatchingCondition(request)); } } testNoMatch(condition, OPTIONS); }
public String httpMethod() { return requestMethod.toString(); }
@Override public RequestMethod getMethod() { return RequestMethod.valueOf(request.getMethod()); }
/** * Return declared HTTP methods. */ public Set<String> getAllowedMethods() { Set<String> result = new LinkedHashSet<>(); for (PartialMatch match : this.partialMatches) { for (RequestMethod method : match.getInfo().getMethodsCondition().getMethods()) { result.add(method.name()); } } return result; }
String httpMethod = requestMethod.toString().toLowerCase(); Operation operation = parseMethod(method, requestMethod);
private RequestMappingInfo createRequestMappingInfo(WebOperation operation) { WebOperationRequestPredicate predicate = operation.getRequestPredicate(); PatternsRequestCondition patterns = new PatternsRequestCondition(pathPatternParser .parse(this.endpointMapping.createSubPath(predicate.getPath()))); RequestMethodsRequestCondition methods = new RequestMethodsRequestCondition( RequestMethod.valueOf(predicate.getHttpMethod().name())); ConsumesRequestCondition consumes = new ConsumesRequestCondition( StringUtils.toStringArray(predicate.getConsumes())); ProducesRequestCondition produces = new ProducesRequestCondition( StringUtils.toStringArray(predicate.getProduces())); return new RequestMappingInfo(null, patterns, methods, null, null, consumes, produces, null); }
@Nullable private RequestMethodsRequestCondition matchRequestMethod(@Nullable HttpMethod httpMethod) { 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; }