FileDocCategorySizeDatePackage
ResponseIncludeWrapper.javaAPI DocApache Tomcat 6.0.148351Fri Jul 20 04:20:34 BST 2007org.apache.catalina.ssi

ResponseIncludeWrapper

public class ResponseIncludeWrapper extends HttpServletResponseWrapper
A HttpServletResponseWrapper, used from SSIServletExternalResolver
author
Bip Thelin
author
David Becker
version
$Revision: 467222 $, $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $

Fields Summary
private static final String
CONTENT_TYPE
The names of some headers we want to capture.
private static final String
LAST_MODIFIED
protected long
lastModified
private String
contentType
protected ServletOutputStream
captureServletOutputStream
Our ServletOutputStream
protected ServletOutputStream
servletOutputStream
protected PrintWriter
printWriter
private ServletContext
context
private HttpServletRequest
request
Constructors Summary
public ResponseIncludeWrapper(ServletContext context, HttpServletRequest request, HttpServletResponse response, ServletOutputStream captureServletOutputStream)
Initialize our wrapper with the current HttpServletResponse and ServletOutputStream.

param
context The servlet context
param
request The HttpServletResponse to use
param
response The response to use
param
captureServletOutputStream The ServletOutputStream to use



                                          
       
    		   
             
        super(response);
        this.context = context;
        this.request = request;
        this.captureServletOutputStream = captureServletOutputStream;
    
Methods Summary
public voidaddDateHeader(java.lang.String name, long value)

        super.addDateHeader(name, value);
        String lname = name.toLowerCase();
        if (lname.equals(LAST_MODIFIED)) {
            lastModified = value;
        }
    
public voidaddHeader(java.lang.String name, java.lang.String value)

        super.addHeader(name, value);
        String lname = name.toLowerCase();
        if (lname.equals(LAST_MODIFIED)) {
            try {
                lastModified = DateTool.rfc1123Format.parse(value).getTime();
            } catch (Throwable ignore) { }
        } else if (lname.equals(CONTENT_TYPE)) {
            contentType = value;
        }
    
public voidflushOutputStreamOrWriter()
Flush the servletOutputStream or printWriter ( only one will be non-null ) This must be called after a requestDispatcher.include, since we can't assume that the included servlet flushed its stream.

        if (servletOutputStream != null) {
            servletOutputStream.flush();
        }
        if (printWriter != null) {
            printWriter.flush();
        }
    
public java.lang.StringgetContentType()
Returns the value of the content-type header field.

return
the content type of the resource referenced by this ResponseIncludeWrapper, or null if not known.

        if (contentType == null) {
            String url = request.getRequestURI();
            String mime = context.getMimeType(url);
            if (mime != null)
            {
                setContentType(mime);
            }
            else
            {
            	// return a safe value
               setContentType("application/x-octet-stream");
            }
        }
        return contentType;
    
public longgetLastModified()
Returns the value of the last-modified header field. The result is the number of milliseconds since January 1, 1970 GMT.

return
the date the resource referenced by this ResponseIncludeWrapper was last modified, or -1 if not known.

                                                                                                                                                           
        if (lastModified == -1) {
            // javadocs say to return -1 if date not known, if you want another
            // default, put it here
            return -1;
        }
        return lastModified;
    
public javax.servlet.ServletOutputStreamgetOutputStream()
Return a OutputStream, throws and exception if a printwriter already been returned.

return
a OutputStream object
exception
java.io.IOException if the printwriter already been called

        if (printWriter == null) {
            if (servletOutputStream == null) {
                servletOutputStream = captureServletOutputStream;
            }
            return servletOutputStream;
        }
        throw new IllegalStateException();
    
public java.io.PrintWritergetWriter()
Return a printwriter, throws and exception if a OutputStream already been returned.

return
a PrintWriter object
exception
java.io.IOException if the outputstream already been called

        if (servletOutputStream == null) {
            if (printWriter == null) {
                setCharacterEncoding(getCharacterEncoding());
                printWriter = new PrintWriter(
                        new OutputStreamWriter(captureServletOutputStream,
                                               getCharacterEncoding()));
            }
            return printWriter;
        }
        throw new IllegalStateException();
    
public voidsetContentType(java.lang.String mime)
Sets the value of the content-type header field.

param
mime a mime type

        contentType = mime;
        if (contentType != null) {
            getResponse().setContentType(contentType);
        }
    
public voidsetDateHeader(java.lang.String name, long value)

        super.setDateHeader(name, value);
        String lname = name.toLowerCase();
        if (lname.equals(LAST_MODIFIED)) {
            lastModified = value;
        }
    
public voidsetHeader(java.lang.String name, java.lang.String value)

        super.setHeader(name, value);
        String lname = name.toLowerCase();
        if (lname.equals(LAST_MODIFIED)) {
            try {
                lastModified = DateTool.rfc1123Format.parse(value).getTime();
            } catch (Throwable ignore) { }
        }
        else if (lname.equals(CONTENT_TYPE))
        {
            contentType = value;
        }
    
public voidsetLastModified(long lastModified)
Sets the value of the last-modified header field.

param
lastModified The number of milliseconds since January 1, 1970 GMT.

        this.lastModified = lastModified;
        ((HttpServletResponse) getResponse()).setDateHeader(LAST_MODIFIED,
                lastModified);