1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
32
33
34
35
36
37
38
39 public class PropertyReplacementResourceFilter implements ResourceFilter {
40
41
42 private File propertyFilePath;
43
44
45
46
47
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
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 }