KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jpublish > RequestContext


1 /*--
2
3  Copyright (C) 2001-2003 Aetrion LLC.
4  All rights reserved.
5  
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions
8  are met:
9  
10  1. Redistributions of source code must retain the above copyright
11     notice, this list of conditions, and the following disclaimer.
12  
13  2. Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions, and the disclaimer that follows
15     these conditions in the documentation and/or other materials
16     provided with the distribution.
17
18  3. The name "JPublish" must not be used to endorse or promote products
19     derived from this software without prior written permission. For
20     written permission, please contact info@aetrion.com.
21  
22  4. Products derived from this software may not be called "JPublish", nor
23     may "JPublish" appear in their name, without prior written permission
24     from Aetrion LLC (info@aetrion.com).
25  
26  In addition, the authors of this software request (but do not require)
27  that you include in the end-user documentation provided with the
28  redistribution and/or in the software itself an acknowledgement equivalent
29  to the following:
30      "This product includes software developed by
31       Aetrion LLC (http://www.aetrion.com/)."
32
33  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
37  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
42  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43  POSSIBILITY OF SUCH DAMAGE.
44
45  For more information on JPublish, please see <http://www.jpublish.org/>.
46  
47  */

48
49 package org.jpublish;
50
51 import java.io.IOException JavaDoc;
52 import java.util.HashMap JavaDoc;
53 import java.util.Map JavaDoc;
54 import java.util.Set JavaDoc;
55
56 import javax.servlet.RequestDispatcher JavaDoc;
57 import javax.servlet.ServletContext JavaDoc;
58 import javax.servlet.ServletException JavaDoc;
59 import javax.servlet.http.HttpServletRequest JavaDoc;
60 import javax.servlet.http.HttpServletResponse JavaDoc;
61 import javax.servlet.http.HttpSession JavaDoc;
62
63 import org.apache.commons.logging.Log;
64 import org.apache.commons.logging.LogFactory;
65 import org.jpublish.page.Page;
66 import org.jpublish.util.DateUtilities;
67 import org.jpublish.util.NumberUtilities;
68 import org.jpublish.util.URLUtilities;
69 import org.jpublish.util.encoding.CharacterEncodingMap;
70
71 /**
72  * The JPublishContext overrides the Velocity context to add name checking as a security measure so that code cannot
73  * replace standard variables.
74  *
75  * @author Anthony Eden
76  */

