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.Set;
22  
23  import org.opensaml.xml.util.DatatypeHelper;
24  import org.opensaml.xml.util.LazySet;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
29  import edu.internet2.middleware.shibboleth.common.attribute.provider.BasicAttribute;
30  import edu.internet2.middleware.shibboleth.common.attribute.resolver.AttributeResolutionException;
31  import edu.internet2.middleware.shibboleth.common.attribute.resolver.provider.ShibbolethResolutionContext;
32  
33  /**
34   * The RegexAttributeDefinition allows regular expression based replacements on attribute values, using the regex syntax
35   * allowed by {@link java.util.regex.Pattern}.
36   */
37  public class MappedAttributeDefinition extends BaseAttributeDefinition {
38  
39      /** Class logger. */
40      private static Logger log = LoggerFactory.getLogger(MappedAttributeDefinition.class);
41  
42      /** Default return value. */
43      private String defaultValue;
44  
45      /** Whether the definition passes thru unmatched values. */
46      private boolean passThru;
47  
48      /** Value maps. */
49      private Collection<ValueMap> valueMaps;
50  
51      /** Constructor. */
52      public MappedAttributeDefinition() {
53          valueMaps = new ArrayList<ValueMap>(5);
54      }
55  
56      /** {@inheritDoc} */
57      protected BaseAttribute doResolve(ShibbolethResolutionContext resolutionContext)
58              throws AttributeResolutionException {
59          BasicAttribute<String> attribute = new BasicAttribute<String>();
60          attribute.setId(getId());
61  
62          Collection<?> values = getValuesFromAllDependencies(resolutionContext);
63          if (values == null || values.isEmpty()) {
64              log.debug("Attribute Definition {}: No values from dependency attribute attribute {}", getId(),
65                      getDependencyIds());
66              if (!DatatypeHelper.isEmpty(getDefaultValue())) {
67                  log.debug(
68                          "Attribute Definition {}: Default value is not empty, adding it as the value for this attribute",
69                          getId());
70                  attribute.getValues().add(getDefaultValue());
71              }
72              return attribute;
73          }
74  
75          Set<String> mappedValues;
76          for (Object o : values) {
77              if (o == null) {
78                  log.debug("Attribute Definition {}: null attribute value, skipping it", getId());
79                  continue;
80              }
81              mappedValues = mapValue(o.toString());
82              attribute.getValues().addAll(mappedValues);
83          }
84  
85          return attribute;
86      }
87  
88      /**
89       * Maps the value from a dependency in to the value(s) for this attribute.
90       * 
91       * @param value the value from the dependency
92       * 
93       * @return the set of attribute values that the given dependency value maps in to
94       */
95      protected Set<String> mapValue(String value) {
96          log.debug("Attribute Definition {}: mapping depdenency attribute value {}", getId(), value);
97          
98          LazySet<String> mappedValues = new LazySet<String>();
99  
100         boolean valueMapMatch = false;
101         if (!DatatypeHelper.isEmpty(value)) {
102             for (ValueMap valueMap : valueMaps) {
103                 mappedValues.addAll(valueMap.evaluate(value));
104                 if (!mappedValues.isEmpty()) {
105                     valueMapMatch = true;
106                 }
107             }
108 
109             if (!valueMapMatch) {
110                 if (passThru) {
111                     mappedValues.add(value);
112                 } else if (getDefaultValue() != null) {
113                     mappedValues.add(getDefaultValue());
114                 }
115             }
116         }
117 
118         log.debug("Attribute Definition {}: mapped depdenency attribute value {} to the values {}", new Object[] {
119                 getId(), value, mappedValues, });
120         
121         return mappedValues;
122     }
123 
124     /** {@inheritDoc} */
125     public void validate() throws AttributeResolutionException {
126         if (passThru && !DatatypeHelper.isEmpty(defaultValue)) {
127             log.error("MappedAttributeDefinition (" + getId()
128                     + ") may not have a DefaultValue string with passThru enabled.");
129             throw new AttributeResolutionException("MappedAttributeDefinition (" + getId()
130                     + ") may not have a DefaultValue string with passThru enabled.");
131         }
132     }
133 
134     /**
135      * Gets the default return value.
136      * 
137      * @return the default return value.
138      */
139     public String getDefaultValue() {
140         return defaultValue;
141     }
142 
143     /**
144      * Sets the default return value.
145      * 
146      * @param newDefaultValue the default return value
147      */
148     public void setDefaultValue(String newDefaultValue) {
149         defaultValue = newDefaultValue;
150     }
151 
152     /**
153      * Gets whether the definition passes thru unmatched values.
154      * 
155      * @return whether the definition passes thru unmatched values.
156      */
157     public boolean isPassThru() {
158         return passThru;
159     }
160 
161     /**
162      * Sets whether the definition passes thru unmatched values.
163      * 
164      * @param newPassThru whether the definition passes thru unmatched values.
165      */
166     public void setPassThru(boolean newPassThru) {
167         passThru = newPassThru;
168     }
169 
170     /**
171      * Get the value maps.
172      * 
173      * @return the value maps.
174      */
175     public Collection<ValueMap> getValueMaps() {
176         return valueMaps;
177     }
178 
179 }