KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > portal > generic > taglib > AbstractUrlTag


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution, if
19  * any, must include the following acknowlegement:
20  * "This product includes software developed by the
21  * Caucho Technology (http://www.caucho.com/)."
22  * Alternately, this acknowlegement may appear in the software itself,
23  * if and wherever such third-party acknowlegements normally appear.
24  *
25  * 4. The names "Hessian", "Resin", and "Caucho" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * info@caucho.com.
29  *
30  * 5. Products derived from this software may not be called "Resin"
31  * nor may "Resin" appear in their names without prior written
32  * permission of Caucho Technology.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
38  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
40  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
43  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
44  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  * @author Sam
47  */

48
49
50 package com.caucho.portal.generic.taglib;
51
52 import com.caucho.portal.generic.InvocationURL;
53
54 import javax.portlet.*;
55 import javax.servlet.ServletRequest JavaDoc;
56 import javax.servlet.jsp.JspException JavaDoc;
57 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
58 import java.io.IOException JavaDoc;
59 import java.util.ArrayList JavaDoc;
60 import java.util.Iterator JavaDoc;
61 import java.util.LinkedHashMap JavaDoc;
62 import java.util.Map JavaDoc;
63
64
65 abstract public class AbstractUrlTag extends TagSupport JavaDoc {
66   // -- attributes --
67
private String JavaDoc _var;
68   private WindowState _windowState;
69   private PortletMode _portletMode;
70   private Boolean JavaDoc _secure;
71
72   // -- non-attributes --
73

74   private LinkedHashMap JavaDoc<String JavaDoc,Object JavaDoc> _paramMap
75     = new LinkedHashMap JavaDoc<String JavaDoc,Object JavaDoc>();
76   private LinkedHashMap JavaDoc<String JavaDoc,LinkedHashMap JavaDoc<String JavaDoc,Object JavaDoc>> _namespaceParamMap
77     = new LinkedHashMap JavaDoc<String JavaDoc,LinkedHashMap JavaDoc<String JavaDoc,Object JavaDoc>>();
78
79   abstract protected PortletURL createPortletURL(RenderResponse response);
80
81   /**
82    * The variable to store the url in. If this is not set,
83    * the url is printed to the output writer.
84    */

85   public void setVar(String JavaDoc var)
86   {
87     _var = var;
88   }
89
90   /**
91    * The window state. If not set, the current window state is maintained.
92    */

93   public void setWindowState(String JavaDoc windowState)
94   {
95     _windowState = new WindowState(windowState);
96   }
97
98   /**
99    * The portlet mode. If not set, the current portlet mode is maintained.
100    */

101   public void setPortletMode(String JavaDoc portletMode)
102   {
103     _portletMode = new PortletMode(portletMode);
104   }
105
106   /**
107    * Indicates if the resulting url should be a secure connection
108    * (secure="true") or an insecure (secure="false"). If not set, the current
109    * security setting is maintained.
110    */

111   public void setSecure(String JavaDoc secure)
112     throws JspException JavaDoc
113   {
114     if (secure == null)
115       _secure = null;
116     else if (secure.equalsIgnoreCase("true"))
117       _secure = Boolean.TRUE;
118     else if (secure.equalsIgnoreCase("false"))
119       _secure = Boolean.FALSE;
120     else
121       throw new JspException JavaDoc("secure must be `true' or `false' or unspecified");
122   }
123
124   public int doStartTag()
125     throws JspException JavaDoc
126   {
127     _paramMap.clear();
128     _namespaceParamMap.clear();
129
130     return EVAL_BODY_INCLUDE;
131   }
132
133   /**
134    * Add a parameter.
135    */

136   public void addParam(String JavaDoc name, String JavaDoc value)
137   {
138     addParam(_paramMap, name, value);
139   }
140
141   /**
142    * Add a parameter for a particular namespace.
143    */

144   public void addParam(String JavaDoc namespace, String JavaDoc name, String JavaDoc value)
145   {
146     LinkedHashMap JavaDoc<String JavaDoc,Object JavaDoc> map = _namespaceParamMap.get(namespace);
147
148     if (map == null) {
149       map = new LinkedHashMap JavaDoc<String JavaDoc,Object JavaDoc>();
150       _namespaceParamMap.put(namespace,map);
151     }
152
153     addParam(map, name, value);
154   }
155
156   private void addParam( LinkedHashMap JavaDoc<String JavaDoc, Object JavaDoc> map,
157                          String JavaDoc name,
158                          String JavaDoc value )
159   {
160     // slight optimization, assume that the parameter doesn't exist yet
161
Object JavaDoc current = map.put(name, value);
162
163     if (current != null) {
164       if (current instanceof ArrayList JavaDoc) {
165         ArrayList JavaDoc<String JavaDoc> list = (ArrayList JavaDoc<String JavaDoc>) current;
166         list.add(value);
167         map.put(name, list);
168       }
169       else {
170         ArrayList JavaDoc<String JavaDoc> list = new ArrayList JavaDoc<String JavaDoc>();
171         list.add((String JavaDoc) current);
172         list.add(value);
173         map.put(name, list);
174       }
175     }
176   }
177
178   public int doEndTag()
179     throws JspException JavaDoc
180   {
181     PortletURL url;
182
183     ServletRequest JavaDoc request = pageContext.getRequest();
184     RenderResponse response = (RenderResponse)
185       request.getAttribute("javax.portlet.renderResponse");
186
187     if (response == null)
188       throw new JspException JavaDoc("No RenderResponse available");
189
190     url = createPortletURL(response);
191
192     if (_windowState != null) {
193       try {
194         url.setWindowState(_windowState);
195       }
196       catch (WindowStateException ex) {
197         throw new JspException JavaDoc(ex);
198       }
199     }
200
201     if (_portletMode != null) {
202       try {
203         url.setPortletMode(_portletMode);
204       }
205       catch (PortletModeException ex) {
206         throw new JspException JavaDoc(ex);
207       }
208     }
209
210     if (_secure != null) {
211       try {
212         if (_secure == Boolean.FALSE)
213           url.setSecure(false);
214         else
215           url.setSecure(true);
216       }
217       catch (PortletSecurityException ex) {
218         throw new JspException JavaDoc(ex);
219       }
220     }
221
222     if (_paramMap.size() > 0) {
223       Iterator JavaDoc<Map.Entry JavaDoc<String JavaDoc,Object JavaDoc>> iter = _paramMap.entrySet().iterator();
224
225       while (iter.hasNext()) {
226         Map.Entry JavaDoc<String JavaDoc,Object JavaDoc> entry = iter.next();
227
228         String JavaDoc name = entry.getKey();
229         Object JavaDoc value = entry.getValue();
230
231         if (value instanceof String JavaDoc)
232           url.setParameter(name,(String JavaDoc) value);
233         else
234           url.setParameter(name, makeStringArray( (ArrayList JavaDoc<String JavaDoc>) value));
235       }
236     }
237
238     if (_namespaceParamMap.size() > 0) {
239       InvocationURL invocationUrl = (InvocationURL) url;
240
241       Iterator JavaDoc<Map.Entry JavaDoc<String JavaDoc,LinkedHashMap JavaDoc<String JavaDoc,Object JavaDoc>>> iter
242         = _namespaceParamMap.entrySet().iterator();
243
244       while (iter.hasNext()) {
245         Map.Entry JavaDoc<String JavaDoc,LinkedHashMap JavaDoc<String JavaDoc,Object JavaDoc>> entry = iter.next();
246
247         String JavaDoc namespace = entry.getKey();
248         LinkedHashMap JavaDoc<String JavaDoc,Object JavaDoc> map = entry.getValue();
249
250         Iterator JavaDoc<Map.Entry JavaDoc<String JavaDoc,Object JavaDoc>> mapIter = map.entrySet().iterator();
251
252         while (mapIter.hasNext()) {
253           Map.Entry JavaDoc<String JavaDoc,Object JavaDoc> mapEntry = mapIter.next();
254
255           String JavaDoc name = mapEntry.getKey();
256           Object JavaDoc value = mapEntry.getValue();
257
258           if (value instanceof String JavaDoc) {
259             invocationUrl.setParameter(namespace, name, (String JavaDoc) value);
260           }
261           else {
262             String JavaDoc[] values = makeStringArray( (ArrayList JavaDoc<String JavaDoc>) value);
263             invocationUrl.setParameter( namespace, name, values );
264           }
265         }
266       }
267     }
268
269     String JavaDoc urlString = url.toString();
270
271     if (_var != null) {
272       pageContext.setAttribute(_var, urlString);
273     }
274     else {
275       try {
276         pageContext.getOut().print(urlString);
277       }
278       catch (IOException JavaDoc ex) {
279         throw new JspException JavaDoc(ex);
280       }
281     }
282
283     return EVAL_PAGE;
284   }
285
286   private String JavaDoc[] makeStringArray(ArrayList JavaDoc<String JavaDoc> list)
287   {
288     int sz = list.size();
289
290     String JavaDoc[] array = new String JavaDoc[sz];
291
292     for (int i = 0; i < sz; i++) {
293       array[i] = list.get(i);
294     }
295
296     return array;
297   }
298 }
299
Popular Tags