View Javadoc

1   /*
2    * Copyright 2008 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 org.opensaml.util.resource;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.File;
21  import java.io.FileInputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.InputStreamReader;
25  import java.util.Iterator;
26  import java.util.Properties;
27  
28  import org.opensaml.xml.util.DatatypeHelper;
29  
30  /**
31   * A resource filter that buffers a resource into a string and replaces instance of macros with properties read from a
32   * file. Macros are of the syntax '${MACRO_NAME}', the same syntax used within the Java Expression Language.
33   * 
34   * The property file is read at invocation of this filter.
35   * 
36   * The {@link InputStream} should be a character stream as {@link InputStreamReader} will be used to convert the stream
37   * into a string.
38   */
39  public class PropertyReplacementResourceFilter implements ResourceFilter {
40  
41      /** Location of the property file. */
42      private File propertyFilePath;
43  
44      /**
45       * Constructor.
46       * 
47       * @param propertyFile property file whose properties will be expanded within the resource
48       */
49      public PropertyReplacementResourceFilter(File propertyFile) {
50          if (propertyFile == null) {
51              throw new IllegalArgumentException("Property file may not be null");
52          }
53          propertyFilePath = propertyFile;
54      }
55  
56      /** {@inheritDoc} */
57      @SuppressWarnings("unchecked")
58      public InputStream applyFilter(InputStream resource) throws ResourceException {
59          Properties props = new Properties();
60          try {
61              props.load(new FileInputStream(propertyFilePath));
62          } catch (IOException e) {
63              throw new ResourceException("Unable to read property file", e);
64          }
65  
66          try {
67              String resourceString = DatatypeHelper.inputstreamToString(resource, null);
68  
69              Iterator<String> keyItr = (Iterator<String>) props.propertyNames();
70              String key;
71              while (keyItr.hasNext()) {
72                  key = keyItr.next();
73                  resourceString = resourceString.replace("${" + key + "}", props.getProperty(key));
74              }
75  
76              resourceString.trim();
77              return new ByteArrayInputStream(resourceString.getBytes());
78          } catch (IOException e) {
79              throw new ResourceException("Unable to read contents of resource", e);
80          }
81      }
82  }