Search in sources :

Example 56 with Handler

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

the class JettyHttpComponent method addJettyHandlers.

protected void addJettyHandlers(Server server, List<Handler> handlers) {
    if (handlers != null && !handlers.isEmpty()) {
        for (Handler handler : handlers) {
            if (handler instanceof HandlerWrapper) {
                // avoid setting a handler more than once
                if (!isHandlerInChain(server.getHandler(), handler)) {
                    ((HandlerWrapper) handler).setHandler(server.getHandler());
                    server.setHandler(handler);
                }
            } else {
                HandlerCollection handlerCollection = new HandlerCollection();
                handlerCollection.addHandler(server.getHandler());
                handlerCollection.addHandler(handler);
                server.setHandler(handlerCollection);
            }
        }
    }
}
Also used : ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) Handler(org.eclipse.jetty.server.Handler) SessionHandler(org.eclipse.jetty.server.session.SessionHandler) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) HandlerWrapper(org.eclipse.jetty.server.handler.HandlerWrapper)

Example 57 with Handler

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

the class SLSWebApp method start.

public void start() throws Exception {
    // static files
    final ResourceHandler staticHandler = new ResourceHandler();
    staticHandler.setMimeTypes(new MimeTypes());
    staticHandler.setResourceBase("html");
    Handler handler = new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            try {
                // timeunit
                // second, divide millionsecond / 1000
                int timeunit = 1000;
                String timeunitLabel = "second";
                if (request.getParameter("u") != null && request.getParameter("u").equalsIgnoreCase("m")) {
                    timeunit = 1000 * 60;
                    timeunitLabel = "minute";
                }
                // http request
                if (target.equals("/")) {
                    printPageIndex(request, response);
                } else if (target.equals("/simulate")) {
                    printPageSimulate(request, response, timeunit, timeunitLabel);
                } else if (target.equals("/track")) {
                    printPageTrack(request, response, timeunit, timeunitLabel);
                } else // js/css request
                if (target.startsWith("/js") || target.startsWith("/css")) {
                    response.setCharacterEncoding("utf-8");
                    staticHandler.handle(target, baseRequest, request, response);
                } else // json request
                if (target.equals("/simulateMetrics")) {
                    printJsonMetrics(request, response);
                } else if (target.equals("/trackMetrics")) {
                    printJsonTrack(request, response);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    server = new Server(port);
    server.setHandler(handler);
    server.start();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Server(org.eclipse.jetty.server.Server) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) Handler(org.eclipse.jetty.server.Handler) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletResponse(javax.servlet.http.HttpServletResponse) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) MimeTypes(org.eclipse.jetty.http.MimeTypes) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 58 with Handler

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

the class MockHttpServer method getMockHandler.

/**
   * Creates an {@link org.mortbay.jetty.handler.AbstractHandler handler} returning an arbitrary String as a response.
   *
   * @return never <code>null</code>.
   */
public Handler getMockHandler() {
    Handler handler = new AbstractHandler() {

        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            targets.add(target);
            setResponseBody(getMockResponseData());
            setRequestBody(IOUtils.toString(baseRequest.getInputStream()));
            response.setStatus(mockResponseStatus);
            response.setContentType("text/xml;charset=utf-8");
            write(getResponseBody(), response.getOutputStream());
            baseRequest.setHandled(true);
        }
    };
    return handler;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) Handler(org.eclipse.jetty.server.Handler) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletResponse(javax.servlet.http.HttpServletResponse) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler)

Example 59 with Handler

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

the class MasterService method setUpRequestLogging.

private void setUpRequestLogging(final Path stateDirectory) {
    // Set up request logging
    final Handler originalHandler = server.getHandler();
    final HandlerCollection handlerCollection;
    if (originalHandler instanceof HandlerCollection) {
        handlerCollection = (HandlerCollection) originalHandler;
    } else {
        handlerCollection = new HandlerCollection();
        handlerCollection.addHandler(originalHandler);
    }
    final RequestLogHandler requestLogHandler = new RequestLogHandler();
    final RequestLogImpl requestLog = new RequestLogImpl();
    requestLog.setQuiet(true);
    if (stateDirectory.resolve(LOGBACK_ACCESS_CONFIG).toFile().exists()) {
        requestLog.setFileName(stateDirectory.resolve(LOGBACK_ACCESS_CONFIG).toString());
    } else if (this.getClass().getResource(LOGBACK_ACCESS_RESOURCE) != null) {
        requestLog.setResource(LOGBACK_ACCESS_RESOURCE);
    }
    requestLogHandler.setRequestLog(requestLog);
    handlerCollection.addHandler(requestLogHandler);
    server.setHandler(handlerCollection);
}
Also used : RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) RequestLogImpl(ch.qos.logback.access.jetty.RequestLogImpl) Handler(org.eclipse.jetty.server.Handler) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection)

Example 60 with Handler

use of org.eclipse.jetty.server.Handler in project spring-boot by spring-projects.

the class JettyWebServer method start.

@Override
public void start() throws WebServerException {
    synchronized (this.monitor) {
        if (this.started) {
            return;
        }
        this.server.setConnectors(this.connectors);
        if (!this.autoStart) {
            return;
        }
        try {
            this.server.start();
            for (Handler handler : this.server.getHandlers()) {
                handleDeferredInitialize(handler);
            }
            Connector[] connectors = this.server.getConnectors();
            for (Connector connector : connectors) {
                try {
                    connector.start();
                } catch (BindException ex) {
                    if (connector instanceof NetworkConnector) {
                        throw new PortInUseException(((NetworkConnector) connector).getPort());
                    }
                    throw ex;
                }
            }
            this.started = true;
            JettyWebServer.logger.info("Jetty started on port(s) " + getActualPortsDescription());
        } catch (WebServerException ex) {
            throw ex;
        } catch (Exception ex) {
            throw new WebServerException("Unable to start embedded Jetty server", ex);
        }
    }
}
Also used : NetworkConnector(org.eclipse.jetty.server.NetworkConnector) Connector(org.eclipse.jetty.server.Connector) PortInUseException(org.springframework.boot.web.server.PortInUseException) WebServerException(org.springframework.boot.web.server.WebServerException) Handler(org.eclipse.jetty.server.Handler) BindException(java.net.BindException) NetworkConnector(org.eclipse.jetty.server.NetworkConnector) WebServerException(org.springframework.boot.web.server.WebServerException) BindException(java.net.BindException) PortInUseException(org.springframework.boot.web.server.PortInUseException)

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