Search in sources :

Example 76 with ServletContextHandler

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

the class AbstractBaseEsbPerformanceIntegrationTest method setUp.

@Before
@Override
public void setUp() throws Exception {
    server = new Server(9000);
    ServerConnector connector0 = new ServerConnector(server);
    connector0.setReuseAddress(true);
    server.setConnectors(new Connector[] { connector0 });
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/service");
    server.setHandler(context);
    context.addServlet(new ServletHolder(new EchoService()), "/EchoService");
    server.start();
    payload = readPayload();
    super.setUp();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Before(org.junit.Before)

Example 77 with ServletContextHandler

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

the class WebsocketComponent method connect.

/**
     * Connects the URL specified on the endpoint to the specified processor.
     */
public void connect(WebsocketProducerConsumer prodcon) throws Exception {
    Server server = null;
    WebsocketEndpoint endpoint = prodcon.getEndpoint();
    String connectorKey = getConnectorKey(endpoint);
    synchronized (CONNECTORS) {
        ConnectorRef connectorRef = CONNECTORS.get(connectorKey);
        if (connectorRef == null) {
            ServerConnector connector;
            // Create Server and add connector
            server = createServer();
            if (endpoint.isEnableJmx()) {
                enableJmx(server);
            }
            if (endpoint.getSslContextParameters() != null) {
                connector = getSslSocketConnector(server, endpoint.getSslContextParameters());
            } else {
                connector = new ServerConnector(server);
            }
            if (endpoint.getPort() != null) {
                connector.setPort(endpoint.getPort());
            } else {
                connector.setPort(port);
            }
            if (endpoint.getHost() != null) {
                connector.setHost(endpoint.getHost());
            } else {
                connector.setHost(host);
            }
            server.addConnector(connector);
            LOG.trace("Jetty Connector added: {}", connector.getName());
            // Create ServletContextHandler
            ServletContextHandler context = createContext(server, connector, endpoint.getHandlers());
            // setup the WebSocketComponentServlet initial parameters 
            setWebSocketComponentServletInitialParameter(context, endpoint);
            server.setHandler(context);
            // Apply CORS (http://www.w3.org/TR/cors/)
            applyCrossOriginFiltering(endpoint, context);
            // Create Static resources
            if (endpoint.getStaticResources() != null) {
                server = createStaticResourcesServer(server, context, endpoint.getStaticResources());
            }
            MemoryWebsocketStore memoryStore = new MemoryWebsocketStore();
            // Don't provide a Servlet object as Producer/Consumer will create them later on
            connectorRef = new ConnectorRef(server, connector, null, memoryStore);
            // must enable session before we start
            if (endpoint.isSessionSupport()) {
                enableSessionSupport(connectorRef.server, connectorKey);
            }
            LOG.info("Jetty Server starting on host: {}:{}", connector.getHost(), connector.getPort());
            connectorRef.memoryStore.start();
            connectorRef.server.start();
            CONNECTORS.put(connectorKey, connectorRef);
        } else {
            connectorRef.increment();
        }
        // check the session support
        if (endpoint.isSessionSupport()) {
            enableSessionSupport(connectorRef.server, connectorKey);
        }
        NodeSynchronization sync = new DefaultNodeSynchronization(connectorRef.memoryStore);
        WebsocketComponentServlet servlet = addServlet(sync, prodcon, endpoint.getResourceUri());
        if (prodcon instanceof WebsocketConsumer) {
            WebsocketConsumer consumer = WebsocketConsumer.class.cast(prodcon);
            if (servlet.getConsumer() == null) {
                servlet.setConsumer(consumer);
            }
            // register the consumer here
            servlet.connect(consumer);
        }
        if (prodcon instanceof WebsocketProducer) {
            WebsocketProducer producer = WebsocketProducer.class.cast(prodcon);
            producer.setStore(connectorRef.memoryStore);
        }
    }
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Server(org.eclipse.jetty.server.Server) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 78 with ServletContextHandler

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

the class WebsocketComponent method doStart.

@Override
protected void doStart() throws Exception {
    super.doStart();
    if (staticResources != null) {
        // host and port must be configured
        ObjectHelper.notEmpty(host, "host", this);
        ObjectHelper.notNull(port, "port", this);
        LOG.info("Starting static resources server {}:{} with static resource: {}", new Object[] { host, port, staticResources });
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        staticResourcesServer = createStaticResourcesServer(context, host, port, staticResources);
        staticResourcesServer.start();
        ServerConnector connector = (ServerConnector) staticResourcesServer.getConnectors()[0];
        // must add static resource server to CONNECTORS in case the websocket producers/consumers
        // uses the same port number, and therefore we must be part of this
        MemoryWebsocketStore memoryStore = new MemoryWebsocketStore();
        ConnectorRef ref = new ConnectorRef(staticResourcesServer, connector, null, memoryStore);
        String key = "websocket:" + host + ":" + port;
        CONNECTORS.put(key, ref);
    }
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 79 with ServletContextHandler

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

the class WebsocketEndpointConfigurationTest method testSetServletThreadPool.

@Test
public void testSetServletThreadPool() throws Exception {
    port = AvailablePortFinder.getNextAvailable(16332);
    String uri = "websocket://localhost:" + port + "/bar?bufferSize=25000&maxIdleTime=3000";
    WebsocketEndpoint websocketEndpoint = (WebsocketEndpoint) context.getEndpoint(uri);
    WebsocketComponent component = websocketEndpoint.getComponent();
    QueuedThreadPool qtp = new QueuedThreadPool(20, 1);
    component.setThreadPool(qtp);
    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);
    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);
}
Also used : WebSocketServletFactory(org.eclipse.jetty.websocket.servlet.WebSocketServletFactory) Consumer(org.apache.camel.Consumer) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ConnectorRef(org.apache.camel.component.websocket.WebsocketComponent.ConnectorRef) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Example 80 with ServletContextHandler

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

the class WebsocketComponentTest method testCreateServerWithoutStaticContent.

@Test
public void testCreateServerWithoutStaticContent() throws Exception {
    ServletContextHandler handler = component.createContext(server, server.getConnectors()[0], null);
    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());
    assertNull(handler.getSessionHandler());
    assertNull(handler.getResourceBase());
    assertNull(handler.getServletHandler().getHolderEntry("/"));
}
Also used : ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) 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