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
38 import java.beans.PropertyDescriptor;
39 import java.io.File;
40 import java.io.FileInputStream;
41 import java.io.FileNotFoundException;
42 import java.io.InputStream;
43 import java.net.MalformedURLException;
44 import java.net.URL;
45 import java.net.URLClassLoader;
46 import java.util.Enumeration;
47 import java.util.HashSet;
48 import java.util.List;
49 import java.util.Set;
50
51 import javax.servlet.RequestDispatcher;
52 import javax.servlet.Servlet;
53 import javax.servlet.ServletContext;
54 import javax.servlet.ServletException;
55
56 import org.apache.commons.io.FileUtils;
57 import org.apache.commons.lang.StringUtils;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
60
61
62
63
64
65
66
67 public class FreemarkerServletContextWrapper implements ServletContext {
68
69 private final ServletContext parentContext;
70
71
72
73
74 private static Logger log = LoggerFactory.getLogger(FreemarkerServletContextWrapper.class);
75
76 public FreemarkerServletContextWrapper(ServletContext parentServletContext) {
77
78 this.parentContext = parentServletContext;
79 }
80
81 @Override
82 public URL getResource(String path) throws MalformedURLException {
83
84 URL result = parentContext.getResource(path);
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 InputStream is = parentContext.getResourceAsStream(path);
98 if (is == null) {
99
100 File file = new File(path);
101 if ((file.exists()) && (file.isFile())) {
102 try {
103 return new FileInputStream(file);
104 }
105 catch (FileNotFoundException e) {
106
107 }
108 }
109 }
110 return is;
111 }
112
113 @Override
114 @SuppressWarnings({"unchecked", "rawtypes"})
115 public Set getResourcePaths(String path) {
116 if (StringUtils.equals(path, "/WEB-INF/lib")) {
117 log.debug("returning resources from classpath");
118
119 final Set<String> resources = new HashSet<String>();
120 final ClassLoader cl = Thread.currentThread().getContextClassLoader();
121
122
123 if (cl instanceof URLClassLoader) {
124 final URLClassLoader urlClassLoader = (URLClassLoader) cl;
125 final URL[] urls = urlClassLoader.getURLs();
126 for (int j = 0; j < urls.length; j++) {
127 final File tofile = ClasspathResourcesUtil.sanitizeToFile(urls[j]);
128 if (tofile == null) {
129 continue;
130 }
131 if (tofile.isDirectory()) {
132 for (File file : ((List<File>) FileUtils.listFiles(tofile, null, true))) {
133 resources.add(file.getAbsolutePath());
134 }
135 }
136 else {
137 resources.add(tofile.getAbsolutePath());
138 }
139 }
140
141 return resources;
142 }
143 try {
144
145
146
147
148
149
150 PropertyDescriptor pd = new PropertyDescriptor("classPath", cl.getClass());
151 if (pd != null && pd.getReadMethod() != null) {
152 String classpath = (String) pd.getReadMethod().invoke(cl, new Object[]{});
153 if (StringUtils.isNotBlank(classpath)) {
154 String[] paths = StringUtils.split(classpath, File.pathSeparator);
155 for (int j = 0; j < paths.length; j++) {
156 final File tofile = new File(paths[j]);
157
158 if (tofile.exists()) {
159 if (tofile.isDirectory()) {
160 for (File file : ((List<File>) FileUtils.listFiles(tofile, null, true))) {
161 resources.add(file.getAbsolutePath());
162 }
163 }
164 else {
165 resources.add(tofile.getAbsolutePath());
166 }
167 }
168 }
169 return resources;
170 }
171 }
172 }
173 catch (Throwable e) {
174
175 }
176
177
178 }
179 return parentContext.getResourcePaths(path);
180 }
181
182
183 @Override
184 public Object getAttribute(String name) {
185 return parentContext.getAttribute(name);
186 }
187
188 @SuppressWarnings("rawtypes")
189 @Override
190 public Enumeration getAttributeNames() {
191 return parentContext.getAttributeNames();
192 }
193
194 @Override
195 public ServletContext getContext(String uripath) {
196 return parentContext.getContext(uripath);
197 }
198
199 @Override
200 public String getContextPath() {
201 return parentContext.getContextPath();
202 }
203
204 @Override
205 public String getInitParameter(String name) {
206 return parentContext.getInitParameter(name);
207 }
208
209 @SuppressWarnings("rawtypes")
210 @Override
211 public Enumeration getInitParameterNames() {
212 return parentContext.getInitParameterNames();
213 }
214
215 @Override
216 public int getMajorVersion() {
217 return parentContext.getMajorVersion();
218 }
219
220 @Override
221 public String getMimeType(String file) {
222 return parentContext.getMimeType(file);
223 }
224
225 @Override
226 public int getMinorVersion() {
227 return parentContext.getMinorVersion();
228 }
229
230 @Override
231 public RequestDispatcher getNamedDispatcher(String name) {
232 return parentContext.getNamedDispatcher(name);
233 }
234
235 @Override
236 public String getRealPath(String path) {
237 return parentContext.getRealPath(path);
238 }
239
240 @Override
241 public RequestDispatcher getRequestDispatcher(String path) {
242 return parentContext.getRequestDispatcher(path);
243 }
244
245 @Override
246 public String getServerInfo() {
247 return parentContext.getServerInfo();
248 }
249
250 @SuppressWarnings("deprecation")
251 @Override
252 public Servlet getServlet(String name) throws ServletException {
253 return parentContext.getServlet(name);
254 }
255
256 @Override
257 public String getServletContextName() {
258 return parentContext.getServletContextName();
259 }
260
261 @SuppressWarnings({"rawtypes", "deprecation"})
262 @Override
263 public Enumeration getServletNames() {
264 return parentContext.getServletNames();
265 }
266
267 @SuppressWarnings({"rawtypes", "deprecation"})
268 @Override
269 public Enumeration getServlets() {
270 return parentContext.getServlets();
271 }
272
273 @Override
274 public void log(String msg) {
275 parentContext.log(msg);
276 }
277
278 @SuppressWarnings("deprecation")
279 @Override
280 public void log(Exception exception, String msg) {
281 parentContext.log(exception, msg);
282 }
283
284 @Override
285 public void log(String message, Throwable throwable) {
286 parentContext.log(message, throwable);
287 }
288
289 @Override
290 public void removeAttribute(String name) {
291 parentContext.removeAttribute(name);
292 }
293
294 @Override
295 public void setAttribute(String name, Object object) {
296 parentContext.setAttribute(name, object);
297 }
298 }