KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > util > template > BaseJetspeedLink


1 /*
2  * Copyright 2000-2001,2004 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
17 package org.apache.jetspeed.util.template;
18
19 // Jetspeed
20
import org.apache.jetspeed.om.profile.Entry;
21 import org.apache.jetspeed.om.profile.Profile;
22 import org.apache.jetspeed.om.profile.Portlets;
23 import org.apache.jetspeed.om.profile.ProfileException;
24 import org.apache.jetspeed.om.profile.ProfileLocator;
25 import org.apache.jetspeed.portal.Portlet;
26 import org.apache.jetspeed.services.Profiler;
27 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
28 import org.apache.jetspeed.services.logging.JetspeedLogger;
29 import org.apache.jetspeed.services.resources.JetspeedResources;
30 import org.apache.jetspeed.services.rundata.JetspeedRunData;
31 import org.apache.jetspeed.util.template.JetspeedLink;
32
33 // Turbine
34
import org.apache.turbine.util.RunData;
35 import org.apache.turbine.util.DynamicURI;
36 import org.apache.turbine.services.pull.ApplicationTool;
37
38 /**
39  * <p>A customized version of the TemplateLink which can handle portlet
40  * references.</p>
41  *
42  * <p>It is inserted into the template context by Turbine, via request tools.</p>
43  *
44  * <p>Each portlet must call setPortlet(this) on it before entering the template
45  * rendering code. This is done currently in VelocityPortlet.</p>
46  *
47  * @author <a HREF="mailto:paulsp@apache.org">Paul Spencer</a>
48  * @version $Id: BaseJetspeedLink.java,v 1.23 2004/02/23 03:20:45 jford Exp $
49  */

50 public class BaseJetspeedLink implements ApplicationTool, JetspeedLink
51 {
52     /**
53      * Static initialization of the logger for this class
54      */

55     private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseJetspeedLink.class.getName());
56     
57     /**
58      *<p>Request to which we refer.</p>
59      */

60     private JetspeedRunData rundata = null;
61
62     /**
63      * Profile locator from <code>rundata</code>. This is here
64      * for performance reasons.
65      */

66     private ProfileLocator locator = null;
67
68     /**
69      * Profile from <code>rundata</code>. This is here
70      * for performance reasons.
71      */

72     private Profile profile = null;
73
74     /**
75      * Has the initialization for the current rundata been performed?. This is
76      * here for performance reasons.
77      */

78     private boolean initDone = false;
79
80     /**
81      *<p>The portlet that will be used to build the reference.</p>
82      */

83     protected Portlet activePortlet = null;
84
85     /**
86      * Empty constructor.for introspection
87      */

88     public BaseJetspeedLink()
89     {
90     }
91
92     /**
93      * Constructor required by ApplicationTool interface
94      *
95      * @param data A Jetspeed RunData object.
96      */

97     public BaseJetspeedLink(RunData data)
98     {
99         init((Object JavaDoc) data);
100     }
101
102     /**
103      * This will initialise a JetspeedLink object that was
104      * constructed with the default constructor
105      *
106      * @param rundata to be a RunData object
107      */

108     public void init(RunData rundata)
109     {
110         init((Object JavaDoc) rundata);
111     }
112
113     /**
114      * Adds a name=value pair to the query string.
115      *
116      * @param name A String with the name to add.
117      * @param value An Object with the value to add.
118      * @return DynamicURI that to the desired page
119      */

120     public DynamicURI addQueryData(String JavaDoc name, Object JavaDoc value)
121     {
122         try
123         {
124             return getRoot().addQueryData(name, value);
125         }
126         catch (ProfileException e)
127         {
128             logger.error("Exception", e);
129             return null;
130         }
131     }
132
133     /**
134      * Adds a name=value pair to the path_info string.
135      *
136      * @param name A String with the name to add.
137      * @param value An Object with the value to add.
138      * @return DynamicURI that to the desired page
139      */

140     public DynamicURI addPathInfo(String JavaDoc name, Object JavaDoc value)
141     {
142         try
143         {
144             return getRoot().addPathInfo(name, value);
145         }
146         catch (ProfileException e)
147         {
148             logger.error("Exception", e);
149             return null;
150         }
151     }
152
153     /**
154      * Return an link to a specific portal element
155      *
156      * @param peid of the portal element
157      * @return DynamicURI to specific portal element
158      *
159      * @deprecated Use getPortletById() or getPaneById()
160      */

161     public DynamicURI setPortalElement(String JavaDoc peid)
162     {
163         if (initDone == false)
164         {
165             initLink();
166         }
167         if (profile.getDocument().getEntryById(peid) != null)
168         {
169             return getPortletById(peid);
170         }
171         else
172         {
173             return setPaneById(peid);
174         }
175     }
176
177     /**
178      * Return an link to a specific portlet using the portet's id
179      *
180      * @param peid of the portlet
181      * @return DynamicURI to specific portlet
182      *
183      * @deprecated Use getPortletById()
184      */

185     public DynamicURI setPortletById(String JavaDoc peid)
186     {
187         return getPortletById(peid);
188     }
189
190     /**
191      * Return link to the home page without user,
192      * page, group, role, template, action, media type, language, or country
193      * in link.
194      *
195      * @return DynamicURI to the home page
196      */

