Search in sources :

Example 81 with ServletContextHandler

use of org.eclipse.jetty.servlet.ServletContextHandler in project camel by apache.

the class WebsocketComponentTest method testCreateServerWithStaticContent.

@Test
public void testCreateServerWithStaticContent() throws Exception {
    ServletContextHandler handler = component.createContext(server, server.getConnectors()[0], null);
    Server server = component.createStaticResourcesServer(handler, "localhost", 1988, "classpath:public");
    assertEquals(1, server.getConnectors().length);
    assertEquals("localhost", ((ServerConnector) server.getConnectors()[0]).getHost());
    assertEquals(1988, ((ServerConnector) server.getConnectors()[0]).getPort());
    assertFalse(server.getConnectors()[0].isStarted());
    assertEquals(handler, server.getHandler());
    assertEquals(1, server.getHandlers().length);
    assertEquals(handler, server.getHandlers()[0]);
    assertEquals("/", handler.getContextPath());
    assertNotNull(handler.getSessionHandler());
    assertNotNull(handler.getResourceBase());
    assertTrue(handler.getResourceBase().startsWith(JettyClassPathResource.class.getName()));
    assertNotNull(handler.getServletHandler().getHolderEntry("/"));
}
Also used : Server(org.eclipse.jetty.server.Server) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Example 82 with ServletContextHandler

use of org.eclipse.jetty.servlet.ServletContextHandler in project camel by apache.

the class WebsocketEndpointConfigurationTest method testSetServletInitalparameters.

@Test
public void testSetServletInitalparameters() throws Exception {
    port = AvailablePortFinder.getNextAvailable(16330);
    String uri = "websocket://localhost:" + port + "/bar?bufferSize=25000&maxIdleTime=3000&maxTextMessageSize=500&maxBinaryMessageSize=550";
    WebsocketEndpoint websocketEndpoint = (WebsocketEndpoint) context.getEndpoint(uri);
    WebsocketComponent component = websocketEndpoint.getComponent();
    component.setMinThreads(1);
    component.setMaxThreads(20);
    Consumer consumer = websocketEndpoint.createConsumer(processor);
    component.connect((WebsocketProducerConsumer) consumer);
    assertNotNull(consumer);
    assertEquals(WebsocketConsumer.class, consumer.getClass());
    // just check the servlet initial parameters
    ConnectorRef conector = WebsocketComponent.getConnectors().values().iterator().next();
    ServletContextHandler context = (ServletContextHandler) conector.server.getHandler();
    String buffersize = context.getInitParameter("bufferSize");
    assertEquals("Got a wrong buffersize", "25000", buffersize);
    String maxIdleTime = context.getInitParameter("maxIdleTime");
    assertEquals("Got a wrong maxIdleTime", "3000", maxIdleTime);
    String maxTextMessageSize = context.getInitParameter("maxTextMessageSize");
    assertEquals("Got a wrong maxTextMessageSize", "500", maxTextMessageSize);
    String maxBinaryMessageSize = context.getInitParameter("maxBinaryMessageSize");
    assertEquals("Got a wrong maxBinaryMessageSize", "550", maxBinaryMessageSize);
    WebSocketServletFactory factory = (WebSocketServletFactory) context.getServletContext().getAttribute(WebSocketServletFactory.class.getName());
    int factoryBufferSize = factory.getPolicy().getInputBufferSize();
    assertEquals("Got a wrong buffersize", 25000, factoryBufferSize);
    long factoryMaxIdleTime = factory.getPolicy().getIdleTimeout();
    assertEquals("Got a wrong maxIdleTime", 3000, factoryMaxIdleTime);
    int factoryMaxTextMessageSize = factory.getPolicy().getMaxTextMessageSize();
    assertEquals("Got a wrong maxTextMessageSize", 500, factoryMaxTextMessageSize);
    int factoryMaxBinaryMessageSize = factory.getPolicy().getMaxBinaryMessageSize();
    assertEquals("Got a wrong maxBinaryMessageSize", 550, factoryMaxBinaryMessageSize);
}
Also used : WebSocketServletFactory(org.eclipse.jetty.websocket.servlet.WebSocketServletFactory) Consumer(org.apache.camel.Consumer) ConnectorRef(org.apache.camel.component.websocket.WebsocketComponent.ConnectorRef) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Example 83 with ServletContextHandler

use of org.eclipse.jetty.servlet.ServletContextHandler in project camel by apache.

the class JettyTestServer method startServer.

public void startServer() {
    server = new Server(PORT);
    port = PORT;
    ServletContextHandler servletContext = new ServletContextHandler(ServletContextHandler.SESSIONS);
    servletContext.setSecurityHandler(basicAuth("camel", "camelPass", "Private!"));
    servletContext.setContextPath("/");
    server.setHandler(servletContext);
    servletContext.addServlet(new ServletHolder(new MyHttpServlet()), "/*");
    try {
        server.start();
    } catch (Exception ex) {
        LOG.error("Could not start Server!", ex);
        fail(ex.getLocalizedMessage());
    }
}
Also used : Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 84 with ServletContextHandler

