Implement SP authn by credential names via metadata, dump 1.1 legacy mode.
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / idp / provider / BaseHandler.java
1 /*
2  * Copyright [2005] [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.idp.provider;
18
19 import java.security.cert.X509Certificate;
20 import java.util.HashSet;
21
22 import javax.security.auth.x500.X500Principal;
23
24 import org.apache.log4j.Logger;
25 import org.w3c.dom.Element;
26 import org.w3c.dom.Node;
27 import org.w3c.dom.NodeList;
28
29 import edu.internet2.middleware.shibboleth.common.ShibbolethConfigurationException;
30 import edu.internet2.middleware.shibboleth.common.provider.ShibbolethTrust;
31 import edu.internet2.middleware.shibboleth.idp.IdPConfig;
32 import edu.internet2.middleware.shibboleth.idp.IdPProtocolHandler;
33
34 /**
35  * Functionality common to all <code>IdPProtocolHandler</code> implementation.
36  * 
37  * @author Walter Hoehn
38  */
39 public abstract class BaseHandler implements IdPProtocolHandler {
40
41         private static Logger log = Logger.getLogger(BaseHandler.class.getName());
42         private HashSet locations = new HashSet();
43
44         /**
45          * Required DOM-based constructor.
46          */
47         public BaseHandler(Element config) throws ShibbolethConfigurationException {
48
49                 // Make sure we have at least one location
50                 NodeList locations = config.getElementsByTagNameNS(IdPConfig.configNameSpace, "Location");
51                 if (locations.getLength() < 1) {
52                         log.error("The <ProtocolHandler/> element must contain at least one <Location/> element.");
53                         throw new ShibbolethConfigurationException("Unable to load ProtocolHandler.");
54                 }
55
56                 // Parse the locations
57                 for (int i = 0; i < locations.getLength(); i++) {
58                         Node tnode = ((Element) locations.item(i)).getFirstChild();
59                         if (tnode != null && tnode.getNodeType() == Node.TEXT_NODE) {
60                                 String rawURI = tnode.getNodeValue();
61
62                                 if (rawURI == null || rawURI.equals("")) {
63                                         log.error("The <Location/> element inside the <ProtocolHandler/> element must "
64                                                         + "contain a URI or regular expressions.");
65                                         throw new ShibbolethConfigurationException("Unable to load ProtocolHandler.");
66                                 }
67                                 this.locations.add(rawURI);
68
69                         } else {
70                                 log.error("The <Location/> element inside the <ProtocolHandler/> element must contain a "
71                                                 + "URI or regular expression.");
72                                 throw new ShibbolethConfigurationException("Unable to load ProtocolHandler.");
73                         }
74                 }
75         }
76
77         /*
78          * @see edu.internet2.middleware.shibboleth.idp.IdPProtocolHandler#getLocations()
79          */
80         public String[] getLocations() {
81
82                 return (String[]) locations.toArray(new String[0]);
83         }
84
85         protected static String getHostNameFromDN(X500Principal dn) {
86
87                 return ShibbolethTrust.getHostNameFromDN(dn);
88         }
89
90         protected static String[] getCredentialNames(X509Certificate cert) {
91                 return ShibbolethTrust.getCredentialNames(cert);
92         }
93 }