KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > displaytag > filter > ExportDelegate


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 JavaDoc;
15 import java.io.OutputStream JavaDoc;
16 import java.io.PrintWriter JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import javax.servlet.ServletRequest JavaDoc;
20 import javax.servlet.http.HttpServletResponse JavaDoc;
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: 871 $ ($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         // unused
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 JavaDoc response, ServletRequest JavaDoc request,
60         BufferedResponseWrapper wrapper) throws IOException JavaDoc
61     {
62
63         if (wrapper.isOutRequested())
64         {
65             // data already written
66
log.debug("Filter operating in unbuffered mode. Everything done, exiting");
67             return;
68         }
69
70         // if you reach this point the PARAMETER_EXPORTING has been found, but the special header has never been set in
71
// response (this is the signal from table tag that it is going to write exported data)
72
log.debug("Filter operating in buffered mode. ");
73
74         Map JavaDoc bean = (Map JavaDoc) request.getAttribute(TableTag.FILTER_CONTENT_OVERRIDE_BODY);
75
76         if (log.isDebugEnabled())
77         {
78             log.debug(bean);
79         }
80
81         Object JavaDoc 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             response.setContentType(wrapper.getContentType());
91             PrintWriter JavaDoc out = response.getWriter();
92
93             out.write(wrapper.getContentAsString());
94             out.flush();
95             return;
96         }
97
98         // clear headers
99
if (!response.isCommitted())
100         {
101             response.reset();
102         }
103
104         String JavaDoc filename = (String JavaDoc) bean.get(TableTagParameters.BEAN_FILENAME);
105         String JavaDoc contentType = (String JavaDoc) bean.get(TableTagParameters.BEAN_CONTENTTYPE);
106
107         if (StringUtils.isNotBlank(filename))
108         {
109             response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
110         }
111
112         String JavaDoc characterEncoding = wrapper.getCharacterEncoding();
113         String JavaDoc wrappedContentType = wrapper.getContentType();
114
115         if (wrappedContentType != null && wrappedContentType.indexOf("charset") > -1)
116         {
117             // charset is already specified (see #921811)
118
characterEncoding = StringUtils.substringAfter(wrappedContentType, "charset=");
119         }
120
121         if (characterEncoding != null && contentType.indexOf("charset") == -1) //$NON-NLS-1$
122
{
123             contentType += "; charset=" + characterEncoding; //$NON-NLS-1$
124
}
125
126         response.setContentType(contentType);
127
128         if (pageContent instanceof String JavaDoc)
129         {
130             // text content
131
if (characterEncoding != null)
132             {
133                 response.setContentLength(((String JavaDoc) pageContent).getBytes(characterEncoding).length);
134             }
135             else
136             {
137                 response.setContentLength(((String JavaDoc) pageContent).getBytes().length);
138             }
139
140             PrintWriter JavaDoc out = response.getWriter();
141             out.write((String JavaDoc) pageContent);
142             out.flush();
143         }
144         else
145         {
146             // dealing with binary content
147
byte[] content = (byte[]) pageContent;
148             response.setContentLength(content.length);
149             OutputStream JavaDoc out = response.getOutputStream();
150             out.write(content);
151             out.flush();
152         }
153     }
154 }
155
Popular Tags