001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.access.jetty;
015
016import org.eclipse.jetty.server.Connector;
017import org.eclipse.jetty.server.Request;
018import org.eclipse.jetty.server.Server;
019import org.eclipse.jetty.server.Handler;
020import org.eclipse.jetty.server.handler.AbstractHandler;
021import org.eclipse.jetty.server.handler.HandlerList;
022import org.eclipse.jetty.server.handler.RequestLogHandler;
023import org.eclipse.jetty.server.nio.SelectChannelConnector;
024import org.eclipse.jetty.util.ByteArrayISO8859Writer;
025
026import javax.servlet.ServletException;
027import javax.servlet.http.HttpServletRequest;
028import javax.servlet.http.HttpServletResponse;
029import java.io.IOException;
030import java.io.OutputStream;
031
032public class JettyFixtureBase {
033    final protected RequestLogImpl requestLogImpl;
034    protected Handler handler = new BasicHandler();
035    private final int port;
036    Server server;
037    protected String url;
038
039    public JettyFixtureBase(RequestLogImpl impl, int port) {
040        requestLogImpl = impl;
041        this.port = port;
042        url = "http://localhost:" + port + "/";
043    }
044
045    public String getName() {
046        return "Jetty Test Setup";
047    }
048
049    public String getUrl() {
050        return url;
051    }
052
053    public void start() throws Exception {
054        server = new Server();
055        Connector connector = new SelectChannelConnector();
056        connector.setPort(port);
057        server.setConnectors(new Connector[] { connector });
058
059        RequestLogHandler requestLogHandler = new RequestLogHandler();
060        configureRequestLogImpl();
061        requestLogHandler.setRequestLog(requestLogImpl);
062
063        HandlerList handlers = new HandlerList();
064        handlers.addHandler(requestLogHandler);
065        handlers.addHandler(getRequestHandler());
066
067        server.setHandler(handlers);
068        server.start();
069    }
070
071    public void stop() throws Exception {
072        server.stop();
073        server = null;
074    }
075
076    protected void configureRequestLogImpl() {
077        requestLogImpl.start();
078    }
079
080    protected Handler getRequestHandler() {
081        return handler;
082    }
083
084    class BasicHandler extends AbstractHandler {
085        @SuppressWarnings("resource")
086        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
087            OutputStream out = response.getOutputStream();
088            ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer();
089            writer.write("hello world");
090            writer.flush();
091            response.setContentLength(writer.size());
092            writer.writeTo(out);
093            out.flush();
094
095            baseRequest.setHandled(true);
096
097        }
098    }
099}