Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.apache.catalina.ssi;
 
 
A HttpServletResponseWrapper, used from SSIServletExternalResolver

Author(s):
Bip Thelin
David Becker
Version:
$Revision: 1473 $, $Date: 2010-05-17 19:46:58 +0200 (Mon, 17 May 2010) $
 
 public class ResponseIncludeWrapper extends HttpServletResponseWrapper {
    
The names of some headers we want to capture.
 
     private static final String CONTENT_TYPE = "content-type";
     private static final String LAST_MODIFIED = "last-modified";
     private static final DateFormat RFC1123_FORMAT;
     private final static String RFC1123_PATTERN = "EEE, dd MMM yyyy HH:mm:ss z";
 
     protected long lastModified = -1;
     private String contentType = null;

    
Our ServletOutputStream
 
     protected PrintWriter printWriter;
     
     private ServletContext context;
     private HttpServletRequest request;
 
     static {
         .setTimeZone(TimeZone.getTimeZone("GMT"));
     }
    
    
Initialize our wrapper with the current HttpServletResponse and ServletOutputStream.

Parameters:
context The servlet context
request The HttpServletResponse to use
response The response to use
captureServletOutputStream The ServletOutputStream to use
 
     public ResponseIncludeWrapper(ServletContext context
             HttpServletRequest requestHttpServletResponse response,
            ServletOutputStream captureServletOutputStream) {
         super(response);
         this. = context;
         this. = request;
         this. = captureServletOutputStream;
     }


    
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.
 
     public void flushOutputStreamOrWriter() throws IOException {
         if ( != null) {
             .flush();
         }
         if ( != null) {
             .flush();
         }
    }


    
Return a printwriter, throws and exception if a OutputStream already been returned.

Returns:
a PrintWriter object
Throws:
java.io.IOException if the outputstream already been called
    public PrintWriter getWriter() throws java.io.IOException {
        if ( == null) {
            if ( == null) {
                setCharacterEncoding(getCharacterEncoding());
                 = new PrintWriter(
                        new OutputStreamWriter(,
                                               getCharacterEncoding()));
            }
            return ;
        }
        throw new IllegalStateException();
    }


    
Return a OutputStream, throws and exception if a printwriter already been returned.

Returns:
a OutputStream object
Throws:
java.io.IOException if the printwriter already been called
        if ( == null) {
            if ( == null) {
                 = ;
            }
            return ;
        }
        throw new IllegalStateException();
    }
    
    
    
Returns the value of the last-modified header field. The result is the number of milliseconds since January 1, 1970 GMT.

Returns:
the date the resource referenced by this ResponseIncludeWrapper was last modified, or -1 if not known.
    public long getLastModified() {                                                                                                                                                           
        if ( == -1) {
            // javadocs say to return -1 if date not known, if you want another
            // default, put it here
            return -1;
        }
        return ;
    }

    
Sets the value of the last-modified header field.

Parameters:
lastModified The number of milliseconds since January 1, 1970 GMT.
    public void setLastModified(long lastModified) {
        this. = lastModified;
                lastModified);
    }

    
Returns the value of the content-type header field.

Returns:
the content type of the resource referenced by this ResponseIncludeWrapper, or null if not known.
    public String getContentType() {
        if ( == null) {
            String url = .getRequestURI();
            String mime = .getMimeType(url);
            if (mime != null)
            {
                setContentType(mime);
            }
            else
            {
                // return a safe value
               setContentType("application/x-octet-stream");
            }
        }
        return ;
    }
    
    
Sets the value of the content-type header field.

Parameters:
mime a mime type
    public void setContentType(String mime) {
         = mime;
        if ( != null) {
            getResponse().setContentType();
        }
    }
    public void addDateHeader(String namelong value) {
        super.addDateHeader(namevalue);
        String lname = name.toLowerCase(.);
        if (lname.equals()) {
             = value;
        }
    }
    public void addHeader(String nameString value) {
        super.addHeader(namevalue);
        String lname = name.toLowerCase(.);
        if (lname.equals()) {
            try {
                synchronized() {
                     = .parse(value).getTime();
                }
            } catch (Throwable ignore) { }
        } else if (lname.equals()) {
             = value;
        }
    }
    public void setDateHeader(String namelong value) {
        super.setDateHeader(namevalue);
        String lname = name.toLowerCase(.);
        if (lname.equals()) {
             = value;
        }
    }
    public void setHeader(String nameString value) {
        super.setHeader(namevalue);
        String lname = name.toLowerCase(.);
        if (lname.equals()) {
            try {
                synchronized() {
                     = .parse(value).getTime();
                }
            } catch (Throwable ignore) { }
        }
        else if (lname.equals())
        {
             = value;
        }
    }
New to GrepCode? Check out our FAQ X