Search in sources :

Example 96 with ServletHolder

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

the class WebsocketComponentTest method testCreateServlet.

@Test
public void testCreateServlet() throws Exception {
    component.createServlet(sync, PATH_SPEC_ONE, servlets, handler);
    InOrder inOrder = inOrder(servlet, consumer, sync, servlets, handler);
    ArgumentCaptor<WebsocketComponentServlet> servletCaptor = ArgumentCaptor.forClass(WebsocketComponentServlet.class);
    inOrder.verify(servlets, times(1)).put(eq(PATH_SPEC_ONE), servletCaptor.capture());
    ArgumentCaptor<ServletHolder> holderCaptor = ArgumentCaptor.forClass(ServletHolder.class);
    inOrder.verify(handler, times(1)).addServlet(holderCaptor.capture(), eq(PATH_SPEC_ONE));
    inOrder.verifyNoMoreInteractions();
    assertEquals(servletCaptor.getValue(), holderCaptor.getValue().getServlet());
}
Also used : InOrder(org.mockito.InOrder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) Test(org.junit.Test)

Example 97 with ServletHolder

use of org.eclipse.jetty.servlet.ServletHolder in project rest.li by linkedin.

the class HttpServerBuilder method build.

public Server build() {
    Server server = new Server();
    // HTTP Configuration
    HttpConfiguration configuration = new HttpConfiguration();
    configuration.setSendXPoweredBy(true);
    configuration.setSendServerVersion(true);
    configuration.setSendXPoweredBy(false);
    configuration.setSendServerVersion(false);
    configuration.setSendDateHeader(false);
    // HTTP Connector
    ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(configuration), new HTTP2CServerConnectionFactory(configuration));
    http.setIdleTimeout(_idleTimeout);
    http.setPort(HTTP_PORT);
    server.addConnector(http);
    ServletContextHandler handler = new ServletContextHandler(server, "");
    handler.addServlet(new ServletHolder(new HttpServlet() {

        private static final long serialVersionUID = 0;

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
            awaitLatch();
            readEntity(req.getReader());
            addStatus(resp);
            addHeader(resp);
            addContent(resp);
        }

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            awaitLatch();
            readEntity(req.getReader());
            addStatus(resp);
            addHeader(resp);
            addContent(resp);
        }

        @Override
        protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            awaitLatch();
            readEntity(req.getReader());
            addStatus(resp);
            addHeader(resp);
            addContent(resp);
        }

        private void addStatus(HttpServletResponse resp) throws IOException {
            resp.setStatus(_status);
        }

        private void addHeader(HttpServletResponse resp) throws IOException {
            if (_headerSize <= 0) {
                return;
            }
            int valueSize = _headerSize - HEADER_NAME.length();
            char[] headerValue = new char[valueSize];
            Arrays.fill(headerValue, 'a');
            resp.addHeader(HEADER_NAME, new String(headerValue));
        }

        private void addContent(HttpServletResponse resp) throws IOException {
            if (_responseSize <= 0) {
                return;
            }
            char[] content = new char[_responseSize];
            Arrays.fill(content, 'a');
            resp.getWriter().write(content);
        }

        private void awaitLatch() {
            if (_responseLatch != null) {
                try {
                    _responseLatch.await(RESPONSE_LATCH_TIMEOUT, RESPONSE_LATCH_TIMEUNIT);
                } catch (InterruptedException e) {
                }
            }
        }

        private void readEntity(BufferedReader reader) throws IOException {
            while (true) {
                char[] bytes = new char[8192];
                int read = reader.read(bytes);
                if (read < 0) {
                    break;
                }
            }
        }
    }), "/*");
    return server;
}
Also used : Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpServletRequest(javax.servlet.http.HttpServletRequest) HTTP2CServerConnectionFactory(org.eclipse.jetty.http2.server.HTTP2CServerConnectionFactory) BufferedReader(java.io.BufferedReader) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 98 with ServletHolder

use of org.eclipse.jetty.servlet.ServletHolder in project rest.li by linkedin.

the class HttpJettyServer method start.

