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.freemarker;
35
36 import info.magnolia.cms.util.ClasspathResourcesUtil;
37 import info.magnolia.servlet.ServletContextWrapper;
38
39 import java.beans.PropertyDescriptor;
40 import java.io.File;
41 import java.io.FileInputStream;
42 import java.io.FileNotFoundException;
43 import java.io.InputStream;
44 import java.net.MalformedURLException;
45 import java.net.URL;
46 import java.net.URLClassLoader;
47 import java.util.HashSet;
48 import java.util.Set;
49
50 import javax.servlet.ServletContext;
51
52 import org.apache.commons.io.FileUtils;
53 import org.apache.commons.lang3.StringUtils;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57
58
59
60
61
62
63
64
65
66 public class FreemarkerServletContextWrapper extends ServletContextWrapper {
67
68 private final ServletContext parentContext;
69
70 private static Logger log = LoggerFactory.getLogger(FreemarkerServletContextWrapper.class);
71
72 public FreemarkerServletContextWrapper(ServletContext parentServletContext) {
73 super(parentServletContext);
74
75 this.parentContext = parentServletContext;
76 }
77
78 @Override
79 public URL getResource(String path) throws MalformedURLException {
80
81 URL result = null;
82 if (path.startsWith("/")) {
83 result = parentContext.getResource(path);
84 }
85 if (result == null) {
86
87 File file = new File(path);
88 if ((file.exists()) && (file.isFile())) {
89 result = file.toURI().toURL();
90 }
91 }
92 return result;
93 }
94
95 @Override
96 public InputStream getResourceAsStream(String path) {
97
98 InputStream is = null;
99 if (path.startsWith("/")) {
100 is = parentContext.getResourceAsStream(path);
101 }
102 if (is == null) {
103
104 File file = new File(path);
105 if ((file.exists()) && (file.isFile())) {
106 try {
107 return new FileInputStream(file);
108 } catch (FileNotFoundException e) {
109
110 }
111 }
112 }
113 return is;
114 }
115
116 @Override
117 public Set<String> getResourcePaths(String path) {
118 if (StringUtils.equals(path, "/WEB-INF/lib")) {
119 log.debug("returning resources from classpath");
120
121 final Set<String> resources = new HashSet<String>();
122 final ClassLoader cl = Thread.currentThread().getContextClassLoader();
123
124
125 if (cl instanceof URLClassLoader) {
126 final URLClassLoader urlClassLoader = (URLClassLoader) cl;
127 final URL[] urls = urlClassLoader.getURLs();
128 for (int j = 0; j < urls.length; j++) {
129 final File tofile = ClasspathResourcesUtil.sanitizeToFile(urls[j]);
130 if (tofile.isDirectory()) {
131 for (File file : (FileUtils.listFiles(tofile, null, true))) {
132 resources.add(file.getAbsolutePath());
133 }
134 } else {
135 resources.add(tofile.getAbsolutePath());
136 }
137 }
138
139 return resources;
140 }
141 try {
142
143
144
145
146
147
148 PropertyDescriptor pd = new PropertyDescriptor("classPath", cl.getClass());
149 if (pd != null && pd.getReadMethod() != null) {
150 String classpath = (String) pd.getReadMethod().invoke(cl);
151 if (StringUtils.isNotBlank(classpath)) {
152 String[] paths = StringUtils.split(classpath, File.pathSeparator);
153 for (int j = 0; j < paths.length; j++) {
154 final File tofile = new File(paths[j]);
155
156 if (tofile.exists()) {
157 if (tofile.isDirectory()) {
158 for (File file : (FileUtils.listFiles(tofile, null, true))) {
159 resources.add(file.getAbsolutePath());
160 }
161 } else {
162 resources.add(tofile.getAbsolutePath());
163 }
164 }
165 }
166 return resources;
167 }
168 }
169 } catch (Throwable e) {
170
171 }
172
173
174 }
175 return parentContext.getResourcePaths(path);
176 }
177 }