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.Map;
15
16 import javax.portlet.PortletRequest;
17 import javax.portlet.RenderResponse;
18 import javax.servlet.jsp.PageContext;
19
20 import org.displaytag.util.Href;
21 import org.displaytag.util.RequestHelper;
22
23
24 /**
25 * Reads parameters and generates URLs using javax.portlet APIs. The {@link javax.servlet.jsp.PageContext} passed into
26 * the constructor must provide the {@link javax.portlet.PortletRequest} via an attribute named
27 * {@link #JAVAX_PORTLET_REQUEST} and {@link javax.portlet.RenderResponse} via an attribute named
28 * {@link #JAVAX_PORTLET_RESPONSE}. <br>
29 * <br>
30 * If the pluto portlet container is being used these objects should be setup appropriatly already.
31 * @author Eric Dalquist <a href="mailto:dalquist@gmail.com">dalquist@gmail.com</a>
32 * @version $Id: PortletRequestHelper.java 996 2006-01-06 15:34:08Z fgiust $
33 */
34 public class PortletRequestHelper implements RequestHelper
35 {
36
37 public static final String JAVAX_PORTLET_RESPONSE = "javax.portlet.response";
38
39 public static final String JAVAX_PORTLET_REQUEST = "javax.portlet.request";
40
41 private final PortletRequest portletRequest;
42
43 private final RenderResponse renderResponse;
44
45 /**
46 * Creates a new request helper for the specified PageContext. Retrieves the PortletRequest and RenderResponse from
47 * the PageContext.
48 * @param pageContext Current JSP context.
49 * @throws IllegalStateException If the PortletRequest or RenderResponse are not found in the PageContext.
50 */
51 public PortletRequestHelper(PageContext pageContext)
52 {
53 if (pageContext == null)
54 {
55 throw new IllegalArgumentException("pageContext may not be null");
56 }
57
58 this.portletRequest = (PortletRequest) pageContext.findAttribute(JAVAX_PORTLET_REQUEST);
59 if (this.portletRequest == null)
60 {
61 throw new IllegalStateException("A PortletRequest could not be found in the PageContext for the key='"
62 + JAVAX_PORTLET_REQUEST
63 + "'");
64 }
65
66 this.renderResponse = (RenderResponse) pageContext.findAttribute(JAVAX_PORTLET_RESPONSE);
67 if (this.portletRequest == null)
68 {
69 throw new IllegalStateException("A RenderResponse could not be found in the PageContext for the key='"
70 + JAVAX_PORTLET_RESPONSE
71 + "'");
72 }
73 }
74
75 /**
76 * @see org.displaytag.util.RequestHelper#getHref()
77 */
78 public Href getHref()
79 {
80 final PortletHref href = new PortletHref(this.portletRequest, this.renderResponse);
81 href.setParameterMap(this.portletRequest.getParameterMap());
82
83 if (this.portletRequest.isSecure())
84 {
85 href.setRequestedSecure(true);
86 }
87
88 return href;
89 }
90
91 /**
92 * @see org.displaytag.util.RequestHelper#getParameter(java.lang.String)
93 */
94 public String getParameter(String key)
95 {
96 return this.portletRequest.getParameter(key);
97 }
98
99 /**
100 * @see org.displaytag.util.RequestHelper#getIntParameter(java.lang.String)
101 */
102 public Integer getIntParameter(String key)
103 {
104 try
105 {
106 return new Integer(this.getParameter(key));
107 }
108 catch (NumberFormatException nfe)
109 {
110 return null;
111 }
112 }
113
114 /**
115 * @see org.displaytag.util.RequestHelper#getParameterMap()
116 */
117 public Map getParameterMap()
118 {
119 return this.portletRequest.getParameterMap();
120 }
121 }