1 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 42 43 public class BaseProfileLocator implements ProfileLocator 44 { 45 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 64 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseProfileLocator.class.getName()); 65 66 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 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 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 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 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 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 273 public Object clone() throws java.lang.CloneNotSupportedException 274 { 275 return super.clone(); 276 } 277 278 283 public String getName() 284 { 285 return name; 286 } 287 288 293 public void setName(String name) 294 { 295 this.name = name; 296 } 297 298 303 public boolean getAnonymous() 304 { 305 return this.anonymous; 306 } 307 308 309 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 338 public String getMediaType() 339 { 340 return mediaType; 341 } 342 343 349 public void setMediaType(String mediaType) 350 { 351 this.mediaType = mediaType; 352 } 353 354 361 public String getLanguage() 362 { 363 return language; 364 } 365 366 373 public void setLanguage(String language) 374 { 375 this.language = language; 376 } 377 378 385 public String getCountry() 386 { 387 return country; 388 } 389 390 397 public void setCountry(String country) 398 { 399 this.country = country; 400 } 401 402 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 425 public void setUser(JetspeedUser user) 426 { 427 this.user = user; 428 } 429 430 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 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 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 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 522 523 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 && mediaTypeEquals(mediaType) 552 && languageEquals(language) 553 && countryEquals(country) 554 && userEquals(locator) 555 && groupEquals(group) 556 && roleEquals(role); 557 } 558 } 559 560 564 private boolean userEquals(ProfileLocator locator) 565 { 566 JetspeedUser user = locator.getUser(); 567 if (exclusiveOr(this.user, user)) 569 { 570 return false; 571 } 572 if (assertNotNull(this.user) && assertNotNull(user)) 574 { 575 return stringEquals(this.user.getUserName(), user.getUserName()); 576 } 577 return this.anonymous == locator.getAnonymous(); 579 } 580 581 585 private boolean groupEquals(Group group) 586 { 587 if (exclusiveOr(this.group, group)) 589 { 590 return false; 591 } 592 if (assertNotNull(this.group) && assertNotNull(group)) 594 { 595 return stringEquals( this.group.getName(), group.getName()); 596 } 597 return true; 599 } 600 601 605 private boolean roleEquals(Role role) 606 { 607 if (exclusiveOr(this.role, role)) 609 { 610 return false; 611 } 612 if (assertNotNull(this.role) && assertNotNull(role)) 614 { 615 return stringEquals(this.role.getName(), role.getName()); 616 } 617 return true; 619 } 620 621 625 private boolean languageEquals(String language) 626 { 627 return stringEquals(this.language, language); 628 } 629 630 634 private boolean countryEquals(String country) 635 { 636 return stringEquals(this.country, country); 637 } 638 639 643 private boolean nameEquals(String name) 644 { 645 return stringEquals(this.name, name); 646 } 647 648 652 private boolean mediaTypeEquals(String mediaType) 653 { 654 return stringEquals(this.mediaType, mediaType); 655 } 656 657 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 return true; 672 } 673 674 677 private boolean assertNotNull(Object object) 678 { 679 return object != null; 680 } 681 682 685 private boolean exclusiveOr(Object obj1, Object obj2) 686 { 687 return (assertNotNull(obj1) && !assertNotNull(obj2)) 688 || (!assertNotNull(obj1) && assertNotNull(obj2)); 689 } 690 } 691 | Popular Tags |