Search in sources :

Example 61 with ServerConnector

use of org.eclipse.jetty.server.ServerConnector in project jetty.project by eclipse.

the class RequestLogHandlerTest method testLogHandlerCollection_OKErrorHandler.

/**
     * Test a RequestLogHandler at the end of a HandlerCollection and also with the ErrorHandler in place.
     * @throws Exception if test failure
     */
@Test(timeout = 4000)
public void testLogHandlerCollection_OKErrorHandler() throws Exception {
    Server server = new Server();
    ServerConnector connector = new ServerConnector(server);
    connector.setPort(0);
    server.setConnectors(new Connector[] { connector });
    OKErrorHandler errorDispatcher = new OKErrorHandler();
    server.addBean(errorDispatcher);
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    ContextHandler errorContext = new ContextHandler("/errorpage");
    errorContext.setHandler(new AltErrorHandler());
    ContextHandler testContext = new ContextHandler("/test");
    testContext.setHandler(testHandler);
    contexts.addHandler(errorContext);
    contexts.addHandler(testContext);
    RequestLogHandler requestLog = new RequestLogHandler();
    CaptureLog captureLog = new CaptureLog();
    requestLog.setRequestLog(captureLog);
    HandlerCollection handlers = new HandlerCollection();
    handlers.setHandlers(new Handler[] { contexts, requestLog });
    server.setHandler(handlers);
    try {
        server.start();
        String host = connector.getHost();
        if (host == null) {
            host = "localhost";
        }
        int port = connector.getLocalPort();
        URI serverUri = new URI("http", null, host, port, requestPath, null, null);
        // Make call to test handler
        HttpURLConnection connection = (HttpURLConnection) serverUri.toURL().openConnection();
        try {
            connection.setAllowUserInteraction(false);
            // log response status code
            int statusCode = connection.getResponseCode();
            LOG.debug("Response Status Code: {}", statusCode);
            if (statusCode == 200) {
                // collect response message and log it
                String content = getResponseContent(connection);
                LOG.debug("Response Content: {}", content);
            }
        } finally {
            connection.disconnect();
        }
        assertRequestLog(captureLog);
    } finally {
        server.stop();
    }
}
Also used : Server(org.eclipse.jetty.server.Server) URI(java.net.URI) ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpURLConnection(java.net.HttpURLConnection) Test(org.junit.Test)

Example 62 with ServerConnector

use of org.eclipse.jetty.server.ServerConnector in project jetty.project by eclipse.

the class RequestLogHandlerTest method testLogHandlerWrapped.

@Test(timeout = 4000)
public void testLogHandlerWrapped() throws Exception {
    Server server = new Server();
    ServerConnector connector = new ServerConnector(server);
    connector.setPort(0);
    server.setConnectors(new Connector[] { connector });
    CaptureLog captureLog = new CaptureLog();
    RequestLogHandler requestLog = new RequestLogHandler();
    requestLog.setRequestLog(captureLog);
    requestLog.setHandler(testHandler);
    server.setHandler(requestLog);
    try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class, HttpChannelState.class)) {
        server.start();
        String host = connector.getHost();
        if (host == null) {
            host = "localhost";
        }
        int port = connector.getLocalPort();
        URI serverUri = new URI("http", null, host, port, requestPath, null, null);
        // Make call to test handler
        HttpURLConnection connection = (HttpURLConnection) serverUri.toURL().openConnection();
        try {
            connection.setAllowUserInteraction(false);
            // log response status code
            int statusCode = connection.getResponseCode();
            LOG.info("Response Status Code: {}", statusCode);
            if (statusCode == 200) {
                // collect response message and log it
                String content = getResponseContent(connection);
                LOG.info("Response Content: {}", content);
            }
        } finally {
            connection.disconnect();
        }
        assertRequestLog(captureLog);
    } finally {
        server.stop();
    }
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpURLConnection(java.net.HttpURLConnection) Server(org.eclipse.jetty.server.Server) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) URI(java.net.URI) Test(org.junit.Test)

Example 63 with ServerConnector

use of org.eclipse.jetty.server.ServerConnector in project jetty.project by eclipse.

