Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright (c) 1997-2011 Oracle and/or its affiliates. All rights reserved.
   *
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
   * may not use this file except in compliance with the License.  You can
  * obtain a copy of the License at
  * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
  * or packager/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  *
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at packager/legal/LICENSE.txt.
  *
  * GPL Classpath Exception:
  * Oracle designates this particular file as subject to the "Classpath"
  * exception as provided by Oracle in the GPL Version 2 section of the License
  * file that accompanied this code.
  *
  * Modifications:
  * If applicable, add the following below the License Header, with the fields
  * enclosed by brackets [] replaced by your own identifying information:
  * "Portions Copyright [year] [name of copyright owner]"
  *
  * Contributor(s):
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
  */
 
 package com.sun.appserv.web.cache.filter;
 
 // import org.apache.catalina.Context;
 
a wrapper to HttpServletResponse to cache the outbound headers and content

 
     private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
 
     int statusCode = .;

    
The HTTP headers explicitly added via addHeader(), but not including those to be added with setContentLength(), setContentType(), and so on. This collection is keyed by the header name, and the elements are ArrayLists containing the associated values that have been set.
 
         new HashMap<StringArrayList<String>>();
    
cache all the set/addDateHeader calls
 
         new HashMap<StringArrayList<Long>>();
    
The set of Cookies associated with this Response.
 
     ArrayList<Cookiecookies = new ArrayList<Cookie>();
 
     String contentType;
     Locale locale;

    
Error flag. True if the response runs into an error. Should not treat the response to be in the error state if the servlet doesn't get the OutpuStream or the Writer.
 
     // IT 12891
     boolean error = false;

    
OutputStream and PrintWriter objects for this response.
 
    PrintWriter writer;

    
Constructs a response adaptor wrapping the given response.

Throws:
java.lang.IllegalArgumentException if the response is null
    public CachingResponseWrapper(HttpServletResponse response) {
	    super(response);
    }
    
    
Create and return a ServletOutputStream to write the content associated with this Response.

Throws:
java.io.IOException if an input/output error occurs
                                throws IOException {
        return new CachingOutputStreamWrapper();
    }

    
Return the servlet output stream associated with this Response.

Throws:
java.lang.IllegalStateException if getWriter has already been called for this response
java.io.IOException if an input/output error occurs
    public ServletOutputStream getOutputStream() throws IOException {
        if ( != null)
            throw new IllegalStateException ("getOutputStream<>getWriter");
        if ( == null) {
             = createCachingOutputStreamWrapper();
        }
        return (ServletOutputStream);
    }  
      
    
Return the writer associated with this Response.

Throws:
java.lang.IllegalStateException if getOutputStream has already been called for this response
java.io.IOException if an input/output error occurs
    public PrintWriter getWriter() throws IOException {
        if ( != null)
            return ();
        if ( != null)
            throw new IllegalStateException ("getWriter<>getOutputStream");
        OutputStreamWriter osw =
            new OutputStreamWriter(getCharacterEncoding());
         = new PrintWriter(osw);
        return ();
	}
    
    
Set the content length (in bytes) for this Response.

Parameters:
len The new content length
    public void setContentLength(int len) {
        super.setContentLength(len);
        this. = len;
    }
    
    
Set the content type for this Response.

Parameters:
type The new content type
    public void setContentType(String type) {
        super.setContentType(type);
        this. = type;
    }
    
    
Set the Locale that is appropriate for this response, including setting the appropriate character encoding.

Parameters:
locale The new locale
    public void setLocale(Locale locale) {
        super.setLocale(locale);
        this. = locale;
    }

    
The default behavior of this method is to call addCookie(Cookie cookie) on the wrapped response object.
    public void addCookie(Cookie cookie) {
        super.addCookie(cookie);
        synchronized () {
            .add(cookie);
        }
    }
    
    
Set the specified header to the specified value.

Parameters:
name Name of the header to set
value Value to be set
    public void setHeader(String nameString value) {
        super.setHeader(namevalue);
        ArrayList<Stringvalues = new ArrayList<String>();
        values.add(value);
        synchronized () {
            .put(namevalues);
        }
    }

    
Set the specified integer header to the specified value.

Parameters:
name Name of the header to set
value Integer value to be set
    public void setIntHeader(String nameint value) {
        setHeader(name"" + value);
    }

    
Add the specified header to the specified value.

Parameters:
name Name of the header to set
value Value to be set
    public void addHeader(String nameString value) {
        super.addHeader(namevalue);
        ArrayList<Stringvalues = .get(name);
        if (values == null) {
            values = new ArrayList<String>();
            synchronized () {
                .put(namevalues);
            }
        }
        values.add(value);
    }

    
Add the specified integer header to the specified value.

Parameters:
name Name of the header to set
value Integer value to be set
    public void addIntHeader(String nameint value) {
        addHeader(name"" + value);
    }

    
Set the specified date header to the specified value.

Parameters:
name Name of the header to set
value Date value to be set
    public void setDateHeader(String namelong value) {
        super.setDateHeader(namevalue);
        ArrayList<Longvalues = new ArrayList<Long>();
        values.add(Long.valueOf(value));
        synchronized () {
            .put(namevalues);
        }
    }

    
Add the specified date header to the specified value.

Parameters:
name Name of the header to set
value Date value to be set
    public void addDateHeader(String namelong value) {
        super.addDateHeader(namevalue);
        ArrayList<Longvalues = .get(name);
        if (values == null) {
            values = new ArrayList<Long>();
            synchronized () {
                .put(namevalues);
            }
        }
        values.add(Long.valueOf(value));
    }

    
Set the HTTP status to be returned with this response.

Parameters:
sc The new HTTP status
    public void setStatus(int sc) {
        super.setStatus(sc);
        this. = sc;
    }
 
    
Send an error response with the specified status and a default message.

Parameters:
status HTTP status code to send
Throws:
java.lang.IllegalStateException if this response has already been committed
java.io.IOException if an input/output error occurs
    public void sendError(int statusthrows IOException {
        super.sendError(status);
         = true;
    }

    
Send an error response with the specified status and message.

Parameters:
status HTTP status code to send
message Corresponding message to send
Throws:
java.lang.IllegalStateException if this response has already been committed
java.io.IOException if an input/output error occurs
    public void sendError(int statusString messagethrows IOException {
        super.sendError(statusmessage);
         = true;
    }

    
has the response been set to error
    public boolean isError() {
        return ;
    }

    
return the Expires: date header value
    public Long getExpiresDateHeader() {
        Long expire = null;
        ArrayList<LongexpireList = .get("Expires");
        if (expireList != null && expireList.size() > 0) {
            expire = expireList.get(0);
        }
        return expire;
    }

    
called by doFilter to cache the response that was just sent out

Returns:
the entry with cached response headers and body.
    public HttpCacheEntry cacheResponse() throws IOException {
        // create a new entry
        HttpCacheEntry entry = new HttpCacheEntry();
        entry.responseHeaders = ;
        entry.dateHeaders = ;
        entry.cookies = ;
        entry.contentLength = ;
        entry.contentType = ;
        entry.locale = ;
        entry.statusCode = ;
        // flush the writer??
        if ( != null) {
            .flush();
        }
        // IT 12891
        entry.bytes = (( != null)? .getBytes() : );
        return entry;
    }
    
    
clear the contents of this wrapper
    public void clear() {
         = null;
         = null;
    }
New to GrepCode? Check out our FAQ X