001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.components.context;
025:
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpSession;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.riotfamily.common.web.util.ServletUtils;
032:
033: public final class PageRequestUtils {
034:
035: private static final String CONTEXT_MAP_ATTRIBUTE = PageRequestUtils.class
036: .getName()
037: + ".contextMap";
038:
039: private static Log log = LogFactory.getLog(PageRequestUtils.class);
040:
041: private PageRequestUtils() {
042: }
043:
044: public static boolean isPartialRequest(HttpServletRequest request) {
045: return PartialPageRequest.isWrapped(request);
046: }
047:
048: public static boolean createAndStoreContext(
049: HttpServletRequest request, Object contextKey,
050: int timeToLive) {
051:
052: PageRequestContext context = createContext(request, contextKey);
053: if (context != null) {
054: storeContext(context, request, timeToLive);
055: return true;
056: }
057: return false;
058: }
059:
060: public static PageRequestContext createContext(
061: HttpServletRequest request, Object contextKey) {
062:
063: if (PartialPageRequest.isWrapped(request, contextKey)) {
064: log.debug("Request is already wrapped - ignoring it ...");
065: return null;
066: }
067: return new PageRequestContext(contextKey, request);
068: }
069:
070: public static void storeContext(PageRequestContext context,
071: HttpServletRequest request, int timeToLive) {
072:
073: String uri = ServletUtils.getOriginatingRequestUri(request);
074: log
075: .debug("Storing context for " + uri + "#"
076: + context.getKey());
077: ContextMap contextMap = getContextMap(request);
078: contextMap.put(uri, context.getKey(), context, timeToLive);
079: }
080:
081: public static void touchContext(HttpServletRequest request,
082: String pageUri) {
083: ContextMap contextMap = getContextMap(request);
084: contextMap.touch(pageUri);
085: contextMap.removeExpiredContexts();
086: }
087:
088: public static PageRequestContext getContext(
089: HttpServletRequest request, String pageUri,
090: Object contextKey) {
091:
092: ContextMap contexts = getContextMap(request);
093: return contexts.get(pageUri, contextKey);
094: }
095:
096: public static HttpServletRequest wrapRequest(
097: HttpServletRequest request, String pageUri,
098: String contextKey) throws RequestContextExpiredException {
099:
100: log.debug("Wrapping context for key " + contextKey);
101: ContextMap contexts = getContextMap(request);
102: PageRequestContext context = contexts.get(pageUri, contextKey);
103: if (context == null) {
104: throw new RequestContextExpiredException(pageUri,
105: contextKey);
106: }
107: return new PartialPageRequest(request, context);
108: }
109:
110: private static ContextMap getContextMap(HttpServletRequest request) {
111: HttpSession session = request.getSession();
112: ContextMap contextMap = (ContextMap) session
113: .getAttribute(CONTEXT_MAP_ATTRIBUTE);
114:
115: if (contextMap == null) {
116: contextMap = new ContextMap();
117: session.setAttribute(CONTEXT_MAP_ATTRIBUTE, contextMap);
118: }
119: return contextMap;
120: }
121:
122: }
|