001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 *
027 * This file is based on:
028 * - org.apache.catalina.servlets.WebdavServlet/WebdavStatus
029 * from the Apache Tomcat project.
030 *
031 * Licensed to the Apache Software Foundation (ASF) under one or more
032 * contributor license agreements.  See the NOTICE file distributed with
033 * this work for additional information regarding copyright ownership.
034 * The ASF licenses this file to You under the Apache License, Version 2.0
035 * (the "License"); you may not use this file except in compliance with
036 * the License.  You may obtain a copy of the License at
037 *
038 *      http://www.apache.org/licenses/LICENSE-2.0
039 *
040 * Unless required by applicable law or agreed to in writing, software
041 * distributed under the License is distributed on an "AS IS" BASIS,
042 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
043 * See the License for the specific language governing permissions and
044 * limitations under the License.
045 */
046
047package org.opencms.webdav;
048
049import java.util.Hashtable;
050
051import javax.servlet.http.HttpServletResponse;
052
053/**
054 * Contains all possible return values for the WebDAV/HTTP protocol.<p>
055 *
056 * @since 6.5.6
057 */
058public final class CmsWebdavStatus {
059
060    /**
061     * Status code (202) indicating that a request was accepted for
062     * processing, but was not completed.
063     */
064    public static final int SC_ACCEPTED = HttpServletResponse.SC_ACCEPTED;
065
066    /**
067     * Status code (502) indicating that the HTTP server received an
068     * invalid response from a server it consulted when acting as a
069     * proxy or gateway.
070     */
071    public static final int SC_BAD_GATEWAY = HttpServletResponse.SC_BAD_GATEWAY;
072
073    /**
074     * Status code (400) indicating the request sent by the client was
075     * syntactically incorrect.
076     */
077    public static final int SC_BAD_REQUEST = HttpServletResponse.SC_BAD_REQUEST;
078
079    /**
080     * Status code (409) indicating that the request could not be
081     * completed due to a conflict with the current state of the
082     * resource.
083     */
084    public static final int SC_CONFLICT = 409;
085
086    /**
087     * Status code (100) indicating the client may continue with
088     * its request.  This interim response is used to inform the
089     * client that the initial part of the request has been
090     * received and has not yet been rejected by the server.
091     */
092    public static final int SC_CONTINUE = 100;
093
094    /**
095     * Status code (201) indicating the request succeeded and created
096     * a new resource on the server.
097     */
098    public static final int SC_CREATED = HttpServletResponse.SC_CREATED;
099
100    /**
101     * Status code (403) indicating the server understood the request
102     * but refused to fulfill it.
103     */
104    public static final int SC_FORBIDDEN = HttpServletResponse.SC_FORBIDDEN;
105
106    /**
107     * Status code (419) indicating that the resource does not have
108     * sufficient space to record the state of the resource after the
109     * execution of this method.
110     */
111    public static final int SC_INSUFFICIENT_SPACE_ON_RESOURCE = 419;
112    // This one colides with HTTP 1.1
113    // "419 Proxy Reauthentication Required"
114
115    /**
116     * Status code (500) indicating an error inside the HTTP service
117     * which prevented it from fulfilling the request.
118     */
119    public static final int SC_INTERNAL_SERVER_ERROR = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
120
121    /**
122     * Status code (423) indicating the destination resource of a
123     * method is locked, and either the request did not contain a
124     * valid Lock-Info header, or the Lock-Info header identifies
125     * a lock held by another principal.
126     */
127    public static final int SC_LOCKED = 423;
128
129    /**
130     * Status code (420) indicating the method was not executed on
131     * a particular resource within its scope because some part of
132     * the method's execution failed causing the entire method to be
133     * aborted.
134     */
135    public static final int SC_METHOD_FAILURE = 420;
136
137    /**
138     * Status code (405) indicating the method specified is not
139     * allowed for the resource.
140     */
141    public static final int SC_METHOD_NOT_ALLOWED = 405;
142
143    /**
144     * Status code (301) indicating that the resource has permanently
145     * moved to a new location, and that future references should use a
146     * new URI with their requests.
147     */
148    public static final int SC_MOVED_PERMANENTLY = HttpServletResponse.SC_MOVED_PERMANENTLY;
149
150    /**
151     * Status code (302) indicating that the resource has temporarily
152     * moved to another location, but that future references should
153     * still use the original URI to access the resource.
154     */
155    public static final int SC_MOVED_TEMPORARILY = HttpServletResponse.SC_MOVED_TEMPORARILY;
156
157    /**
158     * Status code (207) indicating that the response requires
159     * providing status for multiple independent operations.
160     */
161    public static final int SC_MULTI_STATUS = 207;
162    // This one colides with HTTP 1.1
163    // "207 Parital Update OK"
164
165    /**
166     * Status code (204) indicating that the request succeeded but that
167     * there was no new information to return.
168     */
169    public static final int SC_NO_CONTENT = HttpServletResponse.SC_NO_CONTENT;
170
171    /**
172     * Status code (404) indicating that the requested resource is not
173     * available.
174     */
175    public static final int SC_NOT_FOUND = HttpServletResponse.SC_NOT_FOUND;
176
177    /**
178     * Status code (501) indicating the HTTP service does not support
179     * the functionality needed to fulfill the request.
180     */
181    public static final int SC_NOT_IMPLEMENTED = HttpServletResponse.SC_NOT_IMPLEMENTED;
182
183    /**
184     * Status code (304) indicating that a conditional GET operation
185     * found that the resource was available and not modified.
186     */
187    public static final int SC_NOT_MODIFIED = HttpServletResponse.SC_NOT_MODIFIED;
188
189    /**
190     * Status code (200) indicating the request succeeded normally.
191     */
192    public static final int SC_OK = HttpServletResponse.SC_OK;
193
194    /**
195     * Status code (412) indicating the precondition given in one
196     * or more of the request-header fields evaluated to false
197     * when it was tested on the server.
198     */
199    public static final int SC_PRECONDITION_FAILED = 412;
200
201    /**
202     * Status code (413) indicating the server is refusing to
203     * process a request because the request entity is larger
204     * than the server is willing or able to process.
205     */
206    public static final int SC_REQUEST_TOO_LONG = 413;
207
208    /**
209     * Status code (503) indicating that the HTTP service is
210     * temporarily overloaded, and unable to handle the request.
211     */
212    public static final int SC_SERVICE_UNAVAILABLE = HttpServletResponse.SC_SERVICE_UNAVAILABLE;
213
214    /**
215     * Status code (401) indicating that the request requires HTTP
216     * authentication.
217     */
218    public static final int SC_UNAUTHORIZED = HttpServletResponse.SC_UNAUTHORIZED;
219
220    /**
221     * Status code (418) indicating the entity body submitted with
222     * the PATCH method was not understood by the resource.
223     */
224    public static final int SC_UNPROCESSABLE_ENTITY = 418;
225    // This one colides with HTTP 1.1
226    // "418 Reauthentication Required"
227
228    /**
229     * Status code (415) indicating the server is refusing to service
230     * the request because the entity of the request is in a format
231     * not supported by the requested resource for the requested
232     * method.
233     */
234    public static final int SC_UNSUPPORTED_MEDIA_TYPE = 415;
235
236    /**
237     * This Hashtable contains the mapping of HTTP and WebDAV
238     * status codes to descriptive text.  This is a static
239     * variable.
240     */
241    private static Hashtable<Integer, String> mapStatusCodes = new Hashtable<Integer, String>();
242
243    /**
244     * Hide default constructor for utility class.
245     *
246     */
247    private CmsWebdavStatus() {
248
249        // noop
250    }
251
252    static {
253        // HTTP 1.0 tatus Code
254        addStatusCodeMap(SC_OK, "OK");
255        addStatusCodeMap(SC_CREATED, "Created");
256        addStatusCodeMap(SC_ACCEPTED, "Accepted");
257        addStatusCodeMap(SC_NO_CONTENT, "No Content");
258        addStatusCodeMap(SC_MOVED_PERMANENTLY, "Moved Permanently");
259        addStatusCodeMap(SC_MOVED_TEMPORARILY, "Moved Temporarily");
260        addStatusCodeMap(SC_NOT_MODIFIED, "Not Modified");
261        addStatusCodeMap(SC_BAD_REQUEST, "Bad Request");
262        addStatusCodeMap(SC_UNAUTHORIZED, "Unauthorized");
263        addStatusCodeMap(SC_FORBIDDEN, "Forbidden");
264        addStatusCodeMap(SC_NOT_FOUND, "Not Found");
265        addStatusCodeMap(SC_INTERNAL_SERVER_ERROR, "Internal Server Error");
266        addStatusCodeMap(SC_NOT_IMPLEMENTED, "Not Implemented");
267        addStatusCodeMap(SC_BAD_GATEWAY, "Bad Gateway");
268        addStatusCodeMap(SC_SERVICE_UNAVAILABLE, "Service Unavailable");
269        addStatusCodeMap(SC_CONTINUE, "Continue");
270        addStatusCodeMap(SC_METHOD_NOT_ALLOWED, "Method Not Allowed");
271        addStatusCodeMap(SC_CONFLICT, "Conflict");
272        addStatusCodeMap(SC_PRECONDITION_FAILED, "Precondition Failed");
273        addStatusCodeMap(SC_REQUEST_TOO_LONG, "Request Too Long");
274        addStatusCodeMap(SC_UNSUPPORTED_MEDIA_TYPE, "Unsupported Media Type");
275        // WebDav Status Codes
276        addStatusCodeMap(SC_MULTI_STATUS, "Multi-Status");
277        addStatusCodeMap(SC_UNPROCESSABLE_ENTITY, "Unprocessable Entity");
278        addStatusCodeMap(SC_INSUFFICIENT_SPACE_ON_RESOURCE, "Insufficient Space On Resource");
279        addStatusCodeMap(SC_METHOD_FAILURE, "Method Failure");
280        addStatusCodeMap(SC_LOCKED, "Locked");
281    }
282
283    /**
284     * Returns the HTTP status text for the HTTP or WebDav status code
285     * specified by looking it up in the static mapping.  This is a
286     * static function.
287     *
288     * @param   nHttpStatusCode [IN] HTTP or WebDAV status code
289     * @return  A string with a short descriptive phrase for the
290     *                  HTTP status code (e.g., "OK").
291     */
292    public static String getStatusText(int nHttpStatusCode) {
293
294        Integer intKey = new Integer(nHttpStatusCode);
295
296        if (!mapStatusCodes.containsKey(intKey)) {
297            return "";
298        } else {
299            return mapStatusCodes.get(intKey);
300        }
301    }
302
303    /**
304     * Adds a new status code -> status text mapping.  This is a static
305     * method because the mapping is a static variable.
306     *
307     * @param   nKey    [IN] HTTP or WebDAV status code
308     * @param   strVal  [IN] HTTP status text
309     */
310    private static void addStatusCodeMap(int nKey, String strVal) {
311
312        mapStatusCodes.put(new Integer(nKey), strVal);
313    }
314}