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.util.HashMap;
15 import java.util.Map;
16
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpServletResponse;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.displaytag.Messages;
23 import org.displaytag.tags.TableTag;
24 import org.displaytag.tags.TableTagParameters;
25 import org.springframework.web.servlet.HandlerAdapter;
26 import org.springframework.web.servlet.HandlerInterceptor;
27 import org.springframework.web.servlet.ModelAndView;
28 import org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter;
29
30
31 /**
32 * <p>
33 * Allow the author of an included JSP page to reset the content type to something else (like a binary stream), and then
34 * write the new info back as the exclusive response, clearing the buffers of all previously added content.
35 * </p>
36 * <p>
37 * This interceptor allows TableTag users to perform exports from pages that are run as includes, such as from Struts or
38 * a jsp:include. If that is your intention, just add this interceptor to your spring dispatcher context xml and map it
39 * to the appropriate requests, using something like:
40 * </p>
41 *
42 * <pre>
43 * <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
44 * <property name="interceptors">
45 * <list>
46 * <bean class="org.displaytag.filter.DisplayTagSpringInterceptor"/>
47 * </list>
48 * </property
49 * </bean>
50 * </pre>
51 *
52 * <p>
53 * By default the interceptor buffers all the export content before writing it out. You can set an optional parameter
54 * <code>buffer</code> to <code>false</code> to make the interceptor write directly to the output stream. This could
55 * be faster and uses less memory, but the content length will not be set.
56 * </p>
57 *
58 * <pre>
59 * <bean class="org.displaytag.filter.DisplayTagSpringInterceptor">
60 * <property name="buffer"><value>false</value></property>
61 * </bean>
62 * </pre>
63 *
64 * @author Keith Garry Boyce
65 * @author rapruitt
66 * @author Fabrizio Giustina
67 * @version $Revision: 1081 $ ($Author: fgiust $)
68 */
69 public class DisplayTagSpringInterceptor implements HandlerInterceptor
70 {
71
72 /**
73 * Logger.
74 */
75 static Log log = LogFactory.getLog(DisplayTagSpringInterceptor.class);
76
77 /**
78 * Force response buffering. Enabled by default.
79 */
80 private boolean buffer = true;
81
82 /**
83 * Sets the buffer state.
84 * @param bufferingEnabled it <code>true</code> buffering will be used
85 */
86 public void setBuffer(boolean bufferingEnabled)
87 {
88 this.buffer = bufferingEnabled;
89 }
90
91 /**
92 * @see HandlerInterceptor#preHandle(HttpServletRequest,HttpServletResponse, Object)
93 */
94 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
95 {
96
97 if (request.getParameter(TableTagParameters.PARAMETER_EXPORTING) == null)
98 {
99 if (log.isDebugEnabled())
100 {
101 log.debug(Messages.getString("ResponseOverrideFilter.parameternotfound")); //$NON-NLS-1$
102 }
103 // don't intercept!
104 return true;
105 }
106
107 BufferedResponseWrapper wrapper = new BufferedResponseWrapper13Impl(response);
108
109 Map contentBean = new HashMap(4);
110 if (buffer)
111 {
112 contentBean.put(TableTagParameters.BEAN_BUFFER, Boolean.TRUE);
113 }
114 request.setAttribute(TableTag.FILTER_CONTENT_OVERRIDE_BODY, contentBean);
115
116 if (log.isDebugEnabled())
117 {
118 log.debug("handler is " + handler);
119 }
120
121 HandlerAdapter handlerAdaptor = new SimpleControllerHandlerAdapter();
122 handlerAdaptor.handle(request, wrapper, handler);
123
124 ExportDelegate.writeExport(response, request, wrapper);
125
126 return false;
127 }
128
129 /**
130 * @see HandlerInterceptor#postHandle(HttpServletRequest,HttpServletResponse, Object, ModelAndView)
131 */
132 public void postHandle(HttpServletRequest request, HttpServletResponse response, Object obj,
133 ModelAndView modelAndView) throws Exception
134 {
135 // Nothing to do
136 }
137
138 /**
139 * @see HandlerInterceptor#afterCompletion(HttpServletRequest,HttpServletResponse, Object, Exception)
140 */
141 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object obj,
142 Exception exception) throws Exception
143 {
144 // Nothing to do
145 }
146
147 }