View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, 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      StringBuffer buf = new StringBuffer();
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: return "OK";
50      case 201: return "Created";
51      case 202: return "Accepted";
52      case 203: return "Non-Authoritative Information";
53      case 204: return "No Content";
54      case 205: return "Reset Content";
55      case 206: return "Partial Content";
56      case 300: return "Multiple Choices";
57      case 301: return "Moved Permanently";
58      case 302: return "Found";
59      case 303: return "See Other";
60      case 304: return "Not Modified";
61      case 305: return "Use Proxy";
62      case 306: return "(Unused)";
63      case 307: return "Temporary Redirect";
64      case 400: return "Bad Request";
65      case 401: return "Unauthorized";
66      case 402: return "Payment Required";
67      case 403: return "Forbidden";
68      case 404: return "Not Found";
69      case 405: return "Method Not Allowed";
70      case 406: return "Not Acceptable";
71      case 407: return "Proxy Authentication Required";
72      case 408: return "Request Timeout";
73      case 409: return "Conflict";
74      case 410: return "Gone";
75      case 411: return "Length Required";
76      case 412: return "Precondition Failed";
77      case 413: return "Request Entity Too Large";
78      case 414: return "Request-URI Too Long";
79      case 415: return "Unsupported Media Type";
80      case 416: return "Requested Range Not Satisfiable";
81      case 417: return "Expectation Failed";
82      case 500: return "Internal Server Error";
83      case 501: return "Not Implemented";
84      case 502: return "Bad Gateway";
85      case 503: return "Service Unavailable";
86      case 504: return "Gateway Timeout";
87      case 505: return "HTTP Version Not Supported";
88      default: return "NA";
89      }
90    }
91  }