KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > protocol > http > proxy > HttpReplyHdr


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/proxy/HttpReplyHdr.java,v 1.5.2.1 2004/04/13 17:28:56 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.protocol.http.proxy;
20
21 /**
22  * Utility class to generate HTTP responses of various types.
23  *
24  * @version $Revision: 1.5.2.1 $
25  */

26 public final class HttpReplyHdr
27 {
28     /** String representing a carriage-return/line-feed pair. */
29     private static final String JavaDoc CR = "\r\n";
30
31     /** A HTTP protocol version string. */
32     private static final String JavaDoc HTTP_PROTOCOL = "HTTP/1.0";
33
34     /** The HTTP server name. */
35     private static final String JavaDoc HTTP_SERVER = "Java Proxy Server";
36
37
38     /**
39      * Don't allow instantiation of this utility class.
40      */

41     private HttpReplyHdr()
42     {
43     }
44
45     /**
46      * Forms a http ok reply header
47      *
48      * @param contentType the mime-type of the content
49      * @param contentLength the length of the content
50      * @return a string with the header in it
51      */

52     public static String JavaDoc formOk(String JavaDoc contentType, long contentLength)
53     {
54         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
55
56         out.append(HTTP_PROTOCOL).append(" 200 Ok").append(CR);
57         out.append("Server: ").append(HTTP_SERVER).append(CR);
58         out.append("MIME-version: 1.0").append(CR);
59
60         if (0 < contentType.length())
61         {
62             out.append("Content-Type: ").append(contentType).append(CR);
63         }
64         else
65         {
66             out.append("Content-Type: text/html").append(CR);
67         }
68
69         if (0 != contentLength)
70         {
71             out.append("Content-Length: ").append(contentLength).append(CR);
72         }
73
74         out.append(CR);
75
76         return out.toString();
77     }
78
79     /**
80      * private! builds an http document describing a headers reason.
81      *
82      * @param error Error name.
83      * @param description Errors description.
84      * @return A string with the HTML description body
85      */

86     private static String JavaDoc formErrorBody(String JavaDoc error, String JavaDoc description)
87     {
88         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
89         //Generate Error Body
90
out.append("<HTML><HEAD><TITLE>");
91         out.append(error);
92         out.append("</TITLE></HEAD>");
93         out.append("<BODY><H2>").append(error).append("</H2>\n");
94         out.append("</P></H3>");
95         out.append(description);
96         out.append("</BODY></HTML>");
97         return out.toString();
98     }
99
100     /**
101      * builds an http document describing an error.
102      *
103      * @param error Error name.
104      * @param description Errors description.
105      * @return A string with the HTML description body
106      */

107     private static String JavaDoc formError(String JavaDoc error, String JavaDoc description)
108     {
109         /* A HTTP RESPONSE HEADER LOOKS ALOT LIKE:
110          *
111          * HTTP/1.0 200 OK
112          * Date: Wednesday, 02-Feb-94 23:04:12 GMT
113          * Server: NCSA/1.1
114          * MIME-version: 1.0
115          * Last-modified: Monday, 15-Nov-93 23:33:16 GMT
116          * Content-Type: text/html
117          * Content-Length: 2345
118          * \r\n
119          */

120
121         String JavaDoc body = formErrorBody(error, description);
122         StringBuffer JavaDoc header = new StringBuffer JavaDoc();
123
124         header.append(HTTP_PROTOCOL).append(" ").append(error).append(CR);
125         header.append("Server: ").append(HTTP_SERVER).append(CR);
126         header.append("MIME-version: 1.0").append(CR);
127         header.append("Content-Type: text/html").append(CR);
128
129         header.append("Content-Length: ").append(body.length()).append(CR);
130
131         header.append(CR);
132         header.append(body);
133
134         return header.toString();
135     }
136
137     /**
138      * Indicates a new file was created.
139      *
140      * @return The header in a string;
141      */

142     public static String JavaDoc formCreated()
143     {
144         return formError("201 Created", "Object was created");
145     }
146
147     /**
148      * Indicates the document was accepted.
149      *
150      * @return The header in a string;
151      */

152     public static String JavaDoc formAccepted()
153     {
154         return formError("202 Accepted", "Object checked in");
155     }
156
157     /**
158      * Indicates only a partial responce was sent.
159      *
160      * @return The header in a string;
161      */

162     public static String JavaDoc formPartial()
163     {
164         return formError("203 Partial", "Only partail document available");
165     }
166
167     /**
168      * Indicates a requested URL has moved to a new address or name.
169      *
170      * @return The header in a string;
171      */

172     public static String JavaDoc formMoved()
173     {
174         //300 codes tell client to do actions
175
return formError("301 Moved", "File has moved");
176     }
177
178     /**
179      * Never seen this used.
180      *
181      * @return The header in a string;
182      */

183     public static String JavaDoc formFound()
184     {
185         return formError("302 Found", "Object was found");
186     }
187
188     /**
189      * The requested method is not implemented by the server.
190      *
191      * @return The header in a string;
192      */

193     public static String JavaDoc formMethod()
194     {
195         return formError("303 Method unseported", "Method unseported");
196     }
197
198     /**
199      * Indicates remote copy of the requested object is current.
200      *
201      * @return The header in a string;
202      */

203     public static String JavaDoc formNotModified()
204     {
205         return formError("304 Not modified", "Use local copy");
206     }
207
208     /**
209      * Client not otherized for the request.
210      *
211      * @return The header in a string;
212      */

213     public static String JavaDoc formUnautorized()
214     {
215         return formError("401 Unathorized", "Unathorized use of this service");
216     }
217
218     /**
219      * Payment is required for service.
220      *
221      * @return The header in a string;
222      */

223     public static String JavaDoc formPaymentNeeded()
224     {
225         return formError("402 Payment required", "Payment is required");
226     }
227
228     /**
229      * Client if forbidden to get the request service.
230      *
231      * @return The header in a string;
232      */

233     public static String JavaDoc formForbidden()
234     {
235         return formError(
236             "403 Forbidden",
237             "You need permission for this service");
238     }
239
240     /**
241      * The requested object was not found.
242      *
243      * @return The header in a string;
244      */

245     public static String JavaDoc formNotFound()
246     {
247         return formError("404 Not_found", "Requested object was not found");
248     }
249
250     /**
251      * The server had a problem and could not fulfill the request.
252      *
253      * @return The header in a string;
254      */

255     public static String JavaDoc formInternalError()
256     {
257         return formError("500 Internal server error", "Server broke");
258     }
259
260     /**
261      * Server does not do the requested feature.
262      *
263      * @return The header in a string;
264      */

265     public static String JavaDoc formNotImplemented()
266     {
267         return formError(
268             "501 Method not implemented",
269             "Service not implemented, programer was lazy");
270     }
271
272     /**
273      * Server is overloaded, client should try again latter.
274      *
275      * @return The header in a string;
276      */

277     public static String JavaDoc formOverloaded()
278     {
279         return formError("502 Server overloaded", "Try again latter");
280     }
281
282     /**
283      * Indicates the request took to long.
284      *
285      * @return The header in a string;
286      */

287     public static String JavaDoc formTimeout()
288     {
289         return formError("503 Gateway timeout", "The connection timed out");
290     }
291
292     /**
293      * Indicates the client's proxies could not locate a server.
294      *
295      * @return The header in a string;
296      */

297     public static String JavaDoc formServerNotFound()
298     {
299         return formError(
300             "503 Gateway timeout",
301             "The requested server was not found");
302     }
303
304     /**
305      * Indicates the client is not allowed to access the object.
306      *
307      * @return The header in a string;
308      */

309     public static String JavaDoc formNotAllowed()
310     {
311         return formError("403 Access Denied", "Access is not allowed");
312     }
313 }
314
Popular Tags