1 24 package org.riotfamily.components.context; 25 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpSession ; 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 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 request) { 44 return PartialPageRequest.isWrapped(request); 45 } 46 47 public static boolean createAndStoreContext(HttpServletRequest request, 48 Object 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 request, 59 Object 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 request, int timeToLive) { 70 71 String 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 request, String pageUri) { 78 ContextMap contextMap = getContextMap(request); 79 contextMap.touch(pageUri); 80 contextMap.removeExpiredContexts(); 81 } 82 83 public static PageRequestContext getContext(HttpServletRequest request, 84 String pageUri, Object contextKey) { 85 86 ContextMap contexts = getContextMap(request); 87 return contexts.get(pageUri, contextKey); 88 } 89 90 public static HttpServletRequest wrapRequest( 91 HttpServletRequest request, String pageUri, Object 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 request) { 104 HttpSession 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 |