KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > components > context > PageRequestUtils


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.components.context;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpSession JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.riotfamily.common.web.util.ServletUtils;
32
33 public final class PageRequestUtils {
34
35     private static final String JavaDoc CONTEXT_MAP_ATTRIBUTE =
36             PageRequestUtils.class.getName() + ".contextMap";
37
38     private static Log log = LogFactory.getLog(PageRequestUtils.class);
39     
40     private PageRequestUtils() {
41     }
42     
43     public static boolean isPartialRequest(HttpServletRequest JavaDoc request) {
44         return PartialPageRequest.isWrapped(request);
45     }
46     
47     public static boolean createAndStoreContext(HttpServletRequest JavaDoc request,
48             Object JavaDoc contextKey, int timeToLive) {
49         
50         PageRequestContext context = createContext(request, contextKey);
51         if (context != null) {
52             storeContext(context, request, timeToLive);
53             return true;
54         }
55         return false;
56     }
57     
58     public static PageRequestContext createContext(HttpServletRequest JavaDoc request,
59             Object JavaDoc contextKey) {
60         
61         if (PartialPageRequest.isWrapped(request, contextKey)) {
62             log.debug("Request is already wrapped - ignoring it ...");
63             return null;
64         }
65         return new PageRequestContext(contextKey, request);
66     }
67     
68     public static void storeContext(PageRequestContext context,
69             HttpServletRequest JavaDoc request, int timeToLive) {
70         
71         String JavaDoc uri = ServletUtils.getOriginatingRequestUri(request);
72         log.debug("Storing context for " + uri + "#" + context.getKey());
73         ContextMap contextMap = getContextMap(request);
74         contextMap.put(uri, context.getKey(), context, timeToLive);
75     }
76     
77     public static void touchContext(HttpServletRequest JavaDoc request, String JavaDoc pageUri) {
78         ContextMap contextMap = getContextMap(request);
79         contextMap.touch(pageUri);
80         contextMap.removeExpiredContexts();
81     }
82
83     public static PageRequestContext getContext(HttpServletRequest JavaDoc request,
84             String JavaDoc pageUri, Object JavaDoc contextKey) {
85         
86         ContextMap contexts = getContextMap(request);
87         return contexts.get(pageUri, contextKey);
88     }
89     
90     public static HttpServletRequest JavaDoc wrapRequest(
91             HttpServletRequest JavaDoc request, String JavaDoc pageUri, Object JavaDoc contextKey)
92             throws RequestContextExpiredException {
93         
94         log.debug("Wrapping context for key " + contextKey);
95         ContextMap contexts = getContextMap(request);
96         PageRequestContext context = contexts.get(pageUri, contextKey);
97         if (context == null) {
98             throw new RequestContextExpiredException();
99         }
100         return new PartialPageRequest(request, context);
101     }
102     
103     private static ContextMap getContextMap(HttpServletRequest JavaDoc request) {
104         HttpSession JavaDoc session = request.getSession();
105         ContextMap contextMap = (ContextMap) session.getAttribute(
106                 CONTEXT_MAP_ATTRIBUTE);
107         
108         if (contextMap == null) {
109             contextMap = new ContextMap();
110             session.setAttribute(CONTEXT_MAP_ATTRIBUTE, contextMap);
111         }
112         return contextMap;
113     }
114
115 }
116
Popular Tags