KickJava   Java API By Example, From Geeks To Geeks.

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


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) 2007
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.HttpServletResponse JavaDoc;
28
29 import org.riotfamily.common.web.util.ServletUtils;
30 import org.riotfamily.components.editor.EditModeUtils;
31 import org.springframework.util.Assert;
32 import org.springframework.web.servlet.HandlerInterceptor;
33 import org.springframework.web.servlet.ModelAndView;
34 import org.springframework.web.util.WebUtils;
35
36 /**
37  * @author Felix Gnass [fgnass at neteye dot de]
38  * @since 6.5
39  */

40 public class StoreContextInterceptor implements HandlerInterceptor {
41
42     private static final String JavaDoc HANDLED_ATTRIBUTE =
43             StoreContextInterceptor.class.getName() + ".handled";
44     
45     private static final String JavaDoc CONTEXT_ATTRIBUTE =
46             StoreContextInterceptor.class.getName() + ".context";
47     
48     private static final String JavaDoc STORE_ATTRIBUTE =
49             StoreContextInterceptor.class.getName() + ".storeContext";
50     
51     public boolean preHandle(HttpServletRequest JavaDoc request,
52             HttpServletResponse JavaDoc response, Object JavaDoc handler) throws Exception JavaDoc {
53         
54         if (WebUtils.isIncludeRequest(request) && EditModeUtils.isEditMode(request)) {
55             String JavaDoc uri = ServletUtils.getPathWithinApplication(request);
56             PageRequestContext context = PageRequestUtils.createContext(request, uri);
57             request.setAttribute(CONTEXT_ATTRIBUTE, context);
58         }
59         request.setAttribute(HANDLED_ATTRIBUTE, Boolean.TRUE);
60         return true;
61     }
62
63     public void postHandle(HttpServletRequest JavaDoc request,
64             HttpServletResponse JavaDoc response, Object JavaDoc handler,
65             ModelAndView modelAndView) throws Exception JavaDoc {
66     }
67     
68     public void afterCompletion(HttpServletRequest JavaDoc request,
69             HttpServletResponse JavaDoc response, Object JavaDoc handler, Exception JavaDoc ex)
70             throws Exception JavaDoc {
71         
72         if (request.getAttribute(STORE_ATTRIBUTE) != null) {
73             PageRequestContext context = (PageRequestContext)
74                     request.getAttribute(CONTEXT_ATTRIBUTE);
75             
76             PageRequestUtils.storeContext(context, request, 120000);
77         }
78     }
79     
80     public static void storeContext(HttpServletRequest JavaDoc request) {
81         Assert.notNull(request.getAttribute(HANDLED_ATTRIBUTE),
82                 "No context found in request. Make sure you add a "
83                 + StoreContextInterceptor.class.getName()
84                 + " to your HandlerMapping(s).");
85         
86         request.setAttribute(STORE_ATTRIBUTE, Boolean.TRUE);
87     }
88
89 }
90
Popular Tags