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 java.util.HashMap; 020import java.util.Map; 021import java.util.Properties; 022import javax.portlet.PortletMode; 023import javax.portlet.PortletRequest; 024 025import org.springframework.beans.BeansException; 026import org.springframework.util.Assert; 027import org.springframework.util.CollectionUtils; 028 029/** 030 * Implementation of the {@link org.springframework.web.portlet.HandlerMapping} 031 * interface to map from the current PortletMode to request handler beans. 032 * 033 * <p>The bean configuration for this mapping will look something like this: 034 * <pre class="code"> 035 * <bean id="portletModeHandlerMapping" class="org.springframework.web.portlet.handler.PortletModeHandlerMapping"> 036 * <property name="portletModeMap"> 037 * <map> 038 * <entry key="view"><ref bean="viewHandler"/></entry> 039 * <entry key="edit"><ref bean="editHandler"/></entry> 040 * <entry key="help"><ref bean="helpHandler"/></entry> 041 * </map> 042 * </property> 043 * </bean> 044 * </pre> 045 * 046 * @author William G. Thompson, Jr. 047 * @author John A. Lewis 048 * @since 2.0 049 */ 050public class PortletModeHandlerMapping extends AbstractMapBasedHandlerMapping<PortletMode> { 051 052 private final Map<String, Object> portletModeMap = new HashMap<String, Object>(); 053 054 055 /** 056 * Set PortletMode to handler bean name mappings from a Properties object. 057 * @param mappings properties with PortletMode names as keys and bean names as values 058 */ 059 public void setMappings(Properties mappings) { 060 CollectionUtils.mergePropertiesIntoMap(mappings, this.portletModeMap); 061 } 062 063 /** 064 * Set a Map with PortletModes as keys and handler beans as values. 065 * Convenient for population with bean references. 066 * @param portletModeMap map with PortletMode names as keys and beans or bean names as values 067 */ 068 public void setPortletModeMap(Map<String, ?> portletModeMap) { 069 this.portletModeMap.putAll(portletModeMap); 070 } 071 072 073 /** 074 * Calls the {@code registerHandlers} method in addition 075 * to the superclass's initialization. 076 * @see #registerHandlers 077 */ 078 @Override 079 public void initApplicationContext() throws BeansException { 080 super.initApplicationContext(); 081 registerHandlersByMode(this.portletModeMap); 082 } 083 084 /** 085 * Register all handlers specified in the Portlet mode map for the corresponding modes. 086 * @param portletModeMap Map with mode names as keys and handler beans or bean names as values 087 */ 088 protected void registerHandlersByMode(Map<String, Object> portletModeMap) { 089 Assert.notNull(portletModeMap, "'portletModeMap' must not be null"); 090 for (Map.Entry<String, Object> entry : portletModeMap.entrySet()) { 091 registerHandler(new PortletMode(entry.getKey()), entry.getValue()); 092 } 093 } 094 095 096 /** 097 * Uses the current PortletMode as lookup key. 098 */ 099 @Override 100 protected PortletMode getLookupKey(PortletRequest request) throws Exception { 101 return request.getPortletMode(); 102 } 103 104}