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