1 16 17 package org.springframework.web.portlet.handler; 18 19 import java.util.HashMap ; 20 import java.util.HashSet ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 import java.util.Set ; 24 25 import javax.portlet.PortletMode; 26 import javax.portlet.PortletRequest; 27 28 import org.springframework.beans.BeansException; 29 import org.springframework.util.Assert; 30 import org.springframework.util.CollectionUtils; 31 import org.springframework.util.ObjectUtils; 32 33 86 public class PortletModeParameterHandlerMapping extends AbstractMapBasedHandlerMapping { 87 88 91 public final static String DEFAULT_PARAMETER_NAME = "action"; 92 93 94 private String parameterName = DEFAULT_PARAMETER_NAME; 95 96 private Map portletModeParameterMap; 97 98 protected final Map modeHandlerMap = new HashMap (); 99 100 private boolean allowDuplicateParameters = false; 101 102 private Set parametersUsed = new HashSet (); 103 104 105 109 public void setParameterName(String parameterName) { 110 Assert.hasText(parameterName, "'parameterName' must not be empty"); 111 this.parameterName = parameterName; 112 } 113 114 120 public void setPortletModeParameterMap(Map portletModeParameterMap) { 121 this.portletModeParameterMap = portletModeParameterMap; 122 } 123 124 133 public void setAllowDuplicateParameters(boolean allowDuplicateParameters) { 134 this.allowDuplicateParameters = allowDuplicateParameters; 135 } 136 137 138 143 public void initApplicationContext() throws BeansException { 144 super.initApplicationContext(); 145 registerHandlers(this.portletModeParameterMap); 146 } 147 148 153 protected void registerHandlers(Map portletModeParameterMap) throws BeansException { 154 if (CollectionUtils.isEmpty(portletModeParameterMap)) { 155 logger.warn("'portletModeParameterMap' not set on PortletModeParameterHandlerMapping"); 156 } 157 else { 158 for (Iterator it = portletModeParameterMap.entrySet().iterator(); it.hasNext();) { 159 Map.Entry entry = (Map.Entry ) it.next(); 160 String modeKey = (String ) entry.getKey(); 161 PortletMode mode = new PortletMode(modeKey); 162 Object parameterMap = entry.getValue(); 163 if (!(parameterMap instanceof Map )) { 164 throw new IllegalArgumentException ( 165 "The value for the portlet mode must be a Map of parameter Strings to handler Objects"); 166 } 167 registerHandler(mode, (Map ) parameterMap); 168 } 169 } 170 } 171 172 177 protected void registerHandler(PortletMode mode, Map parameterMap) throws BeansException { 178 for (Iterator it = parameterMap.entrySet().iterator(); it.hasNext();) { 179 Map.Entry entry = (Map.Entry ) it.next(); 180 String parameter = (String ) entry.getKey(); 181 Object handler = entry.getValue(); 182 registerHandler(mode, parameter, handler); 183 } 184 } 185 186 196 protected void registerHandler(PortletMode mode, String parameter, Object handler) 197 throws BeansException, IllegalStateException { 198 199 if (!this.allowDuplicateParameters && this.parametersUsed.contains(parameter)) { 201 throw new IllegalStateException ( 202 "Duplicate entries for parameter [" + parameter + "] in different Portlet modes"); 203 } 204 this.parametersUsed.add(parameter); 205 206 registerHandler(new LookupKey(mode, parameter), handler); 207 } 208 209 210 216 protected Object getLookupKey(PortletRequest request) throws Exception { 217 PortletMode mode = request.getPortletMode(); 218 String parameter = request.getParameter(this.parameterName); 219 return new LookupKey(mode, parameter); 220 } 221 222 223 226 private static class LookupKey { 227 228 private final PortletMode mode; 229 230 private final String parameter; 231 232 public LookupKey(PortletMode portletMode, String parameter) { 233 this.mode = portletMode; 234 this.parameter = parameter; 235 } 236 237 public boolean equals(Object other) { 238 if (this == other) { 239 return true; 240 } 241 if (!(other instanceof LookupKey)) { 242 return false; 243 } 244 LookupKey otherKey = (LookupKey) other; 245 return (this.mode.equals(otherKey.mode) && 246 ObjectUtils.nullSafeEquals(this.parameter, otherKey.parameter)); 247 } 248 249 public int hashCode() { 250 return (this.mode.hashCode() * 29 + ObjectUtils.nullSafeHashCode(this.parameter)); 251 } 252 253 public String toString() { 254 return "Portlet mode '" + this.mode + "', parameter '" + this.parameter + "'"; 255 } 256 } 257 258 } 259 | Popular Tags |