KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > wsrp > consumer > URLTemplateComposerImpl


1 /*
2  * Copyright 2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.portal.wsrp.consumer;
17
18 import org.apache.cocoon.portal.coplet.CopletInstanceData;
19 import org.apache.cocoon.portal.wsrp.adapter.WSRPAdapter;
20 import org.apache.wsrp4j.consumer.URLGenerator;
21 import org.apache.wsrp4j.consumer.URLTemplateComposer;
22 import org.apache.wsrp4j.util.Constants;
23
24 /**
25  * Implements the {@link org.apache.wsrp4j.consumer.URLTemplateComposer} interface
26  * providing methods to generate URL templates.<br/>
27  * The generated templates will be transmitted to producers (or respectively portlets)
28  * that are willing to properly write URLs for a consumer. (With templates the consumer
29  * indicates how it needs URLs formatted in order to process them properly.)
30  *
31  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
32  * @author <a HREF="mailto:malessandrini@s-und-n.de">Michel Alessandrini</a>
33  *
34  * @version $Id: URLTemplateComposerImpl.java 264755 2005-08-30 10:29:21Z cziegeler $
35  */

36 public class URLTemplateComposerImpl
37     implements URLTemplateComposer, RequiresWSRPAdapter {
38
39     /** The url generator. */
40     protected URLGenerator urlGenerator;
41
42     /** The wsrp adapter. */
43     protected WSRPAdapter adapter;
44
45     /**
46      * @see org.apache.cocoon.portal.wsrp.consumer.RequiresWSRPAdapter#setWSRPAdapter(org.apache.cocoon.portal.wsrp.adapter.WSRPAdapter)
47      */

48     public void setWSRPAdapter(WSRPAdapter adapter) {
49         this.adapter = adapter;
50     }
51
52     /**
53      * @see org.apache.wsrp4j.consumer.URLTemplateComposer#setURLGenerator(org.apache.wsrp4j.consumer.URLGenerator)
54      */

55     public void setURLGenerator(URLGenerator urlGenerator) {
56         this.urlGenerator = urlGenerator;
57     }
58
59     /**
60      * @see org.apache.wsrp4j.consumer.URLTemplateComposer#createBlockingActionTemplate(boolean, boolean, boolean, boolean)
61      */

62     public String JavaDoc createBlockingActionTemplate(boolean includePortletHandle,
63                                                boolean includeUserContextKey,
64                                                boolean includePortletInstanceKey,
65                                                boolean includeSessionID) {
66
67         return createTemplate(
68                     urlGenerator.getBlockingActionURL(null),
69                     true,
70                     true,
71                     true,
72                     true,
73                     true,
74                     false,
75                     false,
76                     false,
77                     includePortletHandle,
78                     includeUserContextKey,
79                     includePortletInstanceKey,
80                     includeSessionID);
81     }
82
83     /**
84      * @see org.apache.wsrp4j.consumer.URLTemplateComposer#createSecureBlockingActionTemplate(boolean, boolean, boolean, boolean)
85      */

86     public String JavaDoc createSecureBlockingActionTemplate(boolean includePortletHandle,
87                                                      boolean includeUserContextKey,
88                                                      boolean includePortletInstanceKey,
89                                                      boolean includeSessionID) {
90         return createTemplate(
91                     urlGenerator.getBlockingActionURL(null),
92                     true,
93                     true,
94                     true,
95                     true,
96                     true,
97                     true,
98                     false,
99                     false,
100                     includePortletHandle,
101                     includeUserContextKey,
102                     includePortletInstanceKey,
103                     includeSessionID);
104     }
105
106     /**
107      * @see org.apache.wsrp4j.consumer.URLTemplateComposer#createRenderTemplate(boolean, boolean, boolean, boolean)
108      */

109     public String JavaDoc createRenderTemplate(boolean includePortletHandle,
110                                        boolean includeUserContextKey,
111                                        boolean includePortletInstanceKey,
112                                        boolean includeSessionID) {
113         return createTemplate(
114                     urlGenerator.getRenderURL(null),
115                     true,
116                     true,
117                     true,
118                     true,
119                     true,
120                     false,
121                     false,
122                     false,
123                     includePortletHandle,
124                     includeUserContextKey,
125                     includePortletInstanceKey,
126                     includeSessionID);
127     }
128
129     /**
130      * @see org.apache.wsrp4j.consumer.URLTemplateComposer#createSecureRenderTemplate(boolean, boolean, boolean, boolean)
131      */

132     public String JavaDoc createSecureRenderTemplate(boolean includePortletHandle,
133                                              boolean includeUserContextKey,
134                                              boolean includePortletInstanceKey,
135                                              boolean includeSessionID) {
136         return createTemplate(
137                     urlGenerator.getRenderURL(null),
138                     true,
139                     true,
140                     true,
141                     true,
142                     true,
143                     true,
144                     false,
145                     false,
146                     includePortletHandle,
147                     includeUserContextKey,
148                     includePortletInstanceKey,
149                     includeSessionID);
150     }
151
152     /**
153      * @see org.apache.wsrp4j.consumer.URLTemplateComposer#createResourceTemplate(boolean, boolean, boolean, boolean)
154      */

155     public String JavaDoc createResourceTemplate(boolean includePortletHandle,
156                                          boolean includeUserContextKey,
157                                          boolean includePortletInstanceKey,
158                                          boolean includeSessionID) {
159         return createTemplate(
160                     urlGenerator.getResourceURL(null),
161                     true,
162                     false,
163                     false,
164                     false,
165                     false,
166                     false,
167                     true,
168                     true,
169                     includePortletHandle,
170                     includeUserContextKey,
171                     includePortletInstanceKey,
172                     includeSessionID);
173     }
174
175     /**
176      * @see org.apache.wsrp4j.consumer.URLTemplateComposer#createSecureResourceTemplate(boolean, boolean, boolean, boolean)
177      */

178     public String JavaDoc createSecureResourceTemplate(boolean includePortletHandle,
179                                                boolean includeUserContextKey,
180                                                boolean includePortletInstanceKey,
181                                                boolean includeSessionID) {
182         return createTemplate(
183                     urlGenerator.getResourceURL(null),
184                     true,
185                     false,
186                     false,
187                     false,
188                     false,
189                     true,
190                     true,
191                     true,
192                     includePortletHandle,
193                     includeUserContextKey,
194                     includePortletInstanceKey,
195                     includeSessionID);
196     }
197
198     /**
199      * @see org.apache.wsrp4j.consumer.URLTemplateComposer#createDefaultTemplate(boolean, boolean, boolean, boolean)
200      */

201     public String JavaDoc createDefaultTemplate(boolean includePortletHandle,
202                                         boolean includeUserContextKey,
203                                         boolean includePortletInstanceKey,
204                                         boolean includeSessionID) {
205         return createTemplate(
206                     urlGenerator.getRenderURL(null),
207                     true,
208                     true,
209                     true,
210                     true,
211                     true,
212                     false,
213                     true,
214                     true,
215                     includePortletHandle,
216                     includeUserContextKey,
217                     includePortletInstanceKey,
218                     includeSessionID);
219     }
220
221     /**
222      * @see org.apache.wsrp4j.consumer.URLTemplateComposer#createSecureDefaultTemplate(boolean, boolean, boolean, boolean)
223      */

224     public String JavaDoc createSecureDefaultTemplate(boolean includePortletHandle,
225                                               boolean includeUserContextKey,
226                                               boolean includePortletInstanceKey,
227                                               boolean includeSessionID) {
228         return createTemplate(
229                     urlGenerator.getRenderURL(null),
230                     true,
231                     true,
232                     true,
233                     true,
234                     true,
235                     true,
236                     true,
237                     true,
238                     includePortletHandle,
239                     includeUserContextKey,
240                     includePortletInstanceKey,
241                     includeSessionID);
242     }
243
244     /**
245      * @see org.apache.wsrp4j.consumer.URLTemplateComposer#getNamespacePrefix()
246      */

247     public String JavaDoc getNamespacePrefix() {
248         final CopletInstanceData coplet = this.adapter.getCurrentCopletInstanceData();
249         return coplet.getId();
250     }
251
252     /**
253      * creates the url for the producer<br/>
254      *
255      * @param url
256      * @param needsURLType
257      * @param needsPortletMode
258      * @param needsNavState
259      * @param needsInteractionState
260      * @param needsWinState
261      * @param needsSecURL
262      * @param needsURL
263      * @param needsRewriteResource
264      * @param needsPortletHandle
265      * @param needsUserContextKey
266      * @param needsPortletInstanceKey
267      * @param needsSessionID
268      * @return url for the producer
269      */

270     protected String JavaDoc createTemplate(String JavaDoc url,
271                                     boolean needsURLType,
272                                     boolean needsPortletMode,
273                                     boolean needsNavState,
274                                     boolean needsInteractionState,
275                                     boolean needsWinState,
276                                     boolean needsSecURL,
277                                     boolean needsURL,
278                                     boolean needsRewriteResource,
279                                     boolean needsPortletHandle,
280                                     boolean needsUserContextKey,
281                                     boolean needsPortletInstanceKey,
282                                     boolean needsSessionID) {
283
284         StringBuffer JavaDoc template = new StringBuffer JavaDoc();
285         StringBuffer JavaDoc remainder = null;
286
287         boolean isFirstParam = true;
288         int index;
289
290         // check if url already contains parameters
291
if ((index = url.indexOf(Constants.PARAMS_START)) != -1) {
292             template.append(url.substring(0, index));
293             remainder = new StringBuffer JavaDoc(url.substring(index + 1));
294         } else {
295             template.append(url.toString());
296         }
297
298         if (needsURLType) {
299             if (isFirstParam) {
300                 template.append(Constants.PARAMS_START);
301                 isFirstParam = false;
302             }
303             template.append(insertPair(Constants.URL_TYPE));
304         }
305
306         if (needsPortletMode) {
307             if (isFirstParam) {
308                 template.append(Constants.PARAMS_START);
309                 isFirstParam = false;
310             } else {
311                 template.append(Constants.NEXT_PARAM);
312             }
313             template.append(insertPair(Constants.PORTLET_MODE));
314         }
315
316         if (needsNavState) {
317             if (isFirstParam) {
318                 template.append(Constants.PARAMS_START);
319                 isFirstParam = false;
320             } else {
321                 template.append(Constants.NEXT_PARAM);
322             }
323             template.append(insertPair(Constants.NAVIGATIONAL_STATE));
324         }
325
326         if (needsInteractionState) {
327             if (isFirstParam) {
328                 template.append(Constants.PARAMS_START);
329                 isFirstParam = false;
330             } else {
331                 template.append(Constants.NEXT_PARAM);
332             }
333             template.append(insertPair(Constants.INTERACTION_STATE));
334         }
335
336         if (needsWinState) {
337             if (isFirstParam) {
338                 template.append(Constants.PARAMS_START);
339                 isFirstParam = false;
340             } else {
341                 template.append(Constants.NEXT_PARAM);
342             }
343             template.append(insertPair(Constants.WINDOW_STATE));
344         }
345
346         if (needsSecURL) {
347             if (isFirstParam) {
348                 template.append(Constants.PARAMS_START);
349                 isFirstParam = false;
350             } else {
351                 template.append(Constants.NEXT_PARAM);
352             }
353             template.append(insertPair(Constants.SECURE_URL));
354         }
355
356         if (needsURL) {
357             if (isFirstParam) {
358                 template.append(Constants.PARAMS_START);
359                 isFirstParam = false;
360             } else {
361                 template.append(Constants.NEXT_PARAM);
362             }
363             template.append(insertPair(Constants.URL));
364         }
365
366         if (needsRewriteResource) {
367             if (isFirstParam) {
368                 template.append(Constants.PARAMS_START);
369                 isFirstParam = false;
370             } else {
371                 template.append(Constants.NEXT_PARAM);
372             }
373             template.append(insertPair(Constants.REWRITE_RESOURCE));
374         }
375
376         if (needsPortletHandle) {
377             if (isFirstParam) {
378                 template.append(Constants.PARAMS_START);
379                 isFirstParam = false;
380             } else {
381                 template.append(Constants.NEXT_PARAM);
382             }
383             template.append(insertPair(Constants.PORTLET_HANDLE));
384         }
385
386         if (needsUserContextKey) {
387             if (isFirstParam) {
388                 template.append(Constants.PARAMS_START);
389                 isFirstParam = false;
390             } else {
391                 template.append(Constants.NEXT_PARAM);
392             }
393             template.append(insertPair(Constants.USER_CONTEXT_KEY));
394         }
395
396         if (needsPortletInstanceKey) {
397             if (isFirstParam) {
398                 template.append(Constants.PARAMS_START);
399                 isFirstParam = false;
400             } else {
401                 template.append(Constants.NEXT_PARAM);
402             }
403             template.append(insertPair(Constants.PORTLET_INSTANCE_KEY));
404         }
405
406         if (needsSessionID) {
407             if (isFirstParam) {
408                 template.append(Constants.PARAMS_START);
409                 isFirstParam = false;
410             } else {
411                 template.append(Constants.NEXT_PARAM);
412             }
413             template.append(insertPair(Constants.SESSION_ID));
414         }
415
416         // append remainder (static parameters)
417
if (remainder != null) {
418             template.append(Constants.NEXT_PARAM);
419             template.append(remainder);
420         }
421
422         return template.toString();
423     }
424
425     /**
426      * creates a pair of an attribute<br/>
427      *
428      * @param token
429      * @return String with the following format: token={token}
430      */

431     protected String JavaDoc insertPair(String JavaDoc token) {
432         StringBuffer JavaDoc result = new StringBuffer JavaDoc(token);
433         result.append(Constants.EQUALS);
434         result.append(Constants.REPLACE_START);
435         result.append(token);
436         result.append(Constants.REPLACE_END);
437
438         return result.toString();
439     }
440 }
441
Popular Tags