197     public DynamicURI getHomePage()
198     {
199         return getLink(JetspeedLink.DEFAULT, null, "", JetspeedLink.DEFAULT, null, "", "", "", "", "");
200     }
201
202     /**
203      * Return a link that includes the template
204      * from rundata
205      *
206      * @return DynamicURI to template
207      */

208     public DynamicURI getTemplate()
209     {
210         String JavaDoc template = rundata.getRequestedTemplate();
211         return getLink(JetspeedLink.CURRENT, null, null, JetspeedLink.DEFAULT, null, null, template, null, null, null);
212     }
213
214     /**
215      * Return a link to the template.
216      *
217      * @param template to add to link
218      * @return DynamicURI to specific portlet
219      *
220      * @deprecated Use getTemplate()
221      */

222     public DynamicURI setTemplate(String JavaDoc template)
223     {
224         return getTemplate(template);
225     }
226
227     /**
228      * Return a link that includes an action
229      *
230      * @param action Desired action
231      * @return DynamicURI that includes the desire action
232      *
233      * @deprecated Use getAction()
234      */

235     public DynamicURI setAction(String JavaDoc action)
236     {
237         return getAction(action);
238     }
239
240     /**
241      * Return a link that includes an action to a specific portlet, as defined
242      * by a portlets
243      *
244      * @param action Desired action
245      * @param portlets to receive the action
246      * @return DynamicURI that includes the desire action
247      *
248      * @deprecated Use getAction()
249      */

250     public DynamicURI setAction(String JavaDoc action, Portlets portlets)
251     {
252         return getAction(action, (Portlets) portlets);
253     }
254
255     /**
256      * Return a link that includes an action to a specific portlet
257      *
258      * @param action Desired action
259      * @param portlet to receive the action
260      * @return DynamicURI that includes the desire action
261      *
262      * @deprecated Use getAction()
263      */

264     public DynamicURI setAction(String JavaDoc action, Portlet portlet)
265     {
266         return getAction(action, (Portlet) portlet);
267     }
268
269     /**
270      * Return a link that includes an action to a specific portlet, as defined
271      * by an entry
272      *
273      * @param action Desired action
274      * @param entry to receive the action
275      * @return DynamicURI that includes the desire action
276      *
277      * @deprecated Use getAction()
278      */

279     public DynamicURI setAction(String JavaDoc action, Entry entry)
280     {
281         return getAction(action, (Entry) entry);
282     }
283
284     /**
285      * Return a link that includes an action to a specific portlet, as defined
286      * by a PEID
287      *
288      * @param action Desired action
289      * @param peid Id of portlet to receive the action
290      * @return DynamicURI that includes the desire action
291      * @deprecated Use getAction()
292      */

293     public DynamicURI setAction(String JavaDoc action, String JavaDoc peid)
294     {
295         return getAction(action, (String JavaDoc) peid);
296     }
297
298     /**
299      * Return a link to a default page for the group
300      *
301      * @param group Desired group
302      * @return DynamicURI that to the desired page
303      *
304      * @deprecated Use getGroup()
305      */

306     public DynamicURI setGroup(String JavaDoc group)
307     {
308         return getGroup(group);
309     }
310
311     /**
312      * Return a link to a desired page for the group
313      *
314      * @param page Desired page
315      * @param group Desired group
316      * @return DynamicURI that to the desired page
317      *
318      * @deprecated Use getGroup()
319      */

320     public DynamicURI setGroup(String JavaDoc group, String JavaDoc page)
321     {
322         return getGroup(group, page);
323     }
324
325     /**
326      * Return a link to a default page for the
327      * current user, group, or role.
328      *
329      * @return DynamicURI that to the desired page
330      *
331      * @deprecated Use getPage()
332      */

333     public DynamicURI setPage()
334     {
335         return getPage();
336     }
337
338     /**
339      * Return a link to a desired page for the
340      * current user, group, or role.
341      *
342      * @param page Desired page
343      * @return DynamicURI that to the desired page
344      *
345      * @deprecated Use getPage()
346      */

347     public DynamicURI setPage(String JavaDoc page)
348     {
349         return getPage(page);
350     }
351
352     /**
353      * Return a link to a desired page and pane for the
354      * current user, group, or role.
355      *
356      * @param page Desired page
357      * @param paneName Desired pane name
358      * @return DynamicURI that to the desired page
359      * @deprecated Use getPage()
360      */

361     public DynamicURI setPage(String JavaDoc page, String JavaDoc paneName)
362     {
363         return getPage(page, paneName);
364     }
365
366
367     /**
368      * Return a link to a default page for the role
369      *
370      * @param role Desired role
371      * @return DynamicURI that to the desired page
372      *
373      * @deprecated use getRole()
374      */

375     public DynamicURI setRole(String JavaDoc role)
376     {
377         return getRole(role);
378     }
379
380     /**
381      * Return a link to a desired page for the role
382      *
383      * @param role Desired role
384      * @param page Desired page
385      * @return DynamicURI that to the desired page
386      *
387      * @deprecated use getRole()
388      */

389     public DynamicURI setRole(String JavaDoc role, String JavaDoc page)
390     {
391         return getRole(role, page);
392     }
393
394     /**
395      * Return a link to a default page for the user
396      *
397      * @param user Desired user
398      * @return DynamicURI that to the desired page
399      *
400      * @deprecated Use getUser()
401      */

