View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.webscripts;
20  
21  import javax.servlet.http.HttpServletResponse;
22  
23  import org.springframework.extensions.surf.util.I18NUtil;
24  
25  
26  /**
27   * Web Script Status
28   *
29   * Records the outcome of a Web Script.
30   * 
31   * @author davidc
32   */
33  public class Status
34  {
35      /** Status code constants */
36      public static final int STATUS_CONTINUE = HttpServletResponse.SC_CONTINUE;
37      public static final int STATUS_SWITCHING_PROTOCOLS = HttpServletResponse.SC_SWITCHING_PROTOCOLS;
38      public static final int STATUS_OK = HttpServletResponse.SC_OK;
39      public static final int STATUS_CREATED = HttpServletResponse.SC_CREATED;
40      public static final int STATUS_ACCEPTED = HttpServletResponse.SC_ACCEPTED;
41      public static final int STATUS_NON_AUTHORITATIVE_INFORMATION = HttpServletResponse.SC_NON_AUTHORITATIVE_INFORMATION;
42      public static final int STATUS_NO_CONTENT = HttpServletResponse.SC_NO_CONTENT;
43      public static final int STATUS_RESET_CONTENT = HttpServletResponse.SC_RESET_CONTENT;
44      public static final int STATUS_PARTIAL_CONTENT = HttpServletResponse.SC_PARTIAL_CONTENT;
45      public static final int STATUS_MULTIPLE_CHOICES = HttpServletResponse.SC_MULTIPLE_CHOICES;
46      public static final int STATUS_MOVED_PERMANENTLY = HttpServletResponse.SC_MOVED_PERMANENTLY;
47      public static final int STATUS_MOVED_TEMPORARILY = HttpServletResponse.SC_MOVED_TEMPORARILY;
48      public static final int STATUS_FOUND = HttpServletResponse.SC_FOUND;
49      public static final int STATUS_SEE_OTHER = HttpServletResponse.SC_SEE_OTHER;
50      public static final int STATUS_NOT_MODIFIED = HttpServletResponse.SC_NOT_MODIFIED;
51      public static final int STATUS_USE_PROXY = HttpServletResponse.SC_USE_PROXY;
52      public static final int STATUS_TEMPORARY_REDIRECT = HttpServletResponse.SC_TEMPORARY_REDIRECT;
53      public static final int STATUS_BAD_REQUEST = HttpServletResponse.SC_BAD_REQUEST;
54      public static final int STATUS_UNAUTHORIZED = HttpServletResponse.SC_UNAUTHORIZED;
55      public static final int STATUS_PAYMENT_REQUIRED = HttpServletResponse.SC_PAYMENT_REQUIRED;
56      public static final int STATUS_FORBIDDEN = HttpServletResponse.SC_FORBIDDEN;
57      public static final int STATUS_NOT_FOUND = HttpServletResponse.SC_NOT_FOUND;
58      public static final int STATUS_METHOD_NOT_ALLOWED = HttpServletResponse.SC_METHOD_NOT_ALLOWED;
59      public static final int STATUS_NOT_ACCEPTABLE = HttpServletResponse.SC_NOT_ACCEPTABLE;
60      public static final int STATUS_PROXY_AUTHENTICATION_REQUIRED = HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED;
61      public static final int STATUS_REQUEST_TIMEOUT = HttpServletResponse.SC_REQUEST_TIMEOUT;
62      public static final int STATUS_CONFLICT = HttpServletResponse.SC_CONFLICT;
63      public static final int STATUS_GONE = HttpServletResponse.SC_GONE;
64      public static final int STATUS_LENGTH_REQUIRED = HttpServletResponse.SC_LENGTH_REQUIRED;
65      public static final int STATUS_PRECONDITION_FAILED = HttpServletResponse.SC_PRECONDITION_FAILED;
66      public static final int STATUS_REQUEST_ENTITY_TOO_LARGE = HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE;
67      public static final int STATUS_REQUEST_URI_TOO_LONG = HttpServletResponse.SC_REQUEST_URI_TOO_LONG;
68      public static final int STATUS_UNSUPPORTED_MEDIA_TYPE = HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE;
69      public static final int STATUS_REQUESTED_RANGE_NOT_SATISFIABLE = HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE;
70      public static final int STATUS_EXPECTATION_FAILED = HttpServletResponse.SC_EXPECTATION_FAILED;
71      public static final int STATUS_INTERNAL_SERVER_ERROR = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
72      public static final int STATUS_NOT_IMPLEMENTED = HttpServletResponse.SC_NOT_IMPLEMENTED;
73      public static final int STATUS_BAD_GATEWAY = HttpServletResponse.SC_BAD_GATEWAY;
74      public static final int STATUS_SERVICE_UNAVAILABLE = HttpServletResponse.SC_SERVICE_UNAVAILABLE;
75      public static final int STATUS_GATEWAY_TIMEOUT = HttpServletResponse.SC_GATEWAY_TIMEOUT;
76      public static final int STATUS_HTTP_VERSION_NOT_SUPPORTED = HttpServletResponse.SC_HTTP_VERSION_NOT_SUPPORTED;
77      
78      
79      private Throwable exception = null;
80      private String location = "";
81      private int code = HttpServletResponse.SC_OK;
82      private String message = "";
83      private boolean redirect = false;
84  
85      /**
86       * Helper method to set the code and message.  
87       * <p>
88       * Redirect is set to true.
89       * 
90       * @param code      code
91       * @param message   message
92       */
93      public void setCode(int code, String message)
94      {
95          this.code = code;
96          this.message = message;
97          this.redirect = true;
98      }
99      
100     /**
101      * @param exception
102      */
103     public void setException(Throwable exception)
104     {
105         this.exception = exception;
106     }
107 
108     /**
109      * @return  exception
110      */
111     public Throwable getException()
112     {
113         return exception;
114     }
115     
116     /**
117      * @param message
118      */
119     public void setMessage(String message)
120     {
121         this.message = message;
122     }
123 
124     /**
125      * @return  message
126      */
127     public String getMessage()
128     {
129         return message;
130     }
131 
132     /**
133      * @param redirect  redirect to status code response
134      */
135     public void setRedirect(boolean redirect)
136     {
137         this.redirect = redirect;
138     }
139 
140     /**
141      * @return redirect to status code response
142      */
143     public boolean getRedirect()
144     {
145         return redirect;
146     }
147 
148     /**
149      * @see javax.servlet.http.HTTPServletResponse
150      * 
151      * @param code  status code
152      */
153     public void setCode(int code)
154     {
155         this.code = code;
156     }
157 
158     /**
159      * @return  status code
160      */
161     public int getCode()
162     {
163         return code;
164     }
165 
166     /**
167      * Gets the short name of the status code
168      * 
169      * @return  status code name
170      */
171     public String getCodeName()
172     {
173         String codeName =  I18NUtil.getMessage("webscript.code." + code + ".name");
174         return codeName == null ? "" : codeName;
175     }
176     
177     /**
178      * @see javax.servlet.http.HTTPServletResponse
179      * 
180      * @param location  location response-header
181      */
182     public void setLocation(String location)
183     {
184         this.location = location;
185     }
186 
187     /**
188      * @return  location
189      */
190     public String getLocation()
191     {
192         return location;
193     }
194     
195     /**
196      * Gets the description of the status code
197      * 
198      * @return  status code description
199      */
200     public String getCodeDescription()
201     {
202         String codeDescription = I18NUtil.getMessage("webscript.code." + code + ".description");
203         return codeDescription == null ? "" : codeDescription;
204     }
205 
206     @Override
207     public String toString()
208     {
209         return Integer.toString(code);
210     }
211 }