@Test public void testReplaceHandler() throws Exception { ServletContextHandler servletContextHandler = new ServletContextHandler(); ServletHolder sh = new ServletHolder(new TestServlet()); servletContextHandler.addServlet(sh, "/foo"); final AtomicBoolean contextInit = new AtomicBoolean(false); final AtomicBoolean contextDestroy = new AtomicBoolean(false); servletContextHandler.addEventListener( new ServletContextListener() { @Override public void contextInitialized(ServletContextEvent sce) { if (sce.getServletContext() != null) contextInit.set(true); } @Override public void contextDestroyed(ServletContextEvent sce) { if (sce.getServletContext() != null) contextDestroy.set(true); } }); ServletHandler shandler = servletContextHandler.getServletHandler(); ResourceHandler rh = new ResourceHandler(); servletContextHandler.insertHandler(rh); assertEquals(shandler, servletContextHandler.getServletHandler()); assertEquals(rh, servletContextHandler.getHandler()); assertEquals(rh.getHandler(), shandler); _server.setHandler(servletContextHandler); _server.start(); assertTrue(contextInit.get()); _server.stop(); assertTrue(contextDestroy.get()); }
@Test public void testFallThrough() throws Exception { HandlerList list = new HandlerList(); _server.setHandler(list); ServletContextHandler root = new ServletContextHandler(list, "/", ServletContextHandler.SESSIONS); ServletHandler servlet = root.getServletHandler(); servlet.setEnsureDefaultServlet(false); servlet.addServletWithMapping(HelloServlet.class, "/hello/*"); list.addHandler( new AbstractHandler() { @Override public void handle( String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.sendError(404, "Fell Through"); } }); _server.start(); String response = _connector.getResponses("GET /hello HTTP/1.0\r\n\r\n"); Assert.assertThat(response, Matchers.containsString("200 OK")); response = _connector.getResponses("GET /other HTTP/1.0\r\n\r\n"); Assert.assertThat(response, Matchers.containsString("404 Fell Through")); }
@Before public void setUp() throws Exception { HttpConfiguration http_config = new HttpConfiguration(); http_config.setOutputBufferSize(4096); _connector = new ServerConnector(_server, new HttpConnectionFactory(http_config)); _server.setConnectors(new Connector[] {_connector}); ServletContextHandler context = new ServletContextHandler(); context.setContextPath("/ctx"); _server.setHandler(context); _servletHandler = context.getServletHandler(); ServletHolder holder = new ServletHolder(_servlet0); holder.setAsyncSupported(true); _servletHandler.addServletWithMapping(holder, "/path/*"); ServletHolder holder2 = new ServletHolder(_servlet2); holder.setAsyncSupported(true); _servletHandler.addServletWithMapping(holder2, "/path2/*"); _server.start(); _port = _connector.getLocalPort(); _owp.set(0); _oda.set(0); _read.set(0); }
@Test public void testGzipHandlerOption() throws Exception { ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS | ServletContextHandler.GZIP); GzipHandler gzip = context.getGzipHandler(); _server.start(); assertEquals(context.getSessionHandler(), context.getHandler()); assertEquals(gzip, context.getSessionHandler().getHandler()); assertEquals(context.getServletHandler(), gzip.getHandler()); }
@Test public void testGzipHandlerSet() throws Exception { ServletContextHandler context = new ServletContextHandler(); context.setSessionHandler(new SessionHandler()); context.setGzipHandler(new GzipHandler()); GzipHandler gzip = context.getGzipHandler(); _server.start(); assertEquals(context.getSessionHandler(), context.getHandler()); assertEquals(gzip, context.getSessionHandler().getHandler()); assertEquals(context.getServletHandler(), gzip.getHandler()); }
public void removeServletHolder(ServletHolder servletHolder) { synchronized (this.context) { ServletHandler handler = context.getServletHandler(); /* * A list of all the servlets that don't implement the class * 'servlet', (i.e. They should be kept in the context */ List<ServletHolder> servlets = new ArrayList<ServletHolder>(); /* * The names all the servlets that we remove so we can drop the * mappings too */ Set<String> names = new HashSet<String>(); for (ServletHolder holder : handler.getServlets()) { /* * If it is the class we want to remove, then just keep track of * its name */ if (servletHolder.equals(holder)) { names.add(holder.getName()); } else /* We keep it */ { servlets.add(holder); } } List<ServletMapping> mappings = new ArrayList<ServletMapping>(); for (ServletMapping mapping : handler.getServletMappings()) { /* * Only keep the mappings that didn't point to one of the * servlets we removed */ if (!names.contains(mapping.getServletName())) { mappings.add(mapping); } } /* Set the new configuration for the mappings and the servlets */ handler.setServletMappings(mappings.toArray(new ServletMapping[0])); handler.setServlets(servlets.toArray(new ServletHolder[0])); } }
@Test public void testFindContainer() throws Exception { ContextHandlerCollection contexts = new ContextHandlerCollection(); _server.setHandler(contexts); ServletContextHandler root = new ServletContextHandler(contexts, "/", ServletContextHandler.SESSIONS); SessionHandler session = root.getSessionHandler(); ServletHandler servlet = root.getServletHandler(); SecurityHandler security = new ConstraintSecurityHandler(); root.setSecurityHandler(security); _server.start(); assertEquals( root, AbstractHandlerContainer.findContainerOf(_server, ContextHandler.class, session)); assertEquals( root, AbstractHandlerContainer.findContainerOf(_server, ContextHandler.class, security)); assertEquals( root, AbstractHandlerContainer.findContainerOf(_server, ContextHandler.class, servlet)); }
@Test public void testFiltered() throws Exception { testdir.ensureEmpty(); File resBase = testdir.getFile("docroot"); FS.ensureDirExists(resBase); File file0 = new File(resBase, "data0.txt"); createFile(file0, "Hello Text 0"); String resBasePath = resBase.getAbsolutePath(); ServletHolder defholder = context.addServlet(DefaultServlet.class, "/"); defholder.setInitParameter("dirAllowed", "false"); defholder.setInitParameter("redirectWelcome", "false"); defholder.setInitParameter("welcomeServlets", "false"); defholder.setInitParameter("gzip", "false"); defholder.setInitParameter("resourceBase", resBasePath); String response = connector.getResponses("GET /context/data0.txt HTTP/1.0\r\n\r\n"); assertResponseContains("Content-Length: 12", response); assertResponseNotContains("Extra Info", response); context.addFilter(OutputFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST)); response = connector.getResponses("GET /context/data0.txt HTTP/1.0\r\n\r\n"); assertResponseContains("Content-Length: 2", response); // 20 something long assertResponseContains("Extra Info", response); assertResponseNotContains("Content-Length: 12", response); context.getServletHandler().setFilterMappings(new FilterMapping[] {}); context.getServletHandler().setFilters(new FilterHolder[] {}); context.addFilter(WriterFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST)); response = connector.getResponses("GET /context/data0.txt HTTP/1.0\r\n\r\n"); assertResponseContains("Content-Length: 2", response); // 20 something long assertResponseContains("Extra Info", response); assertResponseNotContains("Content-Length: 12", response); }
protected void setupTest(ITestContext context) throws Exception { Reporter.log( String.format( "HTTP:%d, HTTPS:%d , HTTPS(Mutual):%d", PLAIN_PORT, SECURE_PORT, MUTUAL_SECURE_PORT), true); connectorServer = new Server(); HttpConfiguration httpConfig = new HttpConfiguration(); httpConfig.setSecureScheme("https"); httpConfig.setSecurePort(SECURE_PORT); httpConfig.setOutputBufferSize(32768); // HTTP ServerConnector http = new ServerConnector(connectorServer, new HttpConnectionFactory(httpConfig)); http.setPort(PLAIN_PORT); http.setHost("127.0.0.1"); http.setIdleTimeout(30000); // HTTPS SslContextFactory sslContextFactory = createSsllContextFactory(false); // HTTPS Configuration HttpConfiguration httpsConfig = new HttpConfiguration(httpConfig); httpsConfig.addCustomizer(new SecureRequestCustomizer()); // HTTPS connector ServerConnector https = new ServerConnector( connectorServer, new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()), new HttpConnectionFactory(httpsConfig)); https.setPort(SECURE_PORT); http.setHost("127.0.0.1"); https.setIdleTimeout(500000); // Mutual HTTPS connector sslContextFactory = createSsllContextFactory(false); sslContextFactory.setWantClientAuth(true); sslContextFactory.setNeedClientAuth(false); ServerConnector mutualHttps = new ServerConnector( connectorServer, new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()), new HttpConnectionFactory(httpsConfig)); mutualHttps.setPort(MUTUAL_SECURE_PORT); http.setHost("127.0.0.1"); mutualHttps.setIdleTimeout(500000); connectorServer.setConnectors(new Connector[] {http, https, mutualHttps}); // Initializing the security handler ServletContextHandler handler = new ServletContextHandler( connectorServer, "/", ServletContextHandler.SESSIONS | ServletContextHandler.SECURITY); ServletHolder holder = handler.getServletHandler().newServletHolder(BaseHolder.Source.EMBEDDED); serverConnectorFramework = serverConnectorFrameworkFactory.acquire(); localConnectorFramework = localConnectorFrameworkFactory.acquire(); holder.setServlet(new OpenICFWebSocketServletBase(serverConnectorFrameworkFactory)); holder.setInitParameter("maxIdleTime", "300000"); holder.setInitParameter("maxAsyncWriteTimeout", "60000"); holder.setInitParameter("maxBinaryMessageSize", "32768"); holder.setInitParameter("inputBufferSize", "4096"); handler.addServlet(holder, "/openicf/*"); SecurityHandler sh = getSecurityHandler(); sh.setHandler(handler); connectorServer.setHandler(sh); connectorServer.start(); Reporter.log("Jetty Server Started", true); // Initialise the ConnectorFramework serverConnectorFramework .get() .getLocalManager() .addConnectorBundle(TstConnector.class.getProtectionDomain().getCodeSource().getLocation()); localConnectorFramework .get() .getLocalManager() .addConnectorBundle(TstConnector.class.getProtectionDomain().getCodeSource().getLocation()); connectorServer.start(); }