001/* 002 * Copyright 2002-2012 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 * https://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 017package org.springframework.web.portlet.handler; 018 019import javax.portlet.ActionRequest; 020import javax.portlet.ActionResponse; 021import javax.portlet.EventRequest; 022import javax.portlet.EventResponse; 023import javax.portlet.RenderRequest; 024import javax.portlet.RenderResponse; 025import javax.portlet.ResourceRequest; 026import javax.portlet.ResourceResponse; 027 028import org.springframework.util.Assert; 029import org.springframework.web.context.request.WebRequestInterceptor; 030import org.springframework.web.portlet.HandlerInterceptor; 031import org.springframework.web.portlet.ModelAndView; 032import org.springframework.web.portlet.context.PortletWebRequest; 033 034/** 035 * Adapter that implements the Portlet HandlerInterceptor interface 036 * and wraps an underlying WebRequestInterceptor. 037 * 038 * <p><b>NOTE:</b> The WebRequestInterceptor is by default only applied to the Portlet 039 * <b>render</b> phase, which is dealing with preparing and rendering a Portlet view. 040 * The Portlet action phase will only be intercepted with WebRequestInterceptor calls 041 * if the {@code renderPhaseOnly} flag is explicitly set to {@code false}. 042 * In general, it is recommended to use the Portlet-specific HandlerInterceptor 043 * mechanism for differentiating between action and render interception. 044 * 045 * @author Juergen Hoeller 046 * @since 2.0 047 * @see org.springframework.web.context.request.WebRequestInterceptor 048 * @see org.springframework.web.portlet.HandlerInterceptor 049 */ 050public class WebRequestHandlerInterceptorAdapter implements HandlerInterceptor { 051 052 private final WebRequestInterceptor requestInterceptor; 053 054 private final boolean renderPhaseOnly; 055 056 057 /** 058 * Create a new WebRequestHandlerInterceptorAdapter for the given WebRequestInterceptor, 059 * applying to the render phase only. 060 * @param requestInterceptor the WebRequestInterceptor to wrap 061 */ 062 public WebRequestHandlerInterceptorAdapter(WebRequestInterceptor requestInterceptor) { 063 this(requestInterceptor, true); 064 } 065 066 /** 067 * Create a new WebRequestHandlerInterceptorAdapter for the given WebRequestInterceptor. 068 * @param requestInterceptor the WebRequestInterceptor to wrap 069 * @param renderPhaseOnly whether to apply to the render phase only ({@code true}) 070 * or to the action phase as well ({@code false}) 071 */ 072 public WebRequestHandlerInterceptorAdapter(WebRequestInterceptor requestInterceptor, boolean renderPhaseOnly) { 073 Assert.notNull(requestInterceptor, "WebRequestInterceptor must not be null"); 074 this.requestInterceptor = requestInterceptor; 075 this.renderPhaseOnly = renderPhaseOnly; 076 } 077 078 079 @Override 080 public boolean preHandleAction(ActionRequest request, ActionResponse response, Object handler) throws Exception { 081 if (!this.renderPhaseOnly) { 082 this.requestInterceptor.preHandle(new PortletWebRequest(request)); 083 } 084 return true; 085 } 086 087 @Override 088 public void afterActionCompletion( 089 ActionRequest request, ActionResponse response, Object handler, Exception ex) throws Exception { 090 091 if (!this.renderPhaseOnly) { 092 this.requestInterceptor.afterCompletion(new PortletWebRequest(request), ex); 093 } 094 } 095 096 @Override 097 public boolean preHandleRender(RenderRequest request, RenderResponse response, Object handler) throws Exception { 098 this.requestInterceptor.preHandle(new PortletWebRequest(request)); 099 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}