Search in sources :

Example 11 with ErrorHandler

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

the class HttpChannel method onBadMessage.

public void onBadMessage(int status, String reason) {
    if (status < 400 || status > 599)
        status = HttpStatus.BAD_REQUEST_400;
    Action action;
    try {
        action = _state.handling();
    } catch (IllegalStateException e) {
        // The bad message cannot be handled in the current state, so throw
        // to hopefull somebody that can handle
        abort(e);
        throw new BadMessageException(status, reason);
    }
    try {
        if (action == Action.DISPATCH) {
            ByteBuffer content = null;
            HttpFields fields = new HttpFields();
            ErrorHandler handler = getServer().getBean(ErrorHandler.class);
            if (handler != null)
                content = handler.badMessageError(status, reason, fields);
            sendResponse(new MetaData.Response(HttpVersion.HTTP_1_1, status, reason, fields, BufferUtil.length(content)), content, true);
        }
    } catch (IOException e) {
        LOG.debug(e);
    } finally {
        // TODO: review whether it's the right state to check.
        if (_state.unhandle() == Action.COMPLETE)
            _state.onComplete();
        else
            // TODO: don't throw from finally blocks !
            throw new IllegalStateException();
        onCompleted();
    }
}
Also used : ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) Action(org.eclipse.jetty.server.HttpChannelState.Action) BadMessageException(org.eclipse.jetty.http.BadMessageException) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 12 with ErrorHandler

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

the class AsyncListenerTest method test_StartAsync_OnTimeout_SendError_CustomErrorPage.

@Test
public void test_StartAsync_OnTimeout_SendError_CustomErrorPage() throws Exception {
    test_StartAsync_OnTimeout(500, event -> {
        AsyncContext asyncContext = event.getAsyncContext();
        HttpServletResponse response = (HttpServletResponse) asyncContext.getResponse();
        response.sendError(HttpStatus.BAD_GATEWAY_502);
        asyncContext.complete();
    });
    // 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);
        }
    };
    errorHandler.setServer(server);
    server.setErrorHandler(errorHandler);
    String httpResponse = connector.getResponse("" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n");
    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) AsyncContext(javax.servlet.AsyncContext) Matchers.containsString(org.hamcrest.Matchers.containsString) Writer(java.io.Writer) Test(org.junit.Test)

Example 13 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 14 with ErrorHandler

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

the class HttpConnectionTest method init.

@Before
public void init() throws Exception {
    server = new Server();
    HttpConfiguration config = new HttpConfiguration();
    config.setRequestHeaderSize(1024);
    config.setResponseHeaderSize(1024);
    config.setSendDateHeader(true);
    HttpConnectionFactory http = new HttpConnectionFactory(config);
    connector = new LocalConnector(server, http, null);
    connector.setIdleTimeout(5000);
    server.addConnector(connector);
    server.setHandler(new DumpHandler());
    ErrorHandler eh = new ErrorHandler();
    eh.setServer(server);
    server.addBean(eh);
    server.start();
}
Also used : ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) Before(org.junit.Before)

Example 15 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)

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