View Javadoc

1   /*
2    * Copyright [2007] [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.Collection;
20  import java.util.HashSet;
21  import java.util.Set;
22  import java.util.regex.Matcher;
23  import java.util.regex.Pattern;
24  import java.util.regex.PatternSyntaxException;
25  
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  /**
30   * Performs many to one mapping of source values to a return value. SourceValue strings may include regular expressions
31   * and the ReturnValue may include back references to capturing groups as supported by {@link java.util.regex.Pattern}.
32   */
33  public class ValueMap {
34  
35      /** Class logger. */
36      private final Logger log = LoggerFactory.getLogger(ValueMap.class);
37  
38      /** Return value. */
39      private String returnValue;
40  
41      /** Source values. */
42      private Collection<SourceValue> sourceValues;
43  
44      /** Constructor. */
45      public ValueMap() {
46          sourceValues = new HashSet<SourceValue>();
47      }
48  
49      /**
50       * Gets the return value.
51       * 
52       * @return the return value
53       */
54      public String getReturnValue() {
55          return returnValue;
56      }
57  
58      /**
59       * Sets the return value.
60       * 
61       * @param newReturnValue the return value
62       */
63      public void setReturnValue(String newReturnValue) {
64          returnValue = newReturnValue;
65      }
66  
67      /**
68       * Gets the collection of source values.
69       * 
70       * @return the collection of source values
71       */
72      public Collection<SourceValue> getSourceValues() {
73          return sourceValues;
74      }
75  
76      /**
77       * Evaluate an incoming attribute value against this value map.
78       * 
79       * @param attributeValue incoming attribute value
80       * @return set of new values the incoming value mapped to
81       */
82      public Set<String> evaluate(String attributeValue) {
83          log.debug("Attempting to map attribute value '{}'", attributeValue);
84          Set<String> mappedValues = new HashSet<String>();
85          Matcher m;
86  
87          String newValue;
88          for (SourceValue sourceValue : sourceValues) {
89              newValue = null;
90              if (sourceValue.isPartialMatch()) {
91                  log.debug("Performing partial match comparison.");
92                  if (attributeValue.contains(sourceValue.getValue())) {
93                      log.debug("Attribute value '{}' matches source value '{}' it will be mapped to '{}'", new Object[] {
94                              attributeValue, sourceValue.getValue(), newValue });
95                      newValue = returnValue;
96                  }
97              } else {
98                  log.debug("Performing regular expression based comparison");
99                  try {
100                     int flags = sourceValue.isIgnoreCase() ? Pattern.CASE_INSENSITIVE : 0;
101                     m = Pattern.compile(sourceValue.getValue(), flags).matcher(attributeValue);
102                     if (m.matches()) {
103                         newValue = m.replaceAll(returnValue);
104                         log.debug("Attribute value '{}' matches regular expression it will be mapped to '{}'",
105                                 attributeValue, newValue);
106                     }
107                 } catch (PatternSyntaxException e) {
108                     log.debug("Error matching value {}.  Skipping this value.", attributeValue);
109                 }
110             }
111 
112             if (newValue != null) {
113                 mappedValues.add(newValue);
114             }
115         }
116 
117         return mappedValues;
118     }
119 
120     /**
121      * Represents incoming attribute values and rules used for matching them. The value may include regular expressions.
122      */
123     public class SourceValue {
124 
125         /**
126          * Value string. This may contain regular expressions.
127          */
128         private String value;
129 
130         /**
131          * Whether case should be ignored when matching.
132          */
133         private boolean ignoreCase;
134 
135         /**
136          * Whether partial matches should be allowed.
137          */
138         private boolean partialMatch;
139 
140         /**
141          * Constructor.
142          * 
143          * @param newValue value string
144          * @param newIgnoreCase whether case should be ignored when matching
145          * @param newPartialMatch whether partial matches should be allowed
146          */
147         public SourceValue(String newValue, boolean newIgnoreCase, boolean newPartialMatch) {
148             value = newValue;
149             ignoreCase = newIgnoreCase;
150             partialMatch = newPartialMatch;
151         }
152 
153         /**
154          * Gets whether case should be ignored when matching.
155          * 
156          * @return whether case should be ignored when matching
157          */
158         public boolean isIgnoreCase() {
159             return ignoreCase;
160         }
161 
162         /**
163          * Gets whether partial matches should be allowed.
164          * 
165          * @return whether partial matches should be allowed
166          */
167         public boolean isPartialMatch() {
168             return partialMatch;
169         }
170 
171         /**
172          * Gets the value string.
173          * 
174          * @return the value string.
175          */
176         public String getValue() {
177             return value;
178         }
179 
180         /** {@inheritDoc} */
181         public String toString() {
182             return getValue();
183         }
184 
185     }
186 }