View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.access.jetty;
15  
16  import org.eclipse.jetty.server.Connector;
17  import org.eclipse.jetty.server.Request;
18  import org.eclipse.jetty.server.Server;
19  import org.eclipse.jetty.server.Handler;
20  import org.eclipse.jetty.server.handler.AbstractHandler;
21  import org.eclipse.jetty.server.handler.HandlerList;
22  import org.eclipse.jetty.server.handler.RequestLogHandler;
23  import org.eclipse.jetty.server.nio.SelectChannelConnector;
24  import org.eclipse.jetty.util.ByteArrayISO8859Writer;
25  
26  import javax.servlet.ServletException;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  import java.io.IOException;
30  import java.io.OutputStream;
31  
32  public class JettyFixtureBase {
33      final protected RequestLogImpl requestLogImpl;
34      protected Handler handler = new BasicHandler();
35      private final int port;
36      Server server;
37      protected String url;
38  
39      public JettyFixtureBase(RequestLogImpl impl, int port) {
40          requestLogImpl = impl;
41          this.port = port;
42          url = "http://localhost:" + port + "/";
43      }
44  
45      public String getName() {
46          return "Jetty Test Setup";
47      }
48  
49      public String getUrl() {
50          return url;
51      }
52  
53      public void start() throws Exception {
54          server = new Server();
55          Connector connector = new SelectChannelConnector();
56          connector.setPort(port);
57          server.setConnectors(new Connector[] { connector });
58  
59          RequestLogHandler requestLogHandler = new RequestLogHandler();
60          configureRequestLogImpl();
61          requestLogHandler.setRequestLog(requestLogImpl);
62  
63          HandlerList handlers = new HandlerList();
64          handlers.addHandler(requestLogHandler);
65          handlers.addHandler(getRequestHandler());
66  
67          server.setHandler(handlers);
68          server.start();
69      }
70  
71      public void stop() throws Exception {
72          server.stop();
73          server = null;
74      }
75  
76      protected void configureRequestLogImpl() {
77          requestLogImpl.start();
78      }
79  
80      protected Handler getRequestHandler() {
81          return handler;
82      }
83  
84      class BasicHandler extends AbstractHandler {
85          @SuppressWarnings("resource")
86          public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
87              OutputStream out = response.getOutputStream();
88              ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer();
89              writer.write("hello world");
90              writer.flush();
91              response.setContentLength(writer.size());
92              writer.writeTo(out);
93              out.flush();
94  
95              baseRequest.setHandled(true);
96  
97          }
98      }
99  }