Add a configuration option to the Simple Attribute Definition PlugIn that allows...
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / aa / attrresolv / provider / SimpleAttributeDefinition.java
1 /* 
2  * The Shibboleth License, Version 1. 
3  * Copyright (c) 2002 
4  * University Corporation for Advanced Internet Development, Inc. 
5  * All rights reserved
6  * 
7  * 
8  * Redistribution and use in source and binary forms, with or without 
9  * modification, are permitted provided that the following conditions are met:
10  * 
11  * Redistributions of source code must retain the above copyright notice, this 
12  * list of conditions and the following disclaimer.
13  * 
14  * Redistributions in binary form must reproduce the above copyright notice, 
15  * this list of conditions and the following disclaimer in the documentation 
16  * and/or other materials provided with the distribution, if any, must include 
17  * the following acknowledgment: "This product includes software developed by 
18  * the University Corporation for Advanced Internet Development 
19  * <http://www.ucaid.edu>Internet2 Project. Alternately, this acknowledegement 
20  * may appear in the software itself, if and wherever such third-party 
21  * acknowledgments normally appear.
22  * 
23  * Neither the name of Shibboleth nor the names of its contributors, nor 
24  * Internet2, nor the University Corporation for Advanced Internet Development, 
25  * Inc., nor UCAID may be used to endorse or promote products derived from this 
26  * software without specific prior written permission. For written permission, 
27  * please contact shibboleth@shibboleth.org
28  * 
29  * Products derived from this software may not be called Shibboleth, Internet2, 
30  * UCAID, or the University Corporation for Advanced Internet Development, nor 
31  * may Shibboleth appear in their name, without prior written permission of the 
32  * University Corporation for Advanced Internet Development.
33  * 
34  * 
35  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
36  * AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
37  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
38  * PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK 
39  * OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE. 
40  * IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY 
41  * CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY DIRECT, 
42  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
43  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
44  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
47  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  */
49
50 package edu.internet2.middleware.shibboleth.aa.attrresolv.provider;
51
52 import java.security.Principal;
53 import java.util.Arrays;
54 import java.util.Iterator;
55 import java.util.LinkedHashSet;
56 import java.util.Set;
57
58 import javax.naming.NamingEnumeration;
59 import javax.naming.NamingException;
60 import javax.naming.directory.Attribute;
61 import javax.naming.directory.Attributes;
62
63 import org.apache.log4j.Logger;
64 import org.w3c.dom.Element;
65
66 import edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeDefinitionPlugIn;
67 import edu.internet2.middleware.shibboleth.aa.attrresolv.Dependencies;
68 import edu.internet2.middleware.shibboleth.aa.attrresolv.ResolutionPlugInException;
69 import edu.internet2.middleware.shibboleth.aa.attrresolv.ResolverAttribute;
70
71 /**
72  * Basic <code>AttributeDefinitionPlugIn</code> implementation.  Operates as a proxy for attributes
73  * gathered by Connectors.
74  * 
75  * @author Walter Hoehn (wassa@columbia.edu)
76  */
77 public class SimpleAttributeDefinition extends BaseAttributeDefinition implements AttributeDefinitionPlugIn {
78
79         private static Logger log = Logger.getLogger(SimpleAttributeDefinition.class.getName());
80         private String connectorMapping;
81         private String smartScope;
82         private ValueHandler valueHandler;
83         private boolean allowEmpty = false;
84
85         /**
86          * Constructor for SimpleAttributeDefinition.  Creates a PlugIn based on configuration
87          * information presented in a DOM Element.
88          */
89         public SimpleAttributeDefinition(Element e) throws ResolutionPlugInException {
90
91                 super(e);
92
93                 //Parse source name
94                 String sourceName = e.getAttribute("sourceName");
95                 if (sourceName == null || sourceName.equals("")) {
96                         int index = getId().lastIndexOf("#");
97                         if (index < 0) {
98                                 index = getId().lastIndexOf(":");
99                                 int slashIndex = getId().lastIndexOf("/");
100                                 if (slashIndex > index) {
101                                         index = slashIndex;
102                                 }
103                         }
104                         connectorMapping = getId().substring(index + 1);
105                 } else {
106                         connectorMapping = sourceName;
107                 }
108
109                 log.debug("Mapping attribute to name (" + connectorMapping + ") in connector.");
110
111                 //Configure smart scoping
112                 String smartScopingSpec = e.getAttribute("smartScope");
113                 if (smartScopingSpec != null && !smartScopingSpec.equals("")) {
114                         smartScope = smartScopingSpec;
115                 }
116                 if (smartScope != null) {
117                         log.debug("Smart Scope (" + smartScope + ") enabled for attribute (" + getId() + ").");
118                 } else {
119                         log.debug("Smart Scoping disabled for attribute (" + getId() + ").");
120                 }
121
122                 //Load a value handler
123                 String valueHandlerSpec = e.getAttribute("valueHandler");
124
125                 if (valueHandlerSpec != null && !valueHandlerSpec.equals("")) {
126                         if (smartScope == null) {
127                                 try {
128                                         Class handlerClass = Class.forName(valueHandlerSpec);
129                                         valueHandler = (ValueHandler) handlerClass.newInstance();
130                                 } catch (ClassNotFoundException cnfe) {
131                                         log.error(
132                                                 "Value Handler implementation specified for attribute ("
133                                                         + getId()
134                                                         + ") cannot be found: "
135                                                         + cnfe);
136                                         throw new ResolutionPlugInException(
137                                                 "Value Handler implementation specified for attribute (" + getId() + ") cannot be found.");
138                                 } catch (Exception oe) {
139                                         log.error(
140                                                 "Value Handler implementation specified for attribute ("
141                                                         + getId()
142                                                         + ") coudl not be loaded: "
143                                                         + oe);
144                                         throw new ResolutionPlugInException(
145                                                 "Value Handler implementation specified for attribute (" + getId() + ") could not be loaded.");
146                                 }
147                         } else {
148                                 log.error(
149                                         "Specification of \"valueHandler\' cannot be used in combination with \"smartScope\".  Ignoring Value Handler for attribute ("
150                                                 + getId()
151                                                 + ").");
152                         }
153                 }
154
155                 if (valueHandler != null) {
156                         log.debug("Custom Value Handler enabled for attribute (" + getId() + ").");
157                 }
158                 
159                 //Decide whethere or not to allow empty string values
160                 String rawAllowEmpty = e.getAttribute("allowEmpty");
161                 if (rawAllowEmpty != null) {
162                         if (rawAllowEmpty.equalsIgnoreCase("TRUE")) {
163                                 allowEmpty = true;
164                         }
165                 }
166                 log.debug("Allowal of empty string values is set to (" + allowEmpty + ") for attribute (" + getId() + ").");
167         
168         }
169
170         /**
171          * @see edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeDefinitionPlugIn#resolve(edu.internet2.middleware.shibboleth.aa.attrresolv.ArpAttribute, java.security.Principal, java.lang.String, edu.internet2.middleware.shibboleth.aa.attrresolv.Dependencies)
172          */
173         public void resolve(ResolverAttribute attribute, Principal principal, String requester, Dependencies depends)
174                 throws ResolutionPlugInException {
175                 log.debug("Resolving attribute: (" + getId() + ")");
176                 Set results = new LinkedHashSet();
177                 if (!connectorDependencyIds.isEmpty()) {
178                         results.addAll(Arrays.asList(getValuesFromConnectors(depends)));
179                 }
180
181                 if (!attributeDependencyIds.isEmpty()) {
182                         results.addAll(Arrays.asList(getValuesFromAttributes(depends)));
183                 }
184
185                 if (lifeTime != -1) {
186                         attribute.setLifetime(lifeTime);
187                 }
188
189                 if (smartScope != null) {
190                         attribute.registerValueHandler(new ScopedStringValueHandler(smartScope));
191                 }
192                 if (smartScope == null && valueHandler != null) {
193                         attribute.registerValueHandler(valueHandler);
194                 }
195                 
196                 Iterator resultsIt = results.iterator();
197                 while (resultsIt.hasNext()) {
198                         Object value = resultsIt.next();
199                         if (!allowEmpty && value.equals("")) {
200                                 log.debug("Skipping empty string value.");
201                                 continue;
202                         }
203                         attribute.addValue(value);
204                 }
205                 attribute.setResolved();
206         }
207
208         protected Object[] getValuesFromAttributes(Dependencies depends) {
209
210                 Set results = new LinkedHashSet();
211
212                 Iterator attrDependIt = attributeDependencyIds.iterator();
213                 while (attrDependIt.hasNext()) {
214                         ResolverAttribute attribute = depends.getAttributeResolution((String) attrDependIt.next());
215                         if (attribute != null) {
216                                 log.debug("Found value(s) for attribute (" + getId() + ").");
217                                 for (Iterator iterator = attribute.getValues(); iterator.hasNext();) {
218                                         results.add(iterator.next());
219                                 }
220                         } else {
221                                 log.error(
222                                         "An attribute dependency of attribute (" + getId() + ") was not included in the dependency chain.");
223                         }
224                 }
225
226                 if (results.isEmpty()) {
227                         log.debug("An attribute dependency of attribute (" + getId() + ") supplied no values.");
228                 }
229                 return results.toArray();
230         }
231
232         protected Object[] getValuesFromConnectors(Dependencies depends) {
233
234                 Set results = new LinkedHashSet();
235
236                 Iterator connectorDependIt = connectorDependencyIds.iterator();
237                 while (connectorDependIt.hasNext()) {
238                         Attributes attrs = depends.getConnectorResolution((String) connectorDependIt.next());
239                         if (attrs != null) {
240                                 Attribute attr = attrs.get(connectorMapping);
241                                 if (attr != null) {
242                                         log.debug("Found value(s) for attribute (" + getId() + ").");
243                                         try {
244                                                 NamingEnumeration valuesEnum = attr.getAll();
245                                                 while (valuesEnum.hasMore()) {
246                                                         results.add(valuesEnum.next());
247                                                 }
248                                         } catch (NamingException e) {
249                                                 log.error(
250                                                         "An problem was encountered resolving the dependencies of attribute ("
251                                                                 + getId()
252                                                                 + "): "
253                                                                 + e);
254                                         }
255                                 }
256                         }
257                 }
258
259                 if (results.isEmpty()) {
260                         log.debug("A connector dependency of attribute (" + getId() + ") supplied no values.");
261                 }
262                 return results.toArray();
263         }
264 }