Coverage details for org.chwf.servlet.ServletData

LineHitsSource
1 /*
2 Chrysalis Web Framework [http://chrysalis.sourceforge.net]
3 Copyright (c) 2002, 2003, 2004, Paul Strack
4  
5 All rights reserved.
6  
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9  
10 1. Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12  
13 2. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16  
17 3. Neither the name of the copyright holder nor the names of its contributors
18 may be used to endorse or promote products derived from this software without
19 specific prior written permission.
20  
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
25 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32  
33 package org.chwf.servlet;
34  
35 import java.io.IOException;
36  
37 import javax.servlet.RequestDispatcher;
38 import javax.servlet.ServletContext;
39 import javax.servlet.ServletException;
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpServletResponse;
42 import javax.servlet.http.HttpSession;
43  
44 import org.chwf.registry.UserRegistry;
45  
46 /**
47  * Utility class that acts as a mechanism to pass the servlet context, request
48  * and session to lower level methods without passing them explicitly as
49  * parameters. The data is stored in a ThreadLocal variable, allowing it to be
50  * retrieved via static methods.
51  *
52  * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a>
53  */
54477public class ServletData extends UserRegistry {
55  
56   /** Name used to store ServletData singleton in the session. */
572  private static final String SESSION_NAME = ServletData.class.getName();
58  
59   /** ThreadLocal variable to hold servlet data. */
601  private static final ThreadLocal SERVLET_DATA = new ThreadLocal();
61  
62   /**
63    * Return the ServletContext (application in JSP terms).<p>
64    *
65    * @return The servlet context.
66    */
67   public static ServletContext getApplication() {
685    return getServletObjects().getApplication();
69   }
70  
71   /**
72    * Return the session.<p>
73    *
74    * @return The session.
75    */
76   public static HttpSession getSession() {
771891    return getRequest().getSession();
78   }
79  
80   /**
81    * Return the request.<p>
82    *
83    * @return The request.
84    */
85   public static HttpServletRequest getRequest() {
861916    return getServletObjects().getRequest();
87   }
88  
89   /**
90    * Return the response.<p>
91    *
92    * @return The response.
93    */
94   public static HttpServletResponse getResponse() {
957    return getServletObjects().getResponse();
96   }
97  
98   /**
99    * Forward to specified page.<p>
100    *
101    * @param page The target page.
102    * @throws ServletException For servlet exceptions.
103    * @throws IOException For IO exceptions.
104    */
105   public static void forward(String page)
106     throws ServletException, IOException {
1073    RequestDispatcher dispatcher = getRequest().getRequestDispatcher(page);
1083    if (dispatcher == null) {
1090      throw new ServletException("Unknown page '" + page + "'");
110     }
1113    dispatcher.forward(getRequest(), getResponse());
1123  }
113  
114   /**
115    * Cache the error and forward to the error page.<p>
116    *
117    * @param errorPage The target page.
118    * @param error The error.
119    * @throws ServletException For servlet exceptions.
120    * @throws IOException For IO exceptions.
121    */
122   public static void forwardError(String errorPage, Throwable error)
123     throws ServletException, IOException {
1242    ServletUtils.cacheError(getRequest(), error);
1252    forward(errorPage);
1262  }
127  
128   /**
129    * Cache the error and redirect to the error page.<p>
130    *
131    * @param errorPage The target page.
132    * @param error The error.
133    * @throws IOException For IO exceptions.
134    */
135   public static void redirectError(String errorPage, Throwable error)
136     throws IOException {
1373    HttpServletRequest request = getRequest();
1383    RequestParameterMap parameters = new RequestParameterMap(request);
1393    HttpSession session = getSession();
1403    session.setAttribute(RequestParameterMap.CACHING_NAME, parameters);
1413    session.setAttribute(ServletUtils.EXCEPTION_CACHE_NAME, error);
1423    getResponse().sendRedirect(errorPage);
1433  }
144  
145   /** Retrieve error data from the session and cache it in the request.<p> */
146   public static void retrieveErrorDataFromSession() {
147465    recacheData(RequestParameterMap.CACHING_NAME);
148465    recacheData(ServletUtils.EXCEPTION_CACHE_NAME);
149465  }
150  
151   /**
152    * Initialize the servlet registry with servlet data. This is a system
153    * method and should not be used by developers except in testing.<p>
154    *
155    * @param application The servlet context.
156    * @param request The request.
157    * @param response The response.
158    */
159   public static void init(
160     ServletContext application,
161     HttpServletRequest request,
162     HttpServletResponse response) {
163  
164478    setServletObjects(application, request, response);
165478    initUserRegistry();
166478  }
167  
168   /**
169    * Releases servlet data. This is a system method and should not be used by
170    * developers except in testing.<p>
171    */
172   public static void release() {
173479    UserRegistry.releaseLocalRegistry();
174479    SERVLET_DATA.set(null);
175479  }
176  
177   /**
178    * Retrieve data from the session and cache it in the request.
179    *
180    * @param cacheName The attribute name storing the data.
181    */
182   private static void recacheData(String cacheName) {
183930    Object object = getSession().getAttribute(cacheName);
184930    if (object != null) {
1852      getSession().removeAttribute(cacheName);
1862      getRequest().setAttribute(cacheName, object);
187     }
188930  }
189  
190   /** Initialize the registry. */
191   private static void initUserRegistry() {
192478    ServletData registry =
193       (ServletData) getSession().getAttribute(SESSION_NAME);
194478    if (registry == null) {
195476      registry = new ServletData();
196476      getSession().setAttribute(SESSION_NAME, registry);
197     }
198478    UserRegistry.initLocalRegistry(registry);
199478  }
200  
201   /**
202    * Store servlet data in a thread-local object for later retrieval.
203    *
204    * @param application The servlet context.
205    * @param request The request.
206    * @param response The response.
207    */
208   private static void setServletObjects(
209     ServletContext application,
210     HttpServletRequest request,
211     HttpServletResponse response) {
212478    ServletObjects data = new ServletObjects(application, request, response);
213478    SERVLET_DATA.set(data);
214478  }
215  
216   /**
217    * Get the servlet objects from the thread-local variable.
218    *
219    * @return The servlet objects.
220    */
221   private static ServletObjects getServletObjects() {
2221928    return (ServletObjects) SERVLET_DATA.get();
223   }
224  
225   /**
226    * Inner class to hold the servlet-related objects.
227    *
228    * @author Paul Strack
229    */
230   private static class ServletObjects {
231  
232     /** The servlet context. */
233     private final ServletContext application;
234  
235     /** The request. */
236     private final HttpServletRequest request;
237  
238     /** The response. */
239     private final HttpServletResponse response;
240  
241     /**
242      * Constructor.
243      *
244      * @param application The servlet context.
245      * @param request The request.
246      * @param response The response.
247      */
248     ServletObjects(
249       ServletContext application,
250       HttpServletRequest request,
251       HttpServletResponse response) {
252  
253       this.application = application;
254       this.request = request;
255       this.response = response;
256     }
257  
258     /**
259      * The servlet context.
260      *
261      * @return The servlet context.
262      */
263     ServletContext getApplication() {
264       return application;
265     }
266  
267     /**
268      * The request.
269      *
270      * @return The request.
271      */
272     HttpServletRequest getRequest() {
273       return request;
274     }
275  
276     /**
277      * The response.
278      *
279      * @return The response.
280      */
281     HttpServletResponse getResponse() {
282       return response;
283     }
284   }
285 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.