View Javadoc

1   /*
2    * $Id: GZIPResponseWrapper.java 122 2004-11-01 13:06:54Z josem $
3    *
4    * Tarsis
5    * Copyright (C) 2002 Talika Open Source Group
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   *
21   */
22  
23  package org.talika.tarsis.filters.gzip;
24  
25  import javax.servlet.http.HttpServletResponse;
26  import javax.servlet.http.HttpServletResponseWrapper;
27  import javax.servlet.ServletOutputStream;
28  import java.io.PrintWriter;
29  import java.io.IOException;
30  
31  /**
32   * Provides a convenient implementation of the <code>HttpServletResponse</code>
33   * interface that permits us replace default <code>ServletOutputStream</code>
34   * by our <code>GZIPServletOutputStream</code>.
35   *
36   * @author  Jose M. Palomar
37   * @version $Revision: 122 $
38   */
39  public final class GZIPResponseWrapper extends HttpServletResponseWrapper {
40  
41      // Constants
42      /**
43       * Default buffer size.
44       */
45      private static final int DEFAULT_BUFFER_SIZE = 4096;
46  
47      // Fields
48      /**
49       * Response <code>PrintWriter</code>.
50       */
51      private PrintWriter writer;
52  
53      /**
54       * Response <code>ServletOutputStream</code>.
55       */
56      private ServletOutputStream out;
57  
58      /**
59       * Buffer size.
60       */
61      private int bufferSize;
62  
63      // Constructors
64      /**
65       * Constructs a response adaptor wrapping the given response.
66       *
67       * @param response HttpServletResponse reponse to be wrapped.
68       */
69      public GZIPResponseWrapper(HttpServletResponse response) {
70          this(response, DEFAULT_BUFFER_SIZE);
71      }
72  
73      /**
74       * Constructs a response adaptor wrapping the given response and buffer size.
75       *
76       * @param response HttpServletResponse reponse to be wrapped.
77       * @param bufferSize int buffer size.
78       */
79      public GZIPResponseWrapper(HttpServletResponse response, int bufferSize) {
80          super(response);
81          this.bufferSize = bufferSize;
82          response.addHeader("Content-Encoding", "gzip");
83      }
84  
85      // Methods
86      /**
87       * Forces any content in the buffer to be written to the client. A call to this
88       * method automatically commits the response, meaning the status code and headers
89       * will be written.
90       *
91       * @throws IOException if an input or output exception occurs.
92       * @see javax.servlet.ServletResponse#flushBuffer()
93       */
94      public void flushBuffer() throws IOException {
95          if (out != null) {
96              out.flush();
97          }
98      }
99  
100     /**
101      * Returns a <code>ServletOutputStream</code> suitable for writing binary data in
102      * the response. The servlet container does not encode the binary data.
103      *
104      * @return ServletOutputStream a <code>ServletOutputStream</code> for writing
105      * binary data.
106      * @throws IOException if an input or output exception occurs.
107      * @see javax.servlet.ServletResponse#getOutputStream()
108      */
109     public ServletOutputStream getOutputStream() throws IOException {
110 
111         if (writer != null) {
112             throw new IllegalStateException("getWriter() has already been called for this response");
113         }
114 
115         if (out == null) {
116             out = new GZIPServletOutputStream(getResponse().getOutputStream(),
117                                               this.bufferSize);
118         }
119 
120         return out;
121     }
122 
123     /**
124      * Returns a PrintWriter object that can send character text to the client.
125      *
126      * @return PrintWriter a <code>PrintWriter</code> object that can return
127      * character data to the client.
128      * @throws IOException if an input or output exception occurs.
129      * @see javax.servlet.ServletResponse#getWriter()
130      */
131     public PrintWriter getWriter() throws IOException {
132 
133         if (writer != null) {
134             return writer;
135         }
136 
137         if (out != null) {
138             throw new IllegalStateException("getOutputStream() has already been called for this response");
139         }
140 
141         writer = new PrintWriter(getOutputStream());
142 
143         return writer;
144 
145     }
146 
147     /**
148      * Sets the length of the content body in the response In HTTP servlets, this
149      * method sets the HTTP Content-Length header.
150      *
151      * @param length int an integer specifying the length of the content being
152      * returned to the client; sets the Content-Length header
153      * @see javax.servlet.ServletResponse#setContentLength(int)
154      */
155     public void setContentLength(int length) {
156     }
157 
158 }