View Javadoc

1   /*
2    * Copyright 2000-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.om.profile;
18  
19  import java.util.StringTokenizer;
20  
21  
22  import org.apache.jetspeed.om.security.JetspeedUser;
23  import org.apache.jetspeed.om.security.JetspeedUserFactory;
24  import org.apache.jetspeed.om.security.Role;
25  import org.apache.jetspeed.om.security.Group;
26  
27  import org.apache.jetspeed.services.Profiler;
28  import org.apache.jetspeed.services.JetspeedSecurity;
29  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
30  import org.apache.jetspeed.services.logging.JetspeedLogger;
31  
32  /***
33   * Interface definition for a Profile Locator.
34   * Locators are used by the profiler to describe the parameters used to locate
35   * a resource in the persistent configuration store.
36   *
37   *
38   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
39   * @author <a href="mailto:adambalk@cisco.com">Atul Dambalkar</a>
40   * @version $Id: BaseProfileLocator.java,v 1.21 2004/02/23 03:05:01 jford Exp $
41  */
42  
43  public class BaseProfileLocator implements ProfileLocator
44  {
45      // instance state
46      private String name = null;
47      private String mediaType = null;
48      private String language = null;
49      private String country = null;
50      private JetspeedUser user = null;
51      private Role   role = null;
52      private Group  group = null;
53      private boolean anonymous = false;
54      
55      private String roleName = null;
56      private String userName = null;
57      private String groupName = null;
58  
59      private static final String DELIM = "/";
60  
61      /***
62       * Static initialization of the logger for this class
63       */    
64      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseProfileLocator.class.getName());    
65      
66      /*
67       * Gets the unique profile locator id, which is a combination of the params
68       * This ID must follow the one of the 4 sequences below:
69       *
70       *   <username>/<mediaType>/<language>/<country>/<page>
71       *   <group>/<mediaType>/<language>/<country>/<page>
72       *   <role>/<mediaType>/<language>/<country>/<page>
73       *
74       *
75       * @return The profile locator id
76       */
77  
78      public String getId()
79      {
80          StringBuffer id = new StringBuffer(128);
81  
82          if (!anonymous && user != null)
83          {
84              id.append(Profiler.PARAM_USER).append(DELIM);
85              id.append(user.getUserName());
86          }
87          else if (group != null)
88          {
89              id.append(Profiler.PARAM_GROUP).append(DELIM);
90              id.append(group.getName());
91          }
92          else if (role != null)
93          {
94              id.append(Profiler.PARAM_ROLE).append(DELIM);
95              id.append(role.getName());
96          }
97          else
98          {
99              id.append(Profiler.PARAM_ANON);
100         }
101         if (language != null)
102         {
103             id.append(DELIM);
104             id.append(language);
105         }
106         if (country != null)
107         {
108             id.append(DELIM);
109             id.append(country);
110         }
111         if (mediaType != null)
112         {
113             id.append(DELIM);
114             id.append(mediaType);
115         }
116         if (name != null)
117         {
118             id.append(DELIM);
119             id.append(name);
120         }
121 
122         return id.toString();
123     }
124 
125 
126     /*
127      * Gets the unique profile locator path, which is a combination of the name
128      * value pairs. This ID must follow the one of the 4 sequences below:
129      *
130      *   user/<name>/media-type/<mediaType>/language/<language>
131      *               /country/<country>/<page>/page
132      *
133      *   group/ ""
134      *   role/  ""
135      *
136      *
137      * @return The profile locator path
138      */
139 
140     public String getPath()
141     {
142         StringBuffer id = new StringBuffer(128);
143 
144         if (!anonymous && user != null)
145         {
146             id.append(Profiler.PARAM_USER).append(DELIM);
147             id.append(user.getUserName()).append(DELIM);
148         }
149         else if (group != null)
150         {
151             id.append(Profiler.PARAM_GROUP).append(DELIM);
152             id.append(group.getName()).append(DELIM);
153         }
154         else if (role != null)
155         {
156             id.append(Profiler.PARAM_ROLE).append(DELIM);
157             id.append(role.getName()).append(DELIM);
158         }
159         else
160         {
161             id.append(Profiler.PARAM_USER).append(DELIM);
162             id.append(Profiler.PARAM_ANON).append(DELIM);
163         }
164 
165         if (language != null)
166         {
167             id.append(Profiler.PARAM_LANGUAGE).append(DELIM);
168             id.append(language).append(DELIM);
169         }
170         if (country != null)
171         {
172             id.append(Profiler.PARAM_COUNTRY).append(DELIM);
173             id.append(country).append(DELIM);
174         }
175         if (mediaType != null)
176         {
177             id.append(Profiler.PARAM_MEDIA_TYPE).append(DELIM);
178             id.append(mediaType).append(DELIM);
179         }
180         if (name != null)
181         {
182             id.append(Profiler.PARAM_PAGE).append(DELIM);
183             id.append(name).append(DELIM);
184         }
185         id.deleteCharAt(id.length()-1);
186         return id.toString();
187     }
188 
189 
190     /*
191      * populates this profile locator from a given path in the format:
192      *
193      *   user/<name>/media-type/<mediaType>/language/<language>
194      *               /country/<country>/<page>/page
195      *
196      *   group/ ""
197      *   role/  ""
198      *
199      * @param path The formatted profiler path string.
200      */
201     public void createFromPath(String path)
202     {
203         StringTokenizer tok = new StringTokenizer(path, "/");
204         while (tok.hasMoreTokens())
205         {
206             String name = (String)tok.nextToken();
207             if (name.equals(Profiler.PARAM_USER) && tok.hasMoreTokens())
208             {
209                 try
210                 {
211                     // keep profile locator from failing if the user has been removed
212                     // from security as it may still exist in the PSML structure.
213                     this.userName = tok.nextToken();
214                     this.setUser( JetspeedSecurity.getUser(this.userName) );
215                 }
216                 catch (Exception e)
217                 {
218                     logger.error("ProfileLocator: Failed to set User: ", e);
219                 }
220             }
221             else if (name.equals(Profiler.PARAM_GROUP) && tok.hasMoreTokens())
222             {
223                 try
224                 {
225                     // keep profile locator from failing if the group has been removed
226                     // from security as it may still exist in the PSML structure.
227                     this.groupName = tok.nextToken();
228                     this.setGroup( JetspeedSecurity.getGroup(this.groupName) );
229                 }
230                 catch (Exception e)
231                 {
232                     logger.error("ProfileLocator: Failed to set Group: ", e);
233                 }
234             }
235             else if (name.equals(Profiler.PARAM_ROLE) && tok.hasMoreTokens())
236             {
237                 try
238                 {
239                     // keep profile locator from failing if the role has been removed
240                     // from security as it may still exist in the PSML structure.
241                     this.roleName = tok.nextToken();
242                     this.setRole(JetspeedSecurity.getRole(this.roleName));
243                 }
244                 catch (Exception e)
245                 {
246                     logger.error("ProfileLocator: Failed to set Role: ", e);
247                 }
248             }
249             else if (name.equals(Profiler.PARAM_PAGE) && tok.hasMoreTokens())
250             {
251                 this.setName(tok.nextToken());
252             }
253             else if (name.equals(Profiler.PARAM_MEDIA_TYPE) && tok.hasMoreTokens())
254             {
255                 this.setMediaType(tok.nextToken());
256             }
257             else if (name.equals(Profiler.PARAM_LANGUAGE) && tok.hasMoreTokens())
258             {
259                 this.setLanguage(tok.nextToken());
260             }
261             else if (name.equals(Profiler.PARAM_COUNTRY) && tok.hasMoreTokens())
262             {
263                 this.setCountry(tok.nextToken());
264             }
265 
266         }
267     }
268 
269     /***
270      * @see Object#clone
271      * @return an instance copy of this object
272      */
273     public Object clone() throws java.lang.CloneNotSupportedException
274     {
275         return super.clone();
276     }
277 
278     /*
279      * Gets the resource name parameter for this profile.
280      *
281      * @return The resource name parameter for this profile.
282      */
283     public String getName()
284     {
285         return name;
286     }
287 
288     /*
289      * Sets the resource name parameter for this profile.
290      *
291      * @param The resource name parameter for this profile.
292      */
293     public void setName(String name)
294     {
295         this.name = name;
296     }
297 
298     /*
299      * Gets the anonymous user flag for this profile.
300      *
301      * @param The user parameter for this profile.
302      */
303     public boolean getAnonymous()
304     {
305         return this.anonymous;
306     }
307 
308 
309     /*
310      * Sets the user parameter as the anonymous user
311      *
312      * @param anonymous True indicates this is an anonymous user.
313      */
314     public void setAnonymous(boolean anonymous)
315     {
316         try
317         {
318             JetspeedUser user = JetspeedUserFactory.getInstance();
319             user.setUserName(JetspeedSecurity.getAnonymousUserName());
320             this.setUser(user);
321         }
322         catch (Exception e)
323         {
324             logger.error("Could not get Anonymous user", e);
325         }
326         finally
327         {
328             this.anonymous = anonymous;
329         }
330     }
331 
332     /*
333      * Gets the media type parameter for this profile.
334      * Media types are values such as html, wml, xml ...
335      *
336      * @return The media type parameter for this profile.
337      */
338     public String getMediaType()
339     {
340         return mediaType;
341     }
342 
343     /*
344      * Sets the media type parameter for this profile.
345      * Media types are values such as html, wml, xml ...
346      *
347      * @param The media type parameter for this profile.
348      */
349     public void setMediaType(String mediaType)
350     {
351         this.mediaType = mediaType;
352     }
353 
354     /*
355      * Gets the language parameter for this profile.
356      * Language values are ISO-639 standard language abbreviations
357      * en, fr, de, ...
358      *
359      * @return The language parameter for this profile.
360      */
361     public String getLanguage()
362     {
363         return language;
364     }
365 
366     /*
367      * Sets the language parameter for this profile.
368      * Language values are ISO-639 standard language abbreviations
369      * en, fr, de, ...
370      *
371      * @param The language parameter for this profile.
372      */
373     public void setLanguage(String language)
374     {
375         this.language = language;
376     }
377 
378     /*
379      * Gets the country code parameter for this profile.
380      * Country code values are ISO-3166 standard country code abbreviations.
381      * GB, US, FR, CA, DE, ...
382      *
383      * @return The country code parameter for this profile.
384      */
385     public String getCountry()
386     {
387         return country;
388     }
389 
390     /*
391      * Sets the country code parameter for this profile.
392      * Country code values are ISO-3166 standard country code abbreviations.
393      * GB, US, FR, CA, DE, ...
394      *
395      * @param The country code parameter for this profile.
396      */
397     public void setCountry(String country)
398     {
399         this.country = country;
400     }
401 
402     /*
403      * Gets the user parameter for this profile.
404      *
405      * @return The user parameter for this profile.
406      */
407     public JetspeedUser getUser()
408     {
409         return user;
410     }
411 
412     public String getUserName()
413     {
414         if (null == user)
415             return userName;
416 
417         return user.getUserName();
418     }
419 
420     /*
421      * Sets the user parameter for this profile.
422      *
423      * @param The user parameter for this profile.
424      */
425     public void setUser(JetspeedUser user)
426     {
427         this.user = user;
428     }
429 
430     /*
431      * Gets the role parameter for this profile.
432      *
433      * @return The role parameter for this profile.
434      */
435     public Role getRole()
436     {
437         return role;
438     }
439 
440     public String getRoleName()
441     {
442         if (null == role)
443             return roleName;
444 
445         return role.getName();
446     }
447 
448     /*
449      * Sets the role parameter for this profile.
450      *
451      * @param The role parameter for this profile.
452      */
453     public void setRole( Role role )
454     {
455         this.role = role;
456     }
457 
458     public void setRoleByName( String roleName )
459     {
460         try
461         {
462             Role temp = JetspeedSecurity.getRole(roleName);
463             if (null != temp)
464             {
465                 role = temp;
466             }
467         }
468         catch (Exception e)
469         {
470             logger.error("ProfileLocator: Failed to set Role " + roleName, e);
471         }
472     }
473 
474     /*
475      * Gets the group parameter for this profile.
476      *
477      * @return The group parameter for this profile.
478      */
479     public Group getGroup()
480     {
481         return group;
482     }
483 
484     public String getGroupName()
485     {
486         if (null == group)
487             return groupName;
488 
489         return group.getName();
490     }
491 
492     /*
493      * Sets the group parameter for this profile.
494      *
495      * @param The group parameter for this profile.
496      */
497     public void setGroup( Group group )
498     {
499         this.group = group;
500     }
501 
502     public void setGroupByName( String groupName )
503     {
504         try
505         {
506             Group temp = JetspeedSecurity.getGroup(groupName);
507             if (null != temp)
508             {
509                 group = temp;
510             }
511         }
512         catch (Exception e)
513         {
514             logger.error("ProfileLocator: Failed to set Group: " + e);
515         }
516     }
517 
518 
519     /*
520      * Comparision Functions. Contributed by Atul Dambalkar
521      */
522 
523    /***
524      * Define equality criteria for ProfileLocator objects.
525      * @param obj ProfileLocator object to be compared with.
526      */
527     public boolean equals(Object obj)
528     {
529         if( obj == null )
530         {
531             return false;
532         }
533         synchronized (obj)
534         {
535             if ( ! ( obj instanceof ProfileLocator ) )
536             {
537                 return false;
538             }
539 
540             ProfileLocator locator = (ProfileLocator)obj;
541 
542             String name = locator.getName();
543             String mediaType = locator.getMediaType();
544             String language = locator.getLanguage();
545             String country = locator.getCountry();
546             Group group = locator.getGroup();
547             Role role = locator.getRole();
548 
549             return nameEquals(name)
550 //                   && locator.getId() == id
551                    && mediaTypeEquals(mediaType)
552                    && languageEquals(language)
553                    && countryEquals(country)
554                    && userEquals(locator)
555                    && groupEquals(group)
556                    && roleEquals(role);
557         }
558     }
559 
560     /***
561      * Check equality for given User object with this ProfileLocator's User
562      * object.
563      */
564     private boolean userEquals(ProfileLocator locator)
565     {
566         JetspeedUser user = locator.getUser();
567         // if either of reference is null return false.
568         if (exclusiveOr(this.user, user))
569         {
570             return false;
571         }
572         // check if both are non-nulls
573         if (assertNotNull(this.user) && assertNotNull(user))
574         {
575             return stringEquals(this.user.getUserName(), user.getUserName());
576         }
577         // can be anonymous user
578         return this.anonymous == locator.getAnonymous();
579     }
580 
581     /***
582      * Check equality for given Group object with this ProfileLocator's Group
583      * object.
584      */
585     private boolean groupEquals(Group group)
586     {
587         // if either of reference is null return false.
588         if (exclusiveOr(this.group, group))
589         {
590             return false;
591         }
592         // check if both are non-nulls
593         if (assertNotNull(this.group) && assertNotNull(group))
594         {
595             return stringEquals( this.group.getName(), group.getName());
596         }
597         // both are null
598         return true;
599     }
600 
601     /***
602      * Check equality for given Role object with this ProfileLocator's Role
603      * object.
604      */
605     private boolean roleEquals(Role role)
606     {
607         // if either of reference is null return false.
608         if (exclusiveOr(this.role, role))
609         {
610             return false;
611         }
612         // check if both are non-nulls
613         if (assertNotNull(this.role) && assertNotNull(role))
614         {
615             return stringEquals(this.role.getName(), role.getName());
616         }
617         // both are null
618         return true;
619     }
620 
621     /***
622      * Check equality for language object with this ProfileLocator's language
623      * object.
624      */
625     private boolean languageEquals(String language)
626     {
627         return stringEquals(this.language, language);
628     }
629 
630     /***
631      * Check equality for country object with this ProfileLocator's country
632      * object.
633      */
634     private boolean countryEquals(String country)
635     {
636         return stringEquals(this.country, country);
637     }
638 
639     /***
640      * Check equality for name object with this ProfileLocator's name
641      * object.
642      */
643     private boolean nameEquals(String name)
644     {
645         return stringEquals(this.name, name);
646     }
647 
648     /***
649      * Check equality for name object with this ProfileLocator's name
650      * object.
651      */
652     private boolean mediaTypeEquals(String mediaType)
653     {
654         return stringEquals(this.mediaType, mediaType);
655     }
656 
657     /***
658      * AssertNotNull the two String objects and then check the equality.
659      */
660     private boolean stringEquals(String str1, String str2)
661     {
662         if (exclusiveOr(str1, str2))
663         {
664             return false;
665         }
666         if (assertNotNull(str1) && assertNotNull(str2))
667         {
668             return str1.equals(str2);
669         }
670         // both are null
671         return true;
672     }
673 
674     /***
675      * AssertNotNull the given object.
676      */
677     private boolean assertNotNull(Object object)
678     {
679         return object != null;
680     }
681 
682     /***
683      * Exclusive or the two objects fro their references null-ability.
684      */
685     private boolean exclusiveOr(Object obj1, Object obj2)
686     {
687         return (assertNotNull(obj1) && !assertNotNull(obj2))
688                 || (!assertNotNull(obj1) && assertNotNull(obj2));
689     }
690 }