Push SAML Attribute namespace configuration into the resolver. (Needed for proper...
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / aa / attrresolv / provider / CustomAttributeDefinition.java
1 /*
2  * The Shibboleth License, Version 1. Copyright (c) 2002 University Corporation for Advanced Internet Development, Inc.
3  * All rights reserved Redistribution and use in source and binary forms, with or without modification, are permitted
4  * provided that the following conditions are met: Redistributions of source code must retain the above copyright
5  * notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above
6  * copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials
7  * provided with the distribution, if any, must include the following acknowledgment: "This product includes software
8  * developed by the University Corporation for Advanced Internet Development <http://www.ucaid.edu>Internet2 Project.
9  * Alternately, this acknowledegement may appear in the software itself, if and wherever such third-party
10  * acknowledgments normally appear. Neither the name of Shibboleth nor the names of its contributors, nor Internet2, nor
11  * the University Corporation for Advanced Internet Development, Inc., nor UCAID may be used to endorse or promote
12  * products derived from this software without specific prior written permission. For written permission, please contact
13  * shibboleth@shibboleth.org Products derived from this software may not be called Shibboleth, Internet2, UCAID, or the
14  * University Corporation for Advanced Internet Development, nor may Shibboleth appear in their name, without prior
15  * written permission of the University Corporation for Advanced Internet Development. THIS SOFTWARE IS PROVIDED BY THE
16  * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE
18  * DISCLAIMED AND THE ENTIRE RISK OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE. IN NO
19  * EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC.
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
23  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 package edu.internet2.middleware.shibboleth.aa.attrresolv.provider;
27
28 import java.security.Principal;
29
30 import org.apache.log4j.Logger;
31 import org.w3c.dom.Element;
32
33 import edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeDefinitionPlugIn;
34 import edu.internet2.middleware.shibboleth.aa.attrresolv.Dependencies;
35 import edu.internet2.middleware.shibboleth.aa.attrresolv.ResolutionPlugIn;
36 import edu.internet2.middleware.shibboleth.aa.attrresolv.ResolutionPlugInException;
37 import edu.internet2.middleware.shibboleth.aa.attrresolv.ResolverAttribute;
38
39 /**
40  * Wrapper class for custom <code>AttributeDefinitionPlugIn</code> implementations.
41  * 
42  * @author Walter Hoehn (wassa@columbia.edu)
43  */
44 public class CustomAttributeDefinition implements ResolutionPlugIn, AttributeDefinitionPlugIn {
45
46         private static Logger log = Logger.getLogger(CustomAttributeDefinition.class.getName());
47         private AttributeDefinitionPlugIn custom;
48         private String namespace = BaseAttributeDefinition.SHIB_ATTRIBUTE_NAMESPACE_URI;
49
50         /** The time, in seconds, for which attribute created from this definition should be valid. */
51         protected long lifeTime = -1;
52
53         public CustomAttributeDefinition(Element e) throws ResolutionPlugInException {
54
55                 if (!e.getTagName().equals("CustomAttributeDefinition")) {
56                         log.error("Incorrect attribute definition configuration: expected <CustomAttributeDefinition> .");
57                         throw new ResolutionPlugInException("Failed to initialize Attribute Definition PlugIn.");
58                 }
59
60                 String className = e.getAttribute("class");
61                 if (className == null || className.equals("")) {
62                         log.error("Custom Attribute Definition requires specification of the attribute \"class\".");
63                         throw new ResolutionPlugInException("Failed to initialize Attribute Definition PlugIn.");
64                 } else {
65                         try {
66                                 Class[] params = {Class.forName("org.w3c.dom.Element"),};
67                                 Object[] passElement = {e};
68                                 custom = (AttributeDefinitionPlugIn) Class.forName(className).getConstructor(params).newInstance(
69                                                 passElement);
70                         } catch (Exception loaderException) {
71                                 log.error("Failed to load Custom Attribute Definition PlugIn implementation class: "
72                                                 + loaderException.getMessage());
73                                 throw new ResolutionPlugInException("Failed to initialize Attribute Definition PlugIn.");
74                         }
75                 }
76
77                 String lifeTimeSpec = e.getAttribute("lifeTime");
78                 if (lifeTimeSpec != null && !lifeTimeSpec.equals("")) {
79                         try {
80                                 lifeTime = Long.valueOf(lifeTimeSpec).longValue();
81                                 log.debug("Explicit lifetime set for attribute (" + getId() + ").  Lifetime: (" + lifeTime + ").");
82                         } catch (NumberFormatException nfe) {
83                                 log.error("Bad value for attribute (lifeTime) for Attribute Definition (" + getId() + ").");
84                         }
85                 }
86
87                 String namespaceSpec = e.getAttribute("namespace");
88                 if (namespaceSpec != null && !namespaceSpec.equals("")) {
89                         namespace = namespaceSpec;
90                 }
91         }
92
93         /**
94          * @see edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeDefinitionPlugIn#resolve(edu.internet2.middleware.shibboleth.aa.attrresolv.ResolverAttribute,
95          *      java.security.Principal, java.lang.String, java.lang.String,
96          *      edu.internet2.middleware.shibboleth.aa.attrresolv.Dependencies)
97          */
98         public void resolve(ResolverAttribute attribute, Principal principal, String requester, String responder,
99                         Dependencies depends) throws ResolutionPlugInException {
100
101                 custom.resolve(attribute, principal, requester, responder, depends);
102                 if (lifeTime != -1) {
103                         attribute.setLifetime(lifeTime);
104                 }
105                 attribute.setNamespace(namespace);
106         }
107
108         /**
109          * @see edu.internet2.middleware.shibboleth.aa.attrresolv.BaseResolutionPlugIn#getId()
110          */
111         public String getId() {
112
113                 return custom.getId();
114         }
115
116         /**
117          * @see edu.internet2.middleware.shibboleth.aa.attrresolv.provider.BaseResolutionPlugIn#getTTL()
118          */
119         public long getTTL() {
120
121                 return custom.getTTL();
122         }
123
124         /**
125          * @see edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeDefinitionPlugIn#getAttributeDefinitionDependencyIds()
126          */
127         public String[] getAttributeDefinitionDependencyIds() {
128
129                 return custom.getAttributeDefinitionDependencyIds();
130         }
131
132         /**
133          * @see edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeDefinitionPlugIn#getDataConnectorDependencyIds()
134          */
135         public String[] getDataConnectorDependencyIds() {
136
137                 return custom.getDataConnectorDependencyIds();
138         }
139
140         /**
141          * @see edu.internet2.middleware.shibboleth.aa.attrresolv.ResolutionPlugIn#getPropagateErrors()
142          */
143         public boolean getPropagateErrors() {
144
145                 return custom.getPropagateErrors();
146         }
147 }