private List<String[]> getRequestParams(HttpServletRequest request, Set<RequestMappingInfo> partialMatches) { List<String[]> result = new ArrayList<String[]>(); for (RequestMappingInfo partialMatch : partialMatches) { ParamsRequestCondition condition = partialMatch.getParamsCondition(); Set<NameValueExpression<String>> expressions = condition.getExpressions(); if (!CollectionUtils.isEmpty(expressions) && condition.getMatchingCondition(request) == null) { int i = 0; String[] array = new String[expressions.size()]; for (NameValueExpression<String> expression : expressions) { array[i++] = expression.toString(); } result.add(array); } } return result; }
@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 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 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 compareTwoHttpMethodsOneParam() { RequestMappingInfo none = new RequestMappingInfo(null, null, null, null, null, null, null); RequestMappingInfo oneMethod = new RequestMappingInfo(null, new RequestMethodsRequestCondition(RequestMethod.GET), null, null, null, null, null); RequestMappingInfo oneMethodOneParam = new RequestMappingInfo(null, new RequestMethodsRequestCondition(RequestMethod.GET), new ParamsRequestCondition("foo"), null, null, null, null); Comparator<RequestMappingInfo> comparator = new Comparator<RequestMappingInfo>() { @Override public int compare(RequestMappingInfo info, RequestMappingInfo otherInfo) { return info.compareTo(otherInfo, new MockHttpServletRequest()); } }; List<RequestMappingInfo> list = asList(none, oneMethod, oneMethodOneParam); Collections.shuffle(list); Collections.sort(list, comparator); assertEquals(oneMethodOneParam, list.get(0)); assertEquals(oneMethod, list.get(1)); assertEquals(none, list.get(2)); }
@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()); }
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()); }
/** * 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); }
/** * 不使用{@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); }
private Set<String> getRequestParams(HttpServletRequest request, Set<RequestMappingInfo> partialMatches) { for (RequestMappingInfo partialMatch : partialMatches) { ParamsRequestCondition condition = partialMatch.getParamsCondition(); if (!CollectionUtils.isEmpty(condition.getExpressions()) && (condition.getMatchingCondition(request) == null)) { Set<String> expressions = new HashSet<String>(); for (NameValueExpression<String> expr : condition.getExpressions()) { expressions.add(expr.toString()); } return expressions; } } return null; }
/** * 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); }
/** * 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} */ public RequestMappingInfo combine(RequestMappingInfo 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(patterns, methods, params, headers, consumes, produces, custom.getCondition()); }
@Test public void paramEquals() { assertEquals(new ParamsRequestCondition("foo"), new ParamsRequestCondition("foo")); assertFalse(new ParamsRequestCondition("foo").equals(new ParamsRequestCondition("bar"))); assertFalse(new ParamsRequestCondition("foo").equals(new ParamsRequestCondition("FOO"))); assertEquals(new ParamsRequestCondition("foo=bar"), new ParamsRequestCondition("foo=bar")); assertFalse( new ParamsRequestCondition("foo=bar").equals(new ParamsRequestCondition("FOO=bar"))); }
@Test public void paramPresent() { ParamsRequestCondition condition = new ParamsRequestCondition("foo"); MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("foo", ""); assertNotNull(condition.getMatchingCondition(request)); }
@Test public void paramPresentNoMatch() { ParamsRequestCondition condition = new ParamsRequestCondition("foo"); MockHttpServletRequest request = new MockHttpServletRequest(); request.addHeader("bar", ""); assertNull(condition.getMatchingCondition(request)); }
@Test public void paramNotPresent() { ParamsRequestCondition condition = new ParamsRequestCondition("!foo"); MockHttpServletRequest request = new MockHttpServletRequest(); assertNotNull(condition.getMatchingCondition(request)); }
@Test public void paramValueMatch() { ParamsRequestCondition condition = new ParamsRequestCondition("foo=bar"); MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("foo", "bar"); assertNotNull(condition.getMatchingCondition(request)); }
@Test public void paramValueNoMatch() { ParamsRequestCondition condition = new ParamsRequestCondition("foo=bar"); MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("foo", "bazz"); assertNull(condition.getMatchingCondition(request)); }
@Test public void compareTo() { MockHttpServletRequest request = new MockHttpServletRequest(); ParamsRequestCondition condition1 = new ParamsRequestCondition("foo", "bar", "baz"); ParamsRequestCondition condition2 = new ParamsRequestCondition("foo", "bar"); int result = condition1.compareTo(condition2, request); assertTrue("Invalid comparison result: " + result, result < 0); result = condition2.compareTo(condition1, request); assertTrue("Invalid comparison result: " + result, result > 0); }
@Test public void combine() { ParamsRequestCondition condition1 = new ParamsRequestCondition("foo=bar"); ParamsRequestCondition condition2 = new ParamsRequestCondition("foo=baz"); ParamsRequestCondition result = condition1.combine(condition2); Collection<ParamExpression> conditions = result.getContent(); assertEquals(2, conditions.size()); }
@Override public RequestMappingInfo build() { ContentNegotiationManager manager = this.options.getContentNegotiationManager(); PatternsRequestCondition patternsCondition = new PatternsRequestCondition( this.paths, this.options.getUrlPathHelper(), this.options.getPathMatcher(), this.options.useSuffixPatternMatch(), this.options.useTrailingSlashMatch(), this.options.getFileExtensions()); return new RequestMappingInfo(this.mappingName, patternsCondition, new RequestMethodsRequestCondition(methods), new ParamsRequestCondition(this.params), new HeadersRequestCondition(this.headers), new ConsumesRequestCondition(this.consumes, this.headers), new ProducesRequestCondition(this.produces, this.headers, manager), this.customCondition); }
/** * Returns the "parameters" condition of this {@link RequestMappingInfo}; * or instance with 0 parameter expressions, never {@code null}. */ public ParamsRequestCondition getParamsCondition() { return this.paramsCondition; }