|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
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 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| public final class ExportDelegate |
|
36 |
| { |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| private static Log log = LogFactory.getLog(ExportDelegate.class); |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
0
| private ExportDelegate()
|
|
47 |
| { |
|
48 |
| |
|
49 |
| } |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
14
| protected static void writeExport(HttpServletResponse response, ServletRequest request,
|
|
60 |
| BufferedResponseWrapper wrapper) throws IOException |
|
61 |
| { |
|
62 |
| |
|
63 |
14
| if (wrapper.isOutRequested())
|
|
64 |
| { |
|
65 |
| |
|
66 |
0
| log.debug("Filter operating in unbuffered mode. Everything done, exiting");
|
|
67 |
0
| return;
|
|
68 |
| } |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
14
| log.debug("Filter operating in buffered mode. ");
|
|
73 |
| |
|
74 |
14
| Map bean = (Map) request.getAttribute(TableTag.FILTER_CONTENT_OVERRIDE_BODY);
|
|
75 |
| |
|
76 |
14
| if (log.isDebugEnabled())
|
|
77 |
| { |
|
78 |
14
| log.debug(bean);
|
|
79 |
| } |
|
80 |
| |
|
81 |
14
| Object pageContent = bean.get(TableTagParameters.BEAN_BODY);
|
|
82 |
| |
|
83 |
14
| if (pageContent == null)
|
|
84 |
| { |
|
85 |
2
| if (log.isDebugEnabled())
|
|
86 |
| { |
|
87 |
2
| log.debug("Filter is enabled but exported content has not been found. Maybe an error occurred?");
|
|
88 |
| } |
|
89 |
| |
|
90 |
2
| response.setContentType(wrapper.getContentType());
|
|
91 |
2
| PrintWriter out = response.getWriter();
|
|
92 |
| |
|
93 |
2
| out.write(wrapper.getContentAsString());
|
|
94 |
2
| out.flush();
|
|
95 |
2
| return;
|
|
96 |
| } |
|
97 |
| |
|
98 |
| |
|
99 |
12
| if (!response.isCommitted())
|
|
100 |
| { |
|
101 |
12
| response.reset();
|
|
102 |
| } |
|
103 |
| |
|
104 |
12
| String filename = (String) bean.get(TableTagParameters.BEAN_FILENAME);
|
|
105 |
12
| String contentType = (String) bean.get(TableTagParameters.BEAN_CONTENTTYPE);
|
|
106 |
| |
|
107 |
12
| if (StringUtils.isNotBlank(filename))
|
|
108 |
| { |
|
109 |
0
| response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
|
|
110 |
| } |
|
111 |
| |
|
112 |
12
| String characterEncoding = wrapper.getCharacterEncoding();
|
|
113 |
12
| String wrappedContentType = wrapper.getContentType();
|
|
114 |
| |
|
115 |
12
| if (wrappedContentType != null && wrappedContentType.indexOf("charset") > -1)
|
|
116 |
| { |
|
117 |
| |
|
118 |
12
| characterEncoding = StringUtils.substringAfter(wrappedContentType, "charset=");
|
|
119 |
| } |
|
120 |
| |
|
121 |
12
| if (characterEncoding != null && contentType.indexOf("charset") == -1)
|
|
122 |
| { |
|
123 |
12
| contentType += "; charset=" + characterEncoding;
|
|
124 |
| } |
|
125 |
| |
|
126 |
12
| response.setContentType(contentType);
|
|
127 |
| |
|
128 |
12
| if (pageContent instanceof String)
|
|
129 |
| { |
|
130 |
| |
|
131 |
10
| if (characterEncoding != null)
|
|
132 |
| { |
|
133 |
10
| response.setContentLength(((String) pageContent).getBytes(characterEncoding).length);
|
|
134 |
| } |
|
135 |
| else |
|
136 |
| { |
|
137 |
0
| response.setContentLength(((String) pageContent).getBytes().length);
|
|
138 |
| } |
|
139 |
| |
|
140 |
10
| PrintWriter out = response.getWriter();
|
|
141 |
10
| out.write((String) pageContent);
|
|
142 |
10
| out.flush();
|
|
143 |
| } |
|
144 |
| else |
|
145 |
| { |
|
146 |
| |
|
147 |
2
| byte[] content = (byte[]) pageContent;
|
|
148 |
2
| response.setContentLength(content.length);
|
|
149 |
2
| OutputStream out = response.getOutputStream();
|
|
150 |
2
| out.write(content);
|
|
151 |
2
| out.flush();
|
|
152 |
| } |
|
153 |
| } |
|
154 |
| } |