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.tomcat;
015
016import ch.qos.logback.access.spi.ServerAdapter;
017
018import org.apache.catalina.connector.Request;
019import org.apache.catalina.connector.Response;
020
021import java.util.HashMap;
022import java.util.Map;
023
024/**
025 * A tomcat specific implementation of the {@link ServerAdapter} interface.
026 *
027 * @author Sébastien Pennec
028 */
029public class TomcatServerAdapter implements ServerAdapter {
030
031    Request request;
032    Response response;
033
034    public TomcatServerAdapter(Request tomcatRequest, Response tomcatResponse) {
035        this.request = tomcatRequest;
036        this.response = tomcatResponse;
037    }
038
039    @Override
040    public long getContentLength() {
041        return response.getContentLength();
042    }
043
044    @Override
045    public int getStatusCode() {
046        return response.getStatus();
047    }
048
049    @Override
050    public long getRequestTimestamp() {
051        return request.getCoyoteRequest().getStartTime();
052    }
053
054    @Override
055    public Map<String, String> buildResponseHeaderMap() {
056        Map<String, String> responseHeaderMap = new HashMap<String, String>();
057        for (String key : response.getHeaderNames()) {
058            String value = response.getHeader(key);
059            responseHeaderMap.put(key, value);
060        }
061        return responseHeaderMap;
062    }
063}