001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fitnesse.http;
004:
005: import java.util.*;
006: import java.text.*;
007:
008: public abstract class Response {
009: public static final String DEFAULT_CONTENT_TYPE = "text/html; charset=utf-8";
010:
011: protected static final String CRLF = "\r\n";
012:
013: public static SimpleDateFormat makeStandardHttpDateFormat() {
014: // SimpleDateFormat is not thread safe, so we need to create each
015: // instance independently.
016: SimpleDateFormat df = new SimpleDateFormat(
017: "EEE, dd MMM yyyy HH:mm:ss z");
018: df.setTimeZone(TimeZone.getTimeZone("GMT"));
019: return df;
020: }
021:
022: private int status = 200;
023:
024: private HashMap headers = new HashMap(17);
025:
026: private String contentType = DEFAULT_CONTENT_TYPE;
027:
028: public Response() {
029: }
030:
031: public Response(int status) {
032: this .status = status;
033: }
034:
035: public abstract void readyToSend(ResponseSender sender)
036: throws Exception;
037:
038: protected abstract void addSpecificHeaders();
039:
040: public abstract int getContentSize();
041:
042: public int getStatus() {
043: return status;
044: }
045:
046: public void setStatus(int s) {
047: status = s;
048: }
049:
050: public String makeHttpHeaders() {
051: StringBuffer text = new StringBuffer();
052: text.append("HTTP/1.1 ").append(status).append(" ").append(
053: getReasonPhrase()).append(CRLF);
054: makeHeaders(text);
055: text.append(CRLF);
056: return text.toString();
057: }
058:
059: public String getContentType() {
060: return contentType;
061: }
062:
063: public void setContentType(String type) {
064: contentType = type;
065: }
066:
067: public void redirect(String location) {
068: status = 303;
069: addHeader("Location", location);
070: }
071:
072: public void setMaxAge(int age) {
073: addHeader("Cache-Control", "max-age=" + age);
074: }
075:
076: public void setLastModifiedHeader(String date) {
077: addHeader("Last-Modified", date);
078: }
079:
080: public void setExpiresHeader(String date) {
081: addHeader("Expires", date);
082: }
083:
084: public void addHeader(String key, String value) {
085: headers.put(key, value);
086: }
087:
088: public String getHeader(String key) {
089: return (String) headers.get(key);
090: }
091:
092: public byte[] getEncodedBytes(String value) throws Exception {
093: return value.getBytes("UTF-8");
094: }
095:
096: private void makeHeaders(StringBuffer text) {
097: for (Iterator iterator = headers.keySet().iterator(); iterator
098: .hasNext();) {
099: String key = (String) iterator.next();
100: String value = (String) headers.get(key);
101: text.append(key).append(": ").append(value).append(CRLF);
102: }
103: }
104:
105: protected void addStandardHeaders() {
106: addHeader("Content-Type", getContentType());
107: addSpecificHeaders();
108: }
109:
110: protected String getReasonPhrase() {
111: return getReasonPhrase(status);
112: }
113:
114: public static String getReasonPhrase(int status) {
115: switch (status) {
116: case 100:
117: return "Continue";
118: case 101:
119: return "Switching Protocols";
120: case 200:
121: return "OK";
122: case 201:
123: return "Created";
124: case 202:
125: return "Accepted";
126: case 203:
127: return "Non-Authoritative Information";
128: case 204:
129: return "No Content";
130: case 205:
131: return "Reset Content";
132: case 300:
133: return "Multiple Choices";
134: case 301:
135: return "Moved Permanently";
136: case 302:
137: return "Found";
138: case 303:
139: return "See Other";
140: case 304:
141: return "Not Modified";
142: case 305:
143: return "Use Proxy";
144: case 307:
145: return "Temporary Redirect";
146: case 400:
147: return "Bad Request";
148: case 401:
149: return "Unauthorized";
150: case 402:
151: return "Payment Required";
152: case 403:
153: return "Forbidden";
154: case 404:
155: return "Not Found";
156: case 405:
157: return "Method Not Allowed";
158: case 406:
159: return "Not Acceptable";
160: case 407:
161: return "Proxy Authentication Required";
162: case 408:
163: return "Request Time-out";
164: case 409:
165: return "Conflict";
166: case 410:
167: return "Gone";
168: case 411:
169: return "Length Required";
170: case 412:
171: return "Precondition Failed";
172: case 413:
173: return "Request Entity Too Large";
174: case 414:
175: return "Request-URI Too Large";
176: case 415:
177: return "Unsupported Media Type";
178: case 416:
179: return "Requested range not satisfiable";
180: case 417:
181: return "Expectation Failed";
182: case 500:
183: return "Internal Server Error";
184: case 501:
185: return "Not Implemented";
186: case 502:
187: return "Bad Gateway";
188: case 503:
189: return "Service Unavailable";
190: case 504:
191: return "Gateway Time-out";
192: case 505:
193: return "HTTP Version not supported";
194: default:
195: return "Unknown Status";
196: }
197: }
198: }
|