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