View Javadoc

1   /*
2    * Licensed to the University Corporation for Advanced Internet Development, 
3    * Inc. (UCAID) under one or more contributor license agreements.  See the 
4    * NOTICE file distributed with this work for additional information regarding
5    * copyright ownership. The UCAID licenses this file to You under the Apache 
6    * License, Version 2.0 (the "License"); you may not use this file except in 
7    * compliance with the License.  You may obtain a copy of the License at
8    *
9    *    http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package edu.internet2.middleware.shibboleth.common.config.security;
19  
20  import javax.xml.namespace.QName;
21  
22  import org.opensaml.xml.util.DatatypeHelper;
23  import org.opensaml.xml.util.XMLHelper;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  import org.springframework.beans.FatalBeanException;
27  import org.springframework.beans.factory.support.AbstractBeanDefinition;
28  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
29  import org.springframework.beans.factory.support.ManagedList;
30  import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
31  import org.springframework.beans.factory.xml.ParserContext;
32  import org.w3c.dom.Element;
33  
34  import edu.internet2.middleware.shibboleth.common.config.SpringConfigurationUtils;
35  
36  /** Spring bean definition parser for {urn:mace:shibboleth:2.0:security}Chaining elements. */
37  public class ChainingSignatureTrustEngineBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
38  
39      /** Schema type. */
40      public static final QName SCHEMA_TYPE = new QName(SecurityNamespaceHandler.NAMESPACE, "SignatureChaining");
41  
42      /** TrustEngine element name. */
43      private static final QName TRUST_ENGINE_NAME = new QName(SecurityNamespaceHandler.NAMESPACE, "TrustEngine");
44  
45      /** TrustEngineRef element name. */
46      private static final QName TRUST_ENGINE_REF_NAME = new QName(SecurityNamespaceHandler.NAMESPACE, "TrustEngineRef");
47  
48      /** Class logger. */
49      private final Logger log = LoggerFactory.getLogger(ChainingSignatureTrustEngineBeanDefinitionParser.class);
50  
51      /** {@inheritDoc} */
52      protected Class getBeanClass(Element element) {
53          return ChainingSignatureTrustEngineFactoryBean.class;
54      }
55  
56      /** {@inheritDoc} */
57      @SuppressWarnings("unchecked")
58      protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
59          log.info("Parsing configuration for {} trust engine with id: {}", XMLHelper.getXSIType(element).getLocalPart(),
60                  element.getAttributeNS(null, "id"));
61  
62          ManagedList managedChain = new ManagedList();
63  
64          Element child = XMLHelper.getFirstChildElement(element);
65          while (child != null) {
66              QName childName = XMLHelper.getNodeQName(child);
67              if (TRUST_ENGINE_NAME.equals(childName)) {
68                  log.debug("Parsing chain trust engine member {}", element.getAttributeNS(null, "id"));
69                  managedChain.add(SpringConfigurationUtils.parseCustomElement(child, parserContext));
70              } else if (TRUST_ENGINE_REF_NAME.equals(childName)) {
71                  log.debug("Parsing chain trust engine member reference {}", element.getAttributeNS(null, "ref"));
72                  managedChain.add(SpringConfigurationUtils.parseCustomElementReference(child, "ref", parserContext));
73              } else {
74                  log.error("Unsupported child element of chaining trust engine '{}' encountered with name: {}", element
75                          .getAttributeNS(null, "id"), childName);
76                  throw new FatalBeanException("Unsupported child element of chaining trust engine encountered");
77              }
78  
79              child = XMLHelper.getNextSiblingElement(child);
80          }
81  
82          builder.addPropertyValue("chain", managedChain);
83      }
84  
85      /** {@inheritDoc} */
86      protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) {
87          return DatatypeHelper.safeTrim(element.getAttributeNS(null, "id"));
88      }
89  }