1 package org.whatsitcalled.webflange.filters;
2
3 import java.io.File;
4 import java.io.IOException;
5
6 import javax.servlet.Filter;
7 import javax.servlet.FilterChain;
8 import javax.servlet.FilterConfig;
9 import javax.servlet.ServletException;
10 import javax.servlet.ServletRequest;
11 import javax.servlet.ServletResponse;
12
13 import org.apache.commons.io.FileUtils;
14 import org.apache.log4j.Logger;
15 import org.whatsitcalled.webflange.ResourceFactory;
16
17 public class FileServingFilter implements Filter {
18 Logger LOGGER = Logger.getLogger(FileServingFilter.class);
19 public void destroy() {
20
21
22 }
23
24 public void doFilter(ServletRequest request, ServletResponse response,
25 FilterChain chain) throws IOException, ServletException {
26 String chart = request.getParameter("chart");
27 if (chart != null) {
28 File file = new File(ResourceFactory.getFileManager().getDataFolder(), chart);
29 LOGGER.debug("Reading file: " + file.getAbsolutePath());
30 byte[] imageBytes = FileUtils.readFileToByteArray(file);
31 LOGGER.debug("Serving file: " + file.getAbsolutePath());
32 response.getOutputStream().write(imageBytes);
33 } else {
34 chain.doFilter(request, response);
35 }
36
37 }
38
39 public void init(FilterConfig arg0) throws ServletException {
40
41
42 }
43
44 }