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.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
28  import edu.internet2.middleware.shibboleth.common.attribute.provider.BasicAttribute;
29  import edu.internet2.middleware.shibboleth.common.attribute.resolver.AttributeResolutionException;
30  import edu.internet2.middleware.shibboleth.common.attribute.resolver.provider.ShibbolethResolutionContext;
31  
32  /**
33   * The RegexAttributeDefinition allows regular expression based replacements on attribute values, using the regex syntax
34   * allowed by {@link java.util.regex.Pattern}.
35   */
36  public class MappedAttributeDefinition extends BaseAttributeDefinition {
37  
38      /** Class logger. */
39      private static Logger log = LoggerFactory.getLogger(MappedAttributeDefinition.class);
40  
41      /** Default return value. */
42      private String defaultValue;
43  
44      /** Whether the definition passes thru unmatched values. */
45      private boolean passThru;
46  
47      /** Value maps. */
48      private Collection<ValueMap> valueMaps;
49  
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          boolean valueMapMatch;
62  
63          for (Object o : getValuesFromAllDependencies(resolutionContext)) {
64              valueMapMatch = false;
65              Set<String> mappedValues;
66  
67              for (ValueMap valueMap : valueMaps) {
68                  mappedValues = valueMap.evaluate(o.toString());
69                  if (!mappedValues.isEmpty()) {
70                      valueMapMatch = true;
71                      attribute.getValues().addAll(mappedValues);
72                  }
73              }
74  
75              if (!valueMapMatch) {
76                  if (passThru) {
77                      attribute.getValues().add(o.toString());
78                  } else if (!DatatypeHelper.isEmpty(defaultValue)) {
79                      attribute.getValues().add(getDefaultValue());
80                  }
81              }
82          }
83  
84          return attribute;
85      }
86  
87      /** {@inheritDoc} */
88      public void validate() throws AttributeResolutionException {
89          if (passThru && !DatatypeHelper.isEmpty(defaultValue)) {
90              log.error("MappedAttributeDefinition (" + getId()
91                      + ") may not have a DefaultValue string with passThru enabled.");
92              throw new AttributeResolutionException("MappedAttributeDefinition (" + getId()
93                      + ") may not have a DefaultValue string with passThru enabled.");
94          }
95      }
96  
97      /**
98       * Gets the default return value.
99       * @return the default return value.
100      */
101     public String getDefaultValue() {
102         return defaultValue;
103     }
104 
105     /**
106      * Sets the default return value.
107      * @param newDefaultValue the default return value
108      */
109     public void setDefaultValue(String newDefaultValue) {
110         defaultValue = newDefaultValue;
111     }
112 
113     /**
114      * Gets whether the definition passes thru unmatched values.
115      * @return whether the definition passes thru unmatched values.
116      */
117     public boolean isPassThru() {
118         return passThru;
119     }
120 
121     /**
122      * Sets whether the definition passes thru unmatched values.
123      * @param newPassThru whether the definition passes thru unmatched values.
124      */
125     public void setPassThru(boolean newPassThru) {
126         passThru = newPassThru;
127     }
128 
129     /**
130      * Get the value maps.
131      * @return the value maps.
132      */
133     public Collection<ValueMap> getValueMaps() {
134         return valueMaps;
135     }
136 
137 }