View Javadoc
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.pattern;
15  
16  import java.util.List;
17  
18  import ch.qos.logback.access.spi.IAccessEvent;
19  import ch.qos.logback.core.CoreConstants;
20  
21  public class FullResponseConverter extends AccessConverter {
22  
23      @Override
24      public String convert(IAccessEvent ae) {
25          StringBuilder buf = new StringBuilder();
26  
27          buf.append("HTTP/1.1 ");
28          int statusCode = ae.getStatusCode();
29          buf.append(statusCode);
30          buf.append(" ");
31          buf.append(getStatusCodeDescription(statusCode));
32          buf.append(CoreConstants.LINE_SEPARATOR);
33  
34          List<String> hnList = ae.getResponseHeaderNameList();
35          for (String headerName : hnList) {
36              buf.append(headerName);
37              buf.append(": ");
38              buf.append(ae.getResponseHeader(headerName));
39              buf.append(CoreConstants.LINE_SEPARATOR);
40          }
41          buf.append(CoreConstants.LINE_SEPARATOR);
42          buf.append(ae.getResponseContent());
43          buf.append(CoreConstants.LINE_SEPARATOR);
44          return buf.toString();
45      }
46  
47      static String getStatusCodeDescription(int sc) {
48          switch (sc) {
49          case 200:
50              return "OK";
51          case 201:
52              return "Created";
53          case 202:
54              return "Accepted";
55          case 203:
56              return "Non-Authoritative Information";
57          case 204:
58              return "No Content";
59          case 205:
60              return "Reset Content";
61          case 206:
62              return "Partial Content";
63          case 300:
64              return "Multiple Choices";
65          case 301:
66              return "Moved Permanently";
67          case 302:
68              return "Found";
69          case 303:
70              return "See Other";
71          case 304:
72              return "Not Modified";
73          case 305:
74              return "Use Proxy";
75          case 306:
76              return "(Unused)";
77          case 307:
78              return "Temporary Redirect";
79          case 400:
80              return "Bad Request";
81          case 401:
82              return "Unauthorized";
83          case 402:
84              return "Payment Required";
85          case 403:
86              return "Forbidden";
87          case 404:
88              return "Not Found";
89          case 405:
90              return "Method Not Allowed";
91          case 406:
92              return "Not Acceptable";
93          case 407:
94              return "Proxy Authentication Required";
95          case 408:
96              return "Request Timeout";
97          case 409:
98              return "Conflict";
99          case 410:
100             return "Gone";
101         case 411:
102             return "Length Required";
103         case 412:
104             return "Precondition Failed";
105         case 413:
106             return "Request Entity Too Large";
107         case 414:
108             return "Request-URI Too Long";
109         case 415:
110             return "Unsupported Media Type";
111         case 416:
112             return "Requested Range Not Satisfiable";
113         case 417:
114             return "Expectation Failed";
115         case 500:
116             return "Internal Server Error";
117         case 501:
118             return "Not Implemented";
119         case 502:
120             return "Bad Gateway";
121         case 503:
122             return "Service Unavailable";
123         case 504:
124             return "Gateway Timeout";
125         case 505:
126             return "HTTP Version Not Supported";
127         default:
128             return "NA";
129         }
130     }
131 }