@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 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 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 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 notDetectAllHandlerAdapters() throws ServletException, IOException { DispatcherServlet complexDispatcherServlet = new DispatcherServlet(); complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class); complexDispatcherServlet.setNamespace("test"); complexDispatcherServlet.setDetectAllHandlerAdapters(false); complexDispatcherServlet.init(new MockServletConfig(getServletContext(), "complex")); // only ServletHandlerAdapter with bean name "handlerAdapter" detected MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/servlet.do"); MockHttpServletResponse response = new MockHttpServletResponse(); complexDispatcherServlet.service(request, response); assertEquals("body", response.getContentAsString()); // SimpleControllerHandlerAdapter not detected request = new MockHttpServletRequest(getServletContext(), "GET", "/form.do"); response = new MockHttpServletResponse(); complexDispatcherServlet.service(request, response); assertEquals("forwarded to failed", "failed0.jsp", response.getForwardedUrl()); assertTrue("Exception exposed", request.getAttribute("exception").getClass().equals(ServletException.class)); }
@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 detectHandlerMappingFromParent() throws ServletException, IOException { // create a parent context that includes a mapping StaticWebApplicationContext parent = new StaticWebApplicationContext(); parent.setServletContext(getServletContext()); parent.registerSingleton("parentHandler", ControllerFromParent.class, new MutablePropertyValues()); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.addPropertyValue(new PropertyValue("mappings", URL_KNOWN_ONLY_PARENT + "=parentHandler")); parent.registerSingleton("parentMapping", SimpleUrlHandlerMapping.class, pvs); parent.refresh(); DispatcherServlet complexDispatcherServlet = new DispatcherServlet(); // will have parent complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class); complexDispatcherServlet.setNamespace("test"); ServletConfig config = new MockServletConfig(getServletContext(), "complex"); config.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, parent); complexDispatcherServlet.init(config); MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", URL_KNOWN_ONLY_PARENT); MockHttpServletResponse response = new MockHttpServletResponse(); complexDispatcherServlet.service(request, response); assertFalse("Matched through parent controller/handler pair: not response=" + response.getStatus(), 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 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 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); }
private void serviceAdded(HttpService service) { try { DispatcherServlet dispatcherServlet = new DispatcherServlet(); dispatcherServlet.setContextConfigLocation(CONTEXT_CONFIG_LOCATION); dispatcherServlet.setContextClass(WebSecurityApplicationContext.class); ClassLoader old = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); service.registerServlet(SERVLET_URL_MAPPING, dispatcherServlet, null, null); logger.debug("Servlet registered"); } finally { Thread.currentThread().setContextClassLoader(old); } } catch (Exception e) { throw new RuntimeException(e); } }
private void serviceAdded(HttpService service) { try { DispatcherServlet dispatcherServlet = new DispatcherServlet(); dispatcherServlet.setContextConfigLocation(CONTEXT_CONFIG_LOCATION); dispatcherServlet.setContextClass(OpenMrsApplicationContext.class); ClassLoader old = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); service.registerServlet(SERVLET_URL_MAPPING, dispatcherServlet, null, null); logger.debug("Servlet registered"); } finally { Thread.currentThread().setContextClassLoader(old); } } catch (Exception e) { throw new ServletRegistrationException(e); } }
private void serviceAdded(HttpService service) { try { DispatcherServlet dispatcherServlet = new DispatcherServlet(); dispatcherServlet.setContextConfigLocation(CONTEXT_CONFIG_LOCATION); dispatcherServlet.setContextClass(OpenMrsAtomFeedContext.class); ClassLoader old = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); service.registerServlet(SERVLET_URL_MAPPING, dispatcherServlet, null, null); logger.debug("Servlet registered"); } finally { Thread.currentThread().setContextClassLoader(old); } } catch (Exception e) { throw new ServletRegistrationException(e); } }
private void serviceAdded(HttpService service) { try { DispatcherServlet dispatcherServlet = new DispatcherServlet(); dispatcherServlet.setContextConfigLocation(CONTEXT_CONFIG_LOCATION); dispatcherServlet.setContextClass(OpenmrsApiApplicationContext.class); ClassLoader old = Thread.currentThread().getContextClassLoader(); try { // register the mysql driver Class.forName("com.mysql.jdbc.Driver"); Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); service.registerServlet(SERVLET_URL_MAPPING, dispatcherServlet, null, null); logger.debug("Servlet registered"); } finally { Thread.currentThread().setContextClassLoader(old); } } catch (Exception e) { throw new ServletRegistrationException(e); } }
public void register(HttpService service) { if (contextPath == null) { try { DispatcherServlet dispatcherServlet = new OsgiDispatcherServlet(context); dispatcherServlet.setContextClass(MotechOsgiWebApplicationContext.class); dispatcherServlet.setContextConfigLocation(Activator.CONTEXT_CONFIG_LOCATION); ClassLoader old = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader()); contextPath = getContextPath(context); service.unregister(contextPath); service.registerServlet(contextPath, dispatcherServlet, null, null); logger.debug("Servlet registered"); } catch (Exception e) { e.printStackTrace(); } finally { Thread.currentThread().setContextClassLoader(old); } } catch (Exception e) { throw new RuntimeException(e); } } }
DispatcherServlet dispatcherServlet = new DispatcherServlet(); dispatcherServlet.setContextConfigLocation(CONTEXT_CONFIG_LOCATION); dispatcherServlet.setContextClass(WebSecurityApplicationContext.class); ClassLoader old = Thread.currentThread().getContextClassLoader();