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