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;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.List;
22  import java.util.Map;
23  
24  import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
25  import edu.internet2.middleware.shibboleth.common.attribute.resolver.AttributeResolutionException;
26  import edu.internet2.middleware.shibboleth.common.attribute.resolver.provider.attributeDefinition.AttributeDefinition;
27  import edu.internet2.middleware.shibboleth.common.attribute.resolver.provider.dataConnector.DataConnector;
28  
29  
30  /**
31   * Base class for all {@link ResolutionPlugIn}s.
32   * 
33   * @param <ResolvedType> object type this plug-in resolves to
34   */
35  public abstract class AbstractResolutionPlugIn<ResolvedType> implements ResolutionPlugIn<ResolvedType> {
36  
37      /** The identifier for this plug-in. */
38      private String id;
39  
40      /** IDs of the {@link ResolutionPlugIn}s this plug-in depends on. */
41      private List<String> dependencyIds;
42  
43      /** Constructor. */
44      public AbstractResolutionPlugIn() {
45          dependencyIds = new ArrayList<String>();
46      }
47  
48      /** {@inheritDoc} */
49      public List<String> getDependencyIds() {
50          return dependencyIds;
51      }
52  
53      /** {@inheritDoc} */
54      public String getId() {
55          return id;
56      }
57  
58      /**
59       * Set plug-in id.
60       * 
61       * @param newId new plug-in id
62       */
63      public void setId(String newId) {
64          id = newId;
65      }
66  
67      /**
68       * Get values from dependencies.
69       * 
70       * @param context resolution context
71       * @param sourceAttribute ID of attribute to retrieve from dependencies
72       * @return collection of values
73       */
74      protected Collection<Object> getValuesFromAllDependencies(ShibbolethResolutionContext context, String sourceAttribute) {
75          List<Object> values = new ArrayList<Object>();
76      
77          for (String id : getDependencyIds()) {
78              if (context.getResolvedAttributeDefinitions().containsKey(id)) {
79                  values.addAll(getValuesFromAttributeDependency(context, id));
80              } else if (context.getResolvedDataConnectors().containsKey(id)) {
81                  values.addAll(getValuesFromConnectorDependency(context, id, sourceAttribute));
82              }
83          }
84      
85          return values;
86      }
87  
88      /**
89       * Get values from attribute dependencies.
90       * 
91       * @param context resolution context
92       * @param id ID of attribute to retrieve dependencies for
93       * 
94       * @return collection of values
95       */
96      protected Collection<Object> getValuesFromAttributeDependency(ShibbolethResolutionContext context, String id) {
97          List<Object> values = new ArrayList<Object>();
98      
99          AttributeDefinition definition = context.getResolvedAttributeDefinitions().get(id);
100         if (definition != null) {
101             try {
102                 BaseAttribute attribute = definition.resolve(context);
103                 for (Object o : attribute.getValues()) {
104                     values.add(o);
105                 }
106             } catch (AttributeResolutionException e) {
107                 
108             }
109         }
110     
111         return values;
112     }
113 
114     /**
115      * Get values from data connectors.
116      * 
117      * @param context resolution context
118      * @param id ID of attribute to retrieve dependencies for
119      * @param sourceAttribute ID of attribute to retrieve from connector dependencies
120      * 
121      * @return collection of values
122      */
123     protected Collection<Object> getValuesFromConnectorDependency(ShibbolethResolutionContext context, String id, String sourceAttribute) {
124         List<Object> values = new ArrayList<Object>();
125     
126         DataConnector connector = context.getResolvedDataConnectors().get(id);
127         if (connector != null) {
128             try {
129                 Map<String, BaseAttribute> attributes = connector.resolve(context);
130                 for (String attributeId : attributes.keySet()) {
131                     if (attributeId != null && attributeId.equals(sourceAttribute)) {
132                         for (Object o : attributes.get(attributeId).getValues()) {
133                             values.add(o);
134                         }
135                     }
136                 }
137             } catch (AttributeResolutionException e) {
138                 
139             }
140     
141         }
142     
143         return values;
144     }
145 }