View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.wss4j.common.saml;
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  
25  import org.opensaml.core.config.Configuration;
26  import org.opensaml.core.xml.config.XMLConfigurationException;
27  import org.opensaml.core.xml.config.XMLConfigurator;
28  import org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport;
29  
30  /**
31   * This class intializes the Opensaml library.
32   */
33  public final class OpenSAMLBootstrap {
34  
35      /** List of default configuration files. */
36      private static final String[] XML_CONFIGS = {
37          "/default-config.xml",
38          "/schema-config.xml",
39          "/saml1-assertion-config.xml",
40          "/saml1-metadata-config.xml",
41          "/saml1-protocol-config.xml",
42          "/saml2-assertion-config.xml",
43          "/saml2-assertion-delegation-restriction-config.xml",
44          "/saml2-ecp-config.xml",
45          "/saml2-metadata-algorithm-config.xml",
46          "/saml2-metadata-attr-config.xml",
47          "/saml2-metadata-config.xml",
48          "/saml2-metadata-idp-discovery-config.xml",
49          "/saml2-metadata-query-config.xml",
50          "/saml2-metadata-reqinit-config.xml",
51          "/saml2-metadata-ui-config.xml",
52          "/saml2-metadata-rpi-config.xml",
53          "/saml2-protocol-config.xml",
54          "/saml2-protocol-thirdparty-config.xml",
55          "/saml2-protocol-aslo-config.xml",
56          "/saml2-channel-binding-config.xml",
57          "/saml-ec-gss-config.xml",
58          "/signature-config.xml",
59          "/wss4j-signature-config.xml",  // Override the default Base64 Binary Unmarshaller for X.509 Certificates
60          "/encryption-config.xml",
61          "/xacml20-context-config.xml",
62          "/xacml20-policy-config.xml",
63          "/xacml10-saml2-profile-config.xml",
64          "/xacml11-saml2-profile-config.xml",
65          "/xacml2-saml2-profile-config.xml",
66          "/xacml3-saml2-profile-config.xml",
67          "/saml2-xacml2-profile.xml",
68      };
69  
70      private OpenSAMLBootstrap() {
71          // complete
72      }
73  
74      /**
75       * Initializes the OpenSAML library, loading default configurations.
76       *
77       * @throws XMLConfigurationException thrown if there is a problem initializing the OpenSAML library
78       */
79      public static synchronized void bootstrap() throws XMLConfigurationException {
80          bootstrap(true);
81      }
82  
83      public static synchronized void bootstrap(boolean includeXacml) throws XMLConfigurationException {
84          ClassLoader loader = Thread.currentThread().getContextClassLoader();
85          try {
86              XMLConfigurator configurator = new XMLConfigurator();
87  
88              Thread.currentThread().setContextClassLoader(XMLObjectProviderRegistrySupport.class.getClassLoader());
89  
90              for (String config : XML_CONFIGS) {
91                  if (includeXacml || !config.contains("xacml")) {
92                      //most are found in the Configuration.class classloader
93                      InputStream ins = Configuration.class.getResourceAsStream(config);  //NOPMD
94                      if (ins == null) {
95                          //some are from us
96                          ins = OpenSAMLBootstrap.class.getResourceAsStream(config);
97                      }
98                      if (ins != null) {
99                          configurator.load(ins);
100                         try {
101                             ins.close();
102                         } catch (IOException ex) { //NOPMD
103                             // Do nothing
104                         }
105                     }
106                 }
107             }
108         } finally {
109             Thread.currentThread().setContextClassLoader(loader);
110         }
111     }
112 
113 }