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.resource;
18  
19  import java.net.URI;
20  import java.net.URISyntaxException;
21  
22  import javax.xml.namespace.QName;
23  
24  import org.opensaml.util.resource.FileBackedHttpResource;
25  import org.opensaml.xml.util.DatatypeHelper;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  import org.springframework.beans.factory.BeanCreationException;
29  import org.springframework.beans.factory.support.AbstractBeanDefinition;
30  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
31  import org.springframework.beans.factory.xml.ParserContext;
32  import org.w3c.dom.Element;
33  
34  /** Bean definition parser for {@link FileBackedHttpResource}s. */
35  public class FileBackedHttpResourceBeanDefinitionParser extends AbstractResourceBeanDefinitionParser {
36  
37      /** Schema type. */
38      public static final QName SCHEMA_TYPE = new QName(ResourceNamespaceHandler.NAMESPACE, "FileBackedHttpResource");
39  
40      /** Class logger. */
41      private final Logger log = LoggerFactory.getLogger(FileBackedHttpResourceBeanDefinitionParser.class);
42  
43      /** {@inheritDoc} */
44      protected Class getBeanClass(Element arg0) {
45          return FileBackedHttpResource.class;
46      }
47  
48      /** {@inheritDoc} */
49      protected String resolveId(Element configElement, AbstractBeanDefinition beanDefinition, ParserContext parserContext) {
50          return FileBackedHttpResource.class.getName() + ":("
51                  + DatatypeHelper.safeTrimOrNullString(configElement.getAttributeNS(null, "url")) + ","
52                  + DatatypeHelper.safeTrimOrNullString(configElement.getAttributeNS(null, "file")) + ")";
53      }
54  
55      /** {@inheritDoc} */
56      protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
57          super.doParse(element, parserContext, builder);
58          builder.addConstructorArgValue(DatatypeHelper.safeTrimOrNullString(element.getAttributeNS(null, "url")));
59  
60          String file = DatatypeHelper.safeTrimOrNullString(element.getAttributeNS(null, "file"));
61          if (file.startsWith("file:")) {
62              try {
63                  builder.addConstructorArgValue(new URI(file));
64              } catch (URISyntaxException e) {
65                  log.error("Illegal file: URI syntax", e);
66                  throw new BeanCreationException("Illegal file: URI syntax");
67              }
68          } else {
69              builder.addConstructorArgValue(file);
70          }
71  
72          addResourceFilter(element, parserContext, builder);
73      }
74  }