1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.extremecomponents.table.filter;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22
23 import javax.servlet.Filter;
24 import javax.servlet.FilterChain;
25 import javax.servlet.FilterConfig;
26 import javax.servlet.ServletException;
27 import javax.servlet.ServletRequest;
28 import javax.servlet.ServletResponse;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31
32 import org.apache.avalon.framework.logger.ConsoleLogger;
33 import org.apache.avalon.framework.logger.Logger;
34 import org.apache.commons.lang.StringUtils;
35 import org.apache.fop.apps.Driver;
36 import org.apache.fop.messaging.MessageHandler;
37 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
38 import org.extremecomponents.table.bean.Table;
39 import org.extremecomponents.table.model.ExportHandler;
40 import org.extremecomponents.table.model.ParameterRegistry;
41 import org.xml.sax.InputSource;
42
43
44
45 /***
46 * Filter to handle the incoming FOP requests to generate the PDF document.
47 *
48 *
49 * <filter>
50 * <filter-name>ExportFilter</filter-name>
51 * <display-name>ExportFilter</display-name>
52 * <filter-class>org.extremecomponents.table.filter.ExportFilter</filter-class>
53 * </filter>
54 * <filter-mapping>
55 * <filter-name>ExportFilter</filter-name>
56 * <url-pattern>*</url-pattern>
57 * </filter-mapping>
58 *
59 * <!-- Action Servlet Configuration -->
60 * <filter-mapping>
61 * <filter-name>ExportFilter</filter-name>
62 * <servlet-name>eXtremeTest</servlet-name>
63 * </filter-mapping>
64 * @author jeff johnston
65 */
66 public final class ExportFilter implements Filter
67 {
68 private FilterConfig filterConfig = null;
69 private Logger log = null;
70
71 public void init(FilterConfig filterConfig)
72 throws ServletException
73 {
74 this.filterConfig = filterConfig;
75 }
76
77 public void destroy()
78 {
79 this.filterConfig = null;
80 }
81
82 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
83 throws IOException, ServletException
84 {
85 String tableKey = request.getParameter(Table.EXTREME_TABLE);
86
87 String invokeExport = null;
88 String exportName = null;
89 if (StringUtils.isNotBlank(tableKey))
90 {
91 invokeExport = request.getParameter(tableKey +ParameterRegistry.EXPORT);
92 exportName = request.getParameter(tableKey +ExportHandler.EXPORT_NAME);
93 }
94
95 if (StringUtils.isNotBlank(invokeExport))
96 {
97 setResponseHeaders((HttpServletRequest) request, (HttpServletResponse) response, exportName);
98 GenericResponseWrapper wrapper = new GenericResponseWrapper((HttpServletResponse) response);
99 chain.doFilter(request, wrapper);
100 }
101 else
102 {
103 chain.doFilter(request, response);
104 }
105
106 if (StringUtils.isNotBlank(invokeExport))
107 {
108 try
109 {
110 if (exportName.endsWith(ExportHandler.PDF))
111 handlePDF(request, response);
112 else if (exportName.endsWith(ExportHandler.XLS))
113 handleXLS(request, response);
114 }
115 catch (Exception e)
116 {
117 e.printStackTrace();
118 }
119 }
120 }
121
122 public void handleXLS(ServletRequest request, ServletResponse response) throws Exception
123 {
124 HSSFWorkbook wb = (HSSFWorkbook) request.getAttribute(ExportHandler.EXPORT_DATA);
125 wb.write(response.getOutputStream());
126 response.getOutputStream().flush();
127 response.getOutputStream().close();
128 }
129
130 public void handlePDF(ServletRequest request, ServletResponse response) throws Exception
131 {
132 String output = (String)request.getAttribute(ExportHandler.EXPORT_DATA);
133 InputStream is = new ByteArrayInputStream(output.getBytes());
134 renderFO(new InputSource(is), (HttpServletResponse) response);
135 }
136
137 public void renderFO(InputSource foFile, HttpServletResponse response)
138 throws Exception
139 {
140 ByteArrayOutputStream out = new ByteArrayOutputStream();
141
142 Driver driver = new Driver(foFile, out);
143
144 if (log == null)
145 {
146 log = new ConsoleLogger(ConsoleLogger.LEVEL_WARN);
147 MessageHandler.setScreenLogger(log);
148 }
149
150 driver.setLogger(log);
151 driver.setRenderer(Driver.RENDER_PDF);
152 driver.run();
153
154 byte[] content = out.toByteArray();
155 response.setContentLength(content.length);
156 response.getOutputStream().write(content);
157 response.getOutputStream().flush();
158 }
159
160 public void setResponseHeaders(HttpServletRequest request, HttpServletResponse response, String exportName)
161 {
162 if (exportName.endsWith(ExportHandler.PDF))
163 response.setContentType("application/pdf");
164 else if (exportName.endsWith(ExportHandler.XLS))
165 response.setContentType("application/vnd.ms-excel");
166
167 response.setHeader("Content-Disposition", "attachment;filename=\"" + exportName + "\"");
168
169
170
171
172 response.setHeader("Pragma", "public");
173 response.setDateHeader("Expires", (System.currentTimeMillis() + 1000));
174
175
176
177
178
179
180
181
182 }
183 }