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 = 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({"unchecked", "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 == null) {
136 continue;
137 }
138 if (tofile.isDirectory()) {
139 for (File file : ((List<File>) FileUtils.listFiles(tofile, null, true))) {
140 resources.add(file.getAbsolutePath());
141 }
142 }
143 else {
144 resources.add(tofile.getAbsolutePath());
145 }
146 }
147
148 return resources;
149 }
150 try {
151
152
153
154
155
156
157 PropertyDescriptor pd = new PropertyDescriptor("classPath", cl.getClass());
158 if (pd != null && pd.getReadMethod() != null) {
159 String classpath = (String) pd.getReadMethod().invoke(cl, new Object[]{});
160 if (StringUtils.isNotBlank(classpath)) {
161 String[] paths = StringUtils.split(classpath, File.pathSeparator);
162 for (int j = 0; j < paths.length; j++) {
163 final File tofile = new File(paths[j]);
164
165 if (tofile.exists()) {
166 if (tofile.isDirectory()) {
167 for (File file : ((List<File>) FileUtils.listFiles(tofile, null, true))) {
168 resources.add(file.getAbsolutePath());
169 }
170 }
171 else {
172 resources.add(tofile.getAbsolutePath());
173 }
174 }
175 }
176 return resources;
177 }
178 }
179 }
180 catch (Throwable e) {
181
182 }
183
184
185 }
186 return parentContext.getResourcePaths(path);
187 }
188
189
190 @Override
191 public Object getAttribute(String name) {
192 return parentContext.getAttribute(name);
193 }
194
195 @SuppressWarnings("rawtypes")
196 @Override
197 public Enumeration getAttributeNames() {
198 return parentContext.getAttributeNames();
199 }
200
201 @Override
202 public ServletContext getContext(String uripath) {
203 return parentContext.getContext(uripath);
204 }
205
206 @Override
207 public String getContextPath() {
208 return parentContext.getContextPath();
209 }
210
211 @Override
212 public String getInitParameter(String name) {
213 return parentContext.getInitParameter(name);
214 }
215
216 @SuppressWarnings("rawtypes")
217 @Override
218 public Enumeration getInitParameterNames() {
219 return parentContext.getInitParameterNames();
220 }
221
222 @Override
223 public int getMajorVersion() {
224 return parentContext.getMajorVersion();
225 }
226
227 @Override
228 public String getMimeType(String file) {
229 return parentContext.getMimeType(file);
230 }
231
232 @Override
233 public int getMinorVersion() {
234 return parentContext.getMinorVersion();
235 }
236
237 @Override
238 public RequestDispatcher getNamedDispatcher(String name) {
239 return parentContext.getNamedDispatcher(name);
240 }
241
242 @Override
243 public String getRealPath(String path) {
244 return parentContext.getRealPath(path);
245 }
246
247 @Override
248 public RequestDispatcher getRequestDispatcher(String path) {
249 return parentContext.getRequestDispatcher(path);
250 }
251
252 @Override
253 public String getServerInfo() {
254 return parentContext.getServerInfo();
255 }
256
257 @SuppressWarnings("deprecation")
258 @Override
259 public Servlet getServlet(String name) throws ServletException {
260 return parentContext.getServlet(name);
261 }
262
263 @Override
264 public String getServletContextName() {
265 return parentContext.getServletContextName();
266 }
267
268 @SuppressWarnings({"rawtypes", "deprecation"})
269 @Override
270 public Enumeration getServletNames() {
271 return parentContext.getServletNames();
272 }
273
274 @SuppressWarnings({"rawtypes", "deprecation"})
275 @Override
276 public Enumeration getServlets() {
277 return parentContext.getServlets();
278 }
279
280 @Override
281 public void log(String msg) {
282 parentContext.log(msg);
283 }
284
285 @SuppressWarnings("deprecation")
286 @Override
287 public void log(Exception exception, String msg) {
288 parentContext.log(exception, msg);
289 }
290
291 @Override
292 public void log(String message, Throwable throwable) {
293 parentContext.log(message, throwable);
294 }
295
296 @Override
297 public void removeAttribute(String name) {
298 parentContext.removeAttribute(name);
299 }
300
301 @Override
302 public void setAttribute(String name, Object object) {
303 parentContext.setAttribute(name, object);
304 }
305 }