402     public DynamicURI setUser(String JavaDoc user)
403     {
404         return getUser(user);
405     }
406
407     /**
408      * Return a link to a desired page for the user
409      *
410      * @param page Desired page
411      * @param user Desired user
412      * @return DynamicURI that to the desired page
413      *
414      * @deprecated Use getUser()
415      */

416     public DynamicURI setUser(String JavaDoc user, String JavaDoc page)
417     {
418         return getUser(user, page);
419     }
420
421     /**
422      * Return a link to a specific pane using the pane's id
423      *
424      * @param paneId of the Pane
425      * @return URI to specific portlet
426      *
427      * @deprecated Use getPaneById()
428      */

429     public DynamicURI setPaneById(String JavaDoc paneId)
430     {
431         return getPaneById(paneId);
432     }
433
434     /**
435      * Return a link to a specific pane using the pane's id
436      *
437      * @param paneName Name of the Pane
438      * @return URI to specific portlet
439      * @deprecated Use getPaneByName()
440      */

441     public DynamicURI setPaneByName(String JavaDoc paneName)
442     {
443         return getPaneByName(paneName);
444     }
445
446     /**
447      * Return a link to a desired page. This is allows the inclusion of a Group/Role/User,
448      * page, template, action, media type, language, and country.
449      *
450      * @param rootType Type of root PSML docuument. The should be one of the following:
451      * <dl>
452      * <dt>JetspeedLink.CURRENT</dt><dd>The link will retain the current Group/Role/User referance. rootValue is not used</dd>
453      * <dt>JetspeedLink.DEFAULT</dt><dd>Default Group, Role, or User. rootValue is not used</dd>
454      * <dt>JetspeedLink.GROUP</dt><dd>Link will be to a Group PSML. rootValue is a Group Name</dd>
455      * <dt>JetspeedLink.ROLE</dt><dd>Link will be to a Role PSML. rootValue is a Role Name</dd>
456      * <dt>JetspeedLink.USER</dt><dd>Link will be to a User PSML. rootValue is a User Name</dd>
457      * </dl>
458      * @param rootValue See description of rootType
459      * @param pageName Name of page. null = default page
460      * @param elementType
461      * <dl>
462      * <dt>JetspeedLink.CURRENT</dt><dd>The link will retain the current Pane/Portlet referance. elementValue is not used</dd>
463      * <dt>JetspeedLink.DEFAULT</dt><dd>The link will NOT referance a pane or portlet. elementValue is not used</dd>
464      * <dt>JetspeedLink.PANE_ID</dt><dd>Link will be to a Pane using it's ID. elementValue is a Pane's ID</dd>
465      * <dt>JetspeedLink.PANE_NAME</dt><dd>Link will be to a Pane using it's Name. elementValue is a Pane's Name</dd>
466      * <dt>JetspeedLink.PORTLET_ID</dt><dd>Link will be to a Portlet using it's ID. elementValue is a Portlet's ID</dd>
467      * <dt>JetspeedLink.PORTLET_NAME</dt><dd>Link will be to a Portlet using it's Name. elementValue is a Portlet's Name</dd>
468      * <dt>JetspeedLink.PORTLET_ID_QUERY</dt><dd>Link will be to a Portlet using it's ID based on portlet name provided. elementValue is a Portlet's name. ID is for the first portlet with matching name</dd>
469      * </dl>
470      * @param elementValue
471      * See description of elementType
472      * @param actionName Name of action. If no action is desired use JetspeedLink.NO_ACTION.
473      * @param templateName
474      * Name of template. If no template is desired use JetspeedLink.NO_TEMPLATE.
475      * @param mediaType Desired media type. null = default media type
476      * @param language Desired language. null = default language
477      * @param country Desired country. null = default language
478      * @return URI to specific portlet
479      */

