KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > portal > generic > InvocationURL


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;
51
52 import javax.portlet.PortletMode;
53 import javax.portlet.PortletModeException;
54 import javax.portlet.PortletSecurityException;
55 import javax.portlet.WindowState;
56 import javax.portlet.WindowStateException;
57 import java.util.Collections JavaDoc;
58 import java.util.Enumeration JavaDoc;
59 import java.util.Iterator JavaDoc;
60 import java.util.Map JavaDoc;
61
62 abstract public class InvocationURL
63 {
64   private InvocationFactory _invocationFactory;
65   private String JavaDoc _namespace;
66   private Invocation _invocation;
67
68   private Boolean JavaDoc _isSecure;
69
70   /**
71    * @param invocationFactory the InvocationFactory
72    *
73    * @param namespace the namespace that is the default target for setting
74    * parameters, portlet mode, and window state
75    */

76   public InvocationURL( InvocationFactory invocationFactory,
77                         String JavaDoc namespace )
78   {
79     _invocationFactory = invocationFactory;
80     _namespace = namespace;
81   }
82
83   public String JavaDoc getNamespace()
84   {
85     return _namespace;
86   }
87
88   protected Invocation getInvocation()
89   {
90     if (_invocation == null)
91       _invocation = _invocationFactory.getInvocation(_namespace);
92
93     return _invocation;
94   }
95
96   protected Invocation getInvocation(String JavaDoc namespace)
97   {
98     return _invocationFactory.getInvocation(namespace);
99   }
100
101   protected void setWindowState(Invocation invocation, WindowState windowState)
102     throws WindowStateException
103   {
104     invocation.setWindowState(windowState);
105   }
106
107   public void setWindowState(WindowState windowState)
108     throws WindowStateException
109   {
110     setWindowState(getInvocation(), windowState);
111   }
112
113   public void setWindowState(String JavaDoc namespace, WindowState windowState)
114     throws WindowStateException
115   {
116     setWindowState(getInvocation(namespace), windowState);
117   }
118
119   protected void setPortletMode(Invocation invocation, PortletMode portletMode)
120     throws PortletModeException
121   {
122     invocation.setPortletMode(portletMode);
123   }
124
125   public void setPortletMode(PortletMode portletMode)
126     throws PortletModeException
127   {
128     setPortletMode(getInvocation(), portletMode);
129   }
130
131   public void setPortletMode(String JavaDoc namespace, PortletMode portletMode)
132     throws PortletModeException
133   {
134     setPortletMode(getInvocation(namespace), portletMode);
135   }
136
137   protected Map JavaDoc<String JavaDoc, String JavaDoc[]> getRenderParameterMap()
138   {
139     return getInvocation().getParameterMap();
140   }
141
142   protected Map JavaDoc<String JavaDoc, String JavaDoc[]> getRenderParameterMap(String JavaDoc namespace)
143   {
144     return getInvocation(namespace).getParameterMap();
145   }
146
147   private void checkNullName(String JavaDoc name)
148   {
149     if (name == null)
150       throw new IllegalArgumentException JavaDoc("parameter name cannot be null");
151   }
152
153   private void checkNullValue(Object JavaDoc value)
154   {
155     if (value == null)
156       throw new IllegalArgumentException JavaDoc("parameter value cannot be null");
157   }
158
159   protected String JavaDoc getParameterValue(Map JavaDoc<String JavaDoc, String JavaDoc[]> map, String JavaDoc name)
160   {
161     checkNullName(name);
162
163     String JavaDoc values[] = map.get(name);
164
165     return values == null || values.length == 0 ? null : values[0];
166   }
167
168   protected String JavaDoc[] getParameterValues( Map JavaDoc<String JavaDoc, String JavaDoc[]> map,
169                                          String JavaDoc name)
170   {
171     checkNullName(name);
172     return map.get(name);
173   }
174
175   protected Enumeration JavaDoc getParameterNames(Map JavaDoc<String JavaDoc, String JavaDoc[]> map)
176   {
177     return Collections.enumeration(map.keySet());
178   }
179
180   protected void setParameter( Map JavaDoc<String JavaDoc, String JavaDoc[]> map,
181                                String JavaDoc name,
182                                String JavaDoc value)
183   {
184     checkNullName(name);
185     checkNullValue(value);
186
187     map.put(name, new String JavaDoc[] { value });
188   }
189
190   protected void setParameters( Map JavaDoc<String JavaDoc, String JavaDoc[]> destMap,
191                                 Map JavaDoc<String JavaDoc, String JavaDoc[]> srcMap)
192   {
193     checkNullValue(srcMap);
194     destMap.clear();
195
196     Iterator JavaDoc<Map.Entry JavaDoc<String JavaDoc, String JavaDoc[]>> iter
197       = srcMap.entrySet().iterator();
198
199     while (iter.hasNext())
200     {
201       Map.Entry JavaDoc<String JavaDoc, String JavaDoc[]> entry = iter.next();
202
203       setParameter(destMap, entry.getKey(), entry.getValue());
204     }
205   }
206
207   protected void setParameter( Map JavaDoc<String JavaDoc, String JavaDoc[]> map,
208                                String JavaDoc name,
209                                String JavaDoc[] values)
210   {
211     checkNullName(name);
212     checkNullValue(values);
213
214     if (values.length == 0)
215       map.remove(name);
216     else
217       map.put(name, values);
218   }
219
220   public void setParameter(String JavaDoc name, String JavaDoc value)
221   {
222     setParameter(getRenderParameterMap(), name, value);
223   }
224
225   public void setParameter(String JavaDoc name, String JavaDoc[] values)
226   {
227     setParameter(getRenderParameterMap(), name, values);
228   }
229
230   public void setParameters(Map JavaDoc<String JavaDoc, String JavaDoc[]> parameters)
231   {
232     setParameters(getRenderParameterMap(), parameters);
233   }
234
235   public void setParameter(String JavaDoc namespace, String JavaDoc name, String JavaDoc value)
236   {
237     setParameter(getRenderParameterMap(namespace), name, value);
238   }
239
240   public void setParameter(String JavaDoc namespace, String JavaDoc name, String JavaDoc[] values)
241   {
242     setParameter(getRenderParameterMap(namespace), name, values);
243   }
244
245   public void setParameters(String JavaDoc namespace, Map JavaDoc<String JavaDoc, String JavaDoc[]> parameters)
246   {
247     setParameters(getRenderParameterMap(namespace), parameters);
248   }
249
250   public void setSecure(boolean secure)
251     throws PortletSecurityException
252   {
253     _isSecure = secure ? Boolean.TRUE : Boolean.FALSE;
254   }
255
256   /**
257    * True if setSecure() was called for this url, even if it was
258    * called with setSecure(false)
259    */

260   protected boolean isSecureSpecified()
261   {
262     return _isSecure != null;
263   }
264
265   protected boolean isSecure()
266   {
267     return _isSecure == null || _isSecure == Boolean.FALSE;
268   }
269
270   /**
271    * Return a partially formed URL, it is then resolved to a full URL by the
272    * Portal.
273    */

274   abstract public String JavaDoc getURL();
275 }
276
Popular Tags