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.filtering.provider;
18  
19  import java.util.Collection;
20  import java.util.Comparator;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.TreeSet;
25  
26  import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
27  import edu.internet2.middleware.shibboleth.common.profile.provider.SAMLProfileRequestContext;
28  
29  /**
30   * Contextual information for performing attribute filtering.
31   */
32  public class ShibbolethFilteringContext {
33  
34      /** The attribute request. */
35      private SAMLProfileRequestContext attributeRequestContext;
36  
37      /** Attributes being filtered. */
38      private Map<String, BaseAttribute> unfilteredAttributes;
39  
40      /** Retained values for a given attribute. */
41      private Map<String, Collection> retainedValues;
42  
43      /** Deny value rules that apply to the attribute identified by the map key. */
44      private Map<String, List<MatchFunctor>> denyValueRules;
45  
46      /**
47       * Constructor.
48       * 
49       * @param attributes unfiltered attribute set
50       * @param context attribute request context
51       */
52      public ShibbolethFilteringContext(Map<String, BaseAttribute> attributes, SAMLProfileRequestContext context) {
53          attributeRequestContext = context;
54          unfilteredAttributes = attributes;
55          retainedValues = new HashMap<String, Collection>();
56          denyValueRules = new HashMap<String, List<MatchFunctor>>();
57      }
58  
59      /**
60       * Gets the context for the attribute request.
61       * 
62       * @return context for the attribute request
63       */
64      public SAMLProfileRequestContext getAttributeRequestContext() {
65          return attributeRequestContext;
66      }
67  
68      /**
69       * Gets the attributes being filtered.
70       * 
71       * @return attributes being filtered
72       */
73      public Map<String, BaseAttribute> getUnfilteredAttributes() {
74          return unfilteredAttributes;
75      }
76  
77      /**
78       * Gets the values, for the given attribute, that have no yet been filtered out.
79       * 
80       * @param attributeId attribute to retrieve the values for
81       * @param prepopulate whether to pre-populate the retained value list from the unfiltered value list if there is
82       *            currently no set of values retained for the given attribute
83       * 
84       * @return attribute values not yet filtered out, never null
85       */
86      public Collection getRetainedValues(String attributeId, boolean prepopulate) {
87          BaseAttribute attribute = unfilteredAttributes.get(attributeId);
88          Collection attributeValues = null;
89          if (!retainedValues.containsKey(attributeId) && prepopulate) {
90              if (prepopulate) {
91                  if (attribute != null) {
92                      attributeValues = attribute.getValues();
93                  }
94  
95                  retainedValues.put(attributeId, attributeValues);
96              }
97          } else {
98              attributeValues = retainedValues.get(attributeId);
99          }
100 
101         if (attributeValues == null) {
102             Comparator valueComparator = null;
103             if (attribute != null) {
104                 valueComparator = attribute.getValueComparator();
105             }
106             if (valueComparator == null) {
107                 valueComparator = new ObjectStringComparator();
108             }
109             attributeValues = new TreeSet<Object>(valueComparator);
110             retainedValues.put(attributeId, attributeValues);
111         }
112         return attributeValues;
113     }
114 
115     /**
116      * Gets the deny value rules that apply to the attribute. The map key is the ID of the attribute, the value is a
117      * list of deny rules that apply to that attribute.
118      * 
119      * @return deny value rules that apply to the attribute
120      */
121     public Map<String, List<MatchFunctor>> getDenyValueRules() {
122         return denyValueRules;
123     }
124 
125     /** Class that compares objects based on their String representation. */
126     private class ObjectStringComparator implements Comparator<Object> {
127 
128         /** {@inheritDoc} */
129         public int compare(Object o1, Object o2) {
130             if (o1 == null) {
131                 return -1;
132             }
133 
134             if (o2 == null) {
135                 return 1;
136             }
137 
138             return o1.toString().compareTo(o2.toString());
139         }
140     }
141 }