1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.settings4j.contentresolver;
21
22 import java.io.File;
23 import java.io.IOException;
24
25 import org.apache.commons.io.FileUtils;
26 import org.settings4j.ContentResolver;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 public class FSContentResolver implements ContentResolver {
42
43
44 private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(FSContentResolver.class);
45
46
47 public static final String FILE_URL_PREFIX = "file:";
48
49 private File rootFolder;
50
51 @Override
52 public void addContentResolver(final ContentResolver contentResolver) {
53 throw new UnsupportedOperationException("FSContentResolver cannot add other ContentResolvers");
54 }
55
56 @Override
57
58 @SuppressWarnings("PMD.ReturnEmptyArrayRatherThanNull")
59 public byte[] getContent(final String key) {
60 String normalizedKey = key;
61 if (normalizedKey.startsWith(FILE_URL_PREFIX)) {
62 normalizedKey = normalizedKey.substring(FILE_URL_PREFIX.length());
63 }
64
65 if (isUnixRoot(normalizedKey) || isWindowsRoot(normalizedKey)) {
66
67 final byte[] content = getContent(new File(normalizedKey));
68 if (content != null) {
69 return content;
70 }
71 }
72
73 final byte[] content = getContent(new File(getRootFolder(), normalizedKey));
74 if (content != null) {
75 return content;
76 }
77 return null;
78 }
79
80 private byte[] getContent(final File file) {
81 byte[] content = null;
82 if (file.exists()) {
83 try {
84 content = FileUtils.readFileToByteArray(file);
85 } catch (final IOException e) {
86 LOG.info(e.getMessage(), e);
87 }
88 }
89 return content;
90 }
91
92
93
94
95
96
97
98 public void setContent(final String key, final byte[] value) throws IOException {
99
100 final String normalizedKey;
101 if (key.startsWith(FILE_URL_PREFIX)) {
102 normalizedKey = key.substring(FILE_URL_PREFIX.length());
103 } else {
104 normalizedKey = key;
105 }
106
107 if (isUnixRoot(normalizedKey) || isWindowsRoot(normalizedKey)) {
108
109 throw new IllegalArgumentException(String.format(
110 "The FSContentResolver can only store content relative to '%s' and not an absolute URL like '%s'.",
111 getRootFolder().getCanonicalPath(), key));
112 }
113
114 final File file = new File(getRootFolder(), normalizedKey);
115 LOG.debug("Store content in: {}", file.getAbsolutePath());
116
117 FileUtils.writeByteArrayToFile(file, value);
118 }
119
120 private boolean isWindowsRoot(final String normalizedKey) {
121 return normalizedKey.indexOf(':') >= 0;
122 }
123
124 private boolean isUnixRoot(final String normalizedKey) {
125 return normalizedKey.startsWith("/");
126 }
127
128
129
130
131
132
133
134
135
136 public File getRootFolder() {
137 if (this.rootFolder == null) {
138 this.rootFolder = new File(".");
139 LOG.info("FSContentResolver.rootFolder is null. The RootPath Folder will be used: {}", this.rootFolder.getAbsolutePath());
140 }
141 return this.rootFolder;
142 }
143
144
145
146
147 public void setRootFolderPath(final String rootFolderPath) {
148 final File newRootFolder = new File(rootFolderPath);
149 if (!newRootFolder.exists()) {
150 try {
151 FileUtils.forceMkdir(newRootFolder);
152 this.rootFolder = newRootFolder;
153 LOG.info("Unable to create RootFolder for FSConntentResolver: {}", newRootFolder.getAbsolutePath());
154 } catch (final IOException e) {
155 LOG.warn("cannot create rootFolder: {}!", rootFolderPath, e);
156 throw new IllegalArgumentException(String.format("Unable to create RootFolder: '%s'!", rootFolderPath), e);
157 }
158 } else {
159 this.rootFolder = newRootFolder;
160 }
161 }
162 }