View Javadoc

1   /*
2    * Licensed to the University Corporation for Advanced Internet Development, 
3    * Inc. (UCAID) under one or more contributor license agreements.  See the 
4    * NOTICE file distributed with this work for additional information regarding
5    * copyright ownership. The UCAID licenses this file to You under the Apache 
6    * License, Version 2.0 (the "License"); you may not use this file except in 
7    * compliance with the License.  You may obtain a copy of the License at
8    *
9    *    http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package edu.internet2.middleware.shibboleth.common.attribute.filtering.provider;
19  
20  import java.util.Collection;
21  import java.util.Comparator;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.TreeSet;
26  
27  import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
28  import edu.internet2.middleware.shibboleth.common.profile.provider.SAMLProfileRequestContext;
29  
30  /**
31   * Contextual information for performing attribute filtering.
32   */
33  public class ShibbolethFilteringContext {
34  
35      /** The attribute request. */
36      private SAMLProfileRequestContext attributeRequestContext;
37  
38      /** Attributes being filtered. */
39      private Map<String, BaseAttribute> unfilteredAttributes;
40  
41      /** Retained values for a given attribute. */
42      private Map<String, Collection> retainedValues;
43  
44      /** Deny value rules that apply to the attribute identified by the map key. */
45      private Map<String, List<MatchFunctor>> denyValueRules;
46  
47      /**
48       * Constructor.
49       * 
50       * @param attributes unfiltered attribute set
51       * @param context attribute request context
52       */
53      public ShibbolethFilteringContext(Map<String, BaseAttribute> attributes, SAMLProfileRequestContext context) {
54          attributeRequestContext = context;
55          unfilteredAttributes = attributes;
56          retainedValues = new HashMap<String, Collection>();
57          denyValueRules = new HashMap<String, List<MatchFunctor>>();
58      }
59  
60      /**
61       * Gets the context for the attribute request.
62       * 
63       * @return context for the attribute request
64       */
65      public SAMLProfileRequestContext getAttributeRequestContext() {
66          return attributeRequestContext;
67      }
68  
69      /**
70       * Gets the attributes being filtered.
71       * 
72       * @return attributes being filtered
73       */
74      public Map<String, BaseAttribute> getUnfilteredAttributes() {
75          return unfilteredAttributes;
76      }
77  
78      /**
79       * Gets the values, for the given attribute, that have no yet been filtered out.
80       * 
81       * @param attributeId attribute to retrieve the values for
82       * @param prepopulate whether to pre-populate the retained value list from the unfiltered value list if there is
83       *            currently no set of values retained for the given attribute
84       * 
85       * @return attribute values not yet filtered out, never null
86       */
87      public Collection getRetainedValues(String attributeId, boolean prepopulate) {
88          BaseAttribute attribute = unfilteredAttributes.get(attributeId);
89          Collection attributeValues = null;
90          if (!retainedValues.containsKey(attributeId) && prepopulate) {
91              if (prepopulate) {
92                  if (attribute != null) {
93                      attributeValues = attribute.getValues();
94                  }
95  
96                  retainedValues.put(attributeId, attributeValues);
97              }
98          } else {
99              attributeValues = retainedValues.get(attributeId);
100         }
101 
102         if (attributeValues == null) {
103             Comparator valueComparator = null;
104             if (attribute != null) {
105                 valueComparator = attribute.getValueComparator();
106             }
107             if (valueComparator == null) {
108                 valueComparator = new ObjectStringComparator();
109             }
110             attributeValues = new TreeSet<Object>(valueComparator);
111             retainedValues.put(attributeId, attributeValues);
112         }
113         return attributeValues;
114     }
115 
116     /**
117      * Gets the deny value rules that apply to the attribute. The map key is the ID of the attribute, the value is a
118      * list of deny rules that apply to that attribute.
119      * 
120      * @return deny value rules that apply to the attribute
121      */
122     public Map<String, List<MatchFunctor>> getDenyValueRules() {
123         return denyValueRules;
124     }
125 
126     /** Class that compares objects based on their String representation. */
127     private class ObjectStringComparator implements Comparator<Object> {
128 
129         /** {@inheritDoc} */
130         public int compare(Object o1, Object o2) {
131             if (o1 == null) {
132                 return -1;
133             }
134 
135             if (o2 == null) {
136                 return 1;
137             }
138 
139             return o1.toString().compareTo(o2.toString());
140         }
141     }
142 }