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