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 ch.qos.logback.access.spi.ServerAdapter;
017
018import org.eclipse.jetty.http.HttpFields;
019import org.eclipse.jetty.server.Request;
020import org.eclipse.jetty.server.Response;
021
022import java.util.Enumeration;
023import java.util.HashMap;
024import java.util.Map;
025
026/**
027 * A jetty specific implementation of the {@link ServerAdapter} interface.
028 *
029 * @author Sébastien Pennec
030 * @author Ceki Gulcu
031 */
032public class JettyServerAdapter implements ServerAdapter {
033
034    Request request;
035    Response response;
036
037    public JettyServerAdapter(Request jettyRequest, Response jettyResponse) {
038        this.request = jettyRequest;
039        this.response = jettyResponse;
040    }
041
042    @Override
043    public long getContentLength() {
044        return response.getContentCount();
045    }
046
047    @Override
048    public int getStatusCode() {
049        return response.getStatus();
050    }
051
052    @Override
053    public long getRequestTimestamp() {
054        return request.getTimeStamp();
055    }
056
057    @Override
058    public Map<String, String> buildResponseHeaderMap() {
059        Map<String, String> responseHeaderMap = new HashMap<String, String>();
060        HttpFields httpFields = response.getHttpFields();
061        Enumeration<String> e = httpFields.getFieldNames();
062        while (e.hasMoreElements()) {
063            String key = (String) e.nextElement();
064            String value = response.getHeader(key);
065            responseHeaderMap.put(key, value);
066        }
067        return responseHeaderMap;
068    }
069
070}