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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.om.profile;
1819import java.util.StringTokenizer;
202122import org.apache.jetspeed.om.security.JetspeedUser;
23import org.apache.jetspeed.om.security.JetspeedUserFactory;
24import org.apache.jetspeed.om.security.Role;
25import org.apache.jetspeed.om.security.Group;
2627import org.apache.jetspeed.services.Profiler;
28import org.apache.jetspeed.services.JetspeedSecurity;
29import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
30import org.apache.jetspeed.services.logging.JetspeedLogger;
3132/***33 * Interface definition for a Profile Locator.34 * Locators are used by the profiler to describe the parameters used to locate35 * 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*/4243publicclassBaseProfileLocator implements ProfileLocator44 {
45// instance state46private String name = null;
47private String mediaType = null;
48private String language = null;
49private String country = null;
50privateJetspeedUser user = null;
51privateRole role = null;
52privateGroup group = null;
53privateboolean anonymous = false;
5455private String roleName = null;
56private String userName = null;
57private String groupName = null;
5859privatestaticfinal String DELIM = "/";
6061/***62 * Static initialization of the logger for this class63 */64privatestaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseProfileLocator.class.getName());
6566/*67 * Gets the unique profile locator id, which is a combination of the params68 * 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 id76 */7778public String getId()
79 {
80 StringBuffer id = new StringBuffer(128);
8182if (!anonymous && user != null)
83 {
84 id.append(Profiler.PARAM_USER).append(DELIM);
85 id.append(user.getUserName());
86 }
87elseif (group != null)
88 {
89 id.append(Profiler.PARAM_GROUP).append(DELIM);
90 id.append(group.getName());
91 }
92elseif (role != null)
93 {
94 id.append(Profiler.PARAM_ROLE).append(DELIM);
95 id.append(role.getName());
96 }
97else98 {
99 id.append(Profiler.PARAM_ANON);
100 }
101if (language != null)
102 {
103 id.append(DELIM);
104 id.append(language);
105 }
106if (country != null)
107 {
108 id.append(DELIM);
109 id.append(country);
110 }
111if (mediaType != null)
112 {
113 id.append(DELIM);
114 id.append(mediaType);
115 }
116if (name != null)
117 {
118 id.append(DELIM);
119 id.append(name);
120 }
121122return id.toString();
123 }
124125126/*127 * Gets the unique profile locator path, which is a combination of the name128 * 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>/page132 *133 * group/ ""134 * role/ ""135 *136 *137 * @return The profile locator path138 */139140public String getPath()
141 {
142 StringBuffer id = new StringBuffer(128);
143144if (!anonymous && user != null)
145 {
146 id.append(Profiler.PARAM_USER).append(DELIM);
147 id.append(user.getUserName()).append(DELIM);
148 }
149elseif (group != null)
150 {
151 id.append(Profiler.PARAM_GROUP).append(DELIM);
152 id.append(group.getName()).append(DELIM);
153 }
154elseif (role != null)
155 {
156 id.append(Profiler.PARAM_ROLE).append(DELIM);
157 id.append(role.getName()).append(DELIM);
158 }
159else160 {
161 id.append(Profiler.PARAM_USER).append(DELIM);
162 id.append(Profiler.PARAM_ANON).append(DELIM);
163 }
164165if (language != null)
166 {
167 id.append(Profiler.PARAM_LANGUAGE).append(DELIM);
168 id.append(language).append(DELIM);
169 }
170if (country != null)
171 {
172 id.append(Profiler.PARAM_COUNTRY).append(DELIM);
173 id.append(country).append(DELIM);
174 }
175if (mediaType != null)
176 {
177 id.append(Profiler.PARAM_MEDIA_TYPE).append(DELIM);
178 id.append(mediaType).append(DELIM);
179 }
180if (name != null)
181 {
182 id.append(Profiler.PARAM_PAGE).append(DELIM);
183 id.append(name).append(DELIM);
184 }
185 id.deleteCharAt(id.length()-1);
186return id.toString();
187 }
188189190/*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>/page195 *196 * group/ ""197 * role/ ""198 *199 * @param path The formatted profiler path string.200 */201publicvoid createFromPath(String path)
202 {
203 StringTokenizer tok = new StringTokenizer(path, "/");
204while (tok.hasMoreTokens())
205 {
206 String name = (String)tok.nextToken();
207if (name.equals(Profiler.PARAM_USER) && tok.hasMoreTokens())
208 {
209try210 {
211// keep profile locator from failing if the user has been removed212// from security as it may still exist in the PSML structure.213this.userName = tok.nextToken();
214this.setUser( JetspeedSecurity.getUser(this.userName) );
215 }
216catch (Exception e)
217 {
218 logger.error("ProfileLocator: Failed to set User: ", e);
219 }
220 }
221elseif (name.equals(Profiler.PARAM_GROUP) && tok.hasMoreTokens())
222 {
223try224 {
225// keep profile locator from failing if the group has been removed226// from security as it may still exist in the PSML structure.227this.groupName = tok.nextToken();
228this.setGroup( JetspeedSecurity.getGroup(this.groupName) );
229 }
230catch (Exception e)
231 {
232 logger.error("ProfileLocator: Failed to set Group: ", e);
233 }
234 }
235elseif (name.equals(Profiler.PARAM_ROLE) && tok.hasMoreTokens())
236 {
237try238 {
239// keep profile locator from failing if the role has been removed240// from security as it may still exist in the PSML structure.241this.roleName = tok.nextToken();
242this.setRole(JetspeedSecurity.getRole(this.roleName));
243 }
244catch (Exception e)
245 {
246 logger.error("ProfileLocator: Failed to set Role: ", e);
247 }
248 }
249elseif (name.equals(Profiler.PARAM_PAGE) && tok.hasMoreTokens())
250 {
251this.setName(tok.nextToken());
252 }
253elseif (name.equals(Profiler.PARAM_MEDIA_TYPE) && tok.hasMoreTokens())
254 {
255this.setMediaType(tok.nextToken());
256 }
257elseif (name.equals(Profiler.PARAM_LANGUAGE) && tok.hasMoreTokens())
258 {
259this.setLanguage(tok.nextToken());
260 }
261elseif (name.equals(Profiler.PARAM_COUNTRY) && tok.hasMoreTokens())
262 {
263this.setCountry(tok.nextToken());
264 }
265266 }
267 }
268269/***270 * @see Object#clone271 * @return an instance copy of this object272 */273public Object clone() throws java.lang.CloneNotSupportedException
274 {
275returnsuper.clone();
276 }
277278/*279 * Gets the resource name parameter for this profile.280 *281 * @return The resource name parameter for this profile.282 */283public String getName()
284 {
285return name;
286 }
287288/*289 * Sets the resource name parameter for this profile.290 *291 * @param The resource name parameter for this profile.292 */293publicvoid setName(String name)
294 {
295this.name = name;
296 }
297298/*299 * Gets the anonymous user flag for this profile.300 *301 * @param The user parameter for this profile.302 */303publicboolean getAnonymous()
304 {
305returnthis.anonymous;
306 }
307308309/*310 * Sets the user parameter as the anonymous user311 *312 * @param anonymous True indicates this is an anonymous user.313 */314publicvoid setAnonymous(boolean anonymous)
315 {
316try317 {
318JetspeedUser user = JetspeedUserFactory.getInstance();
319 user.setUserName(JetspeedSecurity.getAnonymousUserName());
320this.setUser(user);
321 }
322catch (Exception e)
323 {
324 logger.error("Could not get Anonymous user", e);
325 }
326finally327 {
328this.anonymous = anonymous;
329 }
330 }
331332/*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 */338public String getMediaType()
339 {
340return mediaType;
341 }
342343/*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 */349publicvoid setMediaType(String mediaType)
350 {
351this.mediaType = mediaType;
352 }
353354/*355 * Gets the language parameter for this profile.356 * Language values are ISO-639 standard language abbreviations357 * en, fr, de, ...358 *359 * @return The language parameter for this profile.360 */361public String getLanguage()
362 {
363return language;
364 }
365366/*367 * Sets the language parameter for this profile.368 * Language values are ISO-639 standard language abbreviations369 * en, fr, de, ...370 *371 * @param The language parameter for this profile.372 */373publicvoid setLanguage(String language)
374 {
375this.language = language;
376 }
377378/*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 */385public String getCountry()
386 {
387return country;
388 }
389390/*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 */397publicvoid setCountry(String country)
398 {
399this.country = country;
400 }
401402/*403 * Gets the user parameter for this profile.404 *405 * @return The user parameter for this profile.406 */407publicJetspeedUser getUser()
408 {
409return user;
410 }
411412public String getUserName()
413 {
414if (null == user)
415return userName;
416417return user.getUserName();
418 }
419420/*421 * Sets the user parameter for this profile.422 *423 * @param The user parameter for this profile.424 */425publicvoid setUser(JetspeedUser user)
426 {
427this.user = user;
428 }
429430/*431 * Gets the role parameter for this profile.432 *433 * @return The role parameter for this profile.434 */435publicRole getRole()
436 {
437return role;
438 }
439440public String getRoleName()
441 {
442if (null == role)
443return roleName;
444445return role.getName();
446 }
447448/*449 * Sets the role parameter for this profile.450 *451 * @param The role parameter for this profile.452 */453publicvoid setRole( Role role )
454 {
455this.role = role;
456 }
457458publicvoid setRoleByName( String roleName )
459 {
460try461 {
462Role temp = JetspeedSecurity.getRole(roleName);
463if (null != temp)
464 {
465 role = temp;
466 }
467 }
468catch (Exception e)
469 {
470 logger.error("ProfileLocator: Failed to set Role " + roleName, e);
471 }
472 }
473474/*475 * Gets the group parameter for this profile.476 *477 * @return The group parameter for this profile.478 */479publicGroup getGroup()
480 {
481return group;
482 }
483484public String getGroupName()
485 {
486if (null == group)
487return groupName;
488489return group.getName();
490 }
491492/*493 * Sets the group parameter for this profile.494 *495 * @param The group parameter for this profile.496 */497publicvoid setGroup( Group group )
498 {
499this.group = group;
500 }
501502publicvoid setGroupByName( String groupName )
503 {
504try505 {
506Group temp = JetspeedSecurity.getGroup(groupName);
507if (null != temp)
508 {
509 group = temp;
510 }
511 }
512catch (Exception e)
513 {
514 logger.error("ProfileLocator: Failed to set Group: " + e);
515 }
516 }
517518519/*520 * Comparision Functions. Contributed by Atul Dambalkar521 */522523/***524 * Define equality criteria for ProfileLocator objects.525 * @param obj ProfileLocator object to be compared with.526 */527publicboolean equals(Object obj)
528 {
529if( obj == null )
530 {
531return false;
532 }
533synchronized (obj)
534 {
535if ( ! ( obj instanceof ProfileLocator ) )
536 {
537return false;
538 }
539540ProfileLocator locator = (ProfileLocator)obj;
541542 String name = locator.getName();
543 String mediaType = locator.getMediaType();
544 String language = locator.getLanguage();
545 String country = locator.getCountry();
546Group group = locator.getGroup();
547Role role = locator.getRole();
548549return nameEquals(name)
550// && locator.getId() == id551 && mediaTypeEquals(mediaType)
552 && languageEquals(language)
553 && countryEquals(country)
554 && userEquals(locator)
555 && groupEquals(group)
556 && roleEquals(role);
557 }
558 }
559560/***561 * Check equality for given User object with this ProfileLocator's User562 * object.563 */564privateboolean userEquals(ProfileLocator locator)
565 {
566JetspeedUser user = locator.getUser();
567// if either of reference is null return false.568if (exclusiveOr(this.user, user))
569 {
570return false;
571 }
572// check if both are non-nulls573if (assertNotNull(this.user) && assertNotNull(user))
574 {
575return stringEquals(this.user.getUserName(), user.getUserName());
576 }
577// can be anonymous user578returnthis.anonymous == locator.getAnonymous();
579 }
580581/***582 * Check equality for given Group object with this ProfileLocator's Group583 * object.584 */585privateboolean groupEquals(Group group)
586 {
587// if either of reference is null return false.588if (exclusiveOr(this.group, group))
589 {
590return false;
591 }
592// check if both are non-nulls593if (assertNotNull(this.group) && assertNotNull(group))
594 {
595return stringEquals( this.group.getName(), group.getName());
596 }
597// both are null598returntrue;
599 }
600601/***602 * Check equality for given Role object with this ProfileLocator's Role603 * object.604 */605privateboolean roleEquals(Role role)
606 {
607// if either of reference is null return false.608if (exclusiveOr(this.role, role))
609 {
610return false;
611 }
612// check if both are non-nulls613if (assertNotNull(this.role) && assertNotNull(role))
614 {
615return stringEquals(this.role.getName(), role.getName());
616 }
617// both are null618returntrue;
619 }
620621/***622 * Check equality for language object with this ProfileLocator's language623 * object.624 */625privateboolean languageEquals(String language)
626 {
627return stringEquals(this.language, language);
628 }
629630/***631 * Check equality for country object with this ProfileLocator's country632 * object.633 */634privateboolean countryEquals(String country)
635 {
636return stringEquals(this.country, country);
637 }
638639/***640 * Check equality for name object with this ProfileLocator's name641 * object.642 */643privateboolean nameEquals(String name)
644 {
645return stringEquals(this.name, name);
646 }
647648/***649 * Check equality for name object with this ProfileLocator's name650 * object.651 */652privateboolean mediaTypeEquals(String mediaType)
653 {
654return stringEquals(this.mediaType, mediaType);
655 }
656657/***658 * AssertNotNull the two String objects and then check the equality.659 */660privateboolean stringEquals(String str1, String str2)
661 {
662if (exclusiveOr(str1, str2))
663 {
664return false;
665 }
666if (assertNotNull(str1) && assertNotNull(str2))
667 {
668return str1.equals(str2);
669 }
670// both are null671returntrue;
672 }
673674/***675 * AssertNotNull the given object.676 */677privateboolean assertNotNull(Object object)
678 {
679return object != null;
680 }
681682/***683 * Exclusive or the two objects fro their references null-ability.684 */685privateboolean exclusiveOr(Object obj1, Object obj2)
686 {
687return (assertNotNull(obj1) && !assertNotNull(obj2))
688 || (!assertNotNull(obj1) && assertNotNull(obj2));
689 }
690 }