the class RequestLogHandlerTest method testLogHandlerCollection_ErrorHandler_ServerBean.

/**
     * Test a RequestLogHandler at the end of a HandlerCollection and also with the default ErrorHandler as server bean in place.
     * @throws Exception if test failure
     */
@Test(timeout = 4000)
public void testLogHandlerCollection_ErrorHandler_ServerBean() throws Exception {
    Server server = new Server();
    ServerConnector connector = new ServerConnector(server);
    connector.setPort(0);
    server.setConnectors(new Connector[] { connector });
    ErrorHandler errorHandler = new ErrorHandler();
    server.addBean(errorHandler);
    CaptureLog captureLog = new CaptureLog();
    RequestLogHandler requestLog = new RequestLogHandler();
    requestLog.setRequestLog(captureLog);
    HandlerCollection handlers = new HandlerCollection();
    handlers.setHandlers(new Handler[] { testHandler, requestLog });
    server.setHandler(handlers);
    try {
        server.start();
        String host = connector.getHost();
        if (host == null) {
            host = "localhost";
        }
        int port = connector.getLocalPort();
        URI serverUri = new URI("http", null, host, port, requestPath, null, null);
        // Make call to test handler
        HttpURLConnection connection = (HttpURLConnection) serverUri.toURL().openConnection();
        try {
            connection.setAllowUserInteraction(false);
            // log response status code
            int statusCode = connection.getResponseCode();
            LOG.debug("Response Status Code: {}", statusCode);
            if (statusCode == 200) {
                // collect response message and log it
                String content = getResponseContent(connection);
                LOG.debug("Response Content: {}", content);
            }
        } finally {
            connection.disconnect();
        }
        assertRequestLog(captureLog);
    } finally {
        server.stop();
    }
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpURLConnection(java.net.HttpURLConnection) Server(org.eclipse.jetty.server.Server) URI(java.net.URI) Test(org.junit.Test)

Example 64 with ServerConnector

use of org.eclipse.jetty.server.ServerConnector in project jetty.project by eclipse.

the class CookiesTest method startServer.

protected void startServer(Handler handler) throws Exception {
    server = new Server();
    connector = new ServerConnector(server);
    server.addConnector(connector);
    ContextHandler context = new ContextHandler();
    context.setContextPath("/");
    context.setHandler(handler);
    server.setHandler(context);
    server.start();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) Server(org.eclipse.jetty.server.Server)

Example 65 with ServerConnector

use of org.eclipse.jetty.server.ServerConnector in project jetty.project by eclipse.

the class MisbehavingClassTest method startServer.

@SuppressWarnings("Duplicates")
@BeforeClass
public static void startServer() throws Exception {
    server = new Server();
    ServerConnector connector = new ServerConnector(server);
    server.addConnector(connector);
    handler = new EchoHandler();
    ContextHandler context = new ContextHandler();
    context.setContextPath("/");
    context.setHandler(handler);
    server.setHandler(context);
    // Start Server
    server.start();
    String host = connector.getHost();
    if (host == null) {
        host = "localhost";
    }
    int port = connector.getLocalPort();
    serverUri = new URI(String.format("ws://%s:%d/", host, port));
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) Server(org.eclipse.jetty.server.Server) EchoHandler(org.eclipse.jetty.websocket.jsr356.EchoHandler) URI(java.net.URI) BeforeClass(org.junit.BeforeClass)

Aggregations

ServerConnector (org.eclipse.jetty.server.ServerConnector)291 Server (org.eclipse.jetty.server.Server)213 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)91 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)83 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)67 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)64 SslConnectionFactory (org.eclipse.jetty.server.SslConnectionFactory)55 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)53 SecureRequestCustomizer (org.eclipse.jetty.server.SecureRequestCustomizer)45 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)45 URI (java.net.URI)37 Test (org.junit.Test)32 File (java.io.File)30 BeforeClass (org.junit.BeforeClass)29 IOException (java.io.IOException)27 Before (org.junit.Before)25 BeforeClass (org.testng.annotations.BeforeClass)22 ServletException (javax.servlet.ServletException)21 HttpServletRequest (javax.servlet.http.HttpServletRequest)19 Connector (org.eclipse.jetty.server.Connector)19