77
78 public class RequestContext {
79
80     /* names used to store objects in the JPublish context */
81     public static final String JavaDoc JPUBLISH_PATH = "path";
82     public static final String JavaDoc JPUBLISH_REDIRECT = "redirect";
83     public static final String JavaDoc JPUBLISH_STOP_PROCESSING = "stop-processing";
84     public static final String JavaDoc JPUBLISH_REQUEST = "request";
85     public static final String JavaDoc JPUBLISH_RESPONSE = "response";
86     public static final String JavaDoc JPUBLISH_SESSION = "session";
87     public static final String JavaDoc JPUBLISH_APPLICATION = "application";
88     public static final String JavaDoc JPUBLISH_CHARACTER_ENCODING_MAP =
89             "characterEncodingMap";
90     public static final String JavaDoc JPUBLISH_URL_UTILITIES = "urlUtilities";
91     public static final String JavaDoc JPUBLISH_DATE_UTILITIES = "dateUtilities";
92     public static final String JavaDoc JPUBLISH_NUMBER_UTILITIES = "numberUtilities";
93     public static final String JavaDoc JPUBLISH_SYSLOG = "syslog";
94     public static final String JavaDoc JPUBLISH_SITE = "site";
95     public static final String JavaDoc JPUBLISH_PAGE = "page";
96
97     /**
98      * List of reserved names.
99      */

100     private static final String JavaDoc[] reservedNames = {
101         "path", "request", "response", "session", "application",
102         "page", "site", "components"
103     };
104
105     private static Log log = LogFactory.getLog(RequestContext.class);
106
107     private static RequestContextThreadLocal requestContextThreadLocal =
108             new RequestContextThreadLocal();
109
110     private Map JavaDoc data = new HashMap JavaDoc();
111     private boolean checkReservedNames = false;
112     private boolean forwarded = false;
113
114     /**
115      * Get the current RequestContext which is stored as a ThreadLocal variable.
116      *
117      * @return The thread local RequestContext
118      */

119
120     public static RequestContext getRequestContext() {
121         return (RequestContext) requestContextThreadLocal.get();
122     }
123
124     /**
125      * Get the value for the specified key. If the value is not in the context then this method returns null.
126      *
127      * @param key The key
128      * @return The value or null
129      */

130
131     public Object JavaDoc get(String JavaDoc key) {
132         return data.get(key);
133     }
134
135     /**
136      * Put the given value into the context. If the key cannot be inserted into the context because it conflicts with a
137      * reserved name then a ReservedNameException will be thrown.
138      *
139      * @param key The key
140      * @param value The value
141      * @return The previous value or null if there was no previous value
142      */

143
144     public Object JavaDoc put(String JavaDoc key, Object JavaDoc value) {
145         if (checkReservedNames && (isReservedName(key))) {
146             throw new ReservedNameException(key);
147         }
148
149         return data.put(key, value);
150     }
151
152     /**
153      * Remove the given value from the context. If the key cannot be removed from the context because it is a reserved
154      * name then a ReservedNameException will be thrown.
155      *
156      * @param key The key
157      * @return The removed value or null if there was no removed value
158      */

159
160     public Object JavaDoc remove(String JavaDoc key) {
161         if (checkReservedNames && isReservedName(key)) {
162             throw new ReservedNameException(key);
163         }
164
165         return data.remove(key);
166     }
167
168     /**
169      * Get an array of all keys in the context.
170      *
171      * @return An array of key objects
172      */

173
174     public String JavaDoc[] getKeys() {
175         Set JavaDoc keys = data.keySet();
176         return (String JavaDoc[]) keys.toArray(new String JavaDoc[keys.size()]);
177     }
178
179     /**
180      * Return true if the context contains the specified key.
181      *
182      * @param key The key
183      * @return True if the context contains the key
184      */

185
186     public boolean containsKey(String JavaDoc key) {
187         return data.containsKey(key);
188     }
189
190     /**
191      * Clear the context. This method should be used before a redirect is executed.
192      */

193
194     public void clear() {
195         data.clear();
196     }
197
198     /**
199      * Enable the reserved name check so that code does not accidentally overwrite JPublish defined variables.
200      */

201
202     public final void enableCheckReservedNames() {
203         checkReservedNames = true;
204     }
205
206     /**
207      * Disable the reserved name check so that external code can overwrite JPublish defined variables.
208      */

209
210     public final void disableCheckReservedNames() {
211         checkReservedNames = false;
212     }
213     
214     /*
215      * Convenience methods for getting objects stored in the
216      * JPublish context
217      */

218     
219     /**
220      * Get the request path.
221      *
222      * @return The request path
223      */

224
225     public String JavaDoc getPath() {
226         return (String JavaDoc) get(JPUBLISH_PATH);
227     }
228
229     /**
230      * Get the redirect path. This method will return null unless a redirect should occur. If the value is not null
231      * then the value will be used as the URL for an HTTP redirect.
232      *
233      * @return The redirect address
234      */

235
236     public String JavaDoc getRedirect() {
237         return (String JavaDoc) get(JPUBLISH_REDIRECT);
238     }
239
240     /**
241      * Set the redirect path.
242      *
243      * @param redirect The redirect path
244      */

245
246     public void setRedirect(String JavaDoc redirect) {
247         put(JPUBLISH_REDIRECT, redirect);
248     }
249
250     /**
251      * Get the stop-processing lag. This method will return null unless request processing should be stopped.
252      *
253      * @return The stop processing flag or null
254      */

255
256     public String JavaDoc getStopProcessing() {
257         return (String JavaDoc) get(JPUBLISH_STOP_PROCESSING);
258     }
259
260     /**
261      * Set the stop-processing flag. Any non-null String value will be considered "true" indicating that processing
262      * should stop.
263      *
264      * @param stopProcessing The stop processing flag
265      */

266
267     public void setStopProcessing(String JavaDoc stopProcessing) {
268         put(JPUBLISH_STOP_PROCESSING, stopProcessing);
269     }
270
271     /**
272      * Use the current request's RequestDispatcher to forward to the given path.
273      *
274      * @param path The path
275      */

276
277     public void forward(String JavaDoc path) throws ServletException JavaDoc, IOException JavaDoc {
278         HttpServletRequest JavaDoc request = getRequest();
279         HttpServletResponse JavaDoc response = getResponse();
280         RequestDispatcher JavaDoc dispatcher = request.getRequestDispatcher(path);
281         setStopProcessing("true");
282         setForwarded(true);
283         log.debug("Forwarding now");
284         dispatcher.forward(request, response);
285         setStopProcessing("true");
286         setForwarded(true);
287         log.debug("Forward complete");
288     }
289
290     /**
291      * Return true if the request has been forwarded. If this is the case then the request context must not be
292      * cleared.
293      *
294      * @return True if the request is forwareded
295      */

296
297     public boolean isForwarded() {
298         return forwarded;
299     }
300
301     void setForwarded(boolean forwarded) {
302         this.forwarded = forwarded;
303     }
304
305     /**
306      * Use the current request's RequestDispatcher to include the given path.
307      *
308      * @param path The path
309      */

310
311     public void include(String JavaDoc path) throws ServletException JavaDoc, IOException JavaDoc {
312         HttpServletRequest JavaDoc request = getRequest();
313         HttpServletResponse JavaDoc response = getResponse();
314         RequestDispatcher JavaDoc dispatcher = request.getRequestDispatcher(path);
315         dispatcher.include(request, response);
316     }
317
318     /**
319      * Get the current HTTP-request object.
320      *
321      * @return The HTTP servlet request or null if not available
322      */

323
324     public HttpServletRequest JavaDoc getRequest() {
325         return (HttpServletRequest JavaDoc) get(JPUBLISH_REQUEST);
326     }
327
328     /**
329      * Get the current HTTP-response object.
330      *
331      * @return The HTTP servlet response or null if not available
332      */

333
334     public HttpServletResponse JavaDoc getResponse() {
335         return (HttpServletResponse JavaDoc) get(JPUBLISH_RESPONSE);
336     }
337
338     /**
339      * Get the current HTTP session object.
340      *
341      * @return The HTTP session or null if not available
342      */

343
344     public HttpSession JavaDoc getSession() {
345         return (HttpSession JavaDoc) get(JPUBLISH_SESSION);
346     }
347
348     /**
349      * Get the current servlet context.
350      *
351      * @return The servlet context or null if not available
352      */

353
354     public ServletContext JavaDoc getApplication() {
355         return (ServletContext JavaDoc) get(JPUBLISH_APPLICATION);
356     }
357
358     /**
359      * Get the JPublish character encoding map
360      *
361      * @return The character encoding map or null if not available
362      */

363
364     public CharacterEncodingMap getCharacterEncodingMap() {
365         return (CharacterEncodingMap) get(JPUBLISH_CHARACTER_ENCODING_MAP);
366     }
367
368     /**
369      * Get the JPublish URL utilities
370      *
371      * @return The URL utilities or null if not available
372      */

373
374     public URLUtilities getUrlUtilities() {
375         return (URLUtilities) get(JPUBLISH_URL_UTILITIES);
376     }
377
378     /**
379      * Get the JPublish date utilities
380      *
381      * @return The date utilities or null if not available
382      */

383
384     public DateUtilities getDateUtilities() {
385         return (DateUtilities) get(JPUBLISH_DATE_UTILITIES);
386     }
387
388     /**
389      * Get the JPublish number utilities
390      *
391      * @return The number utilities or null if not available
392      */

393
394     public NumberUtilities getNumberUtilities() {
395         return (NumberUtilities) get(JPUBLISH_NUMBER_UTILITIES);
396     }
397
398     /**
399      * Get the JPublish syslog.
400      *
401      * @return The syslog or null if not available
402      */

403
404     public Log getSyslog() {
405         return (Log) get(JPUBLISH_SYSLOG);
406     }
407
408     /**
409      * Get the SiteContext.
410      *
411      * @return The SiteContext or null if not available
412      */

413
414     public SiteContext getSiteContext() {
415         return (SiteContext) get(JPUBLISH_SITE);
416     }
417
418     /**
419      * Get the current JPublish page.
420      *
421      * @return The current page or null if not available
422      */

423
424     public Page getPage() {
425         return (Page) get(JPUBLISH_PAGE);
426     }
427
428     /**
429      * Return true if the given variable is a reserved name used by the JPublish engine. If checkReservedNames is true
430      * then this method must be called before inserting a value into the context.
431      *
432      * @param name The name to check
433      * @return True if the name is a reserved name
434      */

435
436     private static final boolean isReservedName(String JavaDoc name) {
437         for (int i = 0; i < reservedNames.length; i++) {
438             if (name.equals(reservedNames[i])) {
439                 return true;
440             }
441         }
442         return false;
443     }
444
445     /**
446      * Private inner class which is a ThreadLocal variable representing the current RequestContext.
447      *
448      * @author Anthony Eden
449      */

450
451     private static class RequestContextThreadLocal extends ThreadLocal JavaDoc {
452
453         protected Object JavaDoc initialValue() {
454             return new RequestContext();
455         }
456     }
457
458 }
459
Popular Tags