The following comment has been added to this issue:

     Author: Keith Garry Boyce
    Created: Tue, 16 Nov 2004 2:09 PM
       Body:
Also have you tried it in a real application rather than just in a unit test? 
It could be that the mock request object doesn't fully implement the real 
request object.
---------------------------------------------------------------------
View this comment:
  http://jira.codehaus.org/browse/DISPL-30?page=comments#action_26485

---------------------------------------------------------------------
View the issue:
  http://jira.codehaus.org/browse/DISPL-30

Here is an overview of the issue:
---------------------------------------------------------------------
        Key: DISPL-30
    Summary: Spring Interceptor for use with servlet 2.2 containers
       Type: New Feature

     Status: In Progress
   Priority: Minor

 Original Estimate: Unknown
 Time Spent: 1 day
  Remaining: 0 minutes

    Project: DisplayTag
 Components: 
             Export
   Fix Fors:
             1.0 RC2
   Versions:
             1.0 RC1

   Assignee: fabrizio giustina
   Reporter: fabrizio giustina

    Created: Sat, 2 Oct 2004 4:13 AM
    Updated: Tue, 16 Nov 2004 2:09 PM

Description:
Se http://forum.springframework.org/viewtopic.php?t=1004 and displaytag-user 
mailing list

Boyce, Keith Garry

I developed an interceptor to use instead of responseOverrideFilter for use in 
Servlet 2.2 containers with ServletWrapperController (posted on jira). Please 
give me credit in javadoc if you decide to use it. Here is the code:

Code:
/*
 * DisplayTagInterceptor.java
 *
 * Copyright 2004 by Electronic Data Systems
 * Corporation. All rights reserved.
 *
 * An unpublished work created Sep 26, 2004, 2004. This work is a
 * trade secret of EDS and unauthorized use or copying
 * is prohibited.
 *
 */
package org.springframework.web.servlet.mvc;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.displaytag.filter.BufferedResponseWrapper;
import org.displaytag.tags.TableTag;
import org.displaytag.tags.TableTagParameters;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.HandlerAdapter;
/**
 * @author jztb88
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class DisplayTagInterceptor implements HandlerInterceptor {

    final static Log log = LogFactory.getLog(DisplayTagInterceptor.class);
    /* (non-Javadoc)
     * @see 
org.springframework.web.servlet.HandlerInterceptor#preHandle(javax.servlet.http.HttpServletRequest,
 javax.servlet.http.HttpServletResponse, java.lang.Object)
     */
    public boolean preHandle(HttpServletRequest servletRequest, 
HttpServletResponse servletResponse, Object handler) throws Exception {
        if (servletRequest.getParameter(TableTagParameters.PARAMETER_EXPORTING) 
== null)
        {
            if (log.isDebugEnabled())
            {
                log.debug("Filter has been called, but PARAMETER_EXPORTING 
parameter has not been found.");
            }
            return true;
        }

        HttpServletRequest request = (HttpServletRequest) servletRequest;

        BufferedResponseWrapper wrapper = new 
BufferedResponseWrapper((HttpServletResponse) servletResponse);

        // In a portlet environment, you do not have direct access to the 
actual request object, so any attribute that
        // is added will not be visible outside of your portlet. So instead, 
users MUST append to the StringBuffer, so
        // that the portlets do not have to bind a new attribute to the request.
        request.setAttribute(TableTag.FILTER_CONTENT_OVERRIDE_BODY, new 
StringBuffer(8096));
        request.setAttribute(TableTag.FILTER_CONTENT_OVERRIDE_TYPE, new 
StringBuffer(80));
        request.setAttribute(TableTag.FILTER_CONTENT_OVERRIDE_FILENAME, new 
StringBuffer(80));

        HandlerAdapter handlerAdaptor = new SimpleControllerHandlerAdapter();
        handlerAdaptor.handle(request,wrapper,handler);
       

        String pageContent;
        String contentType;
        StringBuffer buf = (StringBuffer) 
request.getAttribute(TableTag.FILTER_CONTENT_OVERRIDE_BODY);
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        String characterEncoding = resp.getCharacterEncoding();
        if (characterEncoding != null)
        {
            characterEncoding = "; charset=" + characterEncoding;
        }
        if (buf != null && buf.length() > 0)
        {
            pageContent = buf.toString();
            contentType = 
ObjectUtils.toString(request.getAttribute(TableTag.FILTER_CONTENT_OVERRIDE_TYPE));
            if (log.isDebugEnabled())
            {
                log.debug("Overriding output, writing new output with content 
type " + contentType);
            }

            StringBuffer filename = (StringBuffer) 
request.getAttribute(TableTag.FILTER_CONTENT_OVERRIDE_FILENAME);

            if (filename != null && StringUtils.isNotEmpty(filename.toString()))
            {
                if (log.isDebugEnabled())
                {
                    log.debug("Filename specified as " + filename);
                }
                resp.setHeader("Content-Disposition", "attachment; filename=\"" 
+ filename + "\"");
            }
        }
        else
        {
            log.debug("NOT overriding input. ");
            pageContent = wrapper.toString();
            contentType = wrapper.getContentType();
        }

        if (contentType != null)
        {
            if (contentType.indexOf("charset") > -1)
            {
                // charset is already specified (see #921811)
                servletResponse.setContentType(contentType);
            }
            else
            {
                servletResponse.setContentType(contentType + 
StringUtils.defaultString(characterEncoding));
            }
        }
        servletResponse.setContentLength(pageContent.length());

        PrintWriter out = servletResponse.getWriter();
        out.write(pageContent);
        out.close();
        return false;
    }

    /* (non-Javadoc)
     * @see 
org.springframework.web.servlet.HandlerInterceptor#postHandle(javax.servlet.http.HttpServletRequest,
 javax.servlet.http.HttpServletResponse, java.lang.Object, 
org.springframework.web.servlet.ModelAndView)
     */
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, 
Object arg2, ModelAndView arg3) throws Exception {
        // Nothing to do
       
    }

    /* (non-Javadoc)
     * @see 
org.springframework.web.servlet.HandlerInterceptor#afterCompletion(javax.servlet.http.HttpServletRequest,
 javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)
     */
    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse 
arg1, Object arg2, Exception arg3) throws Exception {
//      Nothing to do       
    }

}



---------------------------------------------------------------------
JIRA INFORMATION:
This message is automatically generated by JIRA.

If you think it was sent incorrectly contact one of the administrators:
   http://jira.codehaus.org/secure/Administrators.jspa

If you want more information on JIRA, or have a bug to report see:
   http://www.atlassian.com/software/jira



-------------------------------------------------------
This SF.Net email is sponsored by: InterSystems CACHE
FREE OODBMS DOWNLOAD - A multidimensional database that combines
robust object and relational technologies, making it a perfect match
for Java, C++,COM, XML, ODBC and JDBC. www.intersystems.com/match8
_______________________________________________
displaytag-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/displaytag-devel

Reply via email to