Search in sources :

Example 6 with ErrorHandler

use of org.eclipse.jetty.server.handler.ErrorHandler in project qi4j-sdk by Qi4j.

the class AbstractJettyMixin method startJetty.

@Override
public final void startJetty() throws Exception {
    // Configure Server
    configureServer(server, configuration());
    // Set up HTTP
    HttpConfiguration httpConfig = new HttpConfiguration();
    configureHttp(httpConfig, configuration());
    httpConfig = specializeHttp(httpConfig);
    // Set up connector
    ServerConnector connector = buildConnector(server, httpConfig);
    configureConnector(connector, configuration());
    // Bind Connector to Server
    server.addConnector(connector);
    if (mBeanServer != null) {
        server.addEventListener(new MBeanContainer(mBeanServer));
    }
    // Prepare ServletContext
    ServletContextHandler root = new ServletContextHandler(server, "/", new SessionHandler(), buildSecurityHandler(), new ServletHandler(), new ErrorHandler());
    root.setDisplayName(identity);
    configureContext(root, configuration());
    // Register ContextListeners, Servlets and Filters
    addContextListeners(root, contextListeners);
    addServlets(root, servlets);
    addFilters(root, filters);
    // Start
    server.start();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) SessionHandler(org.eclipse.jetty.server.session.SessionHandler) ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) ServletHandler(org.eclipse.jetty.servlet.ServletHandler) MBeanContainer(org.eclipse.jetty.jmx.MBeanContainer) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 7 with ErrorHandler

use of org.eclipse.jetty.server.handler.ErrorHandler in project ninja by ninjaframework.

the class NinjaJettyTest method checkThatSilentErrorHandlerIsRegistered.

@Test
public void checkThatSilentErrorHandlerIsRegistered() throws Exception {
    NinjaJetty standalone = new NinjaJetty().externalConfigurationPath("conf/jetty.minimal.conf").port(RANDOM_PORT);
    try {
        standalone.start();
        ErrorHandler errorHandler = standalone.contextHandler.getErrorHandler();
        assertThat(errorHandler, instanceOf(NinjaJetty.SilentErrorHandler.class));
    } finally {
        standalone.shutdown();
    }
}
Also used : ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) Test(org.junit.Test)

Example 8 with ErrorHandler

use of org.eclipse.jetty.server.handler.ErrorHandler in project drill by apache.

the class WebServer method start.

/**
   * Start the web server including setup.
   * @throws Exception
   */
public void start() throws Exception {
    if (embeddedJetty == null) {
        return;
    }
    final boolean authEnabled = config.getBoolean(ExecConstants.USER_AUTHENTICATION_ENABLED);
    if (authEnabled && !context.getAuthProvider().containsFactory(PlainFactory.SIMPLE_NAME)) {
        logger.warn("Not starting web server. Currently Drill supports web authentication only through " + "username/password. But PLAIN mechanism is not configured.");
        return;
    }
    final ServerConnector serverConnector;
    if (config.getBoolean(ExecConstants.HTTP_ENABLE_SSL)) {
        serverConnector = createHttpsConnector();
    } else {
        serverConnector = createHttpConnector();
    }
    embeddedJetty.addConnector(serverConnector);
    // Add resources
    final ErrorHandler errorHandler = new ErrorHandler();
    errorHandler.setShowStacks(true);
    errorHandler.setShowMessageInTitle(true);
    final ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
    servletContextHandler.setErrorHandler(errorHandler);
    servletContextHandler.setContextPath("/");
    final ServletHolder servletHolder = new ServletHolder(new ServletContainer(new DrillRestServer(workManager)));
    servletHolder.setInitOrder(1);
    servletContextHandler.addServlet(servletHolder, "/*");
    servletContextHandler.addServlet(new ServletHolder(new MetricsServlet(metrics)), "/status/metrics");
    servletContextHandler.addServlet(new ServletHolder(new ThreadDumpServlet()), "/status/threads");
    final ServletHolder staticHolder = new ServletHolder("static", DefaultServlet.class);
    // Get resource URL for Drill static assets, based on where Drill icon is located
    String drillIconResourcePath = Resource.newClassPathResource(BASE_STATIC_PATH + DRILL_ICON_RESOURCE_RELATIVE_PATH).getURL().toString();
    staticHolder.setInitParameter("resourceBase", drillIconResourcePath.substring(0, drillIconResourcePath.length() - DRILL_ICON_RESOURCE_RELATIVE_PATH.length()));
    staticHolder.setInitParameter("dirAllowed", "false");
    staticHolder.setInitParameter("pathInfoOnly", "true");
    servletContextHandler.addServlet(staticHolder, "/static/*");
    if (authEnabled) {
        servletContextHandler.setSecurityHandler(createSecurityHandler());
        servletContextHandler.setSessionHandler(createSessionHandler(servletContextHandler.getSecurityHandler()));
    }
    if (config.getBoolean(ExecConstants.HTTP_CORS_ENABLED)) {
        FilterHolder holder = new FilterHolder(CrossOriginFilter.class);
        holder.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, StringUtils.join(config.getStringList(ExecConstants.HTTP_CORS_ALLOWED_ORIGINS), ","));
        holder.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, StringUtils.join(config.getStringList(ExecConstants.HTTP_CORS_ALLOWED_METHODS), ","));
        holder.setInitParameter(CrossOriginFilter.ALLOWED_HEADERS_PARAM, StringUtils.join(config.getStringList(ExecConstants.HTTP_CORS_ALLOWED_HEADERS), ","));
        holder.setInitParameter(CrossOriginFilter.ALLOW_CREDENTIALS_PARAM, String.valueOf(config.getBoolean(ExecConstants.HTTP_CORS_CREDENTIALS)));
        for (String path : new String[] { "*.json", "/storage/*/enable/*", "/status*" }) {
            servletContextHandler.addFilter(holder, path, EnumSet.of(DispatcherType.REQUEST));
        }
    }
    embeddedJetty.setHandler(servletContextHandler);
    embeddedJetty.start();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) MetricsServlet(com.codahale.metrics.servlets.MetricsServlet) ThreadDumpServlet(com.codahale.metrics.servlets.ThreadDumpServlet) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 9 with ErrorHandler

use of org.eclipse.jetty.server.handler.ErrorHandler in project dropwizard by dropwizard.

the class AbstractServerFactory method buildServer.

protected Server buildServer(LifecycleEnvironment lifecycle, ThreadPool threadPool) {
    final Server server = new Server(threadPool);
    server.addLifeCycleListener(buildSetUIDListener());
    lifecycle.attach(server);
    final ErrorHandler errorHandler = new ErrorHandler();
    errorHandler.setServer(server);
    errorHandler.setShowStacks(false);
    server.addBean(errorHandler);
    server.setStopAtShutdown(true);
    server.setStopTimeout(shutdownGracePeriod.toMilliseconds());
    return server;
}
Also used : ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) Server(org.eclipse.jetty.server.Server)

Example 10 with ErrorHandler

use of org.eclipse.jetty.server.handler.ErrorHandler in project jetty.project by eclipse.

the class ServletRequestLogTest 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 on 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);
    // First the behavior as defined in etc/jetty.xml
    // id="Handlers"
    HandlerCollection handlers = new HandlerCollection();
    // id="Contexts"
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    // id="DefaultHandler"
    DefaultHandler defaultHandler = new DefaultHandler();
    handlers.setHandlers(new Handler[] { contexts, defaultHandler });
    server.setHandler(handlers);
    // Next the behavior as defined by etc/jetty-requestlog.xml
    // the id="RequestLog"
    RequestLogHandler requestLog = new RequestLogHandler();
    CaptureLog captureLog = new CaptureLog();
    requestLog.setRequestLog(captureLog);
    handlers.addHandler(requestLog);
    // Lastly, the behavior as defined by deployment of a webapp
    // Add the Servlet Context
    ServletContextHandler app = new ServletContextHandler(ServletContextHandler.SESSIONS);
    app.setContextPath("/");
    contexts.addHandler(app);
    // Add the test servlet
    ServletHolder testHolder = new ServletHolder(testServlet);
    app.addServlet(testHolder, "/test");
    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 : ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) Server(org.eclipse.jetty.server.Server) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) URI(java.net.URI) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpURLConnection(java.net.HttpURLConnection) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) Test(org.junit.Test)

Aggregations

ErrorHandler (org.eclipse.jetty.server.handler.ErrorHandler)15 IOException (java.io.IOException)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 Test (org.junit.Test)4 Writer (java.io.Writer)3 Server (org.eclipse.jetty.server.Server)3 ServerConnector (org.eclipse.jetty.server.ServerConnector)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Before (org.junit.Before)3 RuntimeIOException (org.eclipse.jetty.io.RuntimeIOException)2 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)2 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)2 MetricsServlet (com.codahale.metrics.servlets.MetricsServlet)1 ThreadDumpServlet (com.codahale.metrics.servlets.ThreadDumpServlet)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HttpURLConnection (java.net.HttpURLConnection)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ByteBuffer (java.nio.ByteBuffer)1