@Override
public void start() throws IOException {
    _server = new Server(new QueuedThreadPool(_threadPoolSize));
    _server.setConnectors(getConnectors(_server));
    ServletContextHandler root = new ServletContextHandler(_server, _contextPath, ServletContextHandler.SESSIONS);
    root.addServlet(new ServletHolder(_servlet), "/*");
    try {
        _server.start();
    } catch (Exception e) {
        throw new IOException("Failed to start Jetty", e);
    }
}
Also used : Server(org.eclipse.jetty.server.Server) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) IOException(java.io.IOException) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) IOException(java.io.IOException)

Example 99 with ServletHolder

use of org.eclipse.jetty.servlet.ServletHolder in project v7files by thiloplanz.

the class ServeCommand method main.

/**
	 * @param args
	 * @throws Exception
	 */
public static void main(String[] args) throws Exception {
    Configuration.checkEndpoints();
    ServletContextHandler handler = new ServletContextHandler();
    handler.setContextPath("/");
    // WebDAV
    {
        String[] endpoints = Configuration.getArrayProperty("webdav.endpoints");
        for (String endpoint : endpoints) {
            ServletHolder servlet = new ServletHolder(new MiltonServlet());
            servlet.setInitParameter("webdav.endpoint", endpoint);
            servlet.setInitParameter("resource.factory.factory.class", Configuration.getProperty("resource.factory.factory.class"));
            handler.addServlet(servlet, endpoint.equals("/") ? "/*" : (endpoint + "/*"));
        }
    }
    // Buckets
    {
        String[] endpoints = Configuration.getArrayProperty("buckets.endpoints");
        for (String endpoint : endpoints) {
            ServletHolder servlet = new ServletHolder(new BucketsServlet(Configuration.getEndpointProperties(endpoint)));
            handler.addServlet(servlet, endpoint.equals("/") ? "/*" : (endpoint + "/*"));
        }
    }
    int port = Integer.parseInt(Configuration.getProperty("http.port"));
    final Server server = new Server(port);
    server.setHandler(handler);
    try {
        server.start();
    } catch (Exception e) {
        e.printStackTrace();
        System.err.println("failed to start HTTP server");
        try {
            server.stop();
        } catch (Exception f) {
            f.printStackTrace();
            System.exit(1);
        }
    }
    try {
        System.in.read();
        server.stop();
    } catch (IOException e) {
        System.err.println("STDIN unavailable, continue running in daemon mode");
        try {
            synchronized (args) {
                // forever
                args.wait();
            }
        } catch (InterruptedException e1) {
        }
    }
}
Also used : MiltonServlet(v7db.files.milton.MiltonServlet) Server(org.eclipse.jetty.server.Server) BucketsServlet(v7db.files.buckets.BucketsServlet) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) IOException(java.io.IOException) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) IOException(java.io.IOException)

Example 100 with ServletHolder

use of org.eclipse.jetty.servlet.ServletHolder in project gocd by gocd.

the class FakeGoServer method addFakeArtifactiPublisherServlet.

private static void addFakeArtifactiPublisherServlet(WebAppContext wac) {
    ServletHolder holder = new ServletHolder();
    holder.setServlet(new FakeArtifactPublisherServlet());
    wac.addServlet(holder, "/remoting/repository/*");
    wac.addServlet(holder, "/remoting/files/*");
}
Also used : ServletHolder(org.eclipse.jetty.servlet.ServletHolder)

Aggregations

ServletHolder (org.eclipse.jetty.servlet.ServletHolder)265 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)142 Server (org.eclipse.jetty.server.Server)97 Test (org.junit.Test)72 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)44 ServerConnector (org.eclipse.jetty.server.ServerConnector)42 HttpServletRequest (javax.servlet.http.HttpServletRequest)36 HttpClient (org.eclipse.jetty.client.HttpClient)23 CountDownLatch (java.util.concurrent.CountDownLatch)22 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)22 IOException (java.io.IOException)20 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)20 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)20 DefaultServlet (org.eclipse.jetty.servlet.DefaultServlet)18 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)18 BeforeClass (org.junit.BeforeClass)17 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)16 File (java.io.File)15 ServletException (javax.servlet.ServletException)15 HttpServletResponse (javax.servlet.http.HttpServletResponse)15