View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.runtimeinfo.rest;
23  
24  import org.opencastproject.util.doc.rest.RestResponse;
25  
26  import javax.servlet.http.HttpServletResponse;
27  
28  /**
29   * Represents a possible status result for an endpoint
30   */
31  public class StatusData {
32  
33    /**
34     * The HTTP response code.
35     */
36    private int code;
37  
38    /**
39     * The name of this status.
40     */
41    private String name;
42  
43    /**
44     * The description for this HTTP response.
45     */
46    private String description;
47  
48    /**
49     * The XML schema for the response, if applicable.
50     */
51    private String xmlSchema;
52  
53    /**
54     * A constructor that takes a RestResponse annotation type object and constructs a StatusData object.
55     *
56     * @param restResponse
57     *          a RestResponse annotation type object that stores a response code and its description
58     * @throws IllegalArgumentException
59     *           if the response code is out of range (e.g. <100 or >1100)
60     */
61    public StatusData(RestResponse restResponse) throws IllegalArgumentException {
62      this(restResponse.responseCode(), restResponse.description());
63    }
64  
65    /**
66     * A constructor that takes a HTTP response code and a description for this response, and an XML schema for the
67     * response and constructs a StatusData object. A reference of response code constants can be found in <a
68     * href="http://download.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html"
69     * >javax.servlet.http.HttpServletResponse</a>.
70     *
71     * @param code
72     *          a HTTP response code
73     * @param description
74     *          a description of the response
75     * @throws IllegalArgumentException
76     *           if code is out of range (e.g. &lt;100 or &gt;1100)
77     */
78    public StatusData(int code, String description, String xmlSchema) throws IllegalArgumentException {
79      if (code < 100 || code > 1100) {
80        throw new IllegalArgumentException("Code (" + code + ") is outside of the valid range: 100-1100.");
81      }
82      this.code = code;
83      name = findName(code);
84      if (description.isEmpty()) {
85        this.description = null;
86      } else {
87        this.description = description;
88      }
89      this.xmlSchema = xmlSchema;
90    }
91  
92    /**
93     * A constructor that takes a HTTP response code and a description for this response and constructs a StatusData
94     * object. A reference of response code constants can be found in <a
95     * href="http://download.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html"
96     * >javax.servlet.http.HttpServletResponse</a>.
97     *
98     * @param code
99     *          a HTTP response code
100    * @param description
101    *          a description of the response
102    * @throws IllegalArgumentException
103    *           if code is out of range (e.g. &lt;100 or &gt; 1100)
104    */
105   public StatusData(int code, String description) throws IllegalArgumentException {
106     this(code, description, null);
107   }
108 
109   @Override
110   /**
111    * @return a string representation of this object
112    */
113   public String toString() {
114     return "SC:" + code + ":" + name;
115   }
116 
117   /**
118    * @return the response code of this status
119    */
120   public int getCode() {
121     return code;
122   }
123 
124   /**
125    * @return a string name of this status
126    */
127   public String getName() {
128     return name;
129   }
130 
131   /**
132    * @return a description of this status
133    */
134   public String getDescription() {
135     return description;
136   }
137 
138   /**
139    * This will resolve a human readable name for all known status codes.
140    *
141    * @param code
142    *          the status code
143    * @return the name OR UNKNOWN if none found
144    * @throws IllegalArgumentException
145    *           if the code is outside the valid range
146    */
147   public static String findName(int code) throws IllegalArgumentException {
148     if (code < 100 || code > 1100) {
149       throw new IllegalArgumentException("Code (" + code + ") is outside of the valid range: 100-1100.");
150     }
151 
152     // list from http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
153     String result;
154     switch (code) {
155     // 1xx Informational
156       case HttpServletResponse.SC_CONTINUE: // 100
157         result = "Continue";
158         break;
159       case HttpServletResponse.SC_SWITCHING_PROTOCOLS: // 101
160         result = "Switching Protocols";
161         break;
162       // 2xx Success
163       case HttpServletResponse.SC_OK: // 200
164         result = "OK";
165         break;
166       case HttpServletResponse.SC_CREATED: // 201
167         result = "Created";
168         break;
169       case HttpServletResponse.SC_ACCEPTED: // 202
170         result = "Accepted";
171         break;
172       case HttpServletResponse.SC_NON_AUTHORITATIVE_INFORMATION: // 203
173         result = "Non-Authoritative Information";
174         break;
175       case HttpServletResponse.SC_NO_CONTENT: // 204
176         result = "No Content";
177         break;
178       case HttpServletResponse.SC_RESET_CONTENT: // 205
179         result = "Reset Content";
180         break;
181       case HttpServletResponse.SC_PARTIAL_CONTENT: // 206
182         result = "Partial Content";
183         break;
184       // 3xx Redirection
185       case HttpServletResponse.SC_MULTIPLE_CHOICES: // 300
186         result = "Multiple Choices";
187         break;
188       case HttpServletResponse.SC_MOVED_PERMANENTLY: // 301
189         result = "Moved Permanently";
190         break;
191       case HttpServletResponse.SC_MOVED_TEMPORARILY: // 302
192         result = "Found";
193         break;
194       case HttpServletResponse.SC_SEE_OTHER: // 303
195         result = "See Other";
196         break;
197       case HttpServletResponse.SC_NOT_MODIFIED: // 304
198         result = "Not Modified";
199         break;
200       case HttpServletResponse.SC_USE_PROXY: // 305
201         result = "Use Proxy";
202         break;
203       case HttpServletResponse.SC_TEMPORARY_REDIRECT: // 307
204         result = "Temporary Redirect";
205         break;
206       // 4xx Client Error
207       case HttpServletResponse.SC_BAD_REQUEST: // 400
208         result = "Bad Request";
209         break;
210       case HttpServletResponse.SC_UNAUTHORIZED: // 401
211         result = "Unauthorized";
212         break;
213       case HttpServletResponse.SC_PAYMENT_REQUIRED: // 402
214         result = "Payment Required";
215         break;
216       case HttpServletResponse.SC_FORBIDDEN: // 403
217         result = "Forbidden";
218         break;
219       case HttpServletResponse.SC_NOT_FOUND: // 404
220         result = "Not Found";
221         break;
222       case HttpServletResponse.SC_METHOD_NOT_ALLOWED: // 405
223         result = "Method Not Allowed";
224         break;
225       case HttpServletResponse.SC_NOT_ACCEPTABLE: // 406
226         result = "Not Acceptable";
227         break;
228       case HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED: // 407
229         result = "Proxy Authentication Required";
230         break;
231       case HttpServletResponse.SC_REQUEST_TIMEOUT: // 408
232         result = "Request Timeout";
233         break;
234       case HttpServletResponse.SC_CONFLICT: // 409
235         result = "Conflict";
236         break;
237       case HttpServletResponse.SC_GONE: // 410
238         result = "Gone";
239         break;
240       case HttpServletResponse.SC_LENGTH_REQUIRED: // 411
241         result = "Length Required";
242         break;
243       case HttpServletResponse.SC_PRECONDITION_FAILED: // 412
244         result = "Precondition Failed";
245         break;
246       case HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE: // 413
247         result = "Request Entity Too Large";
248         break;
249       case HttpServletResponse.SC_REQUEST_URI_TOO_LONG: // 414
250         result = "Request URI Too Long";
251         break;
252       case HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE: // 415
253         result = "Unsupported Media Type";
254         break;
255       case HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE: // 416
256         result = "Requested Range Not Satisfiable";
257         break;
258       case HttpServletResponse.SC_EXPECTATION_FAILED: // 417
259         result = "Expectation Failed";
260         break;
261       // 5xx Server Error
262       case HttpServletResponse.SC_INTERNAL_SERVER_ERROR: // 500
263         result = "Internal Server Error";
264         break;
265       case HttpServletResponse.SC_NOT_IMPLEMENTED: // 501
266         result = "Not Implemented";
267         break;
268       case HttpServletResponse.SC_BAD_GATEWAY: // 502
269         result = "Bad Gateway";
270         break;
271       case HttpServletResponse.SC_SERVICE_UNAVAILABLE: // 503
272         result = "Service Unavailable";
273         break;
274       case HttpServletResponse.SC_GATEWAY_TIMEOUT: // 504
275         result = "Gateway Timeout";
276         break;
277       case HttpServletResponse.SC_HTTP_VERSION_NOT_SUPPORTED: // 505
278         result = "Version Not Supported";
279         break;
280       default:
281         result = "UNKNOWN";
282     }
283     return result;
284   }
285 
286   /**
287    * @return the xmlSchema
288    */
289   public String getXmlSchema() {
290     return xmlSchema;
291   }
292 
293   /**
294    * @param xmlSchema
295    *          the xmlSchema to set
296    */
297   public void setXmlSchema(String xmlSchema) {
298     this.xmlSchema = xmlSchema;
299   }
300 }