View Javadoc

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