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.attribute.resolver.dataConnector;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  import java.util.Map;
23  
24  import javax.xml.namespace.QName;
25  
26  import org.opensaml.xml.util.DatatypeHelper;
27  import org.opensaml.xml.util.XMLHelper;
28  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
29  import org.springframework.beans.factory.xml.ParserContext;
30  import org.w3c.dom.Element;
31  
32  import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
33  import edu.internet2.middleware.shibboleth.common.attribute.provider.BasicAttribute;
34  
35  /**
36   * Spring Bean Definition Parser for static data connector.
37   */
38  public class StaticDataConnectorBeanDefinitionParser extends BaseDataConnectorBeanDefinitionParser {
39  
40      /** Schema type name. */
41      public static final QName TYPE_NAME = new QName(DataConnectorNamespaceHandler.NAMESPACE, "Static");
42  
43      /** Local name of attribute. */
44      public static final QName ATTRIBUTE_ELEMENT_NAME = new QName(DataConnectorNamespaceHandler.NAMESPACE,
45              "Attribute");
46  
47      /** {@inheritDoc} */
48      protected Class getBeanClass(Element element) {
49          return StaticDataConnectorFactoryBean.class;
50      }
51  
52      /** {@inheritDoc} */
53      protected void doParse(String pluginId, Element pluginConfig, Map<QName, List<Element>> pluginConfigChildren,
54              BeanDefinitionBuilder pluginBuilder, ParserContext parserContext) {
55          super.doParse(pluginId, pluginConfig, pluginConfigChildren, pluginBuilder, parserContext);
56  
57          List<BaseAttribute<String>> attributes = processAttributes(pluginConfigChildren.get(ATTRIBUTE_ELEMENT_NAME));
58  
59          pluginBuilder.addPropertyValue("staticAttributes", attributes);
60      }
61  
62      /**
63       * Parses the configuration elements defining the static {@link BaseAttribute}s.
64       * 
65       * @param attributeElems configuration elements defining the static {@link BaseAttribute}s
66       * 
67       * @return the static {@link BaseAttribute}s
68       */
69      protected List<BaseAttribute<String>> processAttributes(List<Element> attributeElems) {
70          if (attributeElems == null || attributeElems.size() == 0) {
71              return null;
72          }
73  
74          List<BaseAttribute<String>> attributes = new ArrayList<BaseAttribute<String>>();
75          BasicAttribute<String> attribute;
76          for (Element attributeElem : attributeElems) {
77              attribute = new BasicAttribute<String>(DatatypeHelper.safeTrimOrNullString(attributeElem.getAttributeNS(
78                      null, "id")));
79              for (Element valueElem : XMLHelper.getChildElementsByTagNameNS(attributeElem,
80                      DataConnectorNamespaceHandler.NAMESPACE, "Value")) {
81                  attribute.getValues().add(valueElem.getTextContent());
82              }
83  
84              attributes.add(attribute);
85          }
86  
87          return attributes;
88      }
89  }