480     public DynamicURI getLink(int rootType, String JavaDoc rootValue, String JavaDoc pageName, int elementType, String JavaDoc elementValue, String JavaDoc actionName, String JavaDoc templateName, String JavaDoc mediaType, String JavaDoc language, String JavaDoc country)
481     {
482         String JavaDoc uriPathType = null;
483         String JavaDoc uriPathElement = null;
484         try
485         {
486             DynamicURI uri = getRoot();
487
488             // Set Group/Role/User in path
489
switch (rootType)
490             {
491                 case JetspeedLink.DEFAULT:
492                 case JetspeedLink.CURRENT:
493                     break;
494                 case JetspeedLink.GROUP:
495                     uriPathType = Profiler.PARAM_GROUP;
496                     break;
497                 case JetspeedLink.ROLE:
498                     uriPathType = Profiler.PARAM_ROLE;
499                     break;
500                 case JetspeedLink.USER:
501                     uriPathType = Profiler.PARAM_USER;
502                     break;
503             }
504
505             if (rootType != JetspeedLink.CURRENT)
506             {
507                 // Cleanup URI
508
uri.removePathInfo(Profiler.PARAM_GROUP);
509                 uri.removePathInfo(Profiler.PARAM_ROLE);
510                 uri.removePathInfo(Profiler.PARAM_USER);
511
512                 if ((rootType != JetspeedLink.DEFAULT) && (rootValue != null) && (rootValue.trim().length() > 0))
513                 {
514                     uri.addPathInfo(uriPathType, rootValue);
515                 }
516             }
517
518             // Set Page in path
519
if (pageName != null)
520             {
521                 uri.removePathInfo(Profiler.PARAM_PAGE);
522                 if (pageName.trim().length() > 0)
523                 {
524                     uri.addPathInfo(Profiler.PARAM_PAGE, pageName);
525                 }
526             }
527
528             // Set Portlet/Pane in path
529
switch (elementType)
530             {
531                 case JetspeedLink.CURRENT:
532                 case JetspeedLink.DEFAULT:
533                     break;
534                 case JetspeedLink.PANE_ID:
535                     uriPathElement = JetspeedResources.PATH_PANEID_KEY;
536                     break;
537                 case JetspeedLink.PANE_NAME:
538                     uriPathElement = JetspeedResources.PATH_PANENAME_KEY;
539                     break;
540                 case JetspeedLink.PORTLET_ID:
541                     uriPathElement = JetspeedResources.PATH_PORTLETID_KEY;
542                     break;
543                 case JetspeedLink.PORTLET_NAME:
544                     uriPathElement = JetspeedResources.PATH_PORTLET_KEY;
545                     break;
546                 case JetspeedLink.PORTLET_ID_QUERY:
547                     if (logger.isDebugEnabled())
548                     {
549                         logger.debug("BaseJetspeedLink: elementValue = " + elementValue);
550                     }
551                     uriPathElement = JetspeedResources.PATH_PORTLETID_KEY;
552                     ProfileLocator baseLocator = Profiler.createLocator();
553                     Profile baseProfile = null;
554                     switch (rootType)
555                     {
556                         case JetspeedLink.DEFAULT:
557                             break;
558                         case JetspeedLink.CURRENT:
559                             baseProfile = rundata.getProfile();
560                             break;
561                         case JetspeedLink.GROUP:
562                             baseLocator.setGroupByName(rootValue);
563                             break;
564                         case JetspeedLink.ROLE:
565                             baseLocator.setRoleByName(rootValue);
566                             break;
567                         case JetspeedLink.USER:
568                             try
569                             {
570                                 if (logger.isDebugEnabled())
571                                 {
572                                     logger.debug("BaseJetspeedLink: rootValue user = " + rootValue);
573                                 }
574                                 baseLocator.setUser(org.apache.jetspeed.services.JetspeedSecurity.getUser(rootValue));
575                             }
576                             catch (Exception JavaDoc se)
577                             {
578                                 logger.error("Exception", se);
579                                 return null;
580                             }
581                             break;
582                     }
583                     
584                     if ((rootType != JetspeedLink.CURRENT) && (rootType != JetspeedLink.DEFAULT))
585                     {
586                         if (mediaType != null && mediaType.length() > 0)
587                         {
588                             baseLocator.setMediaType(mediaType);
589                         }
590                         if (language != null && language.length() > 0)
591                         {
592                             baseLocator.setLanguage(language);
593                         }
594                         if (country != null && country.length() > 0)
595                         {
596                             baseLocator.setCountry(country);
597                         }
598                         if (pageName != null && pageName.length() > 0)
599                         {
600                             baseLocator.setName(pageName);
601                         }
602                         baseProfile = Profiler.getProfile(baseLocator);
603                     }
604                    
605                     if (logger.isDebugEnabled())
606                     {
607                         logger.debug("BaseJetspeedLink: baseLocator = " + baseLocator.getPath());
608                     }
609
610                     if ((baseProfile != null) && (elementValue != null))
611                     {
612                         if (logger.isDebugEnabled())
613                         {
614                            logger.debug("BaseJetspeedLink: baseProfile = " + baseProfile.toString());
615                         }
616                         if (baseProfile.getDocument() != null)
617                         {
618                             if (logger.isDebugEnabled())
619                             {
620                                 logger.debug("BaseJetspeedLink: baseProfile.getDocment() = " + baseProfile.getDocument());
621                             }
622                             Entry entry = baseProfile.getDocument().getEntry(elementValue);
623                             if (entry != null)
624                             {
625                                 if (logger.isDebugEnabled())
626                                 {
627                                     logger.debug("BaseJetspeedLink: entry id = " + entry.getId());
628                                 }
629                                 elementValue = entry.getId();
630                             }
631                             else
632                             {
633                                 elementValue = null;
634                             }
635                         }
636                     }
637
638                     break;
639             }
640
641             if (elementType != JetspeedLink.CURRENT)
642             {
643                 // Remove Group/Role/User in URI
644
uri.removePathInfo(JetspeedResources.PATH_PANEID_KEY);
645                 uri.removePathInfo(JetspeedResources.PATH_PANENAME_KEY);
646                 uri.removePathInfo(JetspeedResources.PATH_PORTLETID_KEY);
647                 uri.removePathInfo(JetspeedResources.PATH_PORTLET_KEY);
648
649                 if ((elementType != JetspeedLink.DEFAULT) && (elementValue != null) && (elementValue.length() > 0))
650                 {
651                     uri.addPathInfo(uriPathElement, elementValue);
652                 }
653             }
654
655             // Set Template in path
656
if (templateName != null)
657             {
658                 uri.removePathInfo(JetspeedResources.PATH_TEMPLATE_KEY);
659                 if (templateName.length() > 0)
660                 {
661                     uri.addPathInfo(JetspeedResources.PATH_TEMPLATE_KEY, templateName);
662                 }
663             }
664
665             // Set Action in path
666
if (actionName != null)
667             {
668                 uri.removeQueryData(JetspeedResources.PATH_ACTION_KEY);
669                 if (actionName.length() > 0)
670                 {
671                     uri.addQueryData(JetspeedResources.PATH_ACTION_KEY, actionName);
672                 }
673             }
674
675             // Set MediaType in path
676
if (mediaType != null)
677             {
678                 uri.removePathInfo(Profiler.PARAM_MEDIA_TYPE);
679                 if (mediaType.length() > 0)
680                 {
681                     uri.addPathInfo(Profiler.PARAM_MEDIA_TYPE, mediaType);
682                 }
683             }
684
685             // Set Language in path
686
if (language != null)
687             {
688                 uri.removePathInfo(Profiler.PARAM_LANGUAGE);
689                 if (language.length() > 0)
690                 {
691                     uri.addPathInfo(Profiler.PARAM_LANGUAGE, language);
692                 }
693             }
694
695             // Set Country in path
696
if (country != null)
697             {
698                 uri.removePathInfo(Profiler.PARAM_COUNTRY);
699                 if (country.length() > 0)
700                 {
701                     uri.addPathInfo(Profiler.PARAM_COUNTRY, country);
702                 }
703             }
704
705             return uri;
706         }
707         catch (ProfileException e)
708         {
709             logger.error("Exception", e);
710             return null;
711         }
712     }
713     public DynamicURI getLink(int rootType, String JavaDoc rootValue, String JavaDoc pageName, int elementType, String JavaDoc elementValue, String JavaDoc actionName, String JavaDoc templateName, String JavaDoc mediaType, String JavaDoc language)
714     {
715         return getLink(rootType, rootValue, pageName, elementType, elementValue, actionName, templateName, mediaType, language, null);
716     }
717
718     public DynamicURI getLink(int rootType, String JavaDoc rootValue, String JavaDoc pageName, int elementType, String JavaDoc elementValue, String JavaDoc actionName, String JavaDoc templateName, String JavaDoc mediaType)
719     {
720         return getLink(rootType, rootValue, pageName, elementType, elementValue, actionName, templateName, mediaType, null, null);
721     }
722
723     public DynamicURI getLink(int rootType, String JavaDoc rootValue, String JavaDoc pageName, int elementType, String JavaDoc elementValue, String JavaDoc actionName, String JavaDoc templateName)
724     {
725         return getLink(rootType, rootValue, pageName, elementType, elementValue, actionName, actionName, null, null, null);
726     }
727
728     public DynamicURI getLink(int rootType, String JavaDoc rootValue, String JavaDoc pageName, int elementType, String JavaDoc elementValue, String JavaDoc actionName)
729     {
730         return getLink(rootType, rootValue, pageName, elementType, elementValue, actionName, null, null, null, null);
731     }
732
733     /**
734      */

