KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > website > interceptor > pushup > PushUpInterceptor


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.website.interceptor.pushup;
25
26 import java.io.IOException JavaDoc;
27
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30
31 import org.riotfamily.common.web.util.ServletUtils;
32 import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
33
34 /**
35  * HandlerInterceptor that allows an included controller to handle the request
36  * before any other controller is executed. This is useful if an included
37  * controller needs to send a redirect, which would not work under normal
38  * circumstances, since the response would have already been committed.
39  * <p>
40  * The interceptor looks for a special request parameter which contains the URI
41  * of the controller that should handle the request in the fist place. If such
42  * a parameter is present, the interceptor will use a RequestDispatcher to
43  * forward the request to that URI. For this forward the response is replaced
44  * by a wrapper that captures the output and checks whether a redirect or error
45  * has been sent. If yes, the interceptor prevents the execution of the actual
46  * handler. Otherwise the execution continues and the captured output is
47  * rendered when the controller is requested for the second time.
48  * </p>
49  */

50 public class PushUpInterceptor extends HandlerInterceptorAdapter {
51
52     public static final String JavaDoc INCLUDE_URI_PARAM = "__includeUri";
53     
54     private static final String JavaDoc DISPATCHED_ATTRIBUTE =
55             PushUpInterceptor.class.getName() + ".dispatched";
56     
57     private static final String JavaDoc HANDLER_ATTRIBUTE =
58             PushUpInterceptor.class.getName() + ".handler";
59     
60     private static final String JavaDoc RESPONSE_WRAPPER_ATTRIBUTE =
61         PushUpInterceptor.class.getName() + ".responseWrapper";
62
63     
64     public boolean preHandle(HttpServletRequest JavaDoc request,
65             HttpServletResponse JavaDoc response, Object JavaDoc handler) throws Exception JavaDoc {
66         
67         Object JavaDoc firstHandler = request.getAttribute(HANDLER_ATTRIBUTE);
68         if (firstHandler == null) {
69             boolean dispatched = request.getAttribute(DISPATCHED_ATTRIBUTE) != null;
70             if (dispatched) {
71                 return handleFirstInclude(handler, request);
72             }
73             return handleUnknown(request, response);
74         }
75         return handleSubsequentInclude(handler, firstHandler, request, response);
76     }
77         
78     protected boolean handleUnknown(HttpServletRequest JavaDoc request,
79             HttpServletResponse JavaDoc response) throws Exception JavaDoc {
80         
81         String JavaDoc includeUri = request.getParameter(INCLUDE_URI_PARAM);
82         if (includeUri != null) {
83             request.setAttribute(DISPATCHED_ATTRIBUTE, Boolean.TRUE);
84             if (ServletUtils.isXmlHttpRequest(request)) {
85                 request.getRequestDispatcher(includeUri).forward(
86                         request, response);
87                 
88                 return false;
89             }
90             else {
91                 DeferredRenderingResponseWrapper responseWrapper =
92                         new DeferredRenderingResponseWrapper(response);
93                 
94                 request.setAttribute(RESPONSE_WRAPPER_ATTRIBUTE, responseWrapper);
95                 request.getRequestDispatcher(includeUri).forward(
96                         request, responseWrapper);
97                 
98                 if (responseWrapper.isRedirectSent()) {
99                     return false;
100                 }
101             }
102         }
103         return true;
104     }
105     
106     protected boolean handleFirstInclude(Object JavaDoc handler, HttpServletRequest JavaDoc request) {
107         request.setAttribute(HANDLER_ATTRIBUTE, handler);
108         return true;
109     }
110     
111     protected boolean handleSubsequentInclude(Object JavaDoc handler,
112             Object JavaDoc firstHandler, HttpServletRequest JavaDoc request,
113             HttpServletResponse JavaDoc response) throws IOException JavaDoc {
114
115         //TODO Check if getRequestURI() matches the includeUri, to support
116
//multiple includes of the same handler under different URLs.
117
if (handler.equals(firstHandler)) {
118             //Second time we come across the handler ...
119
DeferredRenderingResponseWrapper responseWrapper =
120                 (DeferredRenderingResponseWrapper) request.getAttribute(
121                 RESPONSE_WRAPPER_ATTRIBUTE);
122             
123             responseWrapper.renderResponse(response);
124             return false;
125         }
126         return true;
127     }
128 }
129
Popular Tags