@Override protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) { RequestMapping annotation = AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class); if (annotation != null) { return new RequestMappingInfo( new PatternsRequestCondition(annotation.value(), getUrlPathHelper(), getPathMatcher(), true, 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()), null); } else { return null; } }
@Test public void matchPatternsCondition() { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); RequestMappingInfo info = new RequestMappingInfo( new PatternsRequestCondition("/foo*", "/bar"), null, null, null, null, null, null); RequestMappingInfo expected = new RequestMappingInfo( new PatternsRequestCondition("/foo*"), null, null, null, null, null, null); assertEquals(expected, info.getMatchingCondition(request)); info = new RequestMappingInfo( new PatternsRequestCondition("/**", "/foo*", "/foo"), null, null, null, null, null, null); expected = new RequestMappingInfo( new PatternsRequestCondition("/foo", "/foo*", "/**"), null, null, null, null, null, null); assertEquals(expected, info.getMatchingCondition(request)); }
@Test public void matchParamsCondition() { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); request.setParameter("foo", "bar"); RequestMappingInfo info = new RequestMappingInfo( new PatternsRequestCondition("/foo"), null, new ParamsRequestCondition("foo=bar"), null, null, null, null); RequestMappingInfo match = info.getMatchingCondition(request); assertNotNull(match); info = new RequestMappingInfo( new PatternsRequestCondition("/foo"), null, new ParamsRequestCondition("foo!=bar"), null, null, null, null); match = info.getMatchingCondition(request); assertNull(match); }
@Test public void matchHeadersCondition() { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); request.addHeader("foo", "bar"); RequestMappingInfo info = new RequestMappingInfo( new PatternsRequestCondition("/foo"), null, null, new HeadersRequestCondition("foo=bar"), null, null, null); RequestMappingInfo match = info.getMatchingCondition(request); assertNotNull(match); info = new RequestMappingInfo( new PatternsRequestCondition("/foo"), null, null, new HeadersRequestCondition("foo!=bar"), null, null, null); match = info.getMatchingCondition(request); assertNull(match); }
@Test public void matchConsumesCondition() { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); request.setContentType("text/plain"); RequestMappingInfo info = new RequestMappingInfo( new PatternsRequestCondition("/foo"), null, null, null, new ConsumesRequestCondition("text/plain"), null, null); RequestMappingInfo match = info.getMatchingCondition(request); assertNotNull(match); info = new RequestMappingInfo( new PatternsRequestCondition("/foo"), null, null, null, new ConsumesRequestCondition("application/xml"), null, null); match = info.getMatchingCondition(request); assertNull(match); }
@Test public void matchProducesCondition() { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); request.addHeader("Accept", "text/plain"); RequestMappingInfo info = new RequestMappingInfo( new PatternsRequestCondition("/foo"), null, null, null, null, new ProducesRequestCondition("text/plain"), null); RequestMappingInfo match = info.getMatchingCondition(request); assertNotNull(match); info = new RequestMappingInfo( new PatternsRequestCondition("/foo"), null, null, null, null, new ProducesRequestCondition("application/xml"), null); match = info.getMatchingCondition(request); assertNull(match); }
@Test public void matchCustomCondition() { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); request.setParameter("foo", "bar"); RequestMappingInfo info = new RequestMappingInfo( new PatternsRequestCondition("/foo"), null, null, null, null, null, new ParamsRequestCondition("foo=bar")); RequestMappingInfo match = info.getMatchingCondition(request); assertNotNull(match); info = new RequestMappingInfo( new PatternsRequestCondition("/foo"), null, new ParamsRequestCondition("foo!=bar"), null, null, null, new ParamsRequestCondition("foo!=bar")); match = info.getMatchingCondition(request); assertNull(match); }
@Test public void preFlightRequest() { MockHttpServletRequest request = new MockHttpServletRequest("OPTIONS", "/foo"); request.addHeader(HttpHeaders.ORIGIN, "http://domain.com"); request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "POST"); RequestMappingInfo info = new RequestMappingInfo( new PatternsRequestCondition("/foo"), new RequestMethodsRequestCondition(RequestMethod.POST), null, null, null, null, null); RequestMappingInfo match = info.getMatchingCondition(request); assertNotNull(match); info = new RequestMappingInfo( new PatternsRequestCondition("/foo"), new RequestMethodsRequestCondition(RequestMethod.OPTIONS), null, null, null, null, null); match = info.getMatchingCondition(request); assertNotNull(match); }
@Test public void uriTemplateVariables() { PatternsRequestCondition patterns = new PatternsRequestCondition("/{path1}/{path2}"); RequestMappingInfo key = new RequestMappingInfo(patterns, null, null, null, null, null, null); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/1/2"); String lookupPath = new UrlPathHelper().getLookupPathForRequest(request); this.handlerMapping.handleMatch(key, lookupPath, request); @SuppressWarnings("unchecked") Map<String, String> uriVariables = (Map<String, String>) request.getAttribute( HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE); assertNotNull(uriVariables); assertEquals("1", uriVariables.get("path1")); assertEquals("2", uriVariables.get("path2")); }
@Test public void uriTemplateVariablesDecode() { PatternsRequestCondition patterns = new PatternsRequestCondition("/{group}/{identifier}"); RequestMappingInfo key = new RequestMappingInfo(patterns, null, null, null, null, null, null); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/group/a%2Fb"); UrlPathHelper pathHelper = new UrlPathHelper(); pathHelper.setUrlDecode(false); String lookupPath = pathHelper.getLookupPathForRequest(request); this.handlerMapping.setUrlPathHelper(pathHelper); this.handlerMapping.handleMatch(key, lookupPath, request); @SuppressWarnings("unchecked") Map<String, String> uriVariables = (Map<String, String>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE); assertNotNull(uriVariables); assertEquals("group", uriVariables.get("group")); assertEquals("a/b", uriVariables.get("identifier")); }
@Override protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) { RequestMapping annotation = AnnotationUtils.findAnnotation(method, RequestMapping.class); if (annotation != null) { return new RequestMappingInfo( new PatternsRequestCondition(annotation.value(), getUrlPathHelper(), getPathMatcher(), true, 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()), null); } else { return null; } }
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); }
/** * Checks if all conditions in this request mapping info match the provided request and returns * a potentially new request mapping info with conditions tailored to the current request. * <p>For example the returned instance may contain the subset of URL patterns that match to * the current request, sorted with best matching patterns on top. * @return a new instance in case all conditions match; or {@code null} otherwise */ public RequestMappingInfo getMatchingCondition(HttpServletRequest request) { RequestMethodsRequestCondition methods = this.methodsCondition.getMatchingCondition(request); ParamsRequestCondition params = this.paramsCondition.getMatchingCondition(request); HeadersRequestCondition headers = this.headersCondition.getMatchingCondition(request); ConsumesRequestCondition consumes = this.consumesCondition.getMatchingCondition(request); ProducesRequestCondition produces = this.producesCondition.getMatchingCondition(request); if (methods == null || params == null || headers == null || consumes == null || produces == null) { return null; } PatternsRequestCondition patterns = this.patternsCondition.getMatchingCondition(request); if (patterns == null) { return null; } RequestConditionHolder custom = this.customConditionHolder.getMatchingCondition(request); if (custom == null) { return null; } return new RequestMappingInfo(patterns, methods, params, headers, consumes, produces, custom.getCondition()); }
protected RequestMappingInfo createRequestMappingInfo(String pattern) { String[] patterns = (null == pattern) ? null : this.resolveEmbeddedValuesInPatterns(new String[] { pattern }); return new RequestMappingInfo(new PatternsRequestCondition(patterns, this.getUrlPathHelper(), this.getPathMatcher(), this.useSuffixPatternMatch(), this.useTrailingSlashMatch(), this.getFileExtensions()), null, null, null, null, null, null); }
protected RequestMappingInfo createRequestMappingInfo(String pattern) { String[] patterns = (null == pattern) ? null : this.resolveEmbeddedValuesInPatterns(new String[]{pattern}); return new RequestMappingInfo(new PatternsRequestCondition(patterns, this.getUrlPathHelper(), this.getPathMatcher(), this.useSuffixPatternMatch(), this.useTrailingSlashMatch(), this.getFileExtensions()), null, null, null, null, null, null); }
private String getMatchingURLPattern(HttpServletRequest httpServletRequest) { String res = ""; for (PatternsRequestCondition pattern : getUrlPatterns()) { if (pattern.getMatchingCondition(httpServletRequest) != null && !httpServletRequest.getServletPath().equals("/error")) { res = pattern.getMatchingCondition(httpServletRequest).getPatterns().iterator() .next(); break; } } return res; }
private Set<PatternsRequestCondition> getUrlPatterns() { if (this.urlPatterns == null) { this.urlPatterns = new HashSet<>(); requestMappingHandlerMapping.getHandlerMethods().forEach((mapping, handlerMethod) -> urlPatterns.add(mapping.getPatternsCondition())); RepositoryRestHandlerMapping repositoryRestHandlerMapping = new RepositoryRestHandlerMapping(mappings, repositoryConfiguration); repositoryRestHandlerMapping.setJpaHelper(jpaHelper); repositoryRestHandlerMapping.setApplicationContext(applicationContext); repositoryRestHandlerMapping.afterPropertiesSet(); repositoryRestHandlerMapping.getHandlerMethods().forEach((mapping, handlerMethod) -> urlPatterns.add(mapping.getPatternsCondition())); } return this.urlPatterns; }
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); }
/** * Combines "this" request mapping info (i.e. the current instance) with another request mapping info instance. * <p>Example: combine type- and method-level request mappings. * @return a new request mapping info instance; never {@code null} */ @Override public RequestMappingInfo combine(RequestMappingInfo other) { String name = combineNames(other); PatternsRequestCondition patterns = this.patternsCondition.combine(other.patternsCondition); RequestMethodsRequestCondition methods = this.methodsCondition.combine(other.methodsCondition); ParamsRequestCondition params = this.paramsCondition.combine(other.paramsCondition); HeadersRequestCondition headers = this.headersCondition.combine(other.headersCondition); ConsumesRequestCondition consumes = this.consumesCondition.combine(other.consumesCondition); ProducesRequestCondition produces = this.producesCondition.combine(other.producesCondition); RequestConditionHolder custom = this.customConditionHolder.combine(other.customConditionHolder); return new RequestMappingInfo(name, patterns, methods, params, headers, consumes, produces, custom.getCondition()); }
/** * Checks if all conditions in this request mapping info match the provided request and returns * a potentially new request mapping info with conditions tailored to the current request. * <p>For example the returned instance may contain the subset of URL patterns that match to * the current request, sorted with best matching patterns on top. * @return a new instance in case all conditions match; or {@code null} otherwise */ @Override public RequestMappingInfo getMatchingCondition(HttpServletRequest request) { RequestMethodsRequestCondition methods = this.methodsCondition.getMatchingCondition(request); ParamsRequestCondition params = this.paramsCondition.getMatchingCondition(request); HeadersRequestCondition headers = this.headersCondition.getMatchingCondition(request); ConsumesRequestCondition consumes = this.consumesCondition.getMatchingCondition(request); ProducesRequestCondition produces = this.producesCondition.getMatchingCondition(request); if (methods == null || params == null || headers == null || consumes == null || produces == null) { if (CorsUtils.isPreFlightRequest(request)) { methods = getAccessControlRequestMethodCondition(request); if (methods == null || params == null) { return null; } } else { return null; } } PatternsRequestCondition patterns = this.patternsCondition.getMatchingCondition(request); if (patterns == null) { return null; } RequestConditionHolder custom = this.customConditionHolder.getMatchingCondition(request); if (custom == null) { return null; } return new RequestMappingInfo(this.name, patterns, methods, params, headers, consumes, produces, custom.getCondition()); }
@Test public void getMappingPathPatterns() throws Exception { RequestMappingInfo info = new RequestMappingInfo( new PatternsRequestCondition("/foo/*", "/foo", "/bar/*", "/bar"), null, null, null, null, null, null); Set<String> paths = this.handlerMapping.getMappingPathPatterns(info); HashSet<String> expected = new HashSet<String>(Arrays.asList("/foo/*", "/foo", "/bar/*", "/bar")); assertEquals(expected, paths); }
@Test public void bestMatchingPatternAttribute() { PatternsRequestCondition patterns = new PatternsRequestCondition("/{path1}/2", "/**"); RequestMappingInfo key = new RequestMappingInfo(patterns, null, null, null, null, null, null); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/1/2"); this.handlerMapping.handleMatch(key, "/1/2", request); assertEquals("/{path1}/2", request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE)); }
@Test public void bestMatchingPatternAttributeNoPatternsDefined() { PatternsRequestCondition patterns = new PatternsRequestCondition(); RequestMappingInfo key = new RequestMappingInfo(patterns, null, null, null, null, null, null); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/1/2"); this.handlerMapping.handleMatch(key, "/1/2", request); assertEquals("/1/2", request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE)); }
/** * 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); }
private RequestMappingInfo withNewPatterns(RequestMappingInfo mapping, String[] patternStrings) { PatternsRequestCondition patterns = new PatternsRequestCondition(patternStrings, null, null, useSuffixPatternMatch(), useTrailingSlashMatch(), null); return new RequestMappingInfo(patterns, mapping.getMethodsCondition(), mapping.getParamsCondition(), mapping.getHeadersCondition(), mapping.getConsumesCondition(), mapping.getProducesCondition(), mapping.getCustomCondition()); }
/** * 不使用{@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 withPrefix(RequestMappingInfo mapping) { List<String> newPatterns = getPatterns(mapping); PatternsRequestCondition patterns = new PatternsRequestCondition( newPatterns.toArray(new String[newPatterns.size()])); return new RequestMappingInfo(patterns, mapping.getMethodsCondition(), mapping.getParamsCondition(), mapping.getHeadersCondition(), mapping.getConsumesCondition(), mapping.getProducesCondition(), mapping.getCustomCondition()); }
private String getMatchingURLPattern(HttpServletRequest httpServletRequest) { String res = ""; for (PatternsRequestCondition pattern : getUrlPatterns()) { if (pattern.getMatchingCondition(httpServletRequest) != null && !httpServletRequest.getServletPath().equals("/error")) { res = pattern.getMatchingCondition(httpServletRequest).getPatterns().iterator().next(); break; } } return res; }