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.attributeDefinition;
19  
20  import java.io.FileInputStream;
21  import java.io.IOException;
22  import java.util.List;
23  import java.util.Map;
24  
25  import javax.xml.namespace.QName;
26  
27  import org.opensaml.xml.util.DatatypeHelper;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  import org.springframework.beans.factory.BeanCreationException;
31  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
32  import org.springframework.beans.factory.xml.ParserContext;
33  import org.w3c.dom.Element;
34  
35  /**
36   * Spring bean definition parser for scripted attribute configuration elements.
37   */
38  public class ScriptedAttributeDefinitionBeanDefinitionParser extends BaseAttributeDefinitionBeanDefinitionParser {
39  
40      /** Schema type name. */
41      public static final QName TYPE_NAME = new QName(AttributeDefinitionNamespaceHandler.NAMESPACE, "Script");
42  
43      /** Class logger. */
44      private final Logger log = LoggerFactory.getLogger(ScriptedAttributeDefinitionBeanDefinitionParser.class);
45  
46      /** {@inheritDoc} */
47      protected Class getBeanClass(Element arg0) {
48          return ScriptedAttributeDefinitionFactoryBean.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          String scriptLanguage = "javascript";
57          if (pluginConfig.hasAttributeNS(null, "language")) {
58              scriptLanguage = pluginConfig.getAttributeNS(null, "language");
59          }
60          log.debug("Attribute definition {} scripting language: {}", pluginId, scriptLanguage);
61          pluginBuilder.addPropertyValue("language", scriptLanguage);
62  
63          String script = null;
64          List<Element> scriptElem = pluginConfigChildren.get(new QName(AttributeDefinitionNamespaceHandler.NAMESPACE,
65                  "Script"));
66          if (scriptElem != null && scriptElem.size() > 0) {
67              script = scriptElem.get(0).getTextContent();
68          } else {
69              List<Element> scriptFileElem = pluginConfigChildren.get(new QName(
70                      AttributeDefinitionNamespaceHandler.NAMESPACE, "ScriptFile"));
71              if (scriptFileElem != null && scriptFileElem.size() > 0) {
72                  String scriptFile = scriptFileElem.get(0).getTextContent();
73                  try {
74                      script = DatatypeHelper.inputstreamToString(new FileInputStream(scriptFile), null);
75                  } catch (IOException e) {
76                      throw new BeanCreationException("Unable to read script file " + scriptFile, e);
77                  }
78              }
79          }
80  
81          if (script == null) {
82              throw new BeanCreationException("No script specified for this attribute definition");
83          }
84          log.debug("Attribute definition {} script: {}", pluginId, script);
85          pluginBuilder.addPropertyValue("script", script);
86      }
87  }