Pass IdP identifier into resolver interfaces.
[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
49         /** The time, in seconds, for which attribute created from this definition should be valid. */
50         protected long lifeTime = -1;
51
52         public CustomAttributeDefinition(Element e) throws ResolutionPlugInException {
53
54                 if (!e.getTagName().equals("CustomAttributeDefinition")) {
55                         log.error("Incorrect attribute definition configuration: expected <CustomAttributeDefinition> .");
56                         throw new ResolutionPlugInException("Failed to initialize Attribute Definition PlugIn.");
57                 }
58
59                 String className = e.getAttribute("class");
60                 if (className == null || className.equals("")) {
61                         log.error("Custom Attribute Definition requires specification of the attribute \"class\".");
62                         throw new ResolutionPlugInException("Failed to initialize Attribute Definition PlugIn.");
63                 } else {
64                         try {
65                                 Class[] params = {Class.forName("org.w3c.dom.Element"),};
66                                 Object[] passElement = {e};
67                                 custom = (AttributeDefinitionPlugIn) Class.forName(className).getConstructor(params).newInstance(
68                                                 passElement);
69                         } catch (Exception loaderException) {
70                                 log.error("Failed to load Custom Attribute Definition PlugIn implementation class: "
71                                                 + loaderException.getMessage());
72                                 throw new ResolutionPlugInException("Failed to initialize Attribute Definition PlugIn.");
73                         }
74                 }
75
76                 String lifeTimeSpec = e.getAttribute("lifeTime");
77                 if (lifeTimeSpec != null && !lifeTimeSpec.equals("")) {
78                         try {
79                                 lifeTime = Long.valueOf(lifeTimeSpec).longValue();
80                                 log.debug("Explicit lifetime set for attribute (" + getId() + ").  Lifetime: (" + lifeTime + ").");
81                         } catch (NumberFormatException nfe) {
82                                 log.error("Bad value for attribute (lifeTime) for Attribute Definition (" + getId() + ").");
83                         }
84                 }
85         }
86
87         /**
88          * @see edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeDefinitionPlugIn#resolve(edu.internet2.middleware.shibboleth.aa.attrresolv.ResolverAttribute,
89          *      java.security.Principal, java.lang.String, java.lang.String,
90          *      edu.internet2.middleware.shibboleth.aa.attrresolv.Dependencies)
91          */
92         public void resolve(ResolverAttribute attribute, Principal principal, String requester, String responder,
93                         Dependencies depends) throws ResolutionPlugInException {
94
95                 custom.resolve(attribute, principal, requester, responder, depends);
96                 if (lifeTime != -1) {
97                         attribute.setLifetime(lifeTime);
98                 }
99         }
100
101         /**
102          * @see edu.internet2.middleware.shibboleth.aa.attrresolv.BaseResolutionPlugIn#getId()
103          */
104         public String getId() {
105
106                 return custom.getId();
107         }
108
109         /**
110          * @see edu.internet2.middleware.shibboleth.aa.attrresolv.provider.BaseResolutionPlugIn#getTTL()
111          */
112         public long getTTL() {
113
114                 return custom.getTTL();
115         }
116
117         /**
118          * @see edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeDefinitionPlugIn#getAttributeDefinitionDependencyIds()
119          */
120         public String[] getAttributeDefinitionDependencyIds() {
121
122                 return custom.getAttributeDefinitionDependencyIds();
123         }
124
125         /**
126          * @see edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeDefinitionPlugIn#getDataConnectorDependencyIds()
127          */
128         public String[] getDataConnectorDependencyIds() {
129
130                 return custom.getDataConnectorDependencyIds();
131         }
132
133         /**
134          * @see edu.internet2.middleware.shibboleth.aa.attrresolv.ResolutionPlugIn#getPropagateErrors()
135          */
136         public boolean getPropagateErrors() {
137
138                 return custom.getPropagateErrors();
139         }
140 }