1 12 package org.displaytag.portlet; 13 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 18 import javax.portlet.PortletMode; 19 import javax.portlet.PortletModeException; 20 import javax.portlet.PortletRequest; 21 import javax.portlet.PortletSecurityException; 22 import javax.portlet.PortletURL; 23 import javax.portlet.RenderResponse; 24 import javax.portlet.WindowState; 25 import javax.portlet.WindowStateException; 26 27 import org.apache.commons.collections.Predicate; 28 import org.apache.commons.collections.functors.AnyPredicate; 29 import org.apache.commons.collections.functors.InstanceofPredicate; 30 import org.apache.commons.collections.functors.NullPredicate; 31 import org.apache.commons.collections.map.PredicatedMap; 32 import org.apache.commons.lang.ObjectUtils; 33 import org.apache.commons.lang.builder.EqualsBuilder; 34 import org.apache.commons.lang.builder.HashCodeBuilder; 35 import org.displaytag.util.Href; 36 37 38 73 public class PortletHref implements Href 74 { 75 76 private static final String PARAM_PREFIX = "portlet:"; 78 79 public static final String PARAM_MODE = PARAM_PREFIX + "mode"; 80 81 public static final String PARAM_STATE = PARAM_PREFIX + "state"; 82 83 public static final String PARAM_SECURE = PARAM_PREFIX + "secure"; 84 85 public static final String PARAM_TYPE = PARAM_PREFIX + "type"; 86 87 public static final String TYPE_RENDER = "render"; 88 89 public static final String TYPE_ACTION = "action"; 90 91 94 private static final long serialVersionUID = 899149338534L; 95 96 private static final Predicate PRED_TYPE_OF_STRING = new InstanceofPredicate(String .class); 98 99 private static final Predicate PRED_TYPE_OF_STRING_ARRY = new InstanceofPredicate(String [].class); 100 101 private static final Predicate PRED_OR_STR_STRARR = new AnyPredicate(new Predicate[]{ 102 PRED_TYPE_OF_STRING, 103 PRED_TYPE_OF_STRING_ARRY, 104 NullPredicate.INSTANCE}); 105 106 private final PortletRequest portletRequest; 108 109 private final RenderResponse renderResponse; 110 111 private Map parameters = this.createParameterMap(); 112 113 private boolean isAction; 114 115 private PortletMode requestedMode; 116 117 private WindowState requestedState; 118 119 private boolean requestedSecure; 120 121 private String anchor; 122 123 128 public PortletHref(PortletRequest portletRequest, RenderResponse renderResponse) 129 { 130 if (portletRequest == null) 131 { 132 throw new IllegalArgumentException ("portletRequest may not be null"); 133 } 134 if (renderResponse == null) 135 { 136 throw new IllegalArgumentException ("renderResponse may not be null"); 137 } 138 139 this.portletRequest = portletRequest; 140 this.renderResponse = renderResponse; 141 } 142 143 146 public void setFullUrl(String baseUrl) 147 { 148 } 150 151 154 public boolean isAction() 155 { 156 return this.isAction; 157 } 158 159 162 public void setAction(boolean isAction) 163 { 164 this.isAction = isAction; 165 } 166 167 170 public PortletMode getRequestedMode() 171 { 172 return this.requestedMode; 173 } 174 175 178 public void setRequestedMode(PortletMode requestedMode) 179 { 180 this.requestedMode = requestedMode; 181 } 182 183 186 public boolean isRequestedSecure() 187 { 188 return this.requestedSecure; 189 } 190 191 194 public void setRequestedSecure(boolean requestedSecure) 195 { 196 this.requestedSecure = requestedSecure; 197 } 198 199 202 public WindowState getRequestedState() 203 { 204 return this.requestedState; 205 } 206 207 210 public void setRequestedState(WindowState requestedState) 211 { 212 this.requestedState = requestedState; 213 } 214 215 218 public Href addParameter(String name, int value) 219 { 220 return this.addParameter(name, Integer.toString(value)); 221 } 222 223 226 public Href addParameter(String name, Object objValue) 227 { 228 String value = ObjectUtils.toString(objValue, null); 229 230 if (name != null && name.startsWith(PARAM_PREFIX)) 231 { 232 if (PARAM_TYPE.equals(name)) 233 { 234 if (TYPE_RENDER.equals(value)) 235 { 236 this.setAction(false); 237 } 238 else if (TYPE_ACTION.equals(value)) 239 { 240 this.setAction(true); 241 } 242 else 243 { 244 throw new IllegalArgumentException ("Value of parameter '" 245 + name 246 + "' must be equal to '" 247 + TYPE_RENDER 248 + "' or '" 249 + TYPE_ACTION 250 + "'. '" 251 + value 252 + "' is not allowed."); 253 } 254 } 255 else if (PARAM_SECURE.equals(name)) 256 { 257 if (new Boolean (value).booleanValue()) 258 { 259 this.setRequestedSecure(true); 260 } 261 else 262 { 263 this.setRequestedSecure(false); 264 } 265 } 266 else if (PARAM_MODE.equals(name)) 267 { 268 if (value == null) 269 { 270 this.setRequestedMode(null); 271 } 272 else 273 { 274 final PortletMode mode = new PortletMode(value); 275 276 if (!this.portletRequest.isPortletModeAllowed(mode)) 277 { 278 throw new IllegalArgumentException ("PortletMode '" 279 + mode 280 + "' is not allowed for this request."); 281 } 282 283 this.setRequestedMode(mode); 284 } 285 } 286 else if (PARAM_STATE.equals(name)) 287 { 288 if (value == null) 289 { 290 this.setRequestedState(null); 291 } 292 else 293 { 294 final WindowState state = new WindowState(value); 295 296 if (!this.portletRequest.isWindowStateAllowed(state)) 297 { 298 throw new IllegalArgumentException ("WindowState '" 299 + state 300 + "' is not allowed for this request."); 301 } 302 303 this.setRequestedState(state); 304 } 305 } 306 else 307 { 308 throw new IllegalArgumentException ("'" 309 + name 310 + "' is not a valid '" 311 + PARAM_PREFIX 312 + "' prefixed parameter."); 313 } 314 } 315 else 316 { 317 this.parameters.put(name, value); 318 } 319 320 return this; 321 } 322 323 326 public void addParameterMap(Map parametersMap) 327 { 328 for (final Iterator paramItr = parametersMap.entrySet().iterator(); paramItr.hasNext();) 329 { 330 final Map.Entry entry = (Map.Entry ) paramItr.next(); 331 332 final String name = (String ) entry.getKey(); 333 final Object value = entry.getValue(); 334 335 if (value instanceof String []) 339 { 340 this.parameters.put(name, value); 341 } 342 else if (value == null || value instanceof String ) 343 { 344 this.addParameter(name, value); 345 } 346 else 347 { 348 this.addParameter(name, value.toString()); 349 } 350 } 351 } 352 353 356 public void setParameterMap(Map parametersMap) 357 { 358 this.parameters.clear(); 359 this.addParameterMap(parametersMap); 360 } 361 362 367 public Map getParameterMap() 368 { 369 return this.parameters; 370 } 371 372 375 public void removeParameter(String name) 376 { 377 this.parameters.remove(name); 378 } 379 380 383 public void setAnchor(String name) 384 { 385 this.anchor = name; 386 } 387 388 391 public String getAnchor() 392 { 393 return this.anchor; 394 } 395 396 400 public String getBaseUrl() 401 { 402 if (this.isAction()) 403 { 404 return this.renderResponse.createActionURL().toString(); 405 } 406 else 407 { 408 return this.renderResponse.createRenderURL().toString(); 409 } 410 } 411 412 415 public Object clone() 416 { 417 PortletHref href; 418 419 try 420 { 421 href = (PortletHref) super.clone(); 422 } 423 catch (CloneNotSupportedException cnse) 424 { 425 throw new RuntimeException ("Parent through a CloneNotSupportedException, this should never happen", cnse); 426 } 427 428 href.isAction = this.isAction; 429 href.parameters = this.createParameterMap(); 430 href.parameters.putAll(this.parameters); 431 href.requestedMode = this.requestedMode; 432 href.requestedState = this.requestedState; 433 href.requestedSecure = this.requestedSecure; 434 href.anchor = this.anchor; 435 436 return href; 437 } 438 439 442 public boolean equals(Object object) 443 { 444 if (this == object) 445 { 446 return true; 447 } 448 if (!(object instanceof PortletHref)) 449 { 450 return false; 451 } 452 PortletHref rhs = (PortletHref) object; 453 return new EqualsBuilder().append(this.isAction, rhs.isAction).append(this.parameters, rhs.parameters).append( 454 this.requestedMode, 455 rhs.requestedMode).append(this.requestedState, rhs.requestedState).append( 456 this.requestedSecure, 457 rhs.requestedSecure).append(this.anchor, rhs.anchor).isEquals(); 458 } 459 460 463 public int hashCode() 464 { 465 return new HashCodeBuilder(1313733113, -431360889) 466 .append(this.isAction) 467 .append(this.parameters) 468 .append(this.requestedMode) 469 .append(this.requestedState) 470 .append(this.requestedSecure) 471 .append(this.anchor) 472 .toHashCode(); 473 } 474 475 478 public String toString() 479 { 480 final PortletURL url; 481 if (this.isAction()) 482 { 483 url = this.renderResponse.createActionURL(); 484 } 485 else 486 { 487 url = this.renderResponse.createRenderURL(); 488 } 489 490 if (this.isRequestedSecure()) 491 { 492 try 493 { 494 url.setSecure(true); 495 } 496 catch (PortletSecurityException pse) 497 { 498 throw new RuntimeException ("Creating secure PortletURL Failed.", pse); 499 } 500 } 501 502 if (this.getRequestedMode() != null) 503 { 504 try 505 { 506 url.setPortletMode(this.getRequestedMode()); 507 } 508 catch (PortletModeException pme) 509 { 510 final IllegalStateException ise = new IllegalStateException ("Requested PortletMode='" 511 + this.getRequestedMode() 512 + "' could not be set."); 513 ise.initCause(pme); 514 throw ise; 515 } 516 } 517 518 if (this.getRequestedState() != null) 519 { 520 try 521 { 522 url.setWindowState(this.getRequestedState()); 523 } 524 catch (WindowStateException wse) 525 { 526 final IllegalStateException ise = new IllegalStateException ("Requested WindowState='" 527 + this.getRequestedState() 528 + "' could not be set."); 529 ise.initCause(wse); 530 throw ise; 531 } 532 } 533 534 for (final Iterator paramItr = this.parameters.entrySet().iterator(); paramItr.hasNext();) 535 { 536 final Map.Entry entry = (Map.Entry ) paramItr.next(); 537 538 final String name = (String ) entry.getKey(); 539 final Object value = entry.getValue(); 540 541 if (value instanceof String ) 542 { 543 url.setParameter(name, (String ) value); 544 } 545 else if (value instanceof String []) 546 { 547 url.setParameter(name, (String []) value); 548 } 549 } 550 551 if (this.getAnchor() == null) 552 { 553 return url.toString(); 554 } 555 else 556 { 557 return url.toString() + "#" + this.getAnchor(); 558 } 559 } 560 561 private Map createParameterMap() 562 { 563 return PredicatedMap.decorate(new HashMap (), PRED_TYPE_OF_STRING, PRED_OR_STR_STRARR); 564 } 565 } 566 | Popular Tags |