001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.routetemplate;
018:
019: import java.util.ArrayList;
020: import java.util.Collections;
021: import java.util.List;
022:
023: import edu.iu.uis.eden.engine.RouteContext;
024: import edu.iu.uis.eden.exception.EdenUserNotFoundException;
025: import edu.iu.uis.eden.routeheader.DocumentContent;
026:
027: /**
028: * A simple base RoleAttribute implementation for roles that do not need to be qualified
029: * prior to resolution.
030: *
031: * @author Aaron Hamid (arh14 at cornell dot edu)
032: */
033: public abstract class UnqualifiedRoleAttribute extends
034: AbstractRoleAttribute {
035:
036: protected List<Role> roles;
037:
038: /**
039: * No-arg constructor for subclasses that will override {@link #getRoleNames()} to provide their own roles list
040: */
041: public UnqualifiedRoleAttribute() {
042: roles = Collections.emptyList();
043: }
044:
045: /**
046: * Constructor for subclasses that can provide a role list at construction time
047: */
048: public UnqualifiedRoleAttribute(List<Role> roles) {
049: this .roles = roles;
050: }
051:
052: public List<Role> getRoleNames() {
053: return roles;
054: }
055:
056: /**
057: * Returns a List<String> containing only the roleName parameter; i.e. no qualification occurs
058: */
059: public List<String> getQualifiedRoleNames(String roleName,
060: DocumentContent documentContent)
061: throws EdenUserNotFoundException {
062: List<String> qualifiedRoleName = new ArrayList<String>(1);
063: qualifiedRoleName.add(roleName);
064: return qualifiedRoleName;
065: }
066:
067: /**
068: * Helper method for parsing the actual role name out from the class/rolename combination
069: * as Role class combines the two and does expose the original role name
070: * @param classAndRole the class and role string (e.g. org.blah.MyRoleAttribute!SOME_ROLE_NAME)
071: * @return the role name portion of the class and role string (e.g. SOME_ROLE_NAME);
072: */
073: protected String parseRoleNameFromClassAndRole(String classAndRole) {
074: return classAndRole.substring(classAndRole.indexOf("!") + 1);
075: }
076:
077: public ResolvedQualifiedRole resolveQualifiedRole(
078: RouteContext routeContext, String roleName,
079: String qualifiedRole) throws EdenUserNotFoundException {
080: // some sanity checking
081: if (!roleName.equals(qualifiedRole)) {
082: throw new IllegalArgumentException(
083: "UnqualifiedRoleAttribute resolveQualifiedRole invoked with differing role and qualified role (they should be the same)");
084: }
085: // this attribute should never be called to resolve any roles other than those it advertised as supporting!
086: boolean valid = false;
087: for (Role role : getRoleNames()) {
088: if (parseRoleNameFromClassAndRole(role.getName()).equals(
089: roleName)) {
090: valid = true;
091: break;
092: }
093: }
094: if (!valid) {
095: throw new IllegalArgumentException(
096: "This attribute does not support the role: '"
097: + roleName + "'");
098: }
099: return resolveRole(routeContext, roleName);
100: }
101:
102: /**
103: * Template method for subclasses to implement
104: * @param routeContext the RouteContext
105: * @param roleName the role name
106: * @return a ResolvedQualifiedRole
107: */
108: protected abstract ResolvedQualifiedRole resolveRole(
109: RouteContext routeContext, String roleName)
110: throws EdenUserNotFoundException;
111: }
|