Search in sources :

Example 11 with Handler

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

the class RequestLogHandlerTest method testMultipleLogHandlers.

@Test(timeout = 4000)
public void testMultipleLogHandlers() throws Exception {
    Server server = new Server();
    ServerConnector connector = new ServerConnector(server);
    connector.setPort(0);
    server.setConnectors(new Connector[] { connector });
    List<CaptureLog> captureLogs = new ArrayList<>();
    List<Handler> handlerList = new ArrayList<>();
    handlerList.add(testHandler);
    for (int i = 0; i < 4; ++i) {
        CaptureLog captureLog = new CaptureLog();
        captureLogs.add(captureLog);
        RequestLogHandler requestLog = new RequestLogHandler();
        requestLog.setRequestLog(captureLog);
        handlerList.add(requestLog);
    }
    HandlerCollection handlers = new HandlerCollection();
    handlers.setHandlers(handlerList.toArray(new Handler[0]));
    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();
        }
        for (CaptureLog captureLog : captureLogs) assertRequestLog(captureLog);
    } finally {
        server.stop();
    }
}
Also used : Server(org.eclipse.jetty.server.Server) ArrayList(java.util.ArrayList) Handler(org.eclipse.jetty.server.Handler) URI(java.net.URI) ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpURLConnection(java.net.HttpURLConnection) Test(org.junit.Test)

Example 12 with Handler

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

the class JettyHttpServer method checkIfContextIsFree.

private void checkIfContextIsFree(String path) {
    Handler serverHandler = _server.getHandler();
    if (serverHandler instanceof ContextHandler) {
        ContextHandler ctx = (ContextHandler) serverHandler;
        if (ctx.getContextPath().equals(path))
            throw new RuntimeException("another context already bound to path " + path);
    }
    Handler[] handlers = _server.getHandlers();
    if (handlers == null)
        return;
    for (Handler handler : handlers) {
        if (handler instanceof ContextHandler) {
            ContextHandler ctx = (ContextHandler) handler;
            if (ctx.getContextPath().equals(path))
                throw new RuntimeException("another context already bound to path " + path);
        }
    }
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) Handler(org.eclipse.jetty.server.Handler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) HttpHandler(com.sun.net.httpserver.HttpHandler)

Example 13 with Handler

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

the class JettyHttpServer method findContextHandlerCollection.

private ContextHandlerCollection findContextHandlerCollection(Handler[] handlers) {
    if (handlers == null)
        return null;
    for (Handler handler : handlers) {
        if (handler instanceof ContextHandlerCollection) {
            return (ContextHandlerCollection) handler;
        }
        if (handler instanceof HandlerCollection) {
            HandlerCollection hc = (HandlerCollection) handler;
            ContextHandlerCollection chc = findContextHandlerCollection(hc.getHandlers());
            if (chc != null)
                return chc;
        }
    }
    return null;
}
Also used : Handler(org.eclipse.jetty.server.Handler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) HttpHandler(com.sun.net.httpserver.HttpHandler) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection)

Example 14 with Handler

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

the class Invoker method init.

/* ------------------------------------------------------------ */
public void init() {
    ServletContext config = getServletContext();
    _contextHandler = ((ContextHandler.Context) config).getContextHandler();
    Handler handler = _contextHandler.getHandler();
    while (handler != null && !(handler instanceof ServletHandler) && (handler instanceof HandlerWrapper)) handler = ((HandlerWrapper) handler).getHandler();
    _servletHandler = (ServletHandler) handler;
    Enumeration<String> e = getInitParameterNames();
    while (e.hasMoreElements()) {
        String param = e.nextElement();
        String value = getInitParameter(param);
        String lvalue = value.toLowerCase(Locale.ENGLISH);
        if ("nonContextServlets".equals(param)) {
            _nonContextServlets = value.length() > 0 && lvalue.startsWith("t");
        }
        if ("verbose".equals(param)) {
            _verbose = value.length() > 0 && lvalue.startsWith("t");
        } else {
            if (_parameters == null)
                _parameters = new HashMap<String, String>();
            _parameters.put(param, value);
        }
    }
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) HashMap(java.util.HashMap) ServletContext(javax.servlet.ServletContext) Handler(org.eclipse.jetty.server.Handler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) HandlerWrapper(org.eclipse.jetty.server.handler.HandlerWrapper)

Example 15 with Handler

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

the class ServletContextHandler method setSessionHandler.

/* ------------------------------------------------------------ */
/**
     * @param sessionHandler The sessionHandler to set.
     */
public void setSessionHandler(SessionHandler sessionHandler) {
    if (isStarted())
        throw new IllegalStateException("STARTED");
    Handler next = null;
    if (_sessionHandler != null) {
        next = _sessionHandler.getHandler();
        _sessionHandler.setHandler(null);
        replaceHandler(_sessionHandler, sessionHandler);
    }
    _sessionHandler = sessionHandler;
    if (next != null && _sessionHandler.getHandler() == null)
        _sessionHandler.setHandler(next);
    relinkHandlers();
}
Also used : Handler(org.eclipse.jetty.server.Handler) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) SecurityHandler(org.eclipse.jetty.security.SecurityHandler) GzipHandler(org.eclipse.jetty.server.handler.gzip.GzipHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) SessionHandler(org.eclipse.jetty.server.session.SessionHandler)

Aggregations

Handler (org.eclipse.jetty.server.Handler)63 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)18 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)18 Server (org.eclipse.jetty.server.Server)14 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)13 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)12 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)10 RequestLogHandler (org.eclipse.jetty.server.handler.RequestLogHandler)10 SessionHandler (org.eclipse.jetty.server.session.SessionHandler)10 ConstraintSecurityHandler (org.eclipse.jetty.security.ConstraintSecurityHandler)7 ServerConnector (org.eclipse.jetty.server.ServerConnector)7 ErrorHandler (org.eclipse.jetty.server.handler.ErrorHandler)7 HandlerList (org.eclipse.jetty.server.handler.HandlerList)7 ArrayList (java.util.ArrayList)6 Connector (org.eclipse.jetty.server.Connector)6 ResourceHandler (org.eclipse.jetty.server.handler.ResourceHandler)6 Test (org.junit.Test)6 HandlerWrapper (org.eclipse.jetty.server.handler.HandlerWrapper)5 GzipHandler (org.eclipse.jetty.server.handler.gzip.GzipHandler)5 URI (java.net.URI)4