/** * Create a {@link DispatcherServlet} (or other kind of {@link FrameworkServlet}-derived * dispatcher) with the specified {@link WebApplicationContext}. * <p>Note: This allows for any {@link FrameworkServlet} subclass as of 4.2.3. * Previously, it insisted on returning a {@link DispatcherServlet} or subclass thereof. */ protected FrameworkServlet createDispatcherServlet(WebApplicationContext servletAppContext) { return new DispatcherServlet(servletAppContext); }
@Override public Servlet getInstance() { return new DispatcherServlet(wac); } @Override
@Override public void deployConfig(WebApplicationContext wac, Filter... filters) { ServletHolder servletHolder = new ServletHolder(new DispatcherServlet(wac)); this.contextHandler = new ServletContextHandler(); this.contextHandler.addServlet(servletHolder, "/"); for (Filter filter : filters) { this.contextHandler.addFilter(new FilterHolder(filter), "/*", getDispatcherTypes()); } this.jettyServer.setHandler(this.contextHandler); }
@Test public void contextInitializerClasses() throws Exception { DispatcherServlet servlet = new DispatcherServlet(); servlet.setContextClass(SimpleWebApplicationContext.class); servlet.setContextInitializerClasses( TestWebContextInitializer.class.getName() + "," + OtherWebContextInitializer.class.getName()); servlet.init(servletConfig); assertEquals("true", getServletContext().getAttribute("initialized")); assertEquals("true", getServletContext().getAttribute("otherInitialized")); }
@Test public void allowedOptionsIncludesPatchMethod() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "OPTIONS", "/foo"); MockHttpServletResponse response = spy(new MockHttpServletResponse()); DispatcherServlet servlet = new DispatcherServlet(); servlet.setDispatchOptionsRequest(false); servlet.service(request, response); verify(response, never()).getHeader(anyString()); // SPR-10341 assertThat(response.getHeader("Allow"), equalTo("GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH")); }
@Test public void globalInitializerClasses() throws Exception { DispatcherServlet servlet = new DispatcherServlet(); servlet.setContextClass(SimpleWebApplicationContext.class); getServletContext().setInitParameter(ContextLoader.GLOBAL_INITIALIZER_CLASSES_PARAM, TestWebContextInitializer.class.getName() + "," + OtherWebContextInitializer.class.getName()); servlet.init(servletConfig); assertEquals("true", getServletContext().getAttribute("initialized")); assertEquals("true", getServletContext().getAttribute("otherInitialized")); }
@Test public void mixedInitializerClasses() throws Exception { DispatcherServlet servlet = new DispatcherServlet(); servlet.setContextClass(SimpleWebApplicationContext.class); getServletContext().setInitParameter(ContextLoader.GLOBAL_INITIALIZER_CLASSES_PARAM, TestWebContextInitializer.class.getName()); servlet.setContextInitializerClasses(OtherWebContextInitializer.class.getName()); servlet.init(servletConfig); assertEquals("true", getServletContext().getAttribute("initialized")); assertEquals("true", getServletContext().getAttribute("otherInitialized")); }
@Test public void contextInitializers() throws Exception { DispatcherServlet servlet = new DispatcherServlet(); servlet.setContextClass(SimpleWebApplicationContext.class); servlet.setContextInitializers(new TestWebContextInitializer(), new OtherWebContextInitializer()); servlet.init(servletConfig); assertEquals("true", getServletContext().getAttribute("initialized")); assertEquals("true", getServletContext().getAttribute("otherInitialized")); }
@Test public void testFrameworkServletWithDefaultLocation() throws Exception { DispatcherServlet servlet = new DispatcherServlet(); servlet.setContextClass(XmlWebApplicationContext.class); try { servlet.init(new MockServletConfig(new MockServletContext(""), "test")); fail("Should have thrown BeanDefinitionStoreException"); } catch (BeanDefinitionStoreException ex) { // expected assertTrue(ex.getCause() instanceof IOException); assertTrue(ex.getCause().getMessage().contains("/WEB-INF/test-servlet.xml")); } }
@Before public void setUp() throws ServletException { MockServletConfig complexConfig = new MockServletConfig(getServletContext(), "complex"); complexConfig.addInitParameter("publishContext", "false"); complexConfig.addInitParameter("class", "notWritable"); complexConfig.addInitParameter("unknownParam", "someValue"); simpleDispatcherServlet = new DispatcherServlet(); simpleDispatcherServlet.setContextClass(SimpleWebApplicationContext.class); simpleDispatcherServlet.init(servletConfig); complexDispatcherServlet = new DispatcherServlet(); complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class); complexDispatcherServlet.setNamespace("test"); complexDispatcherServlet.addRequiredProperty("publishContext"); complexDispatcherServlet.init(complexConfig); }
@Test public void controllerAdviceWithNestedExceptionWithinDispatcherServlet() throws Exception { StaticWebApplicationContext ctx = new StaticWebApplicationContext(); ctx.registerSingleton("controller", NestedExceptionThrowingController.class); ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class); ctx.refresh(); DispatcherServlet servlet = new DispatcherServlet(ctx); servlet.init(new MockServletConfig()); try { servlet.service(this.servletRequest, this.servletResponse); } catch (ServletException ex) { assertTrue(ex.getCause() instanceof IllegalStateException); assertTrue(ex.getCause().getCause() instanceof ServletRequestBindingException); } }
private MockHttpServletResponse runTest(Class<?> configClass) throws ServletException, IOException { String basePath = "org/springframework/web/servlet/config/annotation"; MockServletContext servletContext = new MockServletContext(basePath); MockServletConfig servletConfig = new MockServletConfig(servletContext); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/"); MockHttpServletResponse response = new MockHttpServletResponse(); AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.register(configClass); context.setServletContext(servletContext); context.refresh(); DispatcherServlet servlet = new DispatcherServlet(context); servlet.init(servletConfig); servlet.service(request, response); return response; }
@Test public void notDetectAllViewResolvers() throws ServletException, IOException { DispatcherServlet complexDispatcherServlet = new DispatcherServlet(); complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class); complexDispatcherServlet.setNamespace("test"); complexDispatcherServlet.setDetectAllViewResolvers(false); complexDispatcherServlet.init(new MockServletConfig(getServletContext(), "complex")); MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/unknown.do"); MockHttpServletResponse response = new MockHttpServletResponse(); try { complexDispatcherServlet.service(request, response); fail("Should have thrown ServletException"); } catch (ServletException ex) { // expected assertTrue(ex.getMessage().contains("failed0")); } }
@Test public void notDetectAllHandlerExceptionResolvers() throws ServletException, IOException { DispatcherServlet complexDispatcherServlet = new DispatcherServlet(); complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class); complexDispatcherServlet.setNamespace("test"); complexDispatcherServlet.setDetectAllHandlerExceptionResolvers(false); complexDispatcherServlet.init(new MockServletConfig(getServletContext(), "complex")); MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/unknown.do"); MockHttpServletResponse response = new MockHttpServletResponse(); try { complexDispatcherServlet.service(request, response); fail("Should have thrown ServletException"); } catch (ServletException ex) { // expected assertTrue(ex.getMessage().contains("No adapter for handler")); } }
@Test public void handlerNotMappedWithAutodetect() throws ServletException, IOException { DispatcherServlet complexDispatcherServlet = new DispatcherServlet(); // no parent complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class); complexDispatcherServlet.setNamespace("test"); complexDispatcherServlet.init(new MockServletConfig(getServletContext(), "complex")); MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", URL_KNOWN_ONLY_PARENT); MockHttpServletResponse response = new MockHttpServletResponse(); complexDispatcherServlet.service(request, response); assertEquals(HttpServletResponse.SC_NOT_FOUND, response.getStatus()); }
@Test public void testFrameworkServletWithCustomLocation() throws Exception { DispatcherServlet servlet = new DispatcherServlet(); servlet.setContextConfigLocation("/org/springframework/web/context/WEB-INF/testNamespace.xml " + "/org/springframework/web/context/WEB-INF/context-addition.xml"); servlet.init(new MockServletConfig(new MockServletContext(""), "test")); assertTrue(servlet.getWebApplicationContext().containsBean("kerry")); assertTrue(servlet.getWebApplicationContext().containsBean("kerryX")); }
@Test public void notDetectAllHandlerMappings() throws ServletException, IOException { DispatcherServlet complexDispatcherServlet = new DispatcherServlet(); complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class); complexDispatcherServlet.setNamespace("test"); complexDispatcherServlet.setDetectAllHandlerMappings(false); complexDispatcherServlet.init(new MockServletConfig(getServletContext(), "complex")); MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/unknown.do"); MockHttpServletResponse response = new MockHttpServletResponse(); complexDispatcherServlet.service(request, response); assertTrue(response.getStatus() == HttpServletResponse.SC_NOT_FOUND); }
@Test public void throwExceptionIfNoHandlerFound() throws ServletException, IOException { DispatcherServlet complexDispatcherServlet = new DispatcherServlet(); complexDispatcherServlet.setContextClass(SimpleWebApplicationContext.class); complexDispatcherServlet.setNamespace("test"); complexDispatcherServlet.setThrowExceptionIfNoHandlerFound(true); complexDispatcherServlet.init(new MockServletConfig(getServletContext(), "complex")); MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/unknown"); MockHttpServletResponse response = new MockHttpServletResponse(); complexDispatcherServlet.service(request, response); assertTrue("correct error code", response.getStatus() == HttpServletResponse.SC_NOT_FOUND); }
@Test public void controllerAdviceWithinDispatcherServlet() throws Exception { StaticWebApplicationContext ctx = new StaticWebApplicationContext(); ctx.registerSingleton("controller", ExceptionThrowingController.class); ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class); ctx.refresh(); DispatcherServlet servlet = new DispatcherServlet(ctx); servlet.init(new MockServletConfig()); servlet.service(this.servletRequest, this.servletResponse); assertEquals(400, this.servletResponse.getStatus()); assertEquals("error content", this.servletResponse.getContentAsString()); assertEquals("someHeaderValue", this.servletResponse.getHeader("someHeader")); }
@Test public void detectAllHandlerAdapters() throws ServletException, IOException { DispatcherServlet complexDispatcherServlet = new DispatcherServlet(); complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class); complexDispatcherServlet.setNamespace("test"); complexDispatcherServlet.init(new MockServletConfig(getServletContext(), "complex")); MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/servlet.do"); MockHttpServletResponse response = new MockHttpServletResponse(); complexDispatcherServlet.service(request, response); assertEquals("body", response.getContentAsString()); request = new MockHttpServletRequest(getServletContext(), "GET", "/form.do"); response = new MockHttpServletResponse(); complexDispatcherServlet.service(request, response); }