1 /***
2 * Licensed under the Artistic License; you may not use this file
3 * except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://displaytag.sourceforge.net/license.html
7 *
8 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
9 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11 */
12 package org.displaytag.filter;
13
14 import java.io.IOException;
15 import java.io.OutputStream;
16 import java.io.PrintWriter;
17 import java.util.Map;
18
19 import javax.servlet.ServletRequest;
20 import javax.servlet.http.HttpServletResponse;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.displaytag.tags.TableTag;
26 import org.displaytag.tags.TableTagParameters;
27
28
29 /***
30 * Actually writes out the content of the wrapped response. Used by the j2ee filter and the Spring interceptor
31 * implementations.
32 * @author Fabrizio Giustina
33 * @version $Revision: 1.6 $ ($Author: fgiust $)
34 */
35 public final class ExportDelegate
36 {
37
38 /***
39 * logger
40 */
41 private static Log log = LogFactory.getLog(ExportDelegate.class);
42
43 /***
44 * Donět instantiate.
45 */
46 private ExportDelegate()
47 {
48
49 }
50
51 /***
52 * Actually writes exported data. Extracts content from the Map stored in request with the
53 * <code>TableTag.FILTER_CONTENT_OVERRIDE_BODY</code> key.
54 * @param wrapper BufferedResponseWrapper implementation
55 * @param response HttpServletResponse
56 * @param request ServletRequest
57 * @throws IOException exception thrown by response writer/outputStream
58 */
59 protected static void writeExport(HttpServletResponse response, ServletRequest request,
60 BufferedResponseWrapper wrapper) throws IOException
61 {
62
63 if (wrapper.isOutRequested())
64 {
65
66 log.debug("Filter operating in unbuffered mode. Everything done, exiting");
67 return;
68 }
69
70
71
72 log.debug("Filter operating in buffered mode. ");
73
74 Map bean = (Map) request.getAttribute(TableTag.FILTER_CONTENT_OVERRIDE_BODY);
75
76 if (log.isDebugEnabled())
77 {
78 log.debug(bean);
79 }
80
81 Object pageContent = bean.get(TableTagParameters.BEAN_BODY);
82
83 if (pageContent == null)
84 {
85 if (log.isDebugEnabled())
86 {
87 log.debug("Filter is enabled but exported content has not been found. Maybe an error occurred?");
88 }
89
90 PrintWriter out = response.getWriter();
91 out.write(wrapper.getContentAsString());
92 out.flush();
93 return;
94 }
95
96
97 if (!response.isCommitted())
98 {
99 response.reset();
100 }
101
102 String filename = (String) bean.get(TableTagParameters.BEAN_FILENAME);
103 String contentType = (String) bean.get(TableTagParameters.BEAN_CONTENTTYPE);
104
105 if (StringUtils.isNotBlank(filename))
106 {
107 response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
108 }
109
110 String characterEncoding = wrapper.getCharacterEncoding();
111 String wrappedContentType = wrapper.getContentType();
112
113 if (wrappedContentType != null && wrappedContentType.indexOf("charset") > -1)
114 {
115
116 characterEncoding = StringUtils.substringAfter(wrappedContentType, "charset=");
117 }
118
119 if (characterEncoding != null && contentType.indexOf("charset") == -1)
120 {
121 contentType += "; charset=" + characterEncoding;
122 }
123
124 response.setContentType(contentType);
125
126 if (pageContent instanceof String)
127 {
128
129 if (characterEncoding != null)
130 {
131 response.setContentLength(((String) pageContent).getBytes(characterEncoding).length);
132 }
133 else
134 {
135 response.setContentLength(((String) pageContent).getBytes().length);
136 }
137
138 PrintWriter out = response.getWriter();
139 out.write((String) pageContent);
140 out.flush();
141 }
142 else
143 {
144
145 byte[] content = (byte[]) pageContent;
146 response.setContentLength(content.length);
147 OutputStream out = response.getOutputStream();
148 out.write(content);
149 out.flush();
150 }
151 }
152 }