735     public DynamicURI getLink(int rootType, String JavaDoc rootValue, String JavaDoc pageName, int elementType, String JavaDoc elementValue)
736     {
737         return getLink(rootType, rootValue, pageName, elementType, elementValue, null, null, null, null, null);
738     }
739
740     /**
741      * Return a link that includes an action
742      *
743      * @param action action
744      * @return DynamicURI that includes the desire action
745      */

746     public DynamicURI getAction(String JavaDoc action)
747     {
748         return getLink(JetspeedLink.CURRENT, null, null, JetspeedLink.CURRENT, null, action, null, null, null, null);
749     }
750
751     /**
752      * Return a link that includes an action to a specific portlet, as defined
753      * by an entry
754      *
755      * @param action Desired action
756      * @param entry to receive the action
757      * @return DynamicURI that includes the desire action
758      */

759     public DynamicURI getAction(String JavaDoc action, Entry entry)
760     {
761         if (entry != null)
762         {
763             return getLink(JetspeedLink.CURRENT, null, null, JetspeedLink.PORTLET_ID, entry.getId(), null, action, null, null, null);
764         }
765         else
766         {
767             return getLink(JetspeedLink.CURRENT, null, null, JetspeedLink.PORTLET_ID, null, null, action, null, null, null);
768         }
769     }
770
771     /**
772      * Return a link that includes an action to a specific portlet
773      *
774      * @param action Desired action
775      * @param portlet to receive the action
776      * @return DynamicURI that includes the desire action
777      */

778     public DynamicURI getAction(String JavaDoc action, Portlet portlet)
779     {
780         if (portlet != null)
781         {
782             return getLink(JetspeedLink.CURRENT, null, null, JetspeedLink.PORTLET_ID, portlet.getID(), action, null, null, null, null);
783         }
784         else
785         {
786             return getLink(JetspeedLink.CURRENT, null, null, JetspeedLink.PORTLET_ID, null, action, null, null, null, null);
787         }
788     }
789
790     /**
791      * Return a link that includes an action to a specific portlet, as defined
792      * by a portlets
793      *
794      * @param action Desired action
795      * @param portlets to receive the action
796      * @return DynamicURI that includes the desire action
797      */

