KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > portlet > handler > WebRequestHandlerInterceptorAdapter


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.portlet.handler;
18
19 import javax.portlet.ActionRequest;
20 import javax.portlet.ActionResponse;
21 import javax.portlet.RenderRequest;
22 import javax.portlet.RenderResponse;
23
24 import org.springframework.util.Assert;
25 import org.springframework.web.context.request.WebRequestInterceptor;
26 import org.springframework.web.portlet.HandlerInterceptor;
27 import org.springframework.web.portlet.ModelAndView;
28 import org.springframework.web.portlet.context.PortletWebRequest;
29
30 /**
31  * Adapter that implements the Portlet HandlerInterceptor interface
32  * and wraps an underlying WebRequestInterceptor.
33  *
34  * <p><b>NOTE:</b> The WebRequestInterceptor is by default only applied to the Portlet
35  * <b>render</b> phase, which is dealing with preparing and rendering a Portlet view.
36  * The Portlet action phase will only be intercepted with WebRequestInterceptor calls
37  * if the <code>renderPhaseOnly</code> flag is explicitly set to <code>false</code>.
38  * In general, it is recommended to use the Portlet-specific HandlerInterceptor
39  * mechanism for differentiating between action and render interception.
40  *
41  * @author Juergen Hoeller
42  * @since 2.0
43  * @see org.springframework.web.context.request.WebRequestInterceptor
44  * @see org.springframework.web.portlet.HandlerInterceptor
45  */

46 public class WebRequestHandlerInterceptorAdapter implements HandlerInterceptor {
47
48     private final WebRequestInterceptor requestInterceptor;
49
50     private final boolean renderPhaseOnly;
51
52
53     /**
54      * Create a new WebRequestHandlerInterceptorAdapter for the given WebRequestInterceptor,
55      * applying to the render phase only.
56      * @param requestInterceptor the WebRequestInterceptor to wrap
57      */

58     public WebRequestHandlerInterceptorAdapter(WebRequestInterceptor requestInterceptor) {
59         this(requestInterceptor, true);
60     }
61
62     /**
63      * Create a new WebRequestHandlerInterceptorAdapter for the given WebRequestInterceptor.
64      * @param requestInterceptor the WebRequestInterceptor to wrap
65      * @param renderPhaseOnly whether to apply to the render phase only (<code>true</code>)
66      * or to the action phase as well (<code>false</code>)
67      */

68     public WebRequestHandlerInterceptorAdapter(WebRequestInterceptor requestInterceptor, boolean renderPhaseOnly) {
69         Assert.notNull(requestInterceptor, "WebRequestInterceptor must not be null");
70         this.requestInterceptor = requestInterceptor;
71         this.renderPhaseOnly = renderPhaseOnly;
72     }
73
74
75     public boolean preHandleAction(ActionRequest request, ActionResponse response, Object JavaDoc handler) throws Exception JavaDoc {
76         if (!this.renderPhaseOnly) {
77             this.requestInterceptor.preHandle(new PortletWebRequest(request));
78         }
79         return true;
80     }
81
82     public void afterActionCompletion(
83             ActionRequest request, ActionResponse response, Object JavaDoc handler, Exception JavaDoc ex) throws Exception JavaDoc {
84
85         if (!this.renderPhaseOnly) {
86             this.requestInterceptor.afterCompletion(new PortletWebRequest(request), ex);
87         }
88     }
89
90     public boolean preHandleRender(RenderRequest request, RenderResponse response, Object JavaDoc handler) throws Exception JavaDoc {
91         this.requestInterceptor.preHandle(new PortletWebRequest(request));
92         return true;
93     }
94
95     public void postHandleRender(
96             RenderRequest request, RenderResponse response, Object JavaDoc handler, ModelAndView modelAndView) throws Exception JavaDoc {
97
98         this.requestInterceptor.postHandle(new PortletWebRequest(request),
99                 (modelAndView != null ? modelAndView.getModelMap() : null));
100     }
101
102     public void afterRenderCompletion(
103             RenderRequest request, RenderResponse response, Object JavaDoc handler, Exception JavaDoc ex) throws Exception JavaDoc {
104
105         this.requestInterceptor.afterCompletion(new PortletWebRequest(request), ex);
106     }
107
108 }
109
Popular Tags