1 23 package org.infoglue.deliver.portal; 24 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.Map ; 28 29 import javax.portlet.PortletMode; 30 import javax.portlet.WindowState; 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletRequestWrapper ; 33 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 import org.apache.pluto.om.window.PortletWindow; 37 38 44 public class PortalControlURL { 45 private static final Log log = LogFactory.getLog(PortalControlURL.class); 46 47 public static final String IG = "_ig_"; 48 public static final String RENDER = "_rp_"; 49 public static final String ACTION = "_ac"; 50 public static final String PID = "_pid"; 51 public static final String MULTI_VALUE = "__"; 52 53 private String contextPath; 54 private String realPath; 55 private String webWorkAction; 56 private Map queryParams; private Map pathParams; 58 private String local; 60 65 public PortalControlURL(HttpServletRequest req) { 66 while (req instanceof HttpServletRequestWrapper ) { 68 req = (HttpServletRequest ) ((HttpServletRequestWrapper ) req).getRequest(); 69 } 70 this.contextPath = req.getContextPath(); 71 72 String path = req.getRequestURI(); 73 if (contextPath != null && path.length() > 0) { 74 path = path.substring(contextPath.length(), path.length()); 75 } 76 77 if (path.endsWith(".action")) { 78 int last = path.lastIndexOf("/"); 79 this.webWorkAction = path.substring(last + 1); 80 path = path.substring(0, last); 81 } 82 83 this.realPath = PathParser.getRealPath(path); 84 this.queryParams = PathParser.copyParameters(req.getParameterMap()); 85 this.pathParams = PathParser.parsePathParameters("_", path, true); 86 log.debug("realPath: " + realPath); 87 log.debug("wwAction: " + webWorkAction); 88 } 89 90 95 public PortalControlURL(PortalControlURL url) { 96 this.contextPath = url.contextPath; 97 this.realPath = url.realPath; 98 this.webWorkAction = url.webWorkAction; 99 this.queryParams = new HashMap (url.queryParams.size()); 100 this.queryParams.putAll(url.queryParams); 101 this.pathParams = new HashMap (url.pathParams.size()); 102 this.pathParams.putAll(url.pathParams); 103 } 104 105 public PortletMode getPortletMode(PortletWindow window) { 106 return PortletMode.VIEW; 108 } 109 public PortletMode getPreviousPortletMode(PortletWindow portletWindow) { 110 return PortletMode.VIEW; 112 } 113 114 public WindowState getWindowState(PortletWindow portletWindow) { 115 return WindowState.NORMAL; 117 } 118 119 public WindowState getPreviousWindowState(PortletWindow portletWindow) { 120 return WindowState.NORMAL; 122 } 123 124 public void setPortletMode(PortletWindow window, PortletMode mode) { 125 } 127 128 public void setPortletWindowState(PortletWindow window, WindowState state) { 129 } 131 132 public void setPortletId(PortletWindow window) { 133 setPathParameter(PID, new String [] { window.getId().toString()}); 134 } 135 136 138 public void setPathParameter(String nsName, String [] values) { 139 if (values == null || values.length == 0) { 140 pathParams.remove(nsName); 141 } else { 142 pathParams.put(nsName, values); 143 } 144 } 145 146 148 153 public String getActionWindowID() { 154 String [] values = (String []) pathParams.get(ACTION); 155 if (values != null && values.length > 0) { 156 return values[0]; 157 } 158 return null; 159 } 160 161 public void setActionParameter(PortletWindow window) { 162 setPathParameter(ACTION, new String [] { window.getId().toString()}); 163 } 164 165 public void clearActionParameter() { 166 pathParams.remove(ACTION); 167 pathParams.remove(PID); 168 } 169 170 171 173 179 public Map getRenderParameterMap(PortletWindow window) { 180 Map result = new HashMap (); 181 int nsSize = RENDER.length() + window.getId().toString().length() + 1; 182 for (Iterator iter = pathParams.keySet().iterator(); iter.hasNext();) { 183 String name = (String ) iter.next(); 184 if (name.startsWith(RENDER + window.getId())) { 185 String paramName = name.substring(nsSize); 186 result.put(paramName, pathParams.get(name)); 187 } 188 } 189 return result; 190 } 191 192 199 public void setRenderParameter(PortletWindow window, String name, String [] values) { 200 String nsName = RENDER + window.getId() + "_" + name; 201 setPathParameter(nsName, values); 202 } 203 204 209 public void clearRenderParameters(PortletWindow window) { 210 for (Iterator iter = pathParams.keySet().iterator(); iter.hasNext();) { 211 String name = (String ) iter.next(); 212 if (name.startsWith(RENDER + window.getId())) { 213 iter.remove(); 215 } 216 } 217 } 218 219 221 227 public Map getQueryParameterMap(PortletWindow window) { 228 return queryParams; 229 } 230 231 237 public Map getQueryParameterMap() { 238 return queryParams; 239 } 240 241 249 public void setQueryParameter(PortletWindow window, String name, String [] values) { 250 String nsName = name; if (values == null || values.length == 0) { 252 queryParams.remove(nsName); 253 } else { 254 queryParams.put(nsName, values); 255 } 256 } 257 258 public void clearQueryParameters() { 259 queryParams.clear(); 260 } 261 262 268 public String toString() { 269 StringBuffer buf = new StringBuffer (); 270 271 buf.append(contextPath); 273 274 if (realPath != null) { 276 buf.append(realPath); 277 } 278 279 if (!pathParams.isEmpty()) { 281 buf.append("/"); 282 for (Iterator iter = pathParams.keySet().iterator(); iter.hasNext();) { 283 String name = (String ) iter.next(); 284 String [] values = (String []) pathParams.get(name); 285 buf.append(name); 286 buf.append("/"); 287 buf.append(PathParser.encodeValues(values)); 288 289 if (iter.hasNext()) { 290 buf.append("/"); 291 } 292 } 293 } 294 295 if (webWorkAction != null) { 297 buf.append("/"); 298 buf.append(webWorkAction); 299 } 300 301 if (!queryParams.isEmpty()) { 303 buf.append("?"); 304 for (Iterator iter = queryParams.keySet().iterator(); iter.hasNext();) { 305 String name = (String ) iter.next(); 307 String [] values = (String []) queryParams.get(name); 308 for (int i = 0; i < values.length; i++) { 309 buf.append(name); 311 buf.append("="); 312 buf.append(values[i]); 313 if (i < values.length - 1) { 314 buf.append("&"); 315 } 316 } 317 if (iter.hasNext()) { 318 buf.append("&"); 319 } 320 } 321 } 322 323 if (local != null && local.length() > 0) { 325 buf.append("#"); 326 buf.append(local); 327 } 328 if (log.isDebugEnabled()) { 329 log.debug("Generated URL: " + buf.toString()); 330 } 331 return buf.toString(); 332 } 333 334 337 public boolean isTargeted() { 338 return pathParams.containsKey(ACTION) || pathParams.containsKey(PID); 339 } 341 } 342 | Popular Tags |