View Javadoc

1   /*
2    * Copyright [2006] [University Corporation for Advanced Internet Development, Inc.]
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 edu.internet2.middleware.shibboleth.common.attribute.resolver.provider.attributeDefinition;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.List;
22  import java.util.Locale;
23  import java.util.Map;
24  
25  import org.opensaml.xml.util.LazyMap;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
30  import edu.internet2.middleware.shibboleth.common.attribute.encoding.AttributeEncoder;
31  import edu.internet2.middleware.shibboleth.common.attribute.resolver.AttributeResolutionException;
32  import edu.internet2.middleware.shibboleth.common.attribute.resolver.provider.AbstractResolutionPlugIn;
33  import edu.internet2.middleware.shibboleth.common.attribute.resolver.provider.ShibbolethResolutionContext;
34  
35  /**
36   * Base class for {@link AttributeDefinition} plug-ins.
37   */
38  public abstract class BaseAttributeDefinition extends AbstractResolutionPlugIn<BaseAttribute> implements
39          AttributeDefinition {
40      
41      /** Class logger. */
42      private final Logger log = LoggerFactory.getLogger(BaseAttributeDefinition.class);
43  
44      /** Whether this attribute definition is only a dependency and thus its values should never be released. */
45      private boolean dependencyOnly;
46  
47      /** Attribute encoders associated with this definition. */
48      private ArrayList<AttributeEncoder> encoders;
49  
50      /** Name of the attribute from data connectors to use to populate this definition. */
51      private String sourceAttributeID;
52  
53      /** Localized human intelligible attribute name. */
54      private Map<Locale, String> displayNames;
55      
56      /** Localized human readable description of attribute. */
57      private Map<Locale, String> displayDescriptions;
58  
59      /** Constructor. */
60      public BaseAttributeDefinition() {
61          dependencyOnly = false;
62          encoders = new ArrayList<AttributeEncoder>(3);
63          displayNames = new LazyMap<Locale, String>();
64          displayDescriptions = new LazyMap<Locale, String>();
65      }
66      
67      /**
68       * Gets the localized human readable description of attribute.
69       * 
70       * @return human readable description of attribute
71       */
72      public Map<Locale, String> getDisplayDescriptions() {
73          return displayDescriptions;
74      }
75  
76      /**
77       * Gets the localized human readable name of the attribute.
78       * 
79       * @return human readable name of the attribute
80       */
81      public Map<Locale, String> getDisplayNames() {
82          return displayNames;
83      }
84  
85      /** {@inheritDoc} */
86      public boolean isDependencyOnly() {
87          return dependencyOnly;
88      }
89  
90      /**
91       * Sets whether this attribute definition is only a dependency and thus its values should never be released outside
92       * the resolver.
93       * 
94       * @param isDependencyOnly whether this attribute definition is only a dependency
95       */
96      public void setDependencyOnly(boolean isDependencyOnly) {
97          dependencyOnly = isDependencyOnly;
98      }
99  
100     /** {@inheritDoc} */
101     public List<AttributeEncoder> getAttributeEncoders() {
102         return encoders;
103     }
104 
105     /** {@inheritDoc} */
106     public BaseAttribute resolve(ShibbolethResolutionContext resolutionContext) throws AttributeResolutionException {
107         BaseAttribute resolvedAttribute = doResolve(resolutionContext);
108 
109         if(resolvedAttribute == null){
110             log.error("{} produced a null attribute, this is not allowed", getId());
111             throw new AttributeResolutionException(getId() + " produced a null attribute");
112         }
113         
114         if(getDisplayNames() != null) {
115             resolvedAttribute.getDisplayNames().putAll(displayNames);
116         }
117         
118         if(getDisplayDescriptions() != null){
119             resolvedAttribute.getDisplayDescriptions().putAll(displayDescriptions);
120         }
121         
122         if (getAttributeEncoders() != null) {
123             resolvedAttribute.getEncoders().addAll(getAttributeEncoders());
124         }
125 
126         return resolvedAttribute;
127     }
128 
129     /**
130      * Creates and populates the values for the resolved attribute. Implementations should *not* set, or otherwise
131      * manage, the attribute encoders for the resolved attribute.
132      * 
133      * @param resolutionContext current attribute resolution context
134      * 
135      * @return resolved attribute
136      * 
137      * @throws AttributeResolutionException thrown if there is a problem resolving and creating the attribute
138      */
139     protected abstract BaseAttribute doResolve(ShibbolethResolutionContext resolutionContext)
140             throws AttributeResolutionException;
141 
142     /**
143      * Get values from dependencies.
144      * 
145      * @param context resolution context
146      * @return collection of values
147      */
148     protected Collection<Object> getValuesFromAllDependencies(ShibbolethResolutionContext context) {
149         return getValuesFromAllDependencies(context, getSourceAttributeID());
150     }
151 
152     /**
153      * Return the source attribute. If the source attribute is null, return the definition ID.
154      * 
155      * @return Returns the sourceAttribute.
156      */
157     public String getSourceAttributeID() {
158         if (sourceAttributeID != null) {
159             return sourceAttributeID;
160         } else {
161             return getId();
162         }
163     }
164 
165     /**
166      * Set the source attribute.
167      * 
168      * @param newSourceAttributeID The sourceAttribute to set.
169      */
170     public void setSourceAttributeID(String newSourceAttributeID) {
171         sourceAttributeID = newSourceAttributeID;
172     }
173 }