use of org.eclipse.jetty.servlet.ServletContextHandler in project hadoop by apache.

the class HttpServer2 method addDefaultApps.

/**
   * Add default apps.
   * @param appDir The application directory
   * @throws IOException
   */
protected void addDefaultApps(ContextHandlerCollection parent, final String appDir, Configuration conf) throws IOException {
    // set up the context for "/logs/" if "hadoop.log.dir" property is defined
    // and it's enabled.
    String logDir = System.getProperty("hadoop.log.dir");
    boolean logsEnabled = conf.getBoolean(CommonConfigurationKeys.HADOOP_HTTP_LOGS_ENABLED, CommonConfigurationKeys.HADOOP_HTTP_LOGS_ENABLED_DEFAULT);
    if (logDir != null && logsEnabled) {
        ServletContextHandler logContext = new ServletContextHandler(parent, "/logs");
        logContext.setResourceBase(logDir);
        logContext.addServlet(AdminAuthorizedServlet.class, "/*");
        if (conf.getBoolean(CommonConfigurationKeys.HADOOP_JETTY_LOGS_SERVE_ALIASES, CommonConfigurationKeys.DEFAULT_HADOOP_JETTY_LOGS_SERVE_ALIASES)) {
            @SuppressWarnings("unchecked") Map<String, String> params = logContext.getInitParams();
            params.put("org.eclipse.jetty.servlet.Default.aliases", "true");
        }
        logContext.setDisplayName("logs");
        SessionHandler handler = new SessionHandler();
        SessionManager sm = handler.getSessionManager();
        if (sm instanceof AbstractSessionManager) {
            AbstractSessionManager asm = (AbstractSessionManager) sm;
            asm.setHttpOnly(true);
            asm.getSessionCookieConfig().setSecure(true);
        }
        logContext.setSessionHandler(handler);
        setContextAttributes(logContext, conf);
        addNoCacheFilter(logContext);
        defaultContexts.put(logContext, true);
    }
    // set up the context for "/static/*"
    ServletContextHandler staticContext = new ServletContextHandler(parent, "/static");
    staticContext.setResourceBase(appDir + "/static");
    staticContext.addServlet(DefaultServlet.class, "/*");
    staticContext.setDisplayName("static");
    @SuppressWarnings("unchecked") Map<String, String> params = staticContext.getInitParams();
    params.put("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
    params.put("org.eclipse.jetty.servlet.Default.gzip", "true");
    SessionHandler handler = new SessionHandler();
    SessionManager sm = handler.getSessionManager();
    if (sm instanceof AbstractSessionManager) {
        AbstractSessionManager asm = (AbstractSessionManager) sm;
        asm.setHttpOnly(true);
        asm.getSessionCookieConfig().setSecure(true);
    }
    staticContext.setSessionHandler(handler);
    setContextAttributes(staticContext, conf);
    defaultContexts.put(staticContext, true);
}
Also used : SessionHandler(org.eclipse.jetty.server.session.SessionHandler) SessionManager(org.eclipse.jetty.server.SessionManager) AbstractSessionManager(org.eclipse.jetty.server.session.AbstractSessionManager) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) AbstractSessionManager(org.eclipse.jetty.server.session.AbstractSessionManager)

Example 85 with ServletContextHandler

use of org.eclipse.jetty.servlet.ServletContextHandler in project hadoop by apache.

the class TestHTestCase method testJetty.

@Test
@TestJetty
public void testJetty() throws Exception {
    ServletContextHandler context = new ServletContextHandler();
    context.setContextPath("/");
    context.addServlet(MyServlet.class, "/bar");
    Server server = TestJettyHelper.getJettyServer();
    server.setHandler(context);
    server.start();
    URL url = new URL(TestJettyHelper.getJettyURL(), "/bar");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    assertEquals(conn.getResponseCode(), HttpURLConnection.HTTP_OK);
    BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    assertEquals(reader.readLine(), "foo");
    reader.close();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Server(org.eclipse.jetty.server.Server) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) URL(java.net.URL) Test(org.junit.Test)

Aggregations

ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)390 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)253 Server (org.eclipse.jetty.server.Server)217 ServerConnector (org.eclipse.jetty.server.ServerConnector)98 Test (org.junit.Test)70 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)56 IOException (java.io.IOException)42 HttpServletRequest (javax.servlet.http.HttpServletRequest)39 HttpClient (org.eclipse.jetty.client.HttpClient)39 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)39 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)37 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)37 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)32 Connector (org.eclipse.jetty.server.Connector)29 Request (org.eclipse.jetty.client.api.Request)27 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)24 ServletContainer (org.glassfish.jersey.servlet.ServletContainer)24 ServletException (javax.servlet.ServletException)22 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)22 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)22