001: /*
002: * Copyright 2002-2007 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 java.util.Map;
020:
021: import javax.portlet.PortletRequest;
022:
023: import org.springframework.beans.BeansException;
024: import org.springframework.util.Assert;
025: import org.springframework.util.CollectionUtils;
026:
027: /**
028: * Implementation of the HandlerMapping interface to map from a request
029: * parameter to request handler beans.
030: *
031: * <p>The default name of the parameter is "action", but can be changed using
032: * {@link #setParameterName setParameterName()}.
033: *
034: * <p>The bean configuration for this mapping will look somthing like this:
035: *
036: * <pre class="code">
037: * <bean id="parameterHandlerMapping" class="org.springframework.web.portlet.handler.ParameterHandlerMapping">
038: * <property name="parameterMap">
039: * <map>
040: * <entry key="add"><ref bean="addItemHandler"/></entry>
041: * <entry key="edit"><ref bean="editItemHandler"/></entry>
042: * <entry key="delete"><ref bean="deleteItemHandler"/></entry>
043: * </map>
044: * </property>
045: * </bean></pre>
046: *
047: * Thanks to Rainer Schmitz for suggesting this mapping strategy!
048: *
049: * @author John A. Lewis
050: * @author Juergen Hoeller
051: * @since 2.0
052: * @see ParameterMappingInterceptor
053: */
054: public class ParameterHandlerMapping extends
055: AbstractMapBasedHandlerMapping {
056:
057: /**
058: * Default request parameter name to use for mapping to handlers: "action".
059: */
060: public final static String DEFAULT_PARAMETER_NAME = "action";
061:
062: private String parameterName = DEFAULT_PARAMETER_NAME;
063:
064: private Map parameterMap;
065:
066: /**
067: * Set the name of the parameter used for mapping to handlers.
068: * <p>Default is "action".
069: */
070: public void setParameterName(String parameterName) {
071: Assert.hasText(parameterName,
072: "'parameterName' must not be empty");
073: this .parameterName = parameterName;
074: }
075:
076: /**
077: * Set a Map with parameters as keys and handler beans or bean names as values.
078: * Convenient for population with bean references.
079: * @param parameterMap map with parameters as keys and beans as values
080: */
081: public void setParameterMap(Map parameterMap) {
082: this .parameterMap = parameterMap;
083: }
084:
085: /**
086: * Calls the <code>registerHandlers</code> method in addition
087: * to the superclass's initialization.
088: * @see #registerHandlers
089: */
090: public void initApplicationContext() throws BeansException {
091: super .initApplicationContext();
092: registerHandlers(this .parameterMap);
093: }
094:
095: /**
096: * Register all handlers specified in the Portlet mode map for the corresponding modes.
097: * @param parameterMap Map with parameter names as keys and handler beans or bean names as values
098: * @throws BeansException if the handler couldn't be registered
099: */
100: protected void registerHandlers(Map parameterMap)
101: throws BeansException {
102: if (CollectionUtils.isEmpty(parameterMap)) {
103: logger
104: .warn("'parameterMap' is empty on ParameterHandlerMapping");
105: } else {
106: super .registerHandlers(parameterMap);
107: }
108: }
109:
110: /**
111: * Uses the value of the specified parameter as lookup key.
112: * @see #setParameterName
113: */
114: protected Object getLookupKey(PortletRequest request)
115: throws Exception {
116: return request.getParameter(this.parameterName);
117: }
118:
119: }
|