1 9 package org.jboss.portal.portlet.taglib; 10 11 import java.util.ArrayList ; 12 import java.util.HashMap ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 import java.util.Map ; 16 17 import javax.portlet.PortletMode; 18 import javax.portlet.PortletModeException; 19 import javax.portlet.PortletSecurityException; 20 import javax.portlet.PortletURL; 21 import javax.portlet.WindowState; 22 import javax.portlet.WindowStateException; 23 import javax.servlet.jsp.JspException ; 24 25 import org.jboss.portal.portlet.PortletUtils; 26 import org.jboss.portal.server.PortalRequest; 27 import org.jboss.portal.server.PortalResponse; 28 import org.jboss.portal.server.Window; 29 import org.jboss.portal.server.WindowContext; 30 import org.jboss.portal.server.invocation.AttachmentKey; 31 import org.jboss.portal.server.invocation.Invocation; 32 33 44 abstract class GenerateURLTag extends PortletTag 45 { 46 private String windowState; 47 private String portletMode; 48 private String var; 49 private String secure; 50 private Map parameters = new HashMap (5); 51 private WindowContext windowCtx; 52 private PortalRequest preq; 53 private PortalResponse presp; 54 55 69 public String getPortletMode() 70 { 71 return portletMode; 72 } 73 74 77 public void setPortletMode(String portletMode) 78 { 79 this.portletMode = portletMode; 80 } 81 82 96 public String getSecure() 97 { 98 return secure; 99 } 100 101 104 public void setSecure(String secure) 105 { 106 this.secure = secure; 107 } 108 109 115 public String getVar() 116 { 117 return var; 118 } 119 120 123 public void setVar(String var) 124 { 125 this.var = var; 126 } 127 128 142 public String getWindowState() 143 { 144 return windowState; 145 } 146 147 150 public void setWindowState(String windowState) 151 { 152 this.windowState = windowState; 153 } 154 155 158 private void setWindowContext(WindowContext windowCtx) 159 { 160 this.windowCtx = windowCtx; 161 } 162 163 166 private PortalRequest getPortalRequest() 167 { 168 return preq; 169 } 170 171 174 private void setPortalRequest(PortalRequest preq) 175 { 176 this.preq = preq; 177 } 178 179 public PortalResponse getPortalResponse() 180 { 181 return presp; 182 } 183 184 public void setPortalResponse(PortalResponse presp) 185 { 186 this.presp = presp; 187 } 188 189 195 private Map getURLParameters() 196 { 197 Map urlParameters = new HashMap (parameters.size()); 198 199 Iterator it = parameters.entrySet().iterator(); 200 while (it.hasNext()) 201 { 202 Map.Entry entry = (Map.Entry )it.next(); 203 ArrayList l = (ArrayList )entry.getValue(); 204 urlParameters.put(entry.getKey(), l.toArray(new String [l.size()])); 205 } 206 return urlParameters; 207 } 208 209 216 public void addParameter(String name, String value) 217 { 218 List l; 219 220 Object o = parameters.get(name); 221 if (o == null) 222 { 223 l = new ArrayList (5); 224 parameters.put(name, l); 225 226 } 227 else 228 { 229 l = (List )o; 230 } 231 l.add(value); 232 } 233 234 241 private PortletURL getBasePortletEnvironmentAndURL() 242 { 243 Invocation invocation = getInvocation(); 245 246 PortalRequest preq = (PortalRequest)invocation.getAttachment(AttachmentKey.PORTAL_REQUEST); 248 PortalResponse presp = (PortalResponse)invocation.getAttachment(AttachmentKey.PORTAL_RESPONSE); 249 WindowContext windowCtx = (WindowContext)invocation.getAttachment(AttachmentKey.WINDOW_CONTEXT); 250 Window window = (Window)invocation.getAttachment(AttachmentKey.WINDOW); 251 252 setPortalRequest(preq); 254 setPortalResponse(presp); 255 setWindowContext(windowCtx); 256 257 if ("action".equals(getTypeValue())) 259 { 260 return PortletUtils.createActionURL(presp, window, presp.getContentType()); 261 } 262 else if ("render".equals(getTypeValue())) 263 { 264 return PortletUtils.createRenderURL(presp, window, presp.getContentType()); 265 } 266 else 267 { 268 throw new Error ("Impossible - it's a bug"); 269 } 270 } 271 272 279 private void setWindowState(PortletURL portletURL) throws WindowStateException 280 { 281 if (getWindowState() != null && getWindowState().trim().length() > 0) 282 { 283 portletURL.setWindowState(new WindowState(getWindowState().trim())); 284 } 285 } 286 287 294 private void setPortletMode(PortletURL portletURL) throws PortletModeException 295 { 296 if (getPortletMode() != null && getPortletMode().trim().length() > 0) 297 { 298 portletURL.setPortletMode(new PortletMode(getPortletMode().trim())); 299 } 300 } 301 302 309 private void setSecure(PortletURL portletURL) throws PortletSecurityException 310 { 311 if (getSecure() != null && getSecure().trim().length() > 0) 312 { 313 portletURL.setSecure(Boolean.getBoolean(getSecure().trim())); 314 } 315 else 316 { 317 portletURL.setSecure(getPortalRequest().isSecure()); 319 } 320 } 321 322 private void clearPreviousTag() 323 { 324 parameters = new HashMap (5); 325 windowCtx = null; 326 preq = null; 327 } 328 329 334 protected abstract String getTypeValue(); 335 336 339 public int doStartTag() throws JspException 340 { 341 clearPreviousTag(); 342 return EVAL_BODY_INCLUDE; 343 } 344 345 348 public int doEndTag() throws JspException 349 { 350 String resultingURL = null; 351 try 352 { 353 354 PortletURL newPortletURL = getBasePortletEnvironmentAndURL(); 355 356 setWindowState(newPortletURL); 357 358 setPortletMode(newPortletURL); 359 360 setSecure(newPortletURL); 361 362 364 if (!parameters.isEmpty()) 365 { 366 newPortletURL.setParameters(getURLParameters()); 367 } 368 369 resultingURL = newPortletURL.toString(); 370 371 374 if (getVar() != null) 375 { 376 pageContext.setAttribute(getVar(), resultingURL); 377 } 378 else 379 { 380 pageContext.getOut().print(resultingURL); 381 } 382 383 } 384 catch (Exception e) 385 { 386 e.printStackTrace(); 387 throw new JspException (e); 388 } 389 return EVAL_PAGE; 390 } 391 } 392 | Popular Tags |