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.pattern;
015
016import java.util.List;
017
018import ch.qos.logback.access.spi.IAccessEvent;
019import ch.qos.logback.core.CoreConstants;
020
021public class FullResponseConverter extends AccessConverter {
022
023    @Override
024    public String convert(IAccessEvent ae) {
025        StringBuilder buf = new StringBuilder();
026
027        buf.append("HTTP/1.1 ");
028        int statusCode = ae.getStatusCode();
029        buf.append(statusCode);
030        buf.append(" ");
031        buf.append(getStatusCodeDescription(statusCode));
032        buf.append(CoreConstants.LINE_SEPARATOR);
033
034        List<String> hnList = ae.getResponseHeaderNameList();
035        for (String headerName : hnList) {
036            buf.append(headerName);
037            buf.append(": ");
038            buf.append(ae.getResponseHeader(headerName));
039            buf.append(CoreConstants.LINE_SEPARATOR);
040        }
041        buf.append(CoreConstants.LINE_SEPARATOR);
042        buf.append(ae.getResponseContent());
043        buf.append(CoreConstants.LINE_SEPARATOR);
044        return buf.toString();
045    }
046
047    static String getStatusCodeDescription(int sc) {
048        switch (sc) {
049        case 200:
050            return "OK";
051        case 201:
052            return "Created";
053        case 202:
054            return "Accepted";
055        case 203:
056            return "Non-Authoritative Information";
057        case 204:
058            return "No Content";
059        case 205:
060            return "Reset Content";
061        case 206:
062            return "Partial Content";
063        case 300:
064            return "Multiple Choices";
065        case 301:
066            return "Moved Permanently";
067        case 302:
068            return "Found";
069        case 303:
070            return "See Other";
071        case 304:
072            return "Not Modified";
073        case 305:
074            return "Use Proxy";
075        case 306:
076            return "(Unused)";
077        case 307:
078            return "Temporary Redirect";
079        case 400:
080            return "Bad Request";
081        case 401:
082            return "Unauthorized";
083        case 402:
084            return "Payment Required";
085        case 403:
086            return "Forbidden";
087        case 404:
088            return "Not Found";
089        case 405:
090            return "Method Not Allowed";
091        case 406:
092            return "Not Acceptable";
093        case 407:
094            return "Proxy Authentication Required";
095        case 408:
096            return "Request Timeout";
097        case 409:
098            return "Conflict";
099        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}