Clover icon

Magnolia Resources Module 2.4.2

  1. Project Clover database Fri Nov 6 2015 16:15:26 CET
  2. Package info.magnolia.module.resources.loaders

File FileSystemResourceLoader.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart7.png
52% of files have more coverage

Code metrics

12
22
3
1
124
62
9
0.41
7.33
3
3
17.8% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
FileSystemResourceLoader 59 22 17.8% 9 11
0.702702770.3%
 

Contributing tests

This file is covered by 4 tests. .

Source view

1    /**
2    * This file Copyright (c) 2012-2015 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
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    * Loader which looks for resources in file system.
51    * Example of path definition:
52    * [] fileSystemLoader
53    * - class info.magnolia.module.resources.loaders.FileSystemResourceLoader
54    * - path /../<path> - set to 'magnolia.home' property by default
55    *
56    * @deprecated since 2.4 (Magnolia 5.4), please use the new {@link info.magnolia.resourceloader.ResourceOrigin} API.
57    */
58    @Deprecated
 
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   
 
67  4 toggle public FileSystemResourceLoader(MagnoliaConfigurationProperties mcp) {
68  4 path = mcp.getProperty("magnolia.home");
69    }
70   
 
71  4 toggle @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    //final Path templatePath = rootPath.resolve(template);
81    // security check - prevent traversing outside of path which is
82    // set in config (e.g. prevent this: /../../../../etc/passwd)
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   
 
93  4 toggle 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   
 
107    toggle public void setEnabled(boolean enabled) {
108    this.enabled = enabled;
109    }
110   
 
111    toggle @Override
112    public boolean isEnabled() {
113    return this.enabled;
114    }
115   
 
116    toggle public void setPath(String path) {
117    this.path = path;
118    }
119   
 
120    toggle public String getPath() {
121    return this.path;
122    }
123   
124    }