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 ch.qos.logback.access.common.spi.ServerAdapter;
17  
18  import org.eclipse.jetty.http.HttpField;
19  import org.eclipse.jetty.http.HttpFields;
20  import org.eclipse.jetty.server.Request;
21  import org.eclipse.jetty.server.Response;
22  
23  import java.util.Enumeration;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  /**
28   * A jetty specific implementation of the {@link ServerAdapter} interface.
29   *
30   * @author Sébastien Pennec
31   * @author Ceki Gulcu
32   */
33  public class JettyServerAdapter implements ServerAdapter {
34  
35      Request request;
36      Response response;
37  
38      public JettyServerAdapter(Request jettyRequest, Response jettyResponse) {
39          this.request = jettyRequest;
40          this.response = jettyResponse;
41      }
42  
43      @Override
44      public long getContentLength() {
45          return Response.getContentBytesWritten(response);
46      }
47  
48      @Override
49      public int getStatusCode() {
50          return response.getStatus();
51      }
52  
53      @Override
54      public long getRequestTimestamp() {
55          return Request.getTimeStamp(request);
56      }
57  
58      @Override
59      public Map<String, String> buildResponseHeaderMap() {
60          Map<String, String> responseHeaderMap = new HashMap<String, String>();
61          HttpFields.Mutable httpFields = response.getHeaders();
62  
63          for(HttpField field: httpFields) {
64              String key = field.getName();
65              String value = field.getValue();
66              responseHeaderMap.put(key, value);
67          }
68  
69          return responseHeaderMap;
70      }
71  
72  }