View Javadoc

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  
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  /**
39   * Implementation of the Href interface that generates URLs using the javax.portlet APIs. As the portlet API supports
40   * the concept of WindowStates, PorletModes, secure URLs and actions versus render the implementation supports these
41   * concepts as well through the standard {@link Href} APIs. <br>
42   * <br>
43   * The features are manipulated using special parameter names and values: <table>
44   * <tr>
45   * <th>Feature</th>
46   * <th>Parameter Name</th>
47   * <th>Parameter Value</th>
48   * </tr>
49   * <tr>
50   * <td>Render vs Action URL</td>
51   * <td>{@link #PARAM_TYPE} (portlet:type)</td>
52   * <td>"render" for RenderURLs, "action" for ActionURLs</td>
53   * </tr>
54   * <tr>
55   * <td>WindowState</td>
56   * <td>{@link #PARAM_STATE} (portlet:state)</td>
57   * <td>The value is used directly for the WindowState name</td>
58   * </tr>
59   * <tr>
60   * <td>PorltetMode</td>
61   * <td>{@link #PARAM_MODE} (portlet:mode)</td>
62   * <td>The value is used directly for the PortletMode name</td>
63   * </tr>
64   * <tr>
65   * <td>Secure URL</td>
66   * <td>{@link #PARAM_SECURE} (portlet:secure)</td>
67   * <td>"true" requests a secure URL, anything else requests a standard URL</td>
68   * </tr>
69   * </table>
70   * @author Eric Dalquist <a href="mailto:dalquist@gmail.com">dalquist@gmail.com</a>
71   * @version $Id: PortletHref.java 999 2006-01-22 20:01:46Z fgiust $
72   */
73  public class PortletHref implements Href
74  {
75  
76      // Constants for working with the special parameters
77      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      /**
92       * D1597A17A6.
93       */
94      private static final long serialVersionUID = 899149338534L;
95  
96      // Predicated for type checking the parameter map
97      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     // Portlet request and response are needed for feature checking and generating the URLs
107     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     /**
124      * Creates a new PortletHref. The actual PortletURL object is not generated until the toString method is called.
125      * @param portletRequest request to to feature checking with, may not be null.
126      * @param renderResponse response to generate the URLs from, may not be null.
127      */
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     /**
144      * @see org.displaytag.util.Href#setFullUrl(java.lang.String)
145      */
146     public void setFullUrl(String baseUrl)
147     {
148         // do nothing
149     }
150 
151     /**
152      * @return Returns the isAction.
153      */
154     public boolean isAction()
155     {
156         return this.isAction;
157     }
158 
159     /**
160      * @param isAction The isAction to set.
161      */
162     public void setAction(boolean isAction)
163     {
164         this.isAction = isAction;
165     }
166 
167     /**
168      * @return Returns the requestedMode.
169      */
170     public PortletMode getRequestedMode()
171     {
172         return this.requestedMode;
173     }
174 
175     /**
176      * @param requestedMode The requestedMode to set.
177      */
178     public void setRequestedMode(PortletMode requestedMode)
179     {
180         this.requestedMode = requestedMode;
181     }
182 
183     /**
184      * @return Returns the requestedSecure.
185      */
186     public boolean isRequestedSecure()
187     {
188         return this.requestedSecure;
189     }
190 
191     /**
192      * @param requestedSecure The requestedSecure to set.
193      */
194     public void setRequestedSecure(boolean requestedSecure)
195     {
196         this.requestedSecure = requestedSecure;
197     }
198 
199     /**
200      * @return Returns the requestedState.
201      */
202     public WindowState getRequestedState()
203     {
204         return this.requestedState;
205     }
206 
207     /**
208      * @param requestedState The requestedState to set.
209      */
210     public void setRequestedState(WindowState requestedState)
211     {
212         this.requestedState = requestedState;
213     }
214 
215     /**
216      * @see org.displaytag.util.Href#addParameter(java.lang.String, int)
217      */
218     public Href addParameter(String name, int value)
219     {
220         return this.addParameter(name, Integer.toString(value));
221     }
222 
223     /**
224      * @see org.displaytag.util.Href#addParameter(String, Object)
225      */
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     /**
324      * @see org.displaytag.util.Href#addParameterMap(java.util.Map)
325      */
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             // Allow multivalued parameters since code elsewhere calls this method to copy
336             // parameters from the request to the response. Ensures that developer specified
337             // multivalued parameters are retained correctly.
338             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     /**
354      * @see org.displaytag.util.Href#setParameterMap(java.util.Map)
355      */
356     public void setParameterMap(Map parametersMap)
357     {
358         this.parameters.clear();
359         this.addParameterMap(parametersMap);
360     }
361 
362     /**
363      * Warning, parameters added to the Map directly will not be parsed by the PortletUrl feature support portions of
364      * this class.
365      * @see org.displaytag.util.Href#getParameterMap()
366      */
367     public Map getParameterMap()
368     {
369         return this.parameters;
370     }
371 
372     /**
373      * @see org.displaytag.util.Href#removeParameter(java.lang.String)
374      */
375     public void removeParameter(String name)
376     {
377         this.parameters.remove(name);
378     }
379 
380     /**
381      * @see org.displaytag.util.Href#setAnchor(java.lang.String)
382      */
383     public void setAnchor(String name)
384     {
385         this.anchor = name;
386     }
387 
388     /**
389      * @see org.displaytag.util.Href#getAnchor()
390      */
391     public String getAnchor()
392     {
393         return this.anchor;
394     }
395 
396     /**
397      * Generates a render or action URL depending on the use of the PortletUrl specific features of this class.
398      * @see org.displaytag.util.Href#getBaseUrl()
399      */
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     /**
413      * @see org.displaytag.util.Href#clone()
414      */
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     /**
440      * @see org.displaytag.util.Href#equals(java.lang.Object)
441      */
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     /**
461      * @see org.displaytag.util.Href#hashCode()
462      */
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     /**
476      * @see org.displaytag.util.Href#toString()
477      */
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 }