1 16 17 package org.springframework.web.portlet.util; 18 19 import java.io.File ; 20 import java.io.FileNotFoundException ; 21 import java.util.Enumeration ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import java.util.TreeMap ; 26 27 import javax.portlet.ActionRequest; 28 import javax.portlet.ActionResponse; 29 import javax.portlet.PortletContext; 30 import javax.portlet.PortletRequest; 31 import javax.portlet.PortletSession; 32 33 import org.springframework.util.Assert; 34 import org.springframework.web.util.WebUtils; 35 36 45 public abstract class PortletUtils { 46 47 54 public static File getTempDir(PortletContext portletContext) { 55 Assert.notNull(portletContext, "PortletContext must not be null"); 56 return (File ) portletContext.getAttribute(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE); 57 } 58 59 75 public static String getRealPath(PortletContext portletContext, String path) throws FileNotFoundException { 76 Assert.notNull(portletContext, "PortletContext must not be null"); 77 if (!path.startsWith("/")) { 79 path = "/" + path; 80 } 81 String realPath = portletContext.getRealPath(path); 82 if (realPath == null) { 83 throw new FileNotFoundException ( 84 "PortletContext resource [" + path + "] cannot be resolved to absolute file path - " + 85 "web application archive not expanded?"); 86 } 87 return realPath; 88 } 89 90 91 101 public static Object getSessionAttribute(PortletRequest request, String name) { 102 return getSessionAttribute(request, name, PortletSession.PORTLET_SCOPE); 103 } 104 105 115 public static Object getSessionAttribute(PortletRequest request, String name, int scope) { 116 Assert.notNull(request, "Request must not be null"); 117 PortletSession session = request.getPortletSession(false); 118 return (session != null ? session.getAttribute(name, scope) : null); 119 } 120 121 131 public static Object getRequiredSessionAttribute(PortletRequest request, String name) 132 throws IllegalStateException { 133 return getRequiredSessionAttribute(request, name, PortletSession.PORTLET_SCOPE); 134 } 135 136 147 public static Object getRequiredSessionAttribute(PortletRequest request, String name, int scope) 148 throws IllegalStateException { 149 Object attr = getSessionAttribute(request, name, scope); 150 if (attr == null) { 151 throw new IllegalStateException ("No session attribute '" + name + "' found"); 152 } 153 return attr; 154 } 155 156 165 public static void setSessionAttribute(PortletRequest request, String name, Object value) { 166 setSessionAttribute(request, name, value, PortletSession.PORTLET_SCOPE); 167 } 168 169 179 public static void setSessionAttribute(PortletRequest request, String name, Object value, int scope) { 180 Assert.notNull(request, "Request must not be null"); 181 if (value != null) { 182 request.getPortletSession().setAttribute(name, value, scope); 183 } 184 else { 185 PortletSession session = request.getPortletSession(false); 186 if (session != null) { 187 session.removeAttribute(name, scope); 188 } 189 } 190 } 191 192 203 public static Object getOrCreateSessionAttribute(PortletSession session, String name, Class clazz) 204 throws IllegalArgumentException { 205 return getOrCreateSessionAttribute(session, name, clazz, PortletSession.PORTLET_SCOPE); 206 } 207 208 220 public static Object getOrCreateSessionAttribute(PortletSession session, String name, Class clazz, int scope) 221 throws IllegalArgumentException { 222 Assert.notNull(session, "Session must not be null"); 223 Object sessionObject = session.getAttribute(name, scope); 224 if (sessionObject == null) { 225 Assert.notNull(clazz, "Class must not be null if attribute value is to be instantiated"); 226 try { 227 sessionObject = clazz.newInstance(); 228 } 229 catch (InstantiationException ex) { 230 throw new IllegalArgumentException ( 231 "Could not instantiate class [" + clazz.getName() + 232 "] for session attribute '" + name + "': " + ex.getMessage()); 233 } 234 catch (IllegalAccessException ex) { 235 throw new IllegalArgumentException ( 236 "Could not access default constructor of class [" + clazz.getName() + 237 "] for session attribute '" + name + "': " + ex.getMessage()); 238 } 239 session.setAttribute(name, sessionObject, scope); 240 } 241 return sessionObject; 242 } 243 244 267 public static Object getSessionMutex(PortletSession session) { 268 Assert.notNull(session, "Session must not be null"); 269 Object mutex = session.getAttribute(WebUtils.SESSION_MUTEX_ATTRIBUTE); 270 if (mutex == null) { 271 mutex = session; 272 } 273 return mutex; 274 } 275 276 284 public static void exposeRequestAttributes(PortletRequest request, Map attributes) 285 throws IllegalArgumentException { 286 Assert.notNull(request, "Request must not be null"); 287 Assert.notNull(attributes, "attributes Map must not be null"); 288 Iterator it = attributes.entrySet().iterator(); 289 while (it.hasNext()) { 290 Map.Entry entry = (Map.Entry ) it.next(); 291 if (!(entry.getKey() instanceof String )) { 292 throw new IllegalArgumentException ( 293 "Invalid key [" + entry.getKey() + "] in attributes Map - only Strings allowed as attribute keys"); 294 } 295 request.setAttribute((String ) entry.getKey(), entry.getValue()); 296 } 297 } 298 299 308 public static boolean hasSubmitParameter(PortletRequest request, String name) { 309 return getSubmitParameter(request, name) != null; 310 } 311 312 321 public static String getSubmitParameter(PortletRequest request, String name) { 322 Assert.notNull(request, "Request must not be null"); 323 if (request.getParameter(name) != null) { 324 return name; 325 } 326 for (int i = 0; i < WebUtils.SUBMIT_IMAGE_SUFFIXES.length; i++) { 327 String suffix = WebUtils.SUBMIT_IMAGE_SUFFIXES[i]; 328 String parameter = name + suffix; 329 if (request.getParameter(parameter) != null) { 330 return parameter; 331 } 332 } 333 return null; 334 } 335 336 353 public static Map getParametersStartingWith(PortletRequest request, String prefix) { 354 Assert.notNull(request, "Request must not be null"); 355 Enumeration paramNames = request.getParameterNames(); 356 Map params = new TreeMap (); 357 if (prefix == null) { 358 prefix = ""; 359 } 360 while (paramNames != null && paramNames.hasMoreElements()) { 361 String paramName = (String ) paramNames.nextElement(); 362 if ("".equals(prefix) || paramName.startsWith(prefix)) { 363 String unprefixed = paramName.substring(prefix.length()); 364 String [] values = request.getParameterValues(paramName); 365 if (values == null) { 366 } 368 else if (values.length > 1) { 369 params.put(unprefixed, values); 370 } 371 else { 372 params.put(unprefixed, values[0]); 373 } 374 } 375 } 376 return params; 377 } 378 379 380 388 public static void passAllParametersToRenderPhase(ActionRequest request, ActionResponse response) { 389 try { 390 Enumeration en = request.getParameterNames(); 391 while (en.hasMoreElements()) { 392 String param = (String ) en.nextElement(); 393 String values[] = request.getParameterValues(param); 394 response.setRenderParameter(param, values); 395 } 396 } 397 catch (IllegalStateException ex) { 398 } 400 } 401 402 409 public static void clearAllRenderParameters(ActionResponse response) { 410 try { 411 response.setRenderParameters(new HashMap ()); 412 } 413 catch (IllegalStateException ex) { 414 } 416 } 417 418 } 419 | Popular Tags |