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.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.opensaml.xml.util.LazyList;
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          Collection attributeValues;
89          if (!retainedValues.containsKey(attributeId)) {
90              attributeValues = new LazyList();
91              retainedValues.put(attributeId, attributeValues);
92              
93              if (prepopulate) {
94                  BaseAttribute attribute = unfilteredAttributes.get(attributeId);
95                  if (attribute != null) {
96                      attributeValues.addAll(attribute.getValues());
97                  }
98              }
99          } else {
100             attributeValues = retainedValues.get(attributeId);
101         }
102 
103         return attributeValues;
104     }
105 
106     /**
107      * Gets the deny value rules that apply to the attribute. The map key is the ID of the attribute, the value is a
108      * list of deny rules that apply to that attribute.
109      * 
110      * @return deny value rules that apply to the attribute
111      */
112     public Map<String, List<MatchFunctor>> getDenyValueRules() {
113         return denyValueRules;
114     }
115 }