/** * Return the HandlerExecutionChain for this request. * <p>Tries all handler mappings in order. * @param request current HTTP request * @return the HandlerExecutionChain, or {@code null} if no handler could be found */ @Nullable protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception { if (this.handlerMappings != null) { for (HandlerMapping mapping : this.handlerMappings) { HandlerExecutionChain handler = mapping.getHandler(request); if (handler != null) { return handler; } } } return null; }
/** * Return the HandlerExecutionChain for this request. * <p>Tries all handler mappings in order. * @param request current HTTP request * @return the HandlerExecutionChain, or {@code null} if no handler could be found */ @Nullable protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception { if (this.handlerMappings != null) { for (HandlerMapping mapping : this.handlerMappings) { HandlerExecutionChain handler = mapping.getHandler(request); if (handler != null) { return handler; } } } return null; }
/** * Find the {@link HandlerMapping} that would handle the given request and * return it as a {@link MatchableHandlerMapping} that can be used to test * request-matching criteria. * <p>If the matching HandlerMapping is not an instance of * {@link MatchableHandlerMapping}, an IllegalStateException is raised. * @param request the current request * @return the resolved matcher, or {@code null} * @throws Exception if any of the HandlerMapping's raise an exception */ @Nullable public MatchableHandlerMapping getMatchableHandlerMapping(HttpServletRequest request) throws Exception { Assert.notNull(this.handlerMappings, "Handler mappings not initialized"); HttpServletRequest wrapper = new RequestAttributeChangeIgnoringWrapper(request); for (HandlerMapping handlerMapping : this.handlerMappings) { Object handler = handlerMapping.getHandler(wrapper); if (handler == null) { continue; } if (handlerMapping instanceof MatchableHandlerMapping) { return ((MatchableHandlerMapping) handlerMapping); } throw new IllegalStateException("HandlerMapping is not a MatchableHandlerMapping"); } return null; }
/** * Find the {@link HandlerMapping} that would handle the given request and * return it as a {@link MatchableHandlerMapping} that can be used to test * request-matching criteria. * <p>If the matching HandlerMapping is not an instance of * {@link MatchableHandlerMapping}, an IllegalStateException is raised. * @param request the current request * @return the resolved matcher, or {@code null} * @throws Exception if any of the HandlerMapping's raise an exception */ @Nullable public MatchableHandlerMapping getMatchableHandlerMapping(HttpServletRequest request) throws Exception { Assert.notNull(this.handlerMappings, "Handler mappings not initialized"); HttpServletRequest wrapper = new RequestAttributeChangeIgnoringWrapper(request); for (HandlerMapping handlerMapping : this.handlerMappings) { Object handler = handlerMapping.getHandler(wrapper); if (handler == null) { continue; } if (handlerMapping instanceof MatchableHandlerMapping) { return ((MatchableHandlerMapping) handlerMapping); } throw new IllegalStateException("HandlerMapping is not a MatchableHandlerMapping"); } return null; }
@Override @Nullable public CorsConfiguration getCorsConfiguration(HttpServletRequest request) { Assert.notNull(this.handlerMappings, "Handler mappings not initialized"); HttpServletRequest wrapper = new RequestAttributeChangeIgnoringWrapper(request); for (HandlerMapping handlerMapping : this.handlerMappings) { HandlerExecutionChain handler = null; try { handler = handlerMapping.getHandler(wrapper); } catch (Exception ex) { // Ignore } if (handler == null) { continue; } if (handler.getInterceptors() != null) { for (HandlerInterceptor interceptor : handler.getInterceptors()) { if (interceptor instanceof CorsConfigurationSource) { return ((CorsConfigurationSource) interceptor).getCorsConfiguration(wrapper); } } } if (handler.getHandler() instanceof CorsConfigurationSource) { return ((CorsConfigurationSource) handler.getHandler()).getCorsConfiguration(wrapper); } } return null; }
private HandlerExecutionChain getHandler(HandlerMapping hm, MockHttpServletRequest req) throws Exception { HandlerExecutionChain hec = hm.getHandler(req); HandlerInterceptor[] interceptors = hec.getInterceptors(); if (interceptors != null) { for (HandlerInterceptor interceptor : interceptors) { interceptor.preHandle(req, null, hec.getHandler()); } } return hec; }
private HandlerExecutionChain getHandler(MockHttpServletRequest req) throws Exception { HandlerExecutionChain hec = hm.getHandler(req); HandlerInterceptor[] interceptors = hec.getInterceptors(); if (interceptors != null) { for (HandlerInterceptor interceptor : interceptors) { interceptor.preHandle(req, null, hec.getHandler()); } } return hec; }
private HandlerMapping createHandlerMapping(MockHttpServletRequest request, Method requestMappingMethod) throws Exception { System.out.println("createHandlerMapping" + request); HandlerMapping requestMappingHandlerMapping = mock(HandlerMapping.class); HandlerExecutionChain handlerExecutionChain = mock(HandlerExecutionChain.class); HandlerMethod handlerMethod = mock(HandlerMethod.class); when(handlerMethod.getMethod()).thenReturn(requestMappingMethod); doReturn(TestController.class).when(handlerMethod).getBeanType(); when(handlerExecutionChain.getHandler()).thenReturn(handlerMethod); when(requestMappingHandlerMapping.getHandler(ArgumentMatchers.argThat(item -> item.getRequestURI().equals("/test/requestName")))).thenReturn(handlerExecutionChain); return requestMappingHandlerMapping; }
private HttpRequestHandler handler(HttpServletRequest request) throws Exception { HandlerMapping handlerMapping = context.getBean(HandlerMapping.class); return (HttpRequestHandler) handlerMapping.getHandler(request).getHandler(); }
@Test public void requestsWithoutHandlers() throws Exception { HandlerMapping hm = (HandlerMapping) wac.getBean("handlerMapping"); MockHttpServletRequest req = new MockHttpServletRequest("GET", "/mypath/nonsense.html"); req.setContextPath("/myapp"); Object h = hm.getHandler(req); assertTrue("Handler is null", h == null); req = new MockHttpServletRequest("GET", "/foo/bar/baz.html"); h = hm.getHandler(req); assertTrue("Handler is null", h == null); }
@Test public void asteriskMatches() throws Exception { HandlerMapping hm = (HandlerMapping) wac.getBean("handlerMapping"); Object bean = wac.getBean("godCtrl"); MockHttpServletRequest req = new MockHttpServletRequest("GET", "/mypath/test.html"); HandlerExecutionChain hec = hm.getHandler(req); assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); req = new MockHttpServletRequest("GET", "/mypath/testarossa"); hec = hm.getHandler(req); assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); req = new MockHttpServletRequest("GET", "/mypath/tes"); hec = hm.getHandler(req); assertTrue("Handler is correct bean", hec == null); }
@Override @Nullable public CorsConfiguration getCorsConfiguration(HttpServletRequest request) { Assert.notNull(this.handlerMappings, "Handler mappings not initialized"); HttpServletRequest wrapper = new RequestAttributeChangeIgnoringWrapper(request); for (HandlerMapping handlerMapping : this.handlerMappings) { HandlerExecutionChain handler = null; try { handler = handlerMapping.getHandler(wrapper); } catch (Exception ex) { // Ignore } if (handler == null) { continue; } if (handler.getInterceptors() != null) { for (HandlerInterceptor interceptor : handler.getInterceptors()) { if (interceptor instanceof CorsConfigurationSource) { return ((CorsConfigurationSource) interceptor).getCorsConfiguration(wrapper); } } } if (handler.getHandler() instanceof CorsConfigurationSource) { return ((CorsConfigurationSource) handler.getHandler()).getCorsConfiguration(wrapper); } } return null; }
HandlerExecutionChain hec = hm.getHandler(req); assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); hec = hm.getHandler(req); assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); req.setContextPath("/myapp"); req.setServletPath("/mypath/welcome.html"); hec = hm.getHandler(req); assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); req.setContextPath("/myapp"); req.setServletPath("/myservlet"); hec = hm.getHandler(req); assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); req.setContextPath("/myapp"); req.setServletPath("/myapp"); hec = hm.getHandler(req); assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); hec = hm.getHandler(req); assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); hec = hm.getHandler(req); assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
public static HandlerSelectionResult from(HttpServletRequest request, Iterable<HandlerMapping> delegates) throws Exception { Exception ignoredException = null; for (HandlerMapping delegate : delegates) { try { HandlerExecutionChain result = delegate.getHandler(request); if (result != null) { return HandlerSelectionResult.forResult(request, delegate, result); } } catch (HttpMediaTypeNotAcceptableException o_O) { ignoredException = o_O; } catch (HttpRequestMethodNotSupportedException o_O) { ignoredException = o_O; } catch (UnsatisfiedServletRequestParameterException o_O) { ignoredException = o_O; } } return HandlerSelectionResult.withoutResult(request, ignoredException); }
MockHttpServletResponse response = new MockHttpServletResponse(); HandlerExecutionChain executionChain = handlerMapping.getHandler(request); assertNotNull(executionChain); response = new MockHttpServletResponse(); executionChain = handlerMapping.getHandler(request); assertNotNull(executionChain);
@Test public void testParams() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/params"); Object handler = null; try { handler = this.handlerMapping.getHandler(request); } catch (Exception e) { // There is no matching handlers and some default handler //See org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping#handleNoMatch assertTrue(e instanceof UnsatisfiedServletRequestParameterException); } request = new MockHttpServletRequest("GET", "/params"); request.addParameter("param1", "1"); request.addParameter("param2", "1"); handler = this.handlerMapping.getHandler(request).getHandler(); MockHttpServletResponse response = new MockHttpServletResponse(); this.handlerAdapter.handle(request, response, handler); String testResponse = response.getContentAsString(); assertEquals("User=1;account=1", testResponse); request = new MockHttpServletRequest("GET", "/params"); request.addParameter("param1", "1"); handler = this.handlerMapping.getHandler(request).getHandler(); response = new MockHttpServletResponse(); this.handlerAdapter.handle(request, response, handler); testResponse = response.getContentAsString(); assertEquals("User=1", testResponse); }
@Test public void testConsumes() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/consumes"); request.setContentType("text/plain"); Object handler = this.handlerMapping.getHandler(request).getHandler(); MockHttpServletResponse response = new MockHttpServletResponse(); this.handlerAdapter.handle(request, response, handler); String testResponse = response.getContentAsString(); assertEquals("BAR", testResponse); request = new MockHttpServletRequest("GET", "/consumes"); request.setContentType("text/xml"); handler = this.handlerMapping.getHandler(request).getHandler(); response = new MockHttpServletResponse(); this.handlerAdapter.handle(request, response, handler); testResponse = response.getContentAsString(); assertEquals("<test>TEXT_XML</test>", testResponse); }
@Test public void testMultiplePathsTheSameEndpoint() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.setMethod("GET"); request.setRequestURI("/path1"); MockHttpServletResponse response = new MockHttpServletResponse(); Object handler = this.handlerMapping.getHandler(request).getHandler(); this.handlerAdapter.handle(request, response, handler); assertEquals(TEST_STRING_MULTIPLE_PATHS, response.getContentAsString()); request = new MockHttpServletRequest(); request.setMethod("GET"); request.setRequestURI("/path2"); response = new MockHttpServletResponse(); handler = this.handlerMapping.getHandler(request).getHandler(); this.handlerAdapter.handle(request, response, handler); assertEquals(TEST_STRING_MULTIPLE_PATHS, response.getContentAsString()); }
@Test public void getRequestNotAllowed() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.setMethod("GET"); request.setParameter("foo", "bar"); request.setRequestURI("/postOnly"); try { this.integrationRequestMappingHandlerMapping.getHandler(request); } catch (HttpRequestMethodNotSupportedException e) { assertEquals("GET", e.getMethod()); assertArrayEquals(new String[] {"POST"}, e.getSupportedMethods()); } }
@Test public void testProduces() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/produces"); request.addHeader("Accept", "application/xml"); Object handler = this.handlerMapping.getHandler(request).getHandler(); //See org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping#handleMatch assertEquals(Collections.singleton(MediaType.APPLICATION_XML), request.getAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE)); MockHttpServletResponse response = new MockHttpServletResponse(); this.handlerAdapter.handle(request, response, handler); String testResponse = response.getContentAsString(); assertEquals("<test>XML</test>", testResponse); request = new MockHttpServletRequest("GET", "/produces"); request.addHeader("Accept", "application/json"); handler = this.handlerMapping.getHandler(request).getHandler(); //See org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping#handleMatch assertNull("Negated expression should not be listed as a producible type", request.getAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE)); response = new MockHttpServletResponse(); this.handlerAdapter.handle(request, response, handler); testResponse = response.getContentAsString(); assertEquals("{\"json\":\"body\"}", testResponse); }