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 if(props.isEmpty()){
67 return resource;
68 }
69
70 try {
71 String resourceString = DatatypeHelper.inputstreamToString(resource, null);
72
73 Iterator<String> keyItr = (Iterator<String>) props.propertyNames();
74 String key;
75 while (keyItr.hasNext()) {
76 key = keyItr.next();
77 resourceString = resourceString.replace("${" + key + "}", props.getProperty(key));
78 }
79
80 resourceString.trim();
81 return new ByteArrayInputStream(resourceString.getBytes());
82 } catch (IOException e) {
83 throw new ResourceException("Unable to read contents of resource", e);
84 }
85 }
86 }