1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.cms.core;
35
36 import info.magnolia.cms.util.ClasspathResourcesUtil;
37 import info.magnolia.init.MagnoliaConfigurationProperties;
38
39 import java.io.File;
40 import java.io.FileInputStream;
41 import java.io.FileNotFoundException;
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.net.URL;
45 import java.nio.file.Paths;
46
47 import javax.inject.Inject;
48 import javax.inject.Singleton;
49
50 import org.apache.commons.io.IOUtils;
51 import org.apache.commons.lang3.StringUtils;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55
56
57
58 @Singleton
59 public class FileSystemHelper {
60
61 private static final Logger log = LoggerFactory.getLogger(FileSystemHelper.class);
62
63 private final MagnoliaConfigurationProperties magnoliaConfigurationProperties;
64
65 @Inject
66 public FileSystemHelper(MagnoliaConfigurationProperties magnoliaConfigurationProperties) {
67 this.magnoliaConfigurationProperties = magnoliaConfigurationProperties;
68 }
69
70
71
72
73
74 public File getTempDirectory() {
75 String path = magnoliaConfigurationProperties.getProperty(MagnoliaConfigurationProperties.MAGNOLIA_UPLOAD_TMPDIR);
76 if (path == null) {
77 throw new IllegalArgumentException(MagnoliaConfigurationProperties.MAGNOLIA_UPLOAD_TMPDIR + " is not set in magnolia properties. Please revise your configuration.");
78 }
79 File tempDir;
80 if (Paths.get(path).isAbsolute()) {
81 tempDir = new File(path);
82 } else {
83 tempDir = new File(getAppRootDir(), path);
84 }
85
86 if (!tempDir.exists() && !tempDir.mkdir()) {
87 throw new IllegalStateException("Can't create temporary directory under path [" + tempDir + "]. Magnolia configuration property '" + MagnoliaConfigurationProperties.MAGNOLIA_UPLOAD_TMPDIR + "' is set to [" + path +"]. Please revise your environment.");
88 }
89 if (!tempDir.canWrite()) {
90 throw new IllegalStateException("Temp directory [" + tempDir + "] isn't writable. Magnolia configuration property '" + MagnoliaConfigurationProperties.MAGNOLIA_UPLOAD_TMPDIR + "' is set to [" + path +"]. Please revise your environment.");
91 }
92 return tempDir;
93 }
94
95
96
97
98 public File getAppRootDir() {
99 return new File(magnoliaConfigurationProperties.getProperty(MagnoliaConfigurationProperties.MAGNOLIA_APP_ROOTDIR));
100 }
101
102
103
104
105 public String getAbsoluteFileSystemPath(String path) {
106 if (path == null) {
107 throw new IllegalArgumentException("Can't convert null path to an absolute file system path.");
108 }
109 if (Paths.get(path).isAbsolute()) {
110 return path;
111 }
112
113 File file = new File(getAppRootDir(), path);
114 try {
115 return file.getCanonicalPath();
116 } catch (IOException e) {
117 log.debug("Can't resolve canonical path for {}. Returning absolute path.", path, e);
118 return file.getAbsolutePath();
119 }
120 }
121
122
123
124
125
126 public String getTokenizedConfigFile(String path) throws IOException {
127 final InputStream stream = getConfigFile(path);
128 if (stream == null) {
129 throw new IOException("Can't load configuration file: " + path);
130 }
131 return replaceTokens(stream);
132 }
133
134 private InputStream getConfigFile(String fileName) {
135 File file = new File(getAppRootDir(), fileName);
136
137 if (file.exists()) {
138 try {
139 URL url = new URL("file:" + file.getAbsolutePath());
140 return url.openStream();
141 } catch (IOException e) {
142 log.error("Unable to read config file from [{}]: ", fileName, e);
143 return null;
144 }
145 }
146
147
148 file = new File(fileName);
149 if (file.exists()) {
150 try {
151 return new FileInputStream(file);
152 } catch (FileNotFoundException e) {
153 log.error("Unable to read config file from [{}]:", fileName, e);
154 return null;
155 }
156 }
157
158
159 try {
160 return ClasspathResourcesUtil.getStream(fileName);
161 } catch (IOException e) {
162 log.error("Unable to read config file from the resources [{}]", fileName, e);
163 return null;
164 }
165 }
166
167 private String replaceTokens(InputStream stream) throws IOException {
168 String config = IOUtils.toString(stream);
169 IOUtils.closeQuietly(stream);
170 for (String key: magnoliaConfigurationProperties.getKeys()) {
171 config = StringUtils.replace(config, "${" + key + "}", magnoliaConfigurationProperties.getProperty(key));
172 }
173 return config;
174 }
175
176 }