Announcement Announcement Module
Collapse
No announcement yet.
Displaytag Interceptor for use with servlet 2.2 containers Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Displaytag Interceptor for use with servlet 2.2 containers

    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        
        }
    
    }

  • #2
    BTW ignore copyright header.My IDE put that in automatically

    BTW ignore copyright header.My IDE put that in automatically

    Comment


    • #3
      I haven't look at this in detail, but it builds on DisplayTag. As we don't ship DisplayTag in the Spring distribution, it wouldn't be appropriate to ship classes that build on it.

      You could donate it to the DisplayTag project. Matt Raible certainly wouldn't mind stuff that eases Spring/DisplayTag integration :-)

      Juergen

      Comment

      Working...
      X