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