View Javadoc
1   /*
2    * Copyright 2002-2012 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.EventRequest;
22  import javax.portlet.EventResponse;
23  import javax.portlet.RenderRequest;
24  import javax.portlet.RenderResponse;
25  import javax.portlet.ResourceRequest;
26  import javax.portlet.ResourceResponse;
27  
28  import org.springframework.util.Assert;
29  import org.springframework.web.context.request.WebRequestInterceptor;
30  import org.springframework.web.portlet.HandlerInterceptor;
31  import org.springframework.web.portlet.ModelAndView;
32  import org.springframework.web.portlet.context.PortletWebRequest;
33  
34  /**
35   * Adapter that implements the Portlet HandlerInterceptor interface
36   * and wraps an underlying WebRequestInterceptor.
37   *
38   * <p><b>NOTE:</b> The WebRequestInterceptor is by default only applied to the Portlet
39   * <b>render</b> phase, which is dealing with preparing and rendering a Portlet view.
40   * The Portlet action phase will only be intercepted with WebRequestInterceptor calls
41   * if the {@code renderPhaseOnly} flag is explicitly set to {@code false}.
42   * In general, it is recommended to use the Portlet-specific HandlerInterceptor
43   * mechanism for differentiating between action and render interception.
44   *
45   * @author Juergen Hoeller
46   * @since 2.0
47   * @see org.springframework.web.context.request.WebRequestInterceptor
48   * @see org.springframework.web.portlet.HandlerInterceptor
49   */
50  public class WebRequestHandlerInterceptorAdapter implements HandlerInterceptor {
51  
52  	private final WebRequestInterceptor requestInterceptor;
53  
54  	private final boolean renderPhaseOnly;
55  
56  
57  	/**
58  	 * Create a new WebRequestHandlerInterceptorAdapter for the given WebRequestInterceptor,
59  	 * applying to the render phase only.
60  	 * @param requestInterceptor the WebRequestInterceptor to wrap
61  	 */
62  	public WebRequestHandlerInterceptorAdapter(WebRequestInterceptor requestInterceptor) {
63  		this(requestInterceptor, true);
64  	}
65  
66  	/**
67  	 * Create a new WebRequestHandlerInterceptorAdapter for the given WebRequestInterceptor.
68  	 * @param requestInterceptor the WebRequestInterceptor to wrap
69  	 * @param renderPhaseOnly whether to apply to the render phase only ({@code true})
70  	 * or to the action phase as well ({@code false})
71  	 */
72  	public WebRequestHandlerInterceptorAdapter(WebRequestInterceptor requestInterceptor, boolean renderPhaseOnly) {
73  		Assert.notNull(requestInterceptor, "WebRequestInterceptor must not be null");
74  		this.requestInterceptor = requestInterceptor;
75  		this.renderPhaseOnly = renderPhaseOnly;
76  	}
77  
78  
79  	@Override
80  	public boolean preHandleAction(ActionRequest request, ActionResponse response, Object handler) throws Exception {
81  		if (!this.renderPhaseOnly) {
82  			this.requestInterceptor.preHandle(new PortletWebRequest(request));
83  		}
84  		return true;
85  	}
86  
87  	@Override
88  	public void afterActionCompletion(
89  			ActionRequest request, ActionResponse response, Object handler, Exception ex) throws Exception {
90  
91  		if (!this.renderPhaseOnly) {
92  			this.requestInterceptor.afterCompletion(new PortletWebRequest(request), ex);
93  		}
94  	}
95  
96  	@Override
97  	public boolean preHandleRender(RenderRequest request, RenderResponse response, Object handler) throws Exception {
98  		this.requestInterceptor.preHandle(new PortletWebRequest(request));
99  		return true;
100 	}
101 
102 	@Override
103 	public void postHandleRender(
104 			RenderRequest request, RenderResponse response, Object handler, ModelAndView modelAndView) throws Exception {
105 
106 		this.requestInterceptor.postHandle(new PortletWebRequest(request),
107 				(modelAndView != null && !modelAndView.wasCleared() ? modelAndView.getModelMap() : null));
108 	}
109 
110 	@Override
111 	public void afterRenderCompletion(
112 			RenderRequest request, RenderResponse response, Object handler, Exception ex) throws Exception {
113 
114 		this.requestInterceptor.afterCompletion(new PortletWebRequest(request), ex);
115 	}
116 
117 	@Override
118 	public boolean preHandleResource(ResourceRequest request, ResourceResponse response, Object handler)
119 			throws Exception {
120 
121 		this.requestInterceptor.preHandle(new PortletWebRequest(request));
122 		return true;
123 	}
124 
125 	@Override
126 	public void postHandleResource(ResourceRequest request, ResourceResponse response, Object handler, ModelAndView modelAndView)
127 			throws Exception {
128 
129 		this.requestInterceptor.postHandle(new PortletWebRequest(request),
130 				(modelAndView != null ? modelAndView.getModelMap() : null));
131 	}
132 
133 	@Override
134 	public void afterResourceCompletion(ResourceRequest request, ResourceResponse response, Object handler,
135 			Exception ex) throws Exception {
136 
137 		this.requestInterceptor.afterCompletion(new PortletWebRequest(request), ex);
138 	}
139 
140 	@Override
141 	public boolean preHandleEvent(EventRequest request, EventResponse response, Object handler) throws Exception {
142 		this.requestInterceptor.preHandle(new PortletWebRequest(request));
143 		return true;
144 	}
145 
146 	@Override
147 	public void afterEventCompletion(EventRequest request, EventResponse response, Object handler, Exception ex)
148 			throws Exception {
149 
150 		this.requestInterceptor.afterCompletion(new PortletWebRequest(request), ex);
151 	}
152 
153 }