/** * Create a {@link RequestMappingInfo} from the supplied * {@link RequestMapping @RequestMapping} annotation, which is either * a directly declared annotation, a meta-annotation, or the synthesized * result of merging annotation attributes within an annotation hierarchy. */ protected RequestMappingInfo createRequestMappingInfo( RequestMapping requestMapping, RequestCondition<?> customCondition) { return RequestMappingInfo .paths(resolveEmbeddedValuesInPatterns(requestMapping.path())) .methods(requestMapping.method()) .params(requestMapping.params()) .headers(requestMapping.headers()) .consumes(requestMapping.consumes()) .produces(requestMapping.produces()) .mappingName(requestMapping.name()) .customCondition(customCondition) .options(this.config) .build(); }
protected RequestMappingInfo createRequestMappingInfo(RequestMapping annotation, RequestCondition<?> customCondition,Object handler) { //XXX: not used RequestMapping if(annotation == null){ return createRequestMappingInfo(customCondition, handler); } String[] value = annotation.value(); String[] patterns = resolveEmbeddedValuesInPatterns(value); //XXX:thining //XXX:增加 RequestMapping value is null 时 默认使用方法名称(包括驼峰式和小写式) if(patterns == null ||(patterns != null && patterns.length == 0)){ patterns = getPathMaping(handler); } return new RequestMappingInfo( new PatternsRequestCondition(patterns, getUrlPathHelper(), getPathMatcher(), this.useSuffixPatternMatch(), this.useTrailingSlashMatch(), this.getFileExtensions()), new RequestMethodsRequestCondition(annotation.method()), new ParamsRequestCondition(annotation.params()), new HeadersRequestCondition(annotation.headers()), new ConsumesRequestCondition(annotation.consumes(), annotation.headers()), new ProducesRequestCondition(annotation.produces(), annotation.headers(), this.getContentNegotiationManager()), customCondition); }
@Override protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) { RequestMappingInfo info = super.getMappingForMethod(method, handlerType); if (info == null) return null; ApiVersion methodAnnotation = AnnotationUtils.findAnnotation(method, ApiVersion.class); if (methodAnnotation != null) { RequestCondition<?> methodCondition = getCustomMethodCondition(method); // Concatenate our ApiVersion with the usual request mapping info = createApiVersionInfo(methodAnnotation, methodCondition).combine(info); } else { ApiVersion typeAnnotation = AnnotationUtils.findAnnotation(handlerType, ApiVersion.class); if (typeAnnotation != null) { RequestCondition<?> typeCondition = getCustomTypeCondition(handlerType); // Concatenate our ApiVersion with the usual request mapping info = createApiVersionInfo(typeAnnotation, typeCondition).combine(info); } } return info; }
/** * Uses method and type-level @{@link RequestMapping} annotations to create * the RequestMappingInfo. * @return the created RequestMappingInfo, or {@code null} if the method * does not have a {@code @RequestMapping} annotation. * @see #getCustomMethodCondition(Method) * @see #getCustomTypeCondition(Class) */ @Override protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) { RequestMappingInfo info = null; RequestMapping methodAnnotation = AnnotationUtils.findAnnotation(method, RequestMapping.class); if (methodAnnotation != null) { RequestCondition<?> methodCondition = getCustomMethodCondition(method); info = createRequestMappingInfo(methodAnnotation, methodCondition); RequestMapping typeAnnotation = AnnotationUtils.findAnnotation(handlerType, RequestMapping.class); if (typeAnnotation != null) { RequestCondition<?> typeCondition = getCustomTypeCondition(handlerType); info = createRequestMappingInfo(typeAnnotation, typeCondition).combine(info); } } return info; }
@Override protected RequestCondition<?> getCustomTypeCondition(Class<?> handlerType) { VersionedResource versionResourceAnnotation = AnnotationUtils.findAnnotation(handlerType, VersionedResource.class); if (versionResourceAnnotation != null) { if (StringUtils.isEmpty(versionResourceAnnotation.media())) { throw new IllegalArgumentException("Media cannot be null in class annotation"); } return createCondition(versionResourceAnnotation.media(), versionResourceAnnotation.from(), versionResourceAnnotation.to(), ""); } return null; }
@Override protected RequestCondition<?> getCustomMethodCondition(Method method) { VersionedResource methodAnnotation = AnnotationUtils.findAnnotation(method, VersionedResource.class); if (methodAnnotation != null && StringUtils.isEmpty(methodAnnotation.from())) { throw new IllegalArgumentException("From must be declared in method"); } RequestMapping requestMappingClass = AnnotationUtils.findAnnotation(method.getDeclaringClass(), RequestMapping.class); String classPath = processPathRequestMapping("", requestMappingClass); String classMehodReq = processMethodRequestMapping(RequestMethod.GET.name(),requestMappingClass); RequestMapping requestMappingMethod = AnnotationUtils.findAnnotation(method, RequestMapping.class); String path = processPathRequestMapping(classPath, requestMappingMethod); String methodReq = processMethodRequestMapping(classMehodReq, requestMappingMethod); path = path.concat("#").concat(methodReq); if (methodAnnotation != null) { VersionedResource classAnnotation = AnnotationUtils.findAnnotation(method.getDeclaringClass(), VersionedResource.class); if (classAnnotation == null && StringUtils.isEmpty(methodAnnotation.media())) { throw new IllegalArgumentException("Media must be declared"); } PathVersionMap.INSTANCE.checkColisionPathWithVersion(path, methodAnnotation.from(), methodAnnotation.to()); return createCondition(methodAnnotation.media(), methodAnnotation.from(), methodAnnotation.to(), path); } else { return null; } }
public RequestMappingInfo(String name, PatternsRequestCondition patterns, RequestMethodsRequestCondition methods, ParamsRequestCondition params, HeadersRequestCondition headers, ConsumesRequestCondition consumes, ProducesRequestCondition produces, RequestCondition<?> custom) { this.name = (StringUtils.hasText(name) ? name : null); this.patternsCondition = (patterns != null ? patterns : new PatternsRequestCondition()); this.methodsCondition = (methods != null ? methods : new RequestMethodsRequestCondition()); this.paramsCondition = (params != null ? params : new ParamsRequestCondition()); this.headersCondition = (headers != null ? headers : new HeadersRequestCondition()); this.consumesCondition = (consumes != null ? consumes : new ConsumesRequestCondition()); this.producesCondition = (produces != null ? produces : new ProducesRequestCondition()); this.customConditionHolder = new RequestConditionHolder(custom); }
/** * Creates a new instance with the given request conditions. */ public RequestMappingInfo(PatternsRequestCondition patterns, RequestMethodsRequestCondition methods, ParamsRequestCondition params, HeadersRequestCondition headers, ConsumesRequestCondition consumes, ProducesRequestCondition produces, RequestCondition<?> custom) { this(null, patterns, methods, params, headers, consumes, produces, custom); }
/** * Created a RequestMappingInfo from a RequestMapping annotation. */ protected RequestMappingInfo createRequestMappingInfo2(RequestMapping annotation, Method method) { String[] patterns; if (method != null && annotation.value().length == 0) { patterns = new String[] { this.createPattern(method.getName()) }; } else { patterns = resolveEmbeddedValuesInPatterns(annotation.value()); } Map<String, String> headerMap = new LinkedHashMap<String, String>(); ExtensiveDomain extensiveDomain = new ExtensiveDomain(); requestMappingInfoBuilder.getHeaders(annotation, method, extensiveDomain, headerMap); // System.out.println("headerMap:" + headerMap); String[] headers = new String[headerMap.size()]; { int i = 0; for (Entry<String, String> entry : headerMap.entrySet()) { String header = entry.getKey() + "=" + entry.getValue(); headers[i] = header; i++; } } RequestCondition<?> customCondition = new ServerNameRequestCondition(extensiveDomain, headers); return new RequestMappingInfo(new PatternsRequestCondition(patterns, getUrlPathHelper(), getPathMatcher(), false, this.useTrailingSlashMatch(), this.getFileExtensions()), new RequestMethodsRequestCondition(annotation.method()), new ParamsRequestCondition(annotation.params()), new HeadersRequestCondition(), new ConsumesRequestCondition(annotation.consumes(), headers), new ProducesRequestCondition(annotation.produces(), headers, getContentNegotiationManager()), customCondition); }
/** * Store requests have to be handled by different RequestMappings * based on whether the request is targeting a Store or content associated * with an Entity */ @Override protected RequestCondition<?> getCustomMethodCondition(Method method) { StoreType typeAnnotation = AnnotationUtils.findAnnotation(method, StoreType.class); if (typeAnnotation != null) { return new StoreCondition(typeAnnotation, this.contentStores); } return null; }
/** * 不使用{@link @RequestMapping}注解时默认类名和方法名称 * @param customCondition * @param handler * @return * @author:ChenYiCheng */ protected RequestMappingInfo createRequestMappingInfo(RequestCondition<?> customCondition,Object handler) { String[] patterns = getPathMaping(handler);; return new RequestMappingInfo( new PatternsRequestCondition(patterns, getUrlPathHelper(), getPathMatcher(), this.useSuffixPatternMatch(), this.useTrailingSlashMatch(), this.getFileExtensions()), new RequestMethodsRequestCondition(), new ParamsRequestCondition(), new HeadersRequestCondition(), new ConsumesRequestCondition(null,null), new ProducesRequestCondition(null, null, this.getContentNegotiationManager()), customCondition); }
private RequestMappingInfo createApiVersionInfo(ApiVersion annotation, RequestCondition<?> customCondition) { String[] values = annotation.value(); String[] patterns = new String[values.length]; for (int i = 0; i < values.length; i++) { // Build the URL prefix patterns[i] = prefix + values[i]; } return new RequestMappingInfo( new PatternsRequestCondition(patterns, getUrlPathHelper(), getPathMatcher(), useSuffixPatternMatch(), useTrailingSlashMatch(), getFileExtensions()), new RequestMethodsRequestCondition(), new ParamsRequestCondition(), new HeadersRequestCondition(), new ConsumesRequestCondition(), new ProducesRequestCondition(), customCondition); }
protected RequestMappingInfo createRequestMappingInfo(RequestMapping annotation, RequestCondition<?> customCondition) { return new RequestMappingInfo( new PatternsRequestCondition(annotation.value(), getUrlPathHelper(), getPathMatcher(), false, true), new RequestMethodsRequestCondition(annotation.method()), new ParamsRequestCondition(annotation.params()), new HeadersRequestCondition(annotation.headers()), new ConsumesRequestCondition(annotation.consumes(), annotation.headers()), new ProducesRequestCondition(annotation.produces(), annotation.headers()), customCondition); }
/** * Created a RequestMappingInfo from a RequestMapping annotation. */ protected RequestMappingInfo createRequestMappingInfo(RequestMapping annotation, RequestCondition<?> customCondition) { String[] patterns = resolveEmbeddedValuesInPatterns(annotation.value()); return new RequestMappingInfo( new PatternsRequestCondition(patterns, getUrlPathHelper(), getPathMatcher(), this.useSuffixPatternMatch, this.useTrailingSlashMatch, this.fileExtensions), new RequestMethodsRequestCondition(annotation.method()), new ParamsRequestCondition(annotation.params()), new HeadersRequestCondition(annotation.headers()), new ConsumesRequestCondition(annotation.consumes(), annotation.headers()), new ProducesRequestCondition(annotation.produces(), annotation.headers(), getContentNegotiationManager()), customCondition); }
/** * Creates a new instance with the given request conditions. */ public RequestMappingInfo(PatternsRequestCondition patterns, RequestMethodsRequestCondition methods, ParamsRequestCondition params, HeadersRequestCondition headers, ConsumesRequestCondition consumes, ProducesRequestCondition produces, RequestCondition<?> custom) { this.patternsCondition = (patterns != null ? patterns : new PatternsRequestCondition()); this.methodsCondition = (methods != null ? methods : new RequestMethodsRequestCondition()); this.paramsCondition = (params != null ? params : new ParamsRequestCondition()); this.headersCondition = (headers != null ? headers : new HeadersRequestCondition()); this.consumesCondition = (consumes != null ? consumes : new ConsumesRequestCondition()); this.producesCondition = (produces != null ? produces : new ProducesRequestCondition()); this.customConditionHolder = new RequestConditionHolder(custom); }
@Override protected RequestCondition<ApiVersionCondition> getCustomTypeCondition(Class<?> handlerType) { ApiVersion apiVersion = AnnotationUtils.findAnnotation(handlerType, ApiVersion.class); return createCondition(apiVersion); }
@Override protected RequestCondition<ApiVersionCondition> getCustomMethodCondition(Method method) { ApiVersion apiVersion = AnnotationUtils.findAnnotation(method, ApiVersion.class); return createCondition(apiVersion); }
private RequestCondition<ApiVersionCondition> createCondition(ApiVersion apiVersion) { return apiVersion == null ? null : new ApiVersionCondition(apiVersion.value()); }
/** * Re-create a RequestMappingInfo with the given custom request condition. */ public RequestMappingInfo(RequestMappingInfo info, RequestCondition<?> customRequestCondition) { this(info.name, info.patternsCondition, info.methodsCondition, info.paramsCondition, info.headersCondition, info.consumesCondition, info.producesCondition, customRequestCondition); }
/** * Returns the "custom" condition of this {@link RequestMappingInfo}; or {@code null}. */ public RequestCondition<?> getCustomCondition() { return this.customConditionHolder.getCondition(); }
@Override public DefaultBuilder customCondition(RequestCondition<?> condition) { this.customCondition = condition; return this; }
@Override protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) { //跳过父类的方法 if(!isSelfDeclaredMethod(method, handlerType)){ return null ; } RequestMappingInfo info = null; Annotation clsMethodName = AnnotationUtils.findAnnotation(handlerType, DefaultRequestMapping.class); if(clsMethodName == null){ clsMethodName = AnnotationUtils.findAnnotation(handlerType, ClassMethodNameRequestMapping.class); } //包了一层注解 // ClassMethodNameRequestMapping if(clsMethodName != null){ RequestMapping methodAnnotation = AnnotationUtils.findAnnotation(method, RequestMapping.class); NoRequestMapping noMappingAnnotation = AnnotationUtils.findAnnotation(method, NoRequestMapping.class); if(methodAnnotation == null){ //only load public method and not exist @NoRequestMapping if(Modifier.isPublic(method.getModifiers()) && noMappingAnnotation == null){ RequestCondition<?> methodCondition = getCustomMethodCondition(method); info = createRequestMappingInfo(methodAnnotation, methodCondition,method); RequestMapping typeAnnotation = AnnotationUtils.findAnnotation(handlerType, RequestMapping.class); RequestCondition<?> typeCondition = getCustomTypeCondition(handlerType); info = createRequestMappingInfo(typeAnnotation, typeCondition,handlerType).combine(info); } }else{ //Spring MVC RequestMappingHandlerMapping会加载,此类不加载含有@RequestMapping注解的方法 String[] value = methodAnnotation.value(); if(value == null ||(value != null && value.length == 0)){ logger.error(method + "@RequestMapping value must has value.thinking"); throw new IllegalArgumentException(method + "@RequestMapping value must has value.thinking"); } return null; } } return info; }
/** * Re-create a RequestMappingInfo with the given custom request condition. */ public RequestMappingInfo(RequestMappingInfo info, RequestCondition<?> customRequestCondition) { this(info.patternsCondition, info.methodsCondition, info.paramsCondition, info.headersCondition, info.consumesCondition, info.producesCondition, customRequestCondition); }
@Override protected RequestCondition<ApiVesrsionCondition> getCustomTypeCondition(Class<?> handlerType) { ApiVersion apiVersion = AnnotationUtils.findAnnotation(handlerType, ApiVersion.class); return createCondition(apiVersion); }
@Override protected RequestCondition<ApiVesrsionCondition> getCustomMethodCondition(Method method) { ApiVersion apiVersion = AnnotationUtils.findAnnotation(method, ApiVersion.class); return createCondition(apiVersion); }
private RequestCondition<ApiVesrsionCondition> createCondition(ApiVersion apiVersion) { return apiVersion == null ? null : new ApiVesrsionCondition(apiVersion.value()); }
/** * Delegates to {@link #createRequestMappingInfo(RequestMapping, RequestCondition)}, * supplying the appropriate custom {@link RequestCondition} depending on whether * the supplied {@code annotatedElement} is a class or method. * @see #getCustomTypeCondition(Class) * @see #getCustomMethodCondition(Method) */ private RequestMappingInfo createRequestMappingInfo(AnnotatedElement element) { RequestMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation(element, RequestMapping.class); RequestCondition<?> condition = (element instanceof Class<?> ? getCustomTypeCondition((Class<?>) element) : getCustomMethodCondition((Method) element)); return (requestMapping != null ? createRequestMappingInfo(requestMapping, condition) : null); }
private RequestCondition<?> createCondition(String media, String from, String to, String path) { return new VersionRequestCondition(media, from, to, path); }
/** * Provide a custom type-level request condition. * The custom {@link RequestCondition} can be of any type so long as the * same condition type is returned from all calls to this method in order * to ensure custom request conditions can be combined and compared. * <p>Consider extending {@link AbstractRequestCondition} for custom * condition types and using {@link CompositeRequestCondition} to provide * multiple custom conditions. * @param handlerType the handler type for which to create the condition * @return the condition, or {@code null} */ protected RequestCondition<?> getCustomTypeCondition(Class<?> handlerType) { return null; }
/** * Provide a custom method-level request condition. * The custom {@link RequestCondition} can be of any type so long as the * same condition type is returned from all calls to this method in order * to ensure custom request conditions can be combined and compared. * <p>Consider extending {@link AbstractRequestCondition} for custom * condition types and using {@link CompositeRequestCondition} to provide * multiple custom conditions. * @param method the handler method for which to create the condition * @return the condition, or {@code null} */ protected RequestCondition<?> getCustomMethodCondition(Method method) { return null; }
/** * Set a custom condition to use. */ Builder customCondition(RequestCondition<?> condition);