PageRenderTime 148ms CodeModel.GetById 40ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/displaytag-1.2/displaytag-portlet/src/main/java/org/displaytag/portlet/PortletHref.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 565 lines | 368 code | 67 blank | 130 comment | 46 complexity | d8abd6f766c80debcd5f2015825ed57a MD5 | raw file
  1. /**
  2. * Licensed under the Artistic License; you may not use this file
  3. * except in compliance with the License.
  4. * You may obtain a copy of the License at
  5. *
  6. * http://displaytag.sourceforge.net/license.html
  7. *
  8. * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
  9. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  10. * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  11. */
  12. package org.displaytag.portlet;
  13. import java.util.HashMap;
  14. import java.util.Iterator;
  15. import java.util.Map;
  16. import javax.portlet.PortletMode;
  17. import javax.portlet.PortletModeException;
  18. import javax.portlet.PortletRequest;
  19. import javax.portlet.PortletSecurityException;
  20. import javax.portlet.PortletURL;
  21. import javax.portlet.RenderResponse;
  22. import javax.portlet.WindowState;
  23. import javax.portlet.WindowStateException;
  24. import org.apache.commons.collections.Predicate;
  25. import org.apache.commons.collections.functors.AnyPredicate;
  26. import org.apache.commons.collections.functors.InstanceofPredicate;
  27. import org.apache.commons.collections.functors.NullPredicate;
  28. import org.apache.commons.collections.map.PredicatedMap;
  29. import org.apache.commons.lang.ObjectUtils;
  30. import org.apache.commons.lang.builder.EqualsBuilder;
  31. import org.apache.commons.lang.builder.HashCodeBuilder;
  32. import org.displaytag.util.Href;
  33. /**
  34. * Implementation of the Href interface that generates URLs using the javax.portlet APIs. As the portlet API supports
  35. * the concept of WindowStates, PorletModes, secure URLs and actions versus render the implementation supports these
  36. * concepts as well through the standard {@link Href} APIs. <br>
  37. * <br>
  38. * The features are manipulated using special parameter names and values: <table>
  39. * <tr>
  40. * <th>Feature</th>
  41. * <th>Parameter Name</th>
  42. * <th>Parameter Value</th>
  43. * </tr>
  44. * <tr>
  45. * <td>Render vs Action URL</td>
  46. * <td>{@link #PARAM_TYPE} (portlet:type)</td>
  47. * <td>"render" for RenderURLs, "action" for ActionURLs</td>
  48. * </tr>
  49. * <tr>
  50. * <td>WindowState</td>
  51. * <td>{@link #PARAM_STATE} (portlet:state)</td>
  52. * <td>The value is used directly for the WindowState name</td>
  53. * </tr>
  54. * <tr>
  55. * <td>PorltetMode</td>
  56. * <td>{@link #PARAM_MODE} (portlet:mode)</td>
  57. * <td>The value is used directly for the PortletMode name</td>
  58. * </tr>
  59. * <tr>
  60. * <td>Secure URL</td>
  61. * <td>{@link #PARAM_SECURE} (portlet:secure)</td>
  62. * <td>"true" requests a secure URL, anything else requests a standard URL</td>
  63. * </tr>
  64. * </table>
  65. * @author Eric Dalquist <a href="mailto:dalquist@gmail.com">dalquist@gmail.com</a>
  66. * @version $Id: PortletHref.java 999 2006-01-22 20:01:46Z fgiust $
  67. */
  68. public class PortletHref implements Href
  69. {
  70. // Constants for working with the special parameters
  71. private static final String PARAM_PREFIX = "portlet:";
  72. public static final String PARAM_MODE = PARAM_PREFIX + "mode";
  73. public static final String PARAM_STATE = PARAM_PREFIX + "state";
  74. public static final String PARAM_SECURE = PARAM_PREFIX + "secure";
  75. public static final String PARAM_TYPE = PARAM_PREFIX + "type";
  76. public static final String TYPE_RENDER = "render";
  77. public static final String TYPE_ACTION = "action";
  78. /**
  79. * D1597A17A6.
  80. */
  81. private static final long serialVersionUID = 899149338534L;
  82. // Predicated for type checking the parameter map
  83. private static final Predicate PRED_TYPE_OF_STRING = new InstanceofPredicate(String.class);
  84. private static final Predicate PRED_TYPE_OF_STRING_ARRY = new InstanceofPredicate(String[].class);
  85. private static final Predicate PRED_OR_STR_STRARR = new AnyPredicate(new Predicate[]{
  86. PRED_TYPE_OF_STRING,
  87. PRED_TYPE_OF_STRING_ARRY,
  88. NullPredicate.INSTANCE});
  89. // Portlet request and response are needed for feature checking and generating the URLs
  90. private final PortletRequest portletRequest;
  91. private final RenderResponse renderResponse;
  92. private Map parameters = this.createParameterMap();
  93. private boolean isAction;
  94. private PortletMode requestedMode;
  95. private WindowState requestedState;
  96. private boolean requestedSecure;
  97. private String anchor;
  98. /**
  99. * Creates a new PortletHref. The actual PortletURL object is not generated until the toString method is called.
  100. * @param portletRequest request to to feature checking with, may not be null.
  101. * @param renderResponse response to generate the URLs from, may not be null.
  102. */
  103. public PortletHref(PortletRequest portletRequest, RenderResponse renderResponse)
  104. {
  105. if (portletRequest == null)
  106. {
  107. throw new IllegalArgumentException("portletRequest may not be null");
  108. }
  109. if (renderResponse == null)
  110. {
  111. throw new IllegalArgumentException("renderResponse may not be null");
  112. }
  113. this.portletRequest = portletRequest;
  114. this.renderResponse = renderResponse;
  115. }
  116. /**
  117. * @see org.displaytag.util.Href#setFullUrl(java.lang.String)
  118. */
  119. public void setFullUrl(String baseUrl)
  120. {
  121. // do nothing
  122. }
  123. /**
  124. * @return Returns the isAction.
  125. */
  126. public boolean isAction()
  127. {
  128. return this.isAction;
  129. }
  130. /**
  131. * @param isAction The isAction to set.
  132. */
  133. public void setAction(boolean isAction)
  134. {
  135. this.isAction = isAction;
  136. }
  137. /**
  138. * @return Returns the requestedMode.
  139. */
  140. public PortletMode getRequestedMode()
  141. {
  142. return this.requestedMode;
  143. }
  144. /**
  145. * @param requestedMode The requestedMode to set.
  146. */
  147. public void setRequestedMode(PortletMode requestedMode)
  148. {
  149. this.requestedMode = requestedMode;
  150. }
  151. /**
  152. * @return Returns the requestedSecure.
  153. */
  154. public boolean isRequestedSecure()
  155. {
  156. return this.requestedSecure;
  157. }
  158. /**
  159. * @param requestedSecure The requestedSecure to set.
  160. */
  161. public void setRequestedSecure(boolean requestedSecure)
  162. {
  163. this.requestedSecure = requestedSecure;
  164. }
  165. /**
  166. * @return Returns the requestedState.
  167. */
  168. public WindowState getRequestedState()
  169. {
  170. return this.requestedState;
  171. }
  172. /**
  173. * @param requestedState The requestedState to set.
  174. */
  175. public void setRequestedState(WindowState requestedState)
  176. {
  177. this.requestedState = requestedState;
  178. }
  179. /**
  180. * @see org.displaytag.util.Href#addParameter(java.lang.String, int)
  181. */
  182. public Href addParameter(String name, int value)
  183. {
  184. return this.addParameter(name, Integer.toString(value));
  185. }
  186. /**
  187. * @see org.displaytag.util.Href#addParameter(String, Object)
  188. */
  189. public Href addParameter(String name, Object objValue)
  190. {
  191. String value = ObjectUtils.toString(objValue, null);
  192. if (name != null && name.startsWith(PARAM_PREFIX))
  193. {
  194. if (PARAM_TYPE.equals(name))
  195. {
  196. if (TYPE_RENDER.equals(value))
  197. {
  198. this.setAction(false);
  199. }
  200. else if (TYPE_ACTION.equals(value))
  201. {
  202. this.setAction(true);
  203. }
  204. else
  205. {
  206. throw new IllegalArgumentException("Value of parameter '"
  207. + name
  208. + "' must be equal to '"
  209. + TYPE_RENDER
  210. + "' or '"
  211. + TYPE_ACTION
  212. + "'. '"
  213. + value
  214. + "' is not allowed.");
  215. }
  216. }
  217. else if (PARAM_SECURE.equals(name))
  218. {
  219. if (new Boolean(value).booleanValue())
  220. {
  221. this.setRequestedSecure(true);
  222. }
  223. else
  224. {
  225. this.setRequestedSecure(false);
  226. }
  227. }
  228. else if (PARAM_MODE.equals(name))
  229. {
  230. if (value == null)
  231. {
  232. this.setRequestedMode(null);
  233. }
  234. else
  235. {
  236. final PortletMode mode = new PortletMode(value);
  237. if (!this.portletRequest.isPortletModeAllowed(mode))
  238. {
  239. throw new IllegalArgumentException("PortletMode '"
  240. + mode
  241. + "' is not allowed for this request.");
  242. }
  243. this.setRequestedMode(mode);
  244. }
  245. }
  246. else if (PARAM_STATE.equals(name))
  247. {
  248. if (value == null)
  249. {
  250. this.setRequestedState(null);
  251. }
  252. else
  253. {
  254. final WindowState state = new WindowState(value);
  255. if (!this.portletRequest.isWindowStateAllowed(state))
  256. {
  257. throw new IllegalArgumentException("WindowState '"
  258. + state
  259. + "' is not allowed for this request.");
  260. }
  261. this.setRequestedState(state);
  262. }
  263. }
  264. else
  265. {
  266. throw new IllegalArgumentException("'"
  267. + name
  268. + "' is not a valid '"
  269. + PARAM_PREFIX
  270. + "' prefixed parameter.");
  271. }
  272. }
  273. else
  274. {
  275. this.parameters.put(name, value);
  276. }
  277. return this;
  278. }
  279. /**
  280. * @see org.displaytag.util.Href#addParameterMap(java.util.Map)
  281. */
  282. public void addParameterMap(Map parametersMap)
  283. {
  284. for (final Iterator paramItr = parametersMap.entrySet().iterator(); paramItr.hasNext();)
  285. {
  286. final Map.Entry entry = (Map.Entry) paramItr.next();
  287. final String name = (String) entry.getKey();
  288. final Object value = entry.getValue();
  289. // Allow multivalued parameters since code elsewhere calls this method to copy
  290. // parameters from the request to the response. Ensures that developer specified
  291. // multivalued parameters are retained correctly.
  292. if (value instanceof String[])
  293. {
  294. this.parameters.put(name, value);
  295. }
  296. else if (value == null || value instanceof String)
  297. {
  298. this.addParameter(name, value);
  299. }
  300. else
  301. {
  302. this.addParameter(name, value.toString());
  303. }
  304. }
  305. }
  306. /**
  307. * @see org.displaytag.util.Href#setParameterMap(java.util.Map)
  308. */
  309. public void setParameterMap(Map parametersMap)
  310. {
  311. this.parameters.clear();
  312. this.addParameterMap(parametersMap);
  313. }
  314. /**
  315. * Warning, parameters added to the Map directly will not be parsed by the PortletUrl feature support portions of
  316. * this class.
  317. * @see org.displaytag.util.Href#getParameterMap()
  318. */
  319. public Map getParameterMap()
  320. {
  321. return this.parameters;
  322. }
  323. /**
  324. * @see org.displaytag.util.Href#removeParameter(java.lang.String)
  325. */
  326. public void removeParameter(String name)
  327. {
  328. this.parameters.remove(name);
  329. }
  330. /**
  331. * @see org.displaytag.util.Href#setAnchor(java.lang.String)
  332. */
  333. public void setAnchor(String name)
  334. {
  335. this.anchor = name;
  336. }
  337. /**
  338. * @see org.displaytag.util.Href#getAnchor()
  339. */
  340. public String getAnchor()
  341. {
  342. return this.anchor;
  343. }
  344. /**
  345. * Generates a render or action URL depending on the use of the PortletUrl specific features of this class.
  346. * @see org.displaytag.util.Href#getBaseUrl()
  347. */
  348. public String getBaseUrl()
  349. {
  350. if (this.isAction())
  351. {
  352. return this.renderResponse.createActionURL().toString();
  353. }
  354. else
  355. {
  356. return this.renderResponse.createRenderURL().toString();
  357. }
  358. }
  359. /**
  360. * @see org.displaytag.util.Href#clone()
  361. */
  362. public Object clone()
  363. {
  364. PortletHref href;
  365. try
  366. {
  367. href = (PortletHref) super.clone();
  368. }
  369. catch (CloneNotSupportedException cnse)
  370. {
  371. throw new RuntimeException("Parent through a CloneNotSupportedException, this should never happen", cnse);
  372. }
  373. href.isAction = this.isAction;
  374. href.parameters = this.createParameterMap();
  375. href.parameters.putAll(this.parameters);
  376. href.requestedMode = this.requestedMode;
  377. href.requestedState = this.requestedState;
  378. href.requestedSecure = this.requestedSecure;
  379. href.anchor = this.anchor;
  380. return href;
  381. }
  382. /**
  383. * @see org.displaytag.util.Href#equals(java.lang.Object)
  384. */
  385. public boolean equals(Object object)
  386. {
  387. if (this == object)
  388. {
  389. return true;
  390. }
  391. if (!(object instanceof PortletHref))
  392. {
  393. return false;
  394. }
  395. PortletHref rhs = (PortletHref) object;
  396. return new EqualsBuilder().append(this.isAction, rhs.isAction).append(this.parameters, rhs.parameters).append(
  397. this.requestedMode,
  398. rhs.requestedMode).append(this.requestedState, rhs.requestedState).append(
  399. this.requestedSecure,
  400. rhs.requestedSecure).append(this.anchor, rhs.anchor).isEquals();
  401. }
  402. /**
  403. * @see org.displaytag.util.Href#hashCode()
  404. */
  405. public int hashCode()
  406. {
  407. return new HashCodeBuilder(1313733113, -431360889)
  408. .append(this.isAction)
  409. .append(this.parameters)
  410. .append(this.requestedMode)
  411. .append(this.requestedState)
  412. .append(this.requestedSecure)
  413. .append(this.anchor)
  414. .toHashCode();
  415. }
  416. /**
  417. * @see org.displaytag.util.Href#toString()
  418. */
  419. public String toString()
  420. {
  421. final PortletURL url;
  422. if (this.isAction())
  423. {
  424. url = this.renderResponse.createActionURL();
  425. }
  426. else
  427. {
  428. url = this.renderResponse.createRenderURL();
  429. }
  430. if (this.isRequestedSecure())
  431. {
  432. try
  433. {
  434. url.setSecure(true);
  435. }
  436. catch (PortletSecurityException pse)
  437. {
  438. throw new RuntimeException("Creating secure PortletURL Failed.", pse);
  439. }
  440. }
  441. if (this.getRequestedMode() != null)
  442. {
  443. try
  444. {
  445. url.setPortletMode(this.getRequestedMode());
  446. }
  447. catch (PortletModeException pme)
  448. {
  449. final IllegalStateException ise = new IllegalStateException("Requested PortletMode='"
  450. + this.getRequestedMode()
  451. + "' could not be set.");
  452. ise.initCause(pme);
  453. throw ise;
  454. }
  455. }
  456. if (this.getRequestedState() != null)
  457. {
  458. try
  459. {
  460. url.setWindowState(this.getRequestedState());
  461. }
  462. catch (WindowStateException wse)
  463. {
  464. final IllegalStateException ise = new IllegalStateException("Requested WindowState='"
  465. + this.getRequestedState()
  466. + "' could not be set.");
  467. ise.initCause(wse);
  468. throw ise;
  469. }
  470. }
  471. for (final Iterator paramItr = this.parameters.entrySet().iterator(); paramItr.hasNext();)
  472. {
  473. final Map.Entry entry = (Map.Entry) paramItr.next();
  474. final String name = (String) entry.getKey();
  475. final Object value = entry.getValue();
  476. if (value instanceof String)
  477. {
  478. url.setParameter(name, (String) value);
  479. }
  480. else if (value instanceof String[])
  481. {
  482. url.setParameter(name, (String[]) value);
  483. }
  484. }
  485. if (this.getAnchor() == null)
  486. {
  487. return url.toString();
  488. }
  489. else
  490. {
  491. return url.toString() + "#" + this.getAnchor();
  492. }
  493. }
  494. private Map createParameterMap()
  495. {
  496. return PredicatedMap.decorate(new HashMap(), PRED_TYPE_OF_STRING, PRED_OR_STR_STRARR);
  497. }
  498. }