798     public DynamicURI getAction(String JavaDoc action, Portlets portlets)
799     {
800         if (portlets != null)
801         {
802             return getLink(JetspeedLink.CURRENT, null, null, JetspeedLink.PORTLET_ID, portlets.getId(), action, null, null, null, null);
803         }
804         else
805         {
806             return getLink(JetspeedLink.CURRENT, null, null, JetspeedLink.PORTLET_ID, null, action, null, null, null, null);
807         }
808     }
809
810     /**
811      * Return a link that includes an action to a specific portlet, as defined
812      * by a PEID
813      *
814      * @param action Desired action
815      * @param peid Id of the portlet to receive the action
816      * @return DynamicURI that includes the desire action
817      */

818     public DynamicURI getAction(String JavaDoc action, String JavaDoc peid)
819     {
820         return getLink(JetspeedLink.CURRENT, null, null, JetspeedLink.PORTLET_ID, peid, action, null, null, null, null);
821     }
822
823     /**
824      * Return a link to a default page for the group
825      *
826      * @param group Desired group
827      * @return DynamicURI that to the default page for the group
828      */

829     public DynamicURI getGroup(String JavaDoc group)
830     {
831         return getLink(JetspeedLink.GROUP, group, "", JetspeedLink.DEFAULT, null, null, null, null, null, null);
832     }
833
834     /**
835      * Return a link to a desired page for the group
836      *
837      * @param page Desired page
838      * @param group Desired group
839      * @return DynamicURI that to the desired group and page
840      */

841     public DynamicURI getGroup(String JavaDoc group, String JavaDoc page)
842     {
843         return getLink(JetspeedLink.GROUP, group, page, JetspeedLink.DEFAULT, null, null, null, null, null, null);
844     }
845     /**
846      * Return a link to a default page for the
847      * current user, group, or role.
848      *
849      * @return DynamicURI that to the default page
850      */

851     public DynamicURI getPage()
852     {
853         return getLink(JetspeedLink.CURRENT, null, "", JetspeedLink.DEFAULT, null, null, null, null, null, null);
854     }
855
856     /**
857      * Return a link to a desired page for the
858      * current user, group, or role.
859      *
860      * @param page Desired page
861      * @return DynamicURI that to the desired page
862      */

863     public DynamicURI getPage(String JavaDoc page)
864     {
865         return getLink(JetspeedLink.CURRENT, null, page, JetspeedLink.DEFAULT, null, null, null, null, null, null);
866     }
867
868     /**
869      * Return a link to a desired page and pane for the
870      * current user, group, or role.
871      *
872      * @param page Desired page
873      * @param paneName Name of the desired pane
874      * @return DynamicURI that to the desired page
875      */

876     public DynamicURI getPage(String JavaDoc page, String JavaDoc paneName)
877     {
878         return getLink(JetspeedLink.CURRENT, null, page, JetspeedLink.PANE_NAME, paneName, null, null, null, null, null);
879     }
880     /**
881      * Return a link to a specific pane using the pane's id
882      *
883      * @param paneId of the Pane
884      * @return URI to specific portlet
885      */

886     public DynamicURI getPaneById(String JavaDoc paneId)
887     {
888         return getLink(JetspeedLink.CURRENT, null, this.getPageName(), JetspeedLink.PANE_ID, paneId, null, null, null, null, null);
889     }
890
891     /**
892      * Return a link to a specific pane using the pane's id
893      *
894      * @param paneName Name of the Pane
895      * @return URI to specific portlet
896      */

897     public DynamicURI getPaneByName(String JavaDoc paneName)
898     {
899         return getLink(JetspeedLink.CURRENT, null, this.getPageName(), JetspeedLink.PANE_NAME, paneName, null, null, null, null, null);
900     }
901
902     /**
903      * Return an link to a specific portlet using the portet's id
904      *
905      * @param peid of the portlet
906      * @return DynamicURI to specific portlet
907      */

908     public DynamicURI getPortletById(String JavaDoc peid)
909     {
910         return getLink(JetspeedLink.CURRENT, null, this.getPageName(), JetspeedLink.PORTLET_ID, peid, null, null, null, null, null);
911     }
912     /**
913      * Add a portlet reference in the link.
914      *
915      * Note: This must be used with caution, since a portlet may exist may times
916      * in a PSML. setPortletById() is the perfered method.
917      *
918      * @param portletName the name of the portlet to link to
919      * @return a DynamicURI referencing the named portlet
920      */

921     public DynamicURI getPortletByName(String JavaDoc portletName)
922     {
923         return getLink(JetspeedLink.CURRENT, null, this.getPageName(), JetspeedLink.PORTLET_NAME, portletName, null, null, null, null, null);
924     }
925     /**
926      * Return a link to a default page for the role
927      *
928      * @param role Desired role
929      * @return DynamicURI that to the desired page
930      */

931     public DynamicURI getRole(String JavaDoc role)
932     {
933         return getLink(JetspeedLink.ROLE, role, "", JetspeedLink.DEFAULT, null, null, null, null, null, null);
934     }
935
936     /**
937      * Return a link to a desired page for the role
938      *
939      * @param role Desired role
940      * @param page Desired page
941      * @return DynamicURI that to the desired page
942      */

943     public DynamicURI getRole(String JavaDoc role, String JavaDoc page)
944     {
945         return getLink(JetspeedLink.ROLE, role, page, JetspeedLink.DEFAULT, null, null, null, null, null, null);
946     }
947
948     /**
949      * Return a link to the template.
950      *
951      * @param template to add to link
952      * @return DynamicURI to specific portlet
953      */

954     public DynamicURI getTemplate(String JavaDoc template)
955     {
956         return getLink(JetspeedLink.CURRENT, null, null, JetspeedLink.DEFAULT, null, null, template, null, null, null);
957     }
958
959     /**
960      * Return a link to a default page for the user
961      *
962      * @param user Desired user
963      * @return DynamicURI that to the desired page
964      */

965     public DynamicURI getUser(String JavaDoc user)
966     {
967         return getLink(JetspeedLink.USER, user, "", JetspeedLink.DEFAULT, null, null, null, null, null, null);
968     }
969
970     /**
971      * Return a link to a desired page for the user
972      *
973      * @param page Desired page
974      * @param user Desired user
975      * @return DynamicURI that to the desired page
976      */

977     public DynamicURI getUser(String JavaDoc user, String JavaDoc page)
978     {
979         return getLink(JetspeedLink.USER, user, page, JetspeedLink.DEFAULT, null, null, null, null, null, null);
980     }
981
982     private void initLink()
983     {
984         if (initDone == true)
985         {
986             return;
987         }
988
989         try
990         {
991             // get the profile that is set in the rundata
992
profile = rundata.getProfile();
993
994             // if there was no profile, try making one from the rundata
995
if (profile == null)
996             {
997                 // this would only happen if the JetspeedAccessController didn't get a chance
998
// to setup the rundata...
999
profile = Profiler.getProfile(rundata);
1000                rundata.setProfile(profile);
1001                logger.warn("BaseJetspeedLink: profile in rundata was null");
1002            }
1003        }
1004        catch (ProfileException e)
1005        {
1006            logger.error("Exception", e);
1007        }
1008        if (profile != null)
1009        {
1010            // Get ProfileLocator for path info.
1011
if ((profile instanceof ProfileLocator) == true)
1012            {
1013                locator = (ProfileLocator) profile;
1014            }
1015        }
1016        initDone = true;
1017    }
1018
1019    /**
1020     * Return a link to the root portlet or pane
1021     *
1022     * @throws ProfileException if the profile detects an error
1023     * @return URI to the root portlet/pane
1024     */

1025    private DynamicURI getRoot() throws ProfileException
1026    {
1027        DynamicURI uri = null;
1028        initLink();
1029        if (locator != null)
1030        {
1031            uri = Profiler.makeDynamicURI(rundata, locator);
1032        }
1033
1034        if (uri == null)
1035        {
1036            uri = new DynamicURI(rundata);
1037        }
1038
1039        // check if we need to force to a secure (https) link
1040
if (JetspeedResources.getBoolean("force.ssl", false))
1041        {
1042            uri.setSecure();
1043        }
1044
1045        return uri;
1046    }
1047
1048    /**
1049     * Return a URL, as a string, the the root page or pane.
1050     *
1051     * @return a URL, as a string, the the root page or pane.
1052     */

1053    public String JavaDoc toString()
1054    {
1055        try
1056        {
1057            return getRoot().toString();
1058        }
1059        catch (ProfileException e)
1060        {
1061            logger.error("Exception", e);
1062            return null;
1063        }
1064    }
1065
1066    /**
1067     * Return the action key. Used by velocity templates, i.e. $jlink.ActionKey
1068     *
1069     * @return the action parameter name
1070     */

1071    public String JavaDoc getActionKey()
1072    {
1073        return JetspeedResources.PATH_ACTION_KEY;
1074    }
1075
1076    /**
1077     * Is the PSML for the anonymous user?
1078     *
1079     * @return True = PSML is for the anonymous user
1080     */

1081    public boolean getAnonymous()
1082    {
1083        initLink();
1084        try
1085        {
1086            return locator.getAnonymous();
1087        }
1088        catch (Exception JavaDoc e)
1089        {
1090            logger.error("Exception", e);
1091            return true;
1092        }
1093    }
1094
1095    /**
1096     * Return country of the PSML file
1097     *
1098     * @return Country of PSML, or null if no country
1099     */

1100    public String JavaDoc getCountry()
1101    {
1102        initLink();
1103        try
1104        {
1105            return locator.getCountry();
1106        }
1107        catch (Exception JavaDoc e)
1108        {
1109            logger.error("Exception", e);
1110            return null;
1111        }
1112    }
1113
1114    /**
1115     * Return Group name of the PSML file
1116     *
1117     * @return Group name of PSML, or null if no Group name
1118     */

1119    public String JavaDoc getGroupName()
1120    {
1121        initLink();
1122        try
1123        {
1124            return locator.getGroupName();
1125        }
1126        catch (Exception JavaDoc e)
1127        {
1128            logger.error("Exception", e);
1129            return null;
1130        }
1131    }
1132
1133    /**
1134     * Return Language of the PSML file
1135     *
1136     * @return Language of PSML, or null if no Language
1137     */

1138    public String JavaDoc getLanguage()
1139    {
1140        initLink();
1141        try
1142        {
1143            return locator.getLanguage();
1144        }
1145        catch (Exception JavaDoc e)
1146        {
1147            logger.error("Exception", e);
1148            return null;
1149        }
1150    }
1151
1152    /**
1153     * Return Media Type of the PSML file
1154     *
1155     * @return Media Type of PSML, or null if no Media Type
1156     */

1157    public String JavaDoc getMediaType()
1158    {
1159        initLink();
1160        try
1161        {
1162            return locator.getMediaType();
1163        }
1164        catch (Exception JavaDoc e)
1165        {
1166            logger.error("Exception", e);
1167            return null;
1168        }
1169    }
1170
1171    /**
1172     * Return Page name of the PSML file
1173     *
1174     * @return Page name of PSML, or null if no Page name
1175     */

1176    public String JavaDoc getPageName()
1177    {
1178        initLink();
1179        try
1180        {
1181            return locator.getName();
1182        }
1183        catch (Exception JavaDoc e)
1184        {
1185            logger.error("Exception", e);
1186            return null;
1187        }
1188    }
1189
1190    /**
1191     * Return Role name of the PSML file
1192     *
1193     * @return Role name of PSML, or null if no Role name
1194     */

1195    public String JavaDoc getRoleName()
1196    {
1197        initLink();
1198        try
1199        {
1200            return locator.getRoleName();
1201        }
1202        catch (Exception JavaDoc e)
1203        {
1204            logger.error("Exception", e);
1205            return null;
1206        }
1207    }
1208
1209    /**
1210     * Return User name of the PSML file
1211     *
1212     * @return User name of PSML, or null if no User name
1213     */

1214    public String JavaDoc getUserName()
1215    {
1216        initLink();
1217        try
1218        {
1219            return locator.getUserName();
1220        }
1221        catch (Exception JavaDoc e)
1222        {
1223            logger.error("Exception", e);
1224            return null;
1225        }
1226    }
1227
1228    /**
1229     * The following methods used by Velocity to get value of constants
1230     */

1231    public static int getCURRENT()
1232    {
1233        return JetspeedLink.CURRENT;
1234    }
1235    public static int getDEFAULT()
1236    {
1237        return JetspeedLink.DEFAULT;
1238    }
1239    public static int getGROUP()
1240    {
1241        return JetspeedLink.GROUP;
1242    }
1243    public static int getPANE_ID()
1244    {
1245        return JetspeedLink.PANE_ID;
1246    }
1247    public static int getPANE_NAME()
1248    {
1249        return JetspeedLink.PANE_NAME;
1250    }
1251    public static int getPORTLET_ID()
1252    {
1253        return JetspeedLink.PORTLET_ID;
1254    }
1255    public static int getPORTLET_NAME()
1256    {
1257        return JetspeedLink.PORTLET_NAME;
1258    }
1259    public static int getROLE()
1260    {
1261        return JetspeedLink.ROLE;
1262    }
1263    public static int getUSER()
1264    {
1265        return JetspeedLink.USER;
1266    }
1267    public static String JavaDoc getDEFAULT_PAGE()
1268    {
1269        return "";
1270    }
1271
1272    /**
1273     * deprecated methods from JetspeedTemplateLink.
1274     */

1275
1276    /**
1277     * <p> Set the portlet giving context to this Link object.</p>
1278     *
1279     * This method is from JetspeedTemplateLink and is only here
1280     * for backward compatibility. This it should not be used for
1281     * any new development. Also any problems with this method will
1282     * not be fixed
1283     *
1284     * @param portlet the name of the active portlet
1285     * @deprecated the name is confusing. Use @see(#forPaneById()) instead.
1286     */

1287    public void setPortlet(Portlet portlet)
1288    {
1289        this.activePortlet = portlet;
1290    }
1291
1292
1293    /**
1294     * Add a portlet reference in the link.
1295     *
1296     * Note: This must be used with caution, since a portlet may exist may times
1297     * in a PSML. setPortletById() is the perfered method.
1298     *
1299     * @param portletName the name of the portlet to link to
1300     * @return a DynamicURI referencing the named portlet for easy link construction in template
1301     */

1302    public DynamicURI setPortletByName(String JavaDoc portletName)
1303    {
1304        DynamicURI uri = null;
1305        try
1306        {
1307            uri = getRoot();
1308        }
1309        catch (Exception JavaDoc e)
1310        {
1311            logger.error("Exception", e);
1312            return null;
1313        }
1314        if ((portletName != null) && (portletName.length() > 0))
1315        {
1316            uri.addPathInfo(JetspeedResources.PATH_PORTLET_KEY, portletName);
1317        }
1318        return uri;
1319    }
1320
1321    /**
1322     * Methods required by ApplictionTool interface
1323     *
1324     */

1325
1326    /**
1327     * This will initialise a JetspeedLink object that was
1328     * constructed with the default constructor (ApplicationTool
1329     * method).
1330     *
1331     * @param data assumed to be a RunData object
1332     */

1333
1334    public void init(Object JavaDoc data)
1335    {
1336        // Keeping init small and fast
1337
if (data instanceof JetspeedRunData)
1338        {
1339            this.rundata = (JetspeedRunData) data;
1340        }
1341        else
1342        {
1343            this.rundata = null;
1344        }
1345        profile = null;
1346        locator = null;
1347        initDone = false;
1348        return;
1349    }
1350    /**
1351     * Refresh method - does nothing
1352     */

1353    public void refresh()
1354    {
1355        // empty
1356
}
1357
1358    public DynamicURI setMediaType(String JavaDoc mediaType)
1359    {
1360        return getLink(JetspeedLink.CURRENT, null, null, JetspeedLink.DEFAULT, null, null, null, mediaType);
1361    }
1362
1363}
1364
Popular Tags