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