001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.web.portlet.handler;
018:
019: import javax.portlet.ActionRequest;
020: import javax.portlet.ActionResponse;
021: import javax.portlet.RenderRequest;
022: import javax.portlet.RenderResponse;
023:
024: import org.springframework.util.Assert;
025: import org.springframework.web.context.request.WebRequestInterceptor;
026: import org.springframework.web.portlet.HandlerInterceptor;
027: import org.springframework.web.portlet.ModelAndView;
028: import org.springframework.web.portlet.context.PortletWebRequest;
029:
030: /**
031: * Adapter that implements the Portlet HandlerInterceptor interface
032: * and wraps an underlying WebRequestInterceptor.
033: *
034: * <p><b>NOTE:</b> The WebRequestInterceptor is by default only applied to the Portlet
035: * <b>render</b> phase, which is dealing with preparing and rendering a Portlet view.
036: * The Portlet action phase will only be intercepted with WebRequestInterceptor calls
037: * if the <code>renderPhaseOnly</code> flag is explicitly set to <code>false</code>.
038: * In general, it is recommended to use the Portlet-specific HandlerInterceptor
039: * mechanism for differentiating between action and render interception.
040: *
041: * @author Juergen Hoeller
042: * @since 2.0
043: * @see org.springframework.web.context.request.WebRequestInterceptor
044: * @see org.springframework.web.portlet.HandlerInterceptor
045: */
046: public class WebRequestHandlerInterceptorAdapter implements
047: HandlerInterceptor {
048:
049: private final WebRequestInterceptor requestInterceptor;
050:
051: private final boolean renderPhaseOnly;
052:
053: /**
054: * Create a new WebRequestHandlerInterceptorAdapter for the given WebRequestInterceptor,
055: * applying to the render phase only.
056: * @param requestInterceptor the WebRequestInterceptor to wrap
057: */
058: public WebRequestHandlerInterceptorAdapter(
059: WebRequestInterceptor requestInterceptor) {
060: this (requestInterceptor, true);
061: }
062:
063: /**
064: * Create a new WebRequestHandlerInterceptorAdapter for the given WebRequestInterceptor.
065: * @param requestInterceptor the WebRequestInterceptor to wrap
066: * @param renderPhaseOnly whether to apply to the render phase only (<code>true</code>)
067: * or to the action phase as well (<code>false</code>)
068: */
069: public WebRequestHandlerInterceptorAdapter(
070: WebRequestInterceptor requestInterceptor,
071: boolean renderPhaseOnly) {
072: Assert.notNull(requestInterceptor,
073: "WebRequestInterceptor must not be null");
074: this .requestInterceptor = requestInterceptor;
075: this .renderPhaseOnly = renderPhaseOnly;
076: }
077:
078: public boolean preHandleAction(ActionRequest request,
079: ActionResponse response, Object handler) throws Exception {
080: if (!this .renderPhaseOnly) {
081: this .requestInterceptor.preHandle(new PortletWebRequest(
082: request));
083: }
084: return true;
085: }
086:
087: public void afterActionCompletion(ActionRequest request,
088: ActionResponse response, Object handler, Exception ex)
089: throws Exception {
090:
091: if (!this .renderPhaseOnly) {
092: this .requestInterceptor.afterCompletion(
093: new PortletWebRequest(request), ex);
094: }
095: }
096:
097: public boolean preHandleRender(RenderRequest request,
098: RenderResponse response, Object handler) throws Exception {
099: this .requestInterceptor
100: .preHandle(new PortletWebRequest(request));
101: return true;
102: }
103:
104: public void postHandleRender(RenderRequest request,
105: RenderResponse response, Object handler,
106: ModelAndView modelAndView) throws Exception {
107:
108: this .requestInterceptor.postHandle(new PortletWebRequest(
109: request), (modelAndView != null ? modelAndView
110: .getModelMap() : null));
111: }
112:
113: public void afterRenderCompletion(RenderRequest request,
114: RenderResponse response, Object handler, Exception ex)
115: throws Exception {
116:
117: this .requestInterceptor.afterCompletion(new PortletWebRequest(
118: request), ex);
119: }
120:
121: }
|