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; 021 022/** 023 * Interceptor to forward a request parameter from the {@code ActionRequest} to the 024 * {@code RenderRequest}. 025 * 026 * <p>This can be useful when using {@link ParameterHandlerMapping ParameterHandlerMapping} 027 * or {@link PortletModeParameterHandlerMapping PortletModeParameterHandlerMapping}. 028 * It will ensure that the parameter that was used to map the {@code ActionRequest} 029 * to a handler will be forwarded to the {@code RenderRequest} so that it will also be 030 * mapped the same way. 031 * 032 * <p>When using this Interceptor, you can still change the value of the mapping parameter 033 * in your handler in order to change where the render request will get mapped. 034 * 035 * <p>Be aware that this Interceptor does call {@code ActionResponse.setRenderParameter}, 036 * which means that you will not be able to call {@code ActionResponse.sendRedirect} in 037 * your handler. If you may need to issue a redirect, then you should avoid this Interceptor 038 * and either write a different one that does this in a different way, or manually forward 039 * the parameter from within your handler(s). 040 * 041 * <p>Thanks to Rainer Schmitz for suggesting this mapping strategy! 042 * 043 * @author John A. Lewis 044 * @since 2.0 045 * @see ParameterHandlerMapping 046 * @see PortletModeParameterHandlerMapping 047 */ 048public class ParameterMappingInterceptor extends HandlerInterceptorAdapter { 049 050 /** Request parameter name to use for mapping to handlers */ 051 public final static String DEFAULT_PARAMETER_NAME = "action"; 052 053 private String parameterName = DEFAULT_PARAMETER_NAME; 054 055 056 /** 057 * Set the name of the parameter used for mapping. 058 */ 059 public void setParameterName(String parameterName) { 060 this.parameterName = (parameterName != null ? parameterName : DEFAULT_PARAMETER_NAME); 061 } 062 063 064 /** 065 * If request is an {@link javax.portlet.ActionRequest ActionRequest}, 066 * get handler mapping parameter and add it to the ActionResponse. 067 */ 068 @Override 069 public boolean preHandleAction(ActionRequest request, ActionResponse response, Object handler) { 070 String mappingParameter = request.getParameter(this.parameterName); 071 if (mappingParameter != null) { 072 response.setRenderParameter(parameterName, mappingParameter); 073 } 074 return true; 075 } 076 077}