Throw any exceptions thrown unwrapped. This way, the caller knows what it's dealing...
[pithos] / src / gr / ebs / gss / server / webdav / WebdavStatus.java
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  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 package gr.ebs.gss.server.webdav;
18
19 import java.util.Hashtable;
20
21 import javax.servlet.http.HttpServletResponse;
22
23 /**
24  * Wraps the HttpServletResponse class to abstract the
25  * specific protocol used.  To support other protocols
26  * we would only need to modify this class and the
27  * WebDavRetCode classes.
28  *
29  * @author              Marc Eaddy
30  * @version             1.0, 16 Nov 1997
31  */
32 class WebdavStatus {
33
34
35     // ----------------------------------------------------- Instance Variables
36
37
38     /**
39      * This Hashtable contains the mapping of HTTP and WebDAV
40      * status codes to descriptive text.  This is a static
41      * variable.
42      */
43     private static Hashtable<Integer,String> mapStatusCodes =
44         new Hashtable<Integer,String>();
45
46
47     // ------------------------------------------------------ HTTP Status Codes
48
49
50     /**
51      * Status code (200) indicating the request succeeded normally.
52      */
53     public static final int SC_OK = HttpServletResponse.SC_OK;
54
55
56     /**
57      * Status code (201) indicating the request succeeded and created
58      * a new resource on the server.
59      */
60     public static final int SC_CREATED = HttpServletResponse.SC_CREATED;
61
62
63     /**
64      * Status code (202) indicating that a request was accepted for
65      * processing, but was not completed.
66      */
67     public static final int SC_ACCEPTED = HttpServletResponse.SC_ACCEPTED;
68
69
70     /**
71      * Status code (204) indicating that the request succeeded but that
72      * there was no new information to return.
73      */
74     public static final int SC_NO_CONTENT = HttpServletResponse.SC_NO_CONTENT;
75
76
77     /**
78      * Status code (301) indicating that the resource has permanently
79      * moved to a new location, and that future references should use a
80      * new URI with their requests.
81      */
82     public static final int SC_MOVED_PERMANENTLY =
83         HttpServletResponse.SC_MOVED_PERMANENTLY;
84
85
86     /**
87      * Status code (302) indicating that the resource has temporarily
88      * moved to another location, but that future references should
89      * still use the original URI to access the resource.
90      */
91     public static final int SC_MOVED_TEMPORARILY =
92         HttpServletResponse.SC_MOVED_TEMPORARILY;
93
94
95     /**
96      * Status code (304) indicating that a conditional GET operation
97      * found that the resource was available and not modified.
98      */
99     public static final int SC_NOT_MODIFIED =
100         HttpServletResponse.SC_NOT_MODIFIED;
101
102
103     /**
104      * Status code (400) indicating the request sent by the client was
105      * syntactically incorrect.
106      */
107     public static final int SC_BAD_REQUEST =
108         HttpServletResponse.SC_BAD_REQUEST;
109
110
111     /**
112      * Status code (401) indicating that the request requires HTTP
113      * authentication.
114      */
115     public static final int SC_UNAUTHORIZED =
116         HttpServletResponse.SC_UNAUTHORIZED;
117
118
119     /**
120      * Status code (403) indicating the server understood the request
121      * but refused to fulfill it.
122      */
123     public static final int SC_FORBIDDEN = HttpServletResponse.SC_FORBIDDEN;
124
125
126     /**
127      * Status code (404) indicating that the requested resource is not
128      * available.
129      */
130     public static final int SC_NOT_FOUND = HttpServletResponse.SC_NOT_FOUND;
131
132
133     /**
134      * Status code (500) indicating an error inside the HTTP service
135      * which prevented it from fulfilling the request.
136      */
137     public static final int SC_INTERNAL_SERVER_ERROR =
138         HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
139
140
141     /**
142      * Status code (501) indicating the HTTP service does not support
143      * the functionality needed to fulfill the request.
144      */
145     public static final int SC_NOT_IMPLEMENTED =
146         HttpServletResponse.SC_NOT_IMPLEMENTED;
147
148
149     /**
150      * Status code (502) indicating that the HTTP server received an
151      * invalid response from a server it consulted when acting as a
152      * proxy or gateway.
153      */
154     public static final int SC_BAD_GATEWAY =
155         HttpServletResponse.SC_BAD_GATEWAY;
156
157
158     /**
159      * Status code (503) indicating that the HTTP service is
160      * temporarily overloaded, and unable to handle the request.
161      */
162     public static final int SC_SERVICE_UNAVAILABLE =
163         HttpServletResponse.SC_SERVICE_UNAVAILABLE;
164
165
166     /**
167      * Status code (100) indicating the client may continue with
168      * its request.  This interim response is used to inform the
169      * client that the initial part of the request has been
170      * received and has not yet been rejected by the server.
171      */
172     public static final int SC_CONTINUE = 100;
173
174
175     /**
176      * Status code (405) indicating the method specified is not
177      * allowed for the resource.
178      */
179     public static final int SC_METHOD_NOT_ALLOWED = 405;
180
181
182     /**
183      * Status code (409) indicating that the request could not be
184      * completed due to a conflict with the current state of the
185      * resource.
186      */
187     public static final int SC_CONFLICT = 409;
188
189
190     /**
191      * Status code (412) indicating the precondition given in one
192      * or more of the request-header fields evaluated to false
193      * when it was tested on the server.
194      */
195     public static final int SC_PRECONDITION_FAILED = 412;
196
197
198     /**
199      * Status code (413) indicating the server is refusing to
200      * process a request because the request entity is larger
201      * than the server is willing or able to process.
202      */
203     public static final int SC_REQUEST_TOO_LONG = 413;
204
205
206     /**
207      * Status code (415) indicating the server is refusing to service
208      * the request because the entity of the request is in a format
209      * not supported by the requested resource for the requested
210      * method.
211      */
212     public static final int SC_UNSUPPORTED_MEDIA_TYPE = 415;
213
214
215     // -------------------------------------------- Extended WebDav status code
216
217
218     /**
219      * Status code (207) indicating that the response requires
220      * providing status for multiple independent operations.
221      */
222     public static final int SC_MULTI_STATUS = 207;
223     // This one colides with HTTP 1.1
224     // "207 Parital Update OK"
225
226
227     /**
228      * Status code (418) indicating the entity body submitted with
229      * the PATCH method was not understood by the resource.
230      */
231     public static final int SC_UNPROCESSABLE_ENTITY = 418;
232     // This one colides with HTTP 1.1
233     // "418 Reauthentication Required"
234
235
236     /**
237      * Status code (419) indicating that the resource does not have
238      * sufficient space to record the state of the resource after the
239      * execution of this method.
240      */
241     public static final int SC_INSUFFICIENT_SPACE_ON_RESOURCE = 419;
242     // This one colides with HTTP 1.1
243     // "419 Proxy Reauthentication Required"
244
245
246     /**
247      * Status code (420) indicating the method was not executed on
248      * a particular resource within its scope because some part of
249      * the method's execution failed causing the entire method to be
250      * aborted.
251      */
252     public static final int SC_METHOD_FAILURE = 420;
253
254
255     /**
256      * Status code (423) indicating the destination resource of a
257      * method is locked, and either the request did not contain a
258      * valid Lock-Info header, or the Lock-Info header identifies
259      * a lock held by another principal.
260      */
261     public static final int SC_LOCKED = 423;
262
263
264     // ------------------------------------------------------------ Initializer
265
266
267     static {
268         // HTTP 1.0 tatus Code
269         addStatusCodeMap(SC_OK, "OK");
270         addStatusCodeMap(SC_CREATED, "Created");
271         addStatusCodeMap(SC_ACCEPTED, "Accepted");
272         addStatusCodeMap(SC_NO_CONTENT, "No Content");
273         addStatusCodeMap(SC_MOVED_PERMANENTLY, "Moved Permanently");
274         addStatusCodeMap(SC_MOVED_TEMPORARILY, "Moved Temporarily");
275         addStatusCodeMap(SC_NOT_MODIFIED, "Not Modified");
276         addStatusCodeMap(SC_BAD_REQUEST, "Bad Request");
277         addStatusCodeMap(SC_UNAUTHORIZED, "Unauthorized");
278         addStatusCodeMap(SC_FORBIDDEN, "Forbidden");
279         addStatusCodeMap(SC_NOT_FOUND, "Not Found");
280         addStatusCodeMap(SC_INTERNAL_SERVER_ERROR, "Internal Server Error");
281         addStatusCodeMap(SC_NOT_IMPLEMENTED, "Not Implemented");
282         addStatusCodeMap(SC_BAD_GATEWAY, "Bad Gateway");
283         addStatusCodeMap(SC_SERVICE_UNAVAILABLE, "Service Unavailable");
284         addStatusCodeMap(SC_CONTINUE, "Continue");
285         addStatusCodeMap(SC_METHOD_NOT_ALLOWED, "Method Not Allowed");
286         addStatusCodeMap(SC_CONFLICT, "Conflict");
287         addStatusCodeMap(SC_PRECONDITION_FAILED, "Precondition Failed");
288         addStatusCodeMap(SC_REQUEST_TOO_LONG, "Request Too Long");
289         addStatusCodeMap(SC_UNSUPPORTED_MEDIA_TYPE, "Unsupported Media Type");
290         // WebDav Status Codes
291         addStatusCodeMap(SC_MULTI_STATUS, "Multi-Status");
292         addStatusCodeMap(SC_UNPROCESSABLE_ENTITY, "Unprocessable Entity");
293         addStatusCodeMap(SC_INSUFFICIENT_SPACE_ON_RESOURCE,
294                          "Insufficient Space On Resource");
295         addStatusCodeMap(SC_METHOD_FAILURE, "Method Failure");
296         addStatusCodeMap(SC_LOCKED, "Locked");
297     }
298
299
300     // --------------------------------------------------------- Public Methods
301
302
303     /**
304      * Returns the HTTP status text for the HTTP or WebDav status code
305      * specified by looking it up in the static mapping.  This is a
306      * static function.
307      *
308      * @param   nHttpStatusCode [IN] HTTP or WebDAV status code
309      * @return  A string with a short descriptive phrase for the
310      *                  HTTP status code (e.g., "OK").
311      */
312     public static String getStatusText(int nHttpStatusCode) {
313         Integer intKey = new Integer(nHttpStatusCode);
314
315         if (!mapStatusCodes.containsKey(intKey))
316                         return "";
317                 return mapStatusCodes.get(intKey);
318     }
319
320
321     // -------------------------------------------------------- Private Methods
322
323
324     /**
325      * Adds a new status code -> status text mapping.  This is a static
326      * method because the mapping is a static variable.
327      *
328      * @param   nKey    [IN] HTTP or WebDAV status code
329      * @param   strVal  [IN] HTTP status text
330      */
331     private static void addStatusCodeMap(int nKey, String strVal) {
332         mapStatusCodes.put(new Integer(nKey), strVal);
333     }
334
335 }