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.module.resources.loaders; |
35 |
|
|
36 |
|
import info.magnolia.init.MagnoliaConfigurationProperties; |
37 |
|
|
38 |
|
import java.io.BufferedInputStream; |
39 |
|
import java.io.IOException; |
40 |
|
import java.io.InputStream; |
41 |
|
import java.nio.file.Files; |
42 |
|
import java.nio.file.Path; |
43 |
|
import java.nio.file.Paths; |
44 |
|
|
45 |
|
import org.apache.commons.lang3.StringUtils; |
46 |
|
import org.slf4j.Logger; |
47 |
|
import org.slf4j.LoggerFactory; |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
@deprecated@link |
57 |
|
|
58 |
|
@Deprecated |
|
|
| 70.3% |
Uncovered Elements: 11 (37) |
Complexity: 9 |
Complexity Density: 0.41 |
|
59 |
|
public class FileSystemResourceLoader implements ResourceLoader { |
60 |
|
|
61 |
|
private static final Logger log = LoggerFactory.getLogger(FileSystemResourceLoader.class); |
62 |
|
|
63 |
|
private boolean enabled = true; |
64 |
|
|
65 |
|
private String path; |
66 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
67 |
4 |
public FileSystemResourceLoader(MagnoliaConfigurationProperties mcp) {... |
68 |
4 |
path = mcp.getProperty("magnolia.home"); |
69 |
|
} |
70 |
|
|
|
|
| 88.2% |
Uncovered Elements: 2 (17) |
Complexity: 4 |
Complexity Density: 0.36 |
|
71 |
4 |
@Override... |
72 |
|
public InputStream getStream(String template) throws IOException { |
73 |
4 |
final Path rootPath = Paths.get(path); |
74 |
4 |
boolean isRootPathValid = validateBasePath(rootPath); |
75 |
4 |
if (!isRootPathValid) { |
76 |
0 |
return null; |
77 |
|
} |
78 |
|
|
79 |
4 |
final Path templatePath = rootPath.resolve(StringUtils.startsWith(template, "/") ? template.substring(1) : template); |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
4 |
final String absoluteRootPath = rootPath.toRealPath().toString(); |
84 |
4 |
final String absoluteTemplatePath = templatePath.toRealPath().toString(); |
85 |
4 |
if (!StringUtils.startsWith(absoluteTemplatePath, absoluteRootPath)) { |
86 |
1 |
log.warn("Cannot access '{}'. Only access to directory '{}' is provided.", absoluteTemplatePath, absoluteRootPath); |
87 |
1 |
return null; |
88 |
|
} |
89 |
|
|
90 |
3 |
return new BufferedInputStream(Files.newInputStream(templatePath)); |
91 |
|
} |
92 |
|
|
|
|
| 43.8% |
Uncovered Elements: 9 (16) |
Complexity: 4 |
Complexity Density: 0.4 |
|
93 |
4 |
private boolean validateBasePath(Path path) {... |
94 |
4 |
if (!Files.exists(path)) { |
95 |
0 |
log.warn("init param 'basePath' value {} does not exist in the file system.", path.toString()); |
96 |
0 |
return false; |
97 |
4 |
} else if (!Files.isDirectory(path)) { |
98 |
0 |
log.warn("init param 'basePath' value {} is not a directory in the file system.", path.toString()); |
99 |
0 |
return false; |
100 |
4 |
} else if (!Files.isReadable(path)) { |
101 |
0 |
log.warn("init param 'basePath' value {} is not readable in the file system.", path.toString()); |
102 |
0 |
return false; |
103 |
|
} |
104 |
4 |
return true; |
105 |
|
} |
106 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
107 |
|
public void setEnabled(boolean enabled) {... |
108 |
|
this.enabled = enabled; |
109 |
|
} |
110 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
111 |
|
@Override... |
112 |
|
public boolean isEnabled() { |
113 |
|
return this.enabled; |
114 |
|
} |
115 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
116 |
|
public void setPath(String path) {... |
117 |
|
this.path = path; |
118 |
|
} |
119 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
120 |
|
public String getPath() {... |
121 |
|
return this.path; |
122 |
|
} |
123 |
|
|
124 |
|
} |