[66] | 1 | /* |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
---|
| 3 | * contributor license agreements. See the NOTICE file distributed with |
---|
| 4 | * this work for additional information regarding copyright ownership. |
---|
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
---|
| 6 | * (the "License"); you may not use this file except in compliance with |
---|
| 7 | * the License. You may obtain a copy of the License at |
---|
| 8 | * |
---|
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 10 | * |
---|
| 11 | * Unless required by applicable law or agreed to in writing, software |
---|
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 14 | * See the License for the specific language governing permissions and |
---|
| 15 | * limitations under the License. |
---|
| 16 | */ |
---|
| 17 | package compressionFilters; |
---|
| 18 | |
---|
| 19 | import java.io.IOException; |
---|
| 20 | import java.io.OutputStreamWriter; |
---|
| 21 | import java.io.PrintWriter; |
---|
| 22 | import javax.servlet.ServletOutputStream; |
---|
| 23 | import javax.servlet.http.HttpServletResponse; |
---|
| 24 | import javax.servlet.http.HttpServletResponseWrapper; |
---|
| 25 | |
---|
| 26 | /** |
---|
| 27 | * Implementation of <b>HttpServletResponseWrapper</b> that works with |
---|
| 28 | * the CompressionServletResponseStream implementation.. |
---|
| 29 | * |
---|
| 30 | * @author Amy Roh |
---|
| 31 | * @author Dmitri Valdin |
---|
| 32 | * @version $Revision: 500668 $, $Date: 2007-01-28 00:07:51 +0100 (Sun, 28 Jan 2007) $ |
---|
| 33 | */ |
---|
| 34 | |
---|
| 35 | public class CompressionServletResponseWrapper extends HttpServletResponseWrapper { |
---|
| 36 | |
---|
| 37 | // ----------------------------------------------------- Constructor |
---|
| 38 | |
---|
| 39 | /** |
---|
| 40 | * Calls the parent constructor which creates a ServletResponse adaptor |
---|
| 41 | * wrapping the given response object. |
---|
| 42 | */ |
---|
| 43 | |
---|
| 44 | public CompressionServletResponseWrapper(HttpServletResponse response) { |
---|
| 45 | super(response); |
---|
| 46 | origResponse = response; |
---|
| 47 | if (debug > 1) { |
---|
| 48 | System.out.println("CompressionServletResponseWrapper constructor gets called"); |
---|
| 49 | } |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | // ----------------------------------------------------- Instance Variables |
---|
| 54 | |
---|
| 55 | /** |
---|
| 56 | * Original response |
---|
| 57 | */ |
---|
| 58 | |
---|
| 59 | protected HttpServletResponse origResponse = null; |
---|
| 60 | |
---|
| 61 | /** |
---|
| 62 | * Descriptive information about this Response implementation. |
---|
| 63 | */ |
---|
| 64 | |
---|
| 65 | protected static final String info = "CompressionServletResponseWrapper"; |
---|
| 66 | |
---|
| 67 | /** |
---|
| 68 | * The ServletOutputStream that has been returned by |
---|
| 69 | * <code>getOutputStream()</code>, if any. |
---|
| 70 | */ |
---|
| 71 | |
---|
| 72 | protected ServletOutputStream stream = null; |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | /** |
---|
| 76 | * The PrintWriter that has been returned by |
---|
| 77 | * <code>getWriter()</code>, if any. |
---|
| 78 | */ |
---|
| 79 | |
---|
| 80 | protected PrintWriter writer = null; |
---|
| 81 | |
---|
| 82 | /** |
---|
| 83 | * The threshold number to compress |
---|
| 84 | */ |
---|
| 85 | protected int threshold = 0; |
---|
| 86 | |
---|
| 87 | /** |
---|
| 88 | * Debug level |
---|
| 89 | */ |
---|
| 90 | private int debug = 0; |
---|
| 91 | |
---|
| 92 | /** |
---|
| 93 | * Content type |
---|
| 94 | */ |
---|
| 95 | protected String contentType = null; |
---|
| 96 | |
---|
| 97 | // --------------------------------------------------------- Public Methods |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | /** |
---|
| 101 | * Set content type |
---|
| 102 | */ |
---|
| 103 | public void setContentType(String contentType) { |
---|
| 104 | if (debug > 1) { |
---|
| 105 | System.out.println("setContentType to "+contentType); |
---|
| 106 | } |
---|
| 107 | this.contentType = contentType; |
---|
| 108 | origResponse.setContentType(contentType); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | |
---|
| 112 | /** |
---|
| 113 | * Set threshold number |
---|
| 114 | */ |
---|
| 115 | public void setCompressionThreshold(int threshold) { |
---|
| 116 | if (debug > 1) { |
---|
| 117 | System.out.println("setCompressionThreshold to " + threshold); |
---|
| 118 | } |
---|
| 119 | this.threshold = threshold; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | /** |
---|
| 124 | * Set debug level |
---|
| 125 | */ |
---|
| 126 | public void setDebugLevel(int debug) { |
---|
| 127 | this.debug = debug; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | |
---|
| 131 | /** |
---|
| 132 | * Create and return a ServletOutputStream to write the content |
---|
| 133 | * associated with this Response. |
---|
| 134 | * |
---|
| 135 | * @exception IOException if an input/output error occurs |
---|
| 136 | */ |
---|
| 137 | public ServletOutputStream createOutputStream() throws IOException { |
---|
| 138 | if (debug > 1) { |
---|
| 139 | System.out.println("createOutputStream gets called"); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | CompressionResponseStream stream = new CompressionResponseStream(origResponse); |
---|
| 143 | stream.setDebugLevel(debug); |
---|
| 144 | stream.setBuffer(threshold); |
---|
| 145 | |
---|
| 146 | return stream; |
---|
| 147 | |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | |
---|
| 151 | /** |
---|
| 152 | * Finish a response. |
---|
| 153 | */ |
---|
| 154 | public void finishResponse() { |
---|
| 155 | try { |
---|
| 156 | if (writer != null) { |
---|
| 157 | writer.close(); |
---|
| 158 | } else { |
---|
| 159 | if (stream != null) |
---|
| 160 | stream.close(); |
---|
| 161 | } |
---|
| 162 | } catch (IOException e) { |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | |
---|
| 167 | // ------------------------------------------------ ServletResponse Methods |
---|
| 168 | |
---|
| 169 | |
---|
| 170 | /** |
---|
| 171 | * Flush the buffer and commit this response. |
---|
| 172 | * |
---|
| 173 | * @exception IOException if an input/output error occurs |
---|
| 174 | */ |
---|
| 175 | public void flushBuffer() throws IOException { |
---|
| 176 | if (debug > 1) { |
---|
| 177 | System.out.println("flush buffer @ CompressionServletResponseWrapper"); |
---|
| 178 | } |
---|
| 179 | ((CompressionResponseStream)stream).flush(); |
---|
| 180 | |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | /** |
---|
| 184 | * Return the servlet output stream associated with this Response. |
---|
| 185 | * |
---|
| 186 | * @exception IllegalStateException if <code>getWriter</code> has |
---|
| 187 | * already been called for this response |
---|
| 188 | * @exception IOException if an input/output error occurs |
---|
| 189 | */ |
---|
| 190 | public ServletOutputStream getOutputStream() throws IOException { |
---|
| 191 | |
---|
| 192 | if (writer != null) |
---|
| 193 | throw new IllegalStateException("getWriter() has already been called for this response"); |
---|
| 194 | |
---|
| 195 | if (stream == null) |
---|
| 196 | stream = createOutputStream(); |
---|
| 197 | if (debug > 1) { |
---|
| 198 | System.out.println("stream is set to "+stream+" in getOutputStream"); |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | return (stream); |
---|
| 202 | |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | /** |
---|
| 206 | * Return the writer associated with this Response. |
---|
| 207 | * |
---|
| 208 | * @exception IllegalStateException if <code>getOutputStream</code> has |
---|
| 209 | * already been called for this response |
---|
| 210 | * @exception IOException if an input/output error occurs |
---|
| 211 | */ |
---|
| 212 | public PrintWriter getWriter() throws IOException { |
---|
| 213 | |
---|
| 214 | if (writer != null) |
---|
| 215 | return (writer); |
---|
| 216 | |
---|
| 217 | if (stream != null) |
---|
| 218 | throw new IllegalStateException("getOutputStream() has already been called for this response"); |
---|
| 219 | |
---|
| 220 | stream = createOutputStream(); |
---|
| 221 | if (debug > 1) { |
---|
| 222 | System.out.println("stream is set to "+stream+" in getWriter"); |
---|
| 223 | } |
---|
| 224 | //String charset = getCharsetFromContentType(contentType); |
---|
| 225 | String charEnc = origResponse.getCharacterEncoding(); |
---|
| 226 | if (debug > 1) { |
---|
| 227 | System.out.println("character encoding is " + charEnc); |
---|
| 228 | } |
---|
| 229 | // HttpServletResponse.getCharacterEncoding() shouldn't return null |
---|
| 230 | // according the spec, so feel free to remove that "if" |
---|
| 231 | if (charEnc != null) { |
---|
| 232 | writer = new PrintWriter(new OutputStreamWriter(stream, charEnc)); |
---|
| 233 | } else { |
---|
| 234 | writer = new PrintWriter(stream); |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | return (writer); |
---|
| 238 | |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | |
---|
| 242 | public void setContentLength(int length) { |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | } |
---|