@Override public RequestMatchResult match(HttpServletRequest request, String pattern) { RequestMappingInfo info = RequestMappingInfo.paths(pattern).options(this.config).build(); RequestMappingInfo matchingInfo = info.getMatchingCondition(request); if (matchingInfo == null) { return null; } Set<String> patterns = matchingInfo.getPatternsCondition().getPatterns(); String lookupPath = getUrlPathHelper().getLookupPathForRequest(request); return new RequestMatchResult(patterns.iterator().next(), lookupPath, getPathMatcher()); }
private void handleMatch(MockHttpServletRequest request, String pattern, String lookupPath) { RequestMappingInfo info = RequestMappingInfo.paths(pattern).build(); this.handlerMapping.handleMatch(info, lookupPath, request); }
@Test public void matchPatternsCondition() { HttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); RequestMappingInfo info = paths("/foo*", "/bar").build(); RequestMappingInfo expected = paths("/foo*").build(); assertEquals(expected, info.getMatchingCondition(request)); info = paths("/**", "/foo*", "/foo").build(); expected = paths("/foo", "/foo*", "/**").build(); assertEquals(expected, info.getMatchingCondition(request)); }
@Test public void getMappingPathPatterns() throws Exception { String[] patterns = {"/foo/*", "/foo", "/bar/*", "/bar"}; RequestMappingInfo info = RequestMappingInfo.paths(patterns).build(); Set<String> actual = this.handlerMapping.getMappingPathPatterns(info); assertEquals(new HashSet<>(Arrays.asList(patterns)), actual); }
@Test public void compareToWithImpicitVsExplicitHttpMethodDeclaration() { RequestMappingInfo noMethods = paths().build(); RequestMappingInfo oneMethod = paths().methods(GET).build(); RequestMappingInfo oneMethodOneParam = paths().methods(GET).params("foo").build(); Comparator<RequestMappingInfo> comparator = (info, otherInfo) -> info.compareTo(otherInfo, new MockHttpServletRequest()); List<RequestMappingInfo> list = asList(noMethods, oneMethod, oneMethodOneParam); Collections.shuffle(list); Collections.sort(list, comparator); assertEquals(oneMethodOneParam, list.get(0)); assertEquals(oneMethod, list.get(1)); assertEquals(noMethods, list.get(2)); }
/** * 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 @Nullable protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) { RequestMappingInfo info = createRequestMappingInfo(method); if (info != null) { RequestMappingInfo typeInfo = createRequestMappingInfo(handlerType); if (typeInfo != null) { info = typeInfo.combine(info); } String prefix = getPathPrefix(handlerType); if (prefix != null) { info = RequestMappingInfo.paths(prefix).build().combine(info); } } return info; }
@Test // SPR-14383 public void compareToWithHttpHeadMapping() { MockHttpServletRequest request = new MockHttpServletRequest(); request.setMethod("HEAD"); request.addHeader("Accept", "application/json"); RequestMappingInfo noMethods = paths().build(); RequestMappingInfo getMethod = paths().methods(GET).produces("application/json").build(); RequestMappingInfo headMethod = paths().methods(HEAD).build(); Comparator<RequestMappingInfo> comparator = (info, otherInfo) -> info.compareTo(otherInfo, request); List<RequestMappingInfo> list = asList(noMethods, getMethod, headMethod); Collections.shuffle(list); Collections.sort(list, comparator); assertEquals(headMethod, list.get(0)); assertEquals(getMethod, list.get(1)); assertEquals(noMethods, list.get(2)); }
@Override public RequestMatchResult match(HttpServletRequest request, String pattern) { RequestMappingInfo info = RequestMappingInfo.paths(pattern).options(this.config).build(); RequestMappingInfo matchingInfo = info.getMatchingCondition(request); if (matchingInfo == null) { return null; } Set<String> patterns = matchingInfo.getPatternsCondition().getPatterns(); String lookupPath = getUrlPathHelper().getLookupPathForRequest(request); return new RequestMatchResult(patterns.iterator().next(), lookupPath, getPathMatcher()); }
@Test public void handleMatchBestMatchingPatternAttribute() { RequestMappingInfo key = RequestMappingInfo.paths("/{path1}/2", "/**").build(); 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 matchProducesCondition() { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); request.addHeader("Accept", "text/plain"); RequestMappingInfo info = paths("/foo").produces("text/plain").build(); RequestMappingInfo match = info.getMatchingCondition(request); assertNotNull(match); info = paths("/foo").produces("application/xml").build(); match = info.getMatchingCondition(request); assertNull(match); }
@Test public void matchHeadersCondition() { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); request.addHeader("foo", "bar"); RequestMappingInfo info = paths("/foo").headers("foo=bar").build(); RequestMappingInfo match = info.getMatchingCondition(request); assertNotNull(match); info = paths("/foo").headers("foo!=bar").build(); match = info.getMatchingCondition(request); assertNull(match); }
@Test public void matchConsumesCondition() { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); request.setContentType("text/plain"); RequestMappingInfo info = paths("/foo").consumes("text/plain").build(); RequestMappingInfo match = info.getMatchingCondition(request); assertNotNull(match); info = paths("/foo").consumes("application/xml").build(); match = info.getMatchingCondition(request); assertNull(match); }
@Test public void matchParamsCondition() { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); request.setParameter("foo", "bar"); RequestMappingInfo info = paths("/foo").params("foo=bar").build(); RequestMappingInfo match = info.getMatchingCondition(request); assertNotNull(match); info = paths("/foo").params("foo!=bar").build(); match = info.getMatchingCondition(request); assertNull(match); }
@Test public void matchCustomCondition() { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); request.setParameter("foo", "bar"); RequestMappingInfo info = paths("/foo").params("foo=bar").build(); RequestMappingInfo match = info.getMatchingCondition(request); assertNotNull(match); info = paths("/foo").params("foo!=bar").params("foo!=bar").build(); match = info.getMatchingCondition(request); assertNull(match); }
@SuppressWarnings("unchecked") @Test public void handleMatchUriTemplateVariables() { RequestMappingInfo key = RequestMappingInfo.paths("/{path1}/{path2}").build(); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/1/2"); String lookupPath = new UrlPathHelper().getLookupPathForRequest(request); this.handlerMapping.handleMatch(key, lookupPath, request); String name = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE; Map<String, String> uriVariables = (Map<String, String>) request.getAttribute(name); assertNotNull(uriVariables); assertEquals("1", uriVariables.get("path1")); assertEquals("2", uriVariables.get("path2")); }
@Test public void handleMatchBestMatchingPatternAttributeNoPatternsDefined() { RequestMappingInfo key = RequestMappingInfo.paths().build(); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/1/2"); this.handlerMapping.handleMatch(key, "/1/2", request); assertEquals("/1/2", request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE)); }
@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 = paths("/foo").methods(RequestMethod.POST).build(); RequestMappingInfo match = info.getMatchingCondition(request); assertNotNull(match); info = paths("/foo").methods(RequestMethod.OPTIONS).build(); match = info.getMatchingCondition(request); assertNull("Pre-flight should match the ACCESS_CONTROL_REQUEST_METHOD", match); }
/** * 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, @Nullable RequestCondition<?> customCondition) { RequestMappingInfo.Builder builder = RequestMappingInfo .paths(resolveEmbeddedValuesInPatterns(requestMapping.path())) .methods(requestMapping.method()) .params(requestMapping.params()) .headers(requestMapping.headers()) .consumes(requestMapping.consumes()) .produces(requestMapping.produces()) .mappingName(requestMapping.name()); if (customCondition != null) { builder.customCondition(customCondition); } return builder.options(this.config).build(); }
@SuppressWarnings("unchecked") @Test // SPR-9098 public void handleMatchUriTemplateVariablesDecode() { RequestMappingInfo key = RequestMappingInfo.paths("/{group}/{identifier}").build(); 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); String name = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE; Map<String, String> uriVariables = (Map<String, String>) request.getAttribute(name); assertNotNull(uriVariables); assertEquals("group", uriVariables.get("group")); assertEquals("a/b", uriVariables.get("identifier")); }
@Test public void createEmpty() { RequestMappingInfo info = paths().build(); assertEquals(0, info.getPatternsCondition().getPatterns().size()); assertEquals(0, info.getMethodsCondition().getMethods().size()); assertEquals(true, info.getConsumesCondition().isEmpty()); assertEquals(true, info.getProducesCondition().isEmpty()); assertNotNull(info.getParamsCondition()); assertNotNull(info.getHeadersCondition()); assertNull(info.getCustomCondition()); }