View Javadoc

1   /*
2    * Licensed to the University Corporation for Advanced Internet Development, 
3    * Inc. (UCAID) under one or more contributor license agreements.  See the 
4    * NOTICE file distributed with this work for additional information regarding
5    * copyright ownership. The UCAID licenses this file to You under the Apache 
6    * License, Version 2.0 (the "License"); you may not use this file except in 
7    * compliance with the License.  You may obtain a copy of the License at
8    *
9    *    http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package edu.internet2.middleware.shibboleth.common.attribute.resolver.provider.attributeDefinition;
19  
20  import java.util.ArrayList;
21  import java.util.Collection;
22  import java.util.List;
23  import java.util.Locale;
24  import java.util.Map;
25  
26  import org.opensaml.xml.util.LazyMap;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
31  import edu.internet2.middleware.shibboleth.common.attribute.encoding.AttributeEncoder;
32  import edu.internet2.middleware.shibboleth.common.attribute.resolver.AttributeResolutionException;
33  import edu.internet2.middleware.shibboleth.common.attribute.resolver.provider.AbstractResolutionPlugIn;
34  import edu.internet2.middleware.shibboleth.common.attribute.resolver.provider.ShibbolethResolutionContext;
35  
36  /**
37   * Base class for {@link AttributeDefinition} plug-ins.
38   */
39  public abstract class BaseAttributeDefinition extends AbstractResolutionPlugIn<BaseAttribute> implements
40          AttributeDefinition {
41      
42      /** Class logger. */
43      private final Logger log = LoggerFactory.getLogger(BaseAttributeDefinition.class);
44  
45      /** Whether this attribute definition is only a dependency and thus its values should never be released. */
46      private boolean dependencyOnly;
47  
48      /** Attribute encoders associated with this definition. */
49      private ArrayList<AttributeEncoder> encoders;
50  
51      /** Name of the attribute from data connectors to use to populate this definition. */
52      private String sourceAttributeID;
53  
54      /** Localized human intelligible attribute name. */
55      private Map<Locale, String> displayNames;
56      
57      /** Localized human readable description of attribute. */
58      private Map<Locale, String> displayDescriptions;
59  
60      /** Constructor. */
61      public BaseAttributeDefinition() {
62          dependencyOnly = false;
63          encoders = new ArrayList<AttributeEncoder>(3);
64          displayNames = new LazyMap<Locale, String>();
65          displayDescriptions = new LazyMap<Locale, String>();
66      }
67      
68      /**
69       * Gets the localized human readable description of attribute.
70       * 
71       * @return human readable description of attribute
72       */
73      public Map<Locale, String> getDisplayDescriptions() {
74          return displayDescriptions;
75      }
76  
77      /**
78       * Gets the localized human readable name of the attribute.
79       * 
80       * @return human readable name of the attribute
81       */
82      public Map<Locale, String> getDisplayNames() {
83          return displayNames;
84      }
85  
86      /** {@inheritDoc} */
87      public boolean isDependencyOnly() {
88          return dependencyOnly;
89      }
90  
91      /**
92       * Sets whether this attribute definition is only a dependency and thus its values should never be released outside
93       * the resolver.
94       * 
95       * @param isDependencyOnly whether this attribute definition is only a dependency
96       */
97      public void setDependencyOnly(boolean isDependencyOnly) {
98          dependencyOnly = isDependencyOnly;
99      }
100 
101     /** {@inheritDoc} */
102     public List<AttributeEncoder> getAttributeEncoders() {
103         return encoders;
104     }
105 
106     /** {@inheritDoc} */
107     public BaseAttribute resolve(ShibbolethResolutionContext resolutionContext) throws AttributeResolutionException {
108         BaseAttribute resolvedAttribute = doResolve(resolutionContext);
109 
110         if(resolvedAttribute == null){
111             log.error("{} produced a null attribute, this is not allowed", getId());
112             throw new AttributeResolutionException(getId() + " produced a null attribute");
113         }
114         
115         if(getDisplayNames() != null) {
116             resolvedAttribute.getDisplayNames().putAll(displayNames);
117         }
118         
119         if(getDisplayDescriptions() != null){
120             resolvedAttribute.getDisplayDescriptions().putAll(displayDescriptions);
121         }
122         
123         if (getAttributeEncoders() != null) {
124             resolvedAttribute.getEncoders().addAll(getAttributeEncoders());
125         }
126 
127         return resolvedAttribute;
128     }
129 
130     /**
131      * Creates and populates the values for the resolved attribute. Implementations should *not* set, or otherwise
132      * manage, the attribute encoders for the resolved attribute.
133      * 
134      * @param resolutionContext current attribute resolution context
135      * 
136      * @return resolved attribute
137      * 
138      * @throws AttributeResolutionException thrown if there is a problem resolving and creating the attribute
139      */
140     protected abstract BaseAttribute doResolve(ShibbolethResolutionContext resolutionContext)
141             throws AttributeResolutionException;
142 
143     /**
144      * Get values from dependencies.
145      * 
146      * @param context resolution context
147      * @return collection of values
148      */
149     protected Collection<Object> getValuesFromAllDependencies(ShibbolethResolutionContext context) {
150         return getValuesFromAllDependencies(context, getSourceAttributeID());
151     }
152 
153     /**
154      * Return the source attribute. If the source attribute is null, return the definition ID.
155      * 
156      * @return Returns the sourceAttribute.
157      */
158     public String getSourceAttributeID() {
159         if (sourceAttributeID != null) {
160             return sourceAttributeID;
161         } else {
162             return getId();
163         }
164     }
165 
166     /**
167      * Set the source attribute.
168      * 
169      * @param newSourceAttributeID The sourceAttribute to set.
170      */
171     public void setSourceAttributeID(String newSourceAttributeID) {
172         sourceAttributeID = newSourceAttributeID;
173     }
174 }