Search in sources :

Example 1 with HandlerWrapper

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

the class ManyHandlers method main.

public static void main(String[] args) throws Exception {
    Server server = new Server(8080);
    // create the handlers
    Handler param = new ParamHandler();
    HandlerWrapper wrapper = new WelcomeWrapHandler();
    Handler hello = new HelloHandler();
    Handler dft = new DefaultHandler();
    RequestLogHandler requestLog = new RequestLogHandler();
    // configure request logging
    File requestLogFile = File.createTempFile("demo", "log");
    NCSARequestLog ncsaLog = new NCSARequestLog(requestLogFile.getAbsolutePath());
    requestLog.setRequestLog(ncsaLog);
    // create the handler collections
    HandlerCollection handlers = new HandlerCollection();
    HandlerList list = new HandlerList();
    // link them all together
    wrapper.setHandler(hello);
    list.setHandlers(new Handler[] { param, new GzipHandler(), dft });
    handlers.setHandlers(new Handler[] { list, requestLog });
    // Handler tree looks like the following
    // <pre>
    // Server
    // + HandlerCollection
    // . + HandlerList
    // . | + param (ParamHandler)
    // . | + wrapper (WelcomeWrapHandler)
    // . | | \ hello (HelloHandler)
    // . | \ dft (DefaultHandler)
    // . \ requestLog (RequestLogHandler)
    // </pre>
    server.setHandler(handlers);
    server.start();
    server.join();
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) Server(org.eclipse.jetty.server.Server) Handler(org.eclipse.jetty.server.Handler) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) GzipHandler(org.eclipse.jetty.server.handler.gzip.GzipHandler) HandlerWrapper(org.eclipse.jetty.server.handler.HandlerWrapper) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) GzipHandler(org.eclipse.jetty.server.handler.gzip.GzipHandler) NCSARequestLog(org.eclipse.jetty.server.NCSARequestLog) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) File(java.io.File)

Example 2 with HandlerWrapper

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

the class FastCGIProxyServletTest method testURIRewrite.

@Test
public void testURIRewrite() throws Exception {
    String originalPath = "/original/index.php";
    String originalQuery = "foo=bar";
    String remotePath = "/remote/index.php";
    prepare(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            Assert.assertThat((String) request.getAttribute(FCGI.Headers.REQUEST_URI), Matchers.startsWith(originalPath));
            Assert.assertEquals(originalQuery, request.getAttribute(FCGI.Headers.QUERY_STRING));
            Assert.assertThat(request.getRequestURI(), Matchers.endsWith(remotePath));
        }
    });
    context.stop();
    String pathAttribute = "_path_attribute";
    String queryAttribute = "_query_attribute";
    ServletHolder fcgi = context.getServletHandler().getServlet("fcgi");
    fcgi.setInitParameter(FastCGIProxyServlet.ORIGINAL_URI_ATTRIBUTE_INIT_PARAM, pathAttribute);
    fcgi.setInitParameter(FastCGIProxyServlet.ORIGINAL_QUERY_ATTRIBUTE_INIT_PARAM, queryAttribute);
    context.insertHandler(new HandlerWrapper() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            if (target.startsWith("/remote/")) {
                request.setAttribute(pathAttribute, originalPath);
                request.setAttribute(queryAttribute, originalQuery);
            }
            super.handle(target, baseRequest, request, response);
        }
    });
    context.start();
    ContentResponse response = client.newRequest("localhost", httpConnector.getLocalPort()).path(remotePath).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) HandlerWrapper(org.eclipse.jetty.server.handler.HandlerWrapper) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Test(org.junit.Test)

Example 3 with HandlerWrapper

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

the class HandlerTest method testWrapperServerSet.

@Test
public void testWrapperServerSet() {
    Server s = new Server();
    HandlerWrapper a = new HandlerWrapper();
    HandlerWrapper b = new HandlerWrapper();
    HandlerWrapper c = new HandlerWrapper();
    a.setServer(s);
    b.setHandler(c);
    a.setHandler(b);
    assertThat(b.getServer(), equalTo(s));
    assertThat(c.getServer(), equalTo(s));
}
Also used : Server(org.eclipse.jetty.server.Server) HandlerWrapper(org.eclipse.jetty.server.handler.HandlerWrapper) Test(org.junit.Test)

Example 4 with HandlerWrapper

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

the class HandlerTest method testWrapperSimpleLoop.

@Test
public void testWrapperSimpleLoop() {
    HandlerWrapper a = new HandlerWrapper();
    HandlerWrapper b = new HandlerWrapper();
    a.setHandler(b);
    try {
        b.setHandler(a);
        fail();
    } catch (IllegalStateException e) {
        assertThat(e.getMessage(), containsString("loop"));
    }
}
Also used : HandlerWrapper(org.eclipse.jetty.server.handler.HandlerWrapper) Test(org.junit.Test)

Example 5 with HandlerWrapper

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

the class HandlerTest method testWrapperSetServer.

@Test
public void testWrapperSetServer() {
    Server s = new Server();
    HandlerWrapper a = new HandlerWrapper();
    HandlerWrapper b = new HandlerWrapper();
    HandlerWrapper c = new HandlerWrapper();
    a.setHandler(b);
    b.setHandler(c);
    a.setServer(s);
    assertThat(b.getServer(), equalTo(s));
    assertThat(c.getServer(), equalTo(s));
}
Also used : Server(org.eclipse.jetty.server.Server) HandlerWrapper(org.eclipse.jetty.server.handler.HandlerWrapper) Test(org.junit.Test)

Aggregations

HandlerWrapper (org.eclipse.jetty.server.handler.HandlerWrapper)19 Test (org.junit.Test)14 Handler (org.eclipse.jetty.server.Handler)5 Server (org.eclipse.jetty.server.Server)5 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)4 IOException (java.io.IOException)3 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)3 File (java.io.File)2 ServletException (javax.servlet.ServletException)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 SessionHandler (org.eclipse.jetty.server.session.SessionHandler)2 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 UnknownHostException (java.net.UnknownHostException)1 HashMap (java.util.HashMap)1 ServletContext (javax.servlet.ServletContext)1 HttpServlet (javax.servlet.http.HttpServlet)1 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)1