KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > portlet > taglib > GenerateURLTag


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.portlet.taglib;
10
11 import java.util.ArrayList JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Map JavaDoc;
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 JavaDoc;
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 /**
34  * Superclass of the actionURL and renderURL tags for the JSR 168
35  * Portlet specification.
36  *
37  * Creates a URL that must point to the current portlet and must trigger
38  * a render or action request with the supplied parameters.
39  *
40  * @author <a HREF="mailto:sgwood@ix.netcom.com">Sherman Wood</a>
41  * @version $Revision: 1.2 $
42  *
43  */

44 abstract class GenerateURLTag extends PortletTag
45 {
46    private String JavaDoc windowState;
47    private String JavaDoc portletMode;
48    private String JavaDoc var;
49    private String JavaDoc secure;
50    private Map JavaDoc parameters = new HashMap JavaDoc(5);
51    private WindowContext windowCtx;
52    private PortalRequest preq;
53    private PortalResponse presp;
54
55    /**
56     * Indicates the portlet mode that the portlet must have when this
57     * link is executed.
58     *
59     * Predefined states: edit, view, help
60     *
61     * Optional. Defaults to same as the portlet mode for the current
62     * request, by not being included as a parameter in the URL.
63     *
64     * @return Returns the portletMode.
65     *
66     * @jsp.attribute
67     * rtexprvalue="true"
68     */

69    public String JavaDoc getPortletMode()
70    {
71       return portletMode;
72    }
73
74    /**
75     * @param portletMode The portletMode to set.
76     */

77    public void setPortletMode(String JavaDoc portletMode)
78    {
79       this.portletMode = portletMode;
80    }
81
82    /**
83     * Indicates whether the resulting URL should be a secure or insecure
84     * connection.
85     *
86     * "true" or "false"
87     *
88     * Optional. Defaults to security setting for the current
89     * request, by not being included as a parameter in the URL.
90     *
91     * @return Returns the secure connection value.
92     *
93     * @jsp.attribute
94     * rtexprvalue="true"
95     */

96    public String JavaDoc getSecure()
97    {
98       return secure;
99    }
100
101    /**
102     * @param secure The secure connection value to set.
103     */

104    public void setSecure(String JavaDoc secure)
105    {
106       this.secure = secure;
107    }
108
109    /**
110     * @return Returns the var - name of the exported scope variable.
111     *
112     * @jsp.attribute
113     * rtexprvalue="true"
114     */

115    public String JavaDoc getVar()
116    {
117       return var;
118    }
119
120    /**
121     * @param var The var to set.
122     */

123    public void setVar(String JavaDoc var)
124    {
125       this.var = var;
126    }
127
128    /**
129     * Indicates the window state that the portlet should have when this
130     * link is executed.
131     *
132     * Predefined states: minimized, maximized, normal
133     *
134     * Optional. Defaults to same as the window state for the current
135     * request, by not being included as a parameter in the URL.
136     *
137     * @return Returns the windowState.
138     *
139     * @jsp.attribute
140     * rtexprvalue="true"
141     */

142    public String JavaDoc getWindowState()
143    {
144       return windowState;
145    }
146
147    /**
148     * @param windowState The windowState to set.
149     */

150    public void setWindowState(String JavaDoc windowState)
151    {
152       this.windowState = windowState;
153    }
154
155    /**
156     * @param windowCtx The portletWindowState to set.
157     */

158    private void setWindowContext(WindowContext windowCtx)
159    {
160       this.windowCtx = windowCtx;
161    }
162
163    /**
164     * @return Returns the PortalRequest.
165     */

166    private PortalRequest getPortalRequest()
167    {
168       return preq;
169    }
170
171    /**
172     * @param preq The PortalRequest to set.
173     */

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    /**
190     * Convert working parameters to what is needed by
191     * PortletURL - a map with String[] values.
192     *
193     * @return Returns the parameters.
194     */

195    private Map JavaDoc getURLParameters()
196    {
197       Map JavaDoc urlParameters = new HashMap JavaDoc(parameters.size());
198
199       Iterator JavaDoc it = parameters.entrySet().iterator();
200       while (it.hasNext())
201       {
202          Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
203          ArrayList JavaDoc l = (ArrayList JavaDoc)entry.getValue();
204          urlParameters.put(entry.getKey(), l.toArray(new String JavaDoc[l.size()]));
205       }
206       return urlParameters;
207    }
208
209    /**
210     * Add a named parameter. Cater for multiple parameters with the same name
211     * by storing them in a list.
212     *
213     * @param name
214     * @param value
215     */

216    public void addParameter(String JavaDoc name, String JavaDoc value)
217    {
218       List JavaDoc l;
219
220       Object JavaDoc o = parameters.get(name);
221       if (o == null)
222       {
223          l = new ArrayList JavaDoc(5);
224          parameters.put(name, l);
225
226       }
227       else
228       {
229          l = (List JavaDoc)o;
230       }
231       l.add(value);
232    }
233
234    /**
235     * Set up the environment for generating the PortletURL and
236     * get the base PortletURL to decorate. Includes setting the "type"
237     * parameter from the implementing subclass.
238     *
239     * @return The PortelURL to decorate
240     */

241    private PortletURL getBasePortletEnvironmentAndURL()
242    {
243       // Get the invocation
244
Invocation invocation = getInvocation();
245
246       // Get required values out of the invocation
247
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       // Update state
253
setPortalRequest(preq);
254       setPortalResponse(presp);
255       setWindowContext(windowCtx);
256
257       // Create the URL
258
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 JavaDoc("Impossible - it's a bug");
269       }
270    }
271
272    /**
273     * Set the window state for the URL to be what was given in the tag.
274     * If not given, default to what was there previously
275     *
276     * @param portletURL
277     * @throws WindowStateException
278     */

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    /**
288     * Set the portlet mode for the URL to be what was given in the tag.
289     * If not given, default to what was there previously
290     *
291     * @param portletURL
292     * @throws PortletModeException
293     */

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    /**
303     * Set the secure/unsecure state for the URL to be what was given in the tag.
304     * If not given, default to what was there previously
305     *
306     * @param portletURL
307     * @throws PortletSecurityException
308     */

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          // Maybe we don't need to set this again
318
portletURL.setSecure(getPortalRequest().isSecure());
319       }
320    }
321
322    private void clearPreviousTag()
323    {
324       parameters = new HashMap JavaDoc(5);
325       windowCtx = null;
326       preq = null;
327    }
328
329    /**
330     * Get the type - action or render - from the implementing subclass
331     *
332     * @return the value for the "type" portal parameter
333     */

334    protected abstract String JavaDoc getTypeValue();
335
336    /**
337     * Process the body, which may contain portlet:param tags
338     */

339    public int doStartTag() throws JspException JavaDoc
340    {
341       clearPreviousTag();
342       return EVAL_BODY_INCLUDE;
343    }
344
345    /**
346     * Generate the URL
347     */

348    public int doEndTag() throws JspException JavaDoc
349    {
350       String JavaDoc resultingURL = null;
351       try
352       {
353
354          PortletURL newPortletURL = getBasePortletEnvironmentAndURL();
355
356          setWindowState(newPortletURL);
357
358          setPortletMode(newPortletURL);
359
360          setSecure(newPortletURL);
361
362          // Include parameters as part of the tag request.
363

364          if (!parameters.isEmpty())
365          {
366             newPortletURL.setParameters(getURLParameters());
367          }
368
369          resultingURL = newPortletURL.toString();
370
371          // If a variable was given in the tag to be set in the pageContext,
372
// do that. Otherwise, just write out the URL to the page.
373

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 JavaDoc e)
385       {
386          e.printStackTrace();
387          throw new JspException JavaDoc(e);
388       }
389       return EVAL_PAGE;
390    }
391 }
392
Popular Tags