001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.protocol.http.proxy;
020:
021: /**
022: * Utility class to generate HTTP responses of various types.
023: *
024: * @version $Revision: 595875 $
025: */
026: public final class HttpReplyHdr {
027: /** String representing a carriage-return/line-feed pair. */
028: private static final String CR = "\r\n";
029:
030: /** A HTTP protocol version string. */
031: private static final String HTTP_PROTOCOL = "HTTP/1.0";
032:
033: /** The HTTP server name. */
034: private static final String HTTP_SERVER = "Java Proxy Server";
035:
036: /**
037: * Don't allow instantiation of this utility class.
038: */
039: private HttpReplyHdr() {
040: }
041:
042: /**
043: * Forms a http ok reply header
044: *
045: * @param contentType
046: * the mime-type of the content
047: * @param contentLength
048: * the length of the content
049: * @return a string with the header in it
050: */
051: public static String formOk(String contentType, long contentLength) {
052: StringBuffer out = new StringBuffer();
053:
054: out.append(HTTP_PROTOCOL).append(" 200 Ok").append(CR);
055: out.append("Server: ").append(HTTP_SERVER).append(CR);
056: out.append("MIME-version: 1.0").append(CR);
057:
058: if (0 < contentType.length()) {
059: out.append("Content-Type: ").append(contentType).append(CR);
060: } else {
061: out.append("Content-Type: text/html").append(CR);
062: }
063:
064: if (0 != contentLength) {
065: out.append("Content-Length: ").append(contentLength)
066: .append(CR);
067: }
068:
069: out.append(CR);
070:
071: return out.toString();
072: }
073:
074: /**
075: * private! builds an http document describing a headers reason.
076: *
077: * @param error
078: * Error name.
079: * @param description
080: * Errors description.
081: * @return A string with the HTML description body
082: */
083: private static String formErrorBody(String error, String description) {
084: StringBuffer out = new StringBuffer();
085: // Generate Error Body
086: out.append("<HTML><HEAD><TITLE>");
087: out.append(error);
088: out.append("</TITLE></HEAD>");
089: out.append("<BODY><H2>").append(error).append("</H2>\n");
090: out.append("</P></H3>");
091: out.append(description);
092: out.append("</BODY></HTML>");
093: return out.toString();
094: }
095:
096: /**
097: * builds an http document describing an error.
098: *
099: * @param error
100: * Error name.
101: * @param description
102: * Errors description.
103: * @return A string with the HTML description body
104: */
105: private static String formError(String error, String description) {
106: /*
107: * A HTTP RESPONSE HEADER LOOKS ALOT LIKE:
108: *
109: * HTTP/1.0 200 OK Date: Wednesday, 02-Feb-94 23:04:12 GMT Server:
110: * NCSA/1.1 MIME-version: 1.0 Last-modified: Monday, 15-Nov-93 23:33:16
111: * GMT Content-Type: text/html Content-Length: 2345 \r\n
112: */
113:
114: String body = formErrorBody(error, description);
115: StringBuffer header = new StringBuffer();
116:
117: header.append(HTTP_PROTOCOL).append(" ").append(error).append(
118: CR);
119: header.append("Server: ").append(HTTP_SERVER).append(CR);
120: header.append("MIME-version: 1.0").append(CR);
121: header.append("Content-Type: text/html").append(CR);
122:
123: header.append("Content-Length: ").append(body.length()).append(
124: CR);
125:
126: header.append(CR);
127: header.append(body);
128:
129: return header.toString();
130: }
131:
132: /**
133: * Indicates a new file was created.
134: *
135: * @return The header in a string;
136: */
137: public static String formCreated() {
138: return formError("201 Created", "Object was created");
139: }
140:
141: /**
142: * Indicates the document was accepted.
143: *
144: * @return The header in a string;
145: */
146: public static String formAccepted() {
147: return formError("202 Accepted", "Object checked in");
148: }
149:
150: /**
151: * Indicates only a partial responce was sent.
152: *
153: * @return The header in a string;
154: */
155: public static String formPartial() {
156: return formError("203 Partial",
157: "Only partail document available");
158: }
159:
160: /**
161: * Indicates a requested URL has moved to a new address or name.
162: *
163: * @return The header in a string;
164: */
165: public static String formMoved() {
166: // 300 codes tell client to do actions
167: return formError("301 Moved", "File has moved");
168: }
169:
170: /**
171: * Never seen this used.
172: *
173: * @return The header in a string;
174: */
175: public static String formFound() {
176: return formError("302 Found", "Object was found");
177: }
178:
179: /**
180: * The requested method is not implemented by the server.
181: *
182: * @return The header in a string;
183: */
184: public static String formMethod() {
185: return formError("303 Method unseported", "Method unseported");
186: }
187:
188: /**
189: * Indicates remote copy of the requested object is current.
190: *
191: * @return The header in a string;
192: */
193: public static String formNotModified() {
194: return formError("304 Not modified", "Use local copy");
195: }
196:
197: /**
198: * Client not otherized for the request.
199: *
200: * @return The header in a string;
201: */
202: public static String formUnautorized() {
203: return formError("401 Unathorized",
204: "Unathorized use of this service");
205: }
206:
207: /**
208: * Payment is required for service.
209: *
210: * @return The header in a string;
211: */
212: public static String formPaymentNeeded() {
213: return formError("402 Payment required", "Payment is required");
214: }
215:
216: /**
217: * Client if forbidden to get the request service.
218: *
219: * @return The header in a string;
220: */
221: public static String formForbidden() {
222: return formError("403 Forbidden",
223: "You need permission for this service");
224: }
225:
226: /**
227: * The requested object was not found.
228: *
229: * @return The header in a string;
230: */
231: public static String formNotFound() {
232: return formError("404 Not_found",
233: "Requested object was not found");
234: }
235:
236: /**
237: * The server had a problem and could not fulfill the request.
238: *
239: * @return The header in a string;
240: */
241: public static String formInternalError() {
242: return formError("500 Internal server error", "Server broke");
243: }
244:
245: /**
246: * Server does not do the requested feature.
247: *
248: * @return The header in a string;
249: */
250: public static String formNotImplemented() {
251: return formError("501 Method not implemented",
252: "Service not implemented");
253: }
254:
255: /**
256: * Server is overloaded, client should try again latter.
257: *
258: * @return The header in a string;
259: */
260: public static String formOverloaded() {
261: return formError("502 Server overloaded", "Try again latter");
262: }
263:
264: /**
265: * Indicates the request took to long.
266: *
267: * @return The header in a string;
268: */
269: public static String formTimeout() {
270: return formError("503 Gateway timeout",
271: "The connection timed out");
272: }
273:
274: /**
275: * Indicates the client's proxies could not locate a server.
276: *
277: * @return The header in a string;
278: */
279: public static String formServerNotFound() {
280: return formError("503 Gateway timeout",
281: "The requested server was not found");
282: }
283:
284: /**
285: * Indicates the client is not allowed to access the object.
286: *
287: * @return The header in a string;
288: */
289: public static String formNotAllowed() {
290: return formError("403 Access Denied", "Access is not allowed");
291: }
292: }
|