Search in sources :

Example 16 with ErrorHandler

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

the class AsyncListenerTest method test_StartAsync_Throw_OnError_SendError_CustomErrorPage.

@Test
public void test_StartAsync_Throw_OnError_SendError_CustomErrorPage() throws Exception {
    test_StartAsync_Throw_OnError(event -> {
        HttpServletResponse response = (HttpServletResponse) event.getAsyncContext().getResponse();
        response.sendError(HttpStatus.BAD_GATEWAY_502);
    });
    // Add a custom error page.
    ErrorHandler errorHandler = new ErrorHandler() {

        @Override
        protected void writeErrorPageMessage(HttpServletRequest request, Writer writer, int code, String message, String uri) throws IOException {
            writer.write("CUSTOM\n");
            super.writeErrorPageMessage(request, writer, code, message, uri);
        }
    };
    server.setErrorHandler(errorHandler);
    String httpResponse = connector.getResponse("" + "GET /ctx/path HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n", 10, TimeUnit.MINUTES);
    assertThat(httpResponse, containsString("HTTP/1.1 502 "));
    assertThat(httpResponse, containsString("CUSTOM"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) HttpServletResponse(javax.servlet.http.HttpServletResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) Writer(java.io.Writer) Test(org.junit.Test)

Example 17 with ErrorHandler

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

the class JettyServletWebServerFactory method getErrorPageConfiguration.

/**
	 * Create a configuration object that adds error handlers.
	 * @return a configuration object for adding error pages
	 */
private Configuration getErrorPageConfiguration() {
    return new AbstractConfiguration() {

        @Override
        public void configure(WebAppContext context) throws Exception {
            ErrorHandler errorHandler = context.getErrorHandler();
            context.setErrorHandler(new JettyEmbeddedErrorHandler(errorHandler));
            addJettyErrorPages(errorHandler, getErrorPages());
        }
    };
}
Also used : AbstractConfiguration(org.eclipse.jetty.webapp.AbstractConfiguration) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) ErrorPageErrorHandler(org.eclipse.jetty.servlet.ErrorPageErrorHandler)

Example 18 with ErrorHandler

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

the class JettyHTTPServerEngine method createServer.

private Server createServer() {
    Server s = null;
    if (connector != null && connector.getServer() != null) {
        s = connector.getServer();
    }
    if (threadPool != null) {
        try {
            if (s == null) {
                s = new Server(threadPool);
            } else {
                s.addBean(threadPool);
            }
        } catch (Exception e) {
        // ignore
        }
    }
    if (s == null) {
        s = new Server();
    }
    // need an error handler that won't leak information about the exception
    // back to the client.
    ErrorHandler eh = new ErrorHandler() {

        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            String msg = (String) request.getAttribute(RequestDispatcher.ERROR_MESSAGE);
            if (StringUtils.isEmpty(msg) || msg.contains("org.apache.cxf.interceptor.Fault")) {
                msg = HttpStatus.getMessage(response.getStatus());
                request.setAttribute(RequestDispatcher.ERROR_MESSAGE, msg);
            }
            if (response instanceof Response) {
                ((Response) response).setStatusWithReason(response.getStatus(), msg);
            }
            super.handle(target, baseRequest, request, response);
        }

        protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks) throws IOException {
            super.writeErrorPage(request, writer, code, message, false);
        }
    };
    s.addBean(eh);
    return s;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Response(org.eclipse.jetty.server.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) Server(org.eclipse.jetty.server.Server) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletException(javax.servlet.ServletException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) Writer(java.io.Writer)

Example 19 with ErrorHandler

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

the class APIServer method open.

private static void open(int port, String htmlPath) throws IOException {
    try {
        QueuedThreadPool pool = new QueuedThreadPool();
        pool.setMaxThreads(500);
        server = new Server(pool);
        ServerConnector connector = new ServerConnector(server);
        HttpConfiguration http_config = new HttpConfiguration();
        http_config.setRequestHeaderSize(65536);
        http_config.setResponseHeaderSize(65536);
        connector.addConnectionFactory(new HttpConnectionFactory(http_config));
        connector.setPort(port);
        connector.setName("httpd:" + port);
        // timout in ms when no bytes send / received
        connector.setIdleTimeout(20000);
        server.addConnector(connector);
        server.setStopAtShutdown(true);
        // add services
        ServletContextHandler servletHandler = new ServletContextHandler();
        for (Class<? extends Servlet> service : services) try {
            servletHandler.addServlet(service, ((APIHandler) (service.getConstructor().newInstance())).getAPIPath());
        } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            Data.logger.warn(service.getName() + " instantiation error", e);
        }
        ErrorHandler errorHandler = new ErrorHandler();
        errorHandler.setShowStacks(true);
        servletHandler.setErrorHandler(errorHandler);
        HandlerList handlerlist2 = new HandlerList();
        if (htmlPath == null) {
            handlerlist2.setHandlers(new Handler[] { servletHandler, new DefaultHandler() });
        } else {
            FileHandler fileHandler = new FileHandler();
            fileHandler.setDirectoriesListed(true);
            fileHandler.setWelcomeFiles(new String[] { "index.html" });
            fileHandler.setResourceBase(htmlPath);
            handlerlist2.setHandlers(new Handler[] { fileHandler, servletHandler, new DefaultHandler() });
        }
        server.setHandler(handlerlist2);
        server.start();
    } catch (Throwable e) {
        throw new IOException(e.getMessage());
    }
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) IOException(java.io.IOException) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) ServerConnector(org.eclipse.jetty.server.ServerConnector) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 20 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());
    server.setDumpAfterStart(dumpAfterStart);
    server.setDumpBeforeStop(dumpBeforeStop);
    return server;
}
Also used : ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) Server(org.eclipse.jetty.server.Server)

Aggregations

ErrorHandler (org.eclipse.jetty.server.handler.ErrorHandler)22 IOException (java.io.IOException)7 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 Server (org.eclipse.jetty.server.Server)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)6 Writer (java.io.Writer)5 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)5 ServerConnector (org.eclipse.jetty.server.ServerConnector)4 Request (org.eclipse.jetty.server.Request)3 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)3 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)3 Test (org.junit.Test)3 MetricsServlet (com.codahale.metrics.servlets.MetricsServlet)2 ThreadDumpServlet (com.codahale.metrics.servlets.ThreadDumpServlet)2 ServletException (javax.servlet.ServletException)2 DrillErrorHandler (org.apache.drill.exec.server.rest.auth.DrillErrorHandler)2 DrillHttpSecurityHandlerProvider (org.apache.drill.exec.server.rest.auth.DrillHttpSecurityHandlerProvider)2 RuntimeIOException (org.eclipse.jetty.io.RuntimeIOException)2 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2