View Javadoc

1   /*
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.rest.filter;
22  
23  import java.io.IOException;
24  import java.io.PrintWriter;
25  
26  import javax.servlet.ServletOutputStream;
27  import javax.servlet.http.HttpServletResponse;
28  import javax.servlet.http.HttpServletResponseWrapper;
29  
30  public class GZIPResponseWrapper extends HttpServletResponseWrapper {
31    private HttpServletResponse response;
32    private ServletOutputStream os;
33    private PrintWriter writer;
34    private boolean compress = true;
35  
36    public GZIPResponseWrapper(HttpServletResponse response) {
37      super(response);
38      this.response = response;
39    }
40  
41    @Override
42    public void setStatus(int status) {
43      super.setStatus(status);
44      if (status < 200 || status >= 300) {
45        compress = false;
46      }
47    }
48  
49    @Override
50    public void addHeader(String name, String value) {
51      if (!"content-length".equalsIgnoreCase(name)) {
52        super.addHeader(name, value);
53      }
54    }
55  
56    @Override
57    public void setContentLength(int length) {
58      // do nothing
59    }
60  
61    @Override
62    public void setIntHeader(String name, int value) {
63      if (!"content-length".equalsIgnoreCase(name)) {
64        super.setIntHeader(name, value);
65      }
66    }
67  
68    @Override
69    public void setHeader(String name, String value) {
70      if (!"content-length".equalsIgnoreCase(name)) {
71        super.setHeader(name, value);
72      }
73    }
74  
75    @Override
76    public void flushBuffer() throws IOException {
77      if (writer != null) {
78        writer.flush();
79      }
80      if (os != null && (os instanceof GZIPResponseStream)) {
81        ((GZIPResponseStream)os).finish();
82      } else {
83        getResponse().flushBuffer();
84      }
85    }
86  
87    @Override
88    public void reset() {
89      super.reset();
90      if (os != null && (os instanceof GZIPResponseStream)) {
91        ((GZIPResponseStream)os).resetBuffer();
92      }
93      writer = null;
94      os = null;
95      compress = true;
96    }
97  
98    @Override
99    public void resetBuffer() {
100     super.resetBuffer();
101     if (os != null && (os instanceof GZIPResponseStream)) {
102       ((GZIPResponseStream)os).resetBuffer();
103     }
104     writer = null;
105     os = null;
106   }
107 
108   @Override
109   public void sendError(int status, String msg) throws IOException {
110     resetBuffer();
111     super.sendError(status, msg);
112   }
113 
114   @Override
115   public void sendError(int status) throws IOException {
116     resetBuffer();
117     super.sendError(status);
118   }
119 
120   @Override
121   public void sendRedirect(String location) throws IOException {
122     resetBuffer();
123     super.sendRedirect(location);
124   }
125 
126   @Override
127   public ServletOutputStream getOutputStream() throws IOException {
128     if (os == null) {
129       if (!response.isCommitted() && compress) {
130         os = (ServletOutputStream)new GZIPResponseStream(response);
131       } else {
132         os = response.getOutputStream();
133       }
134     }
135     return os;
136   }
137 
138   @Override
139   public PrintWriter getWriter() throws IOException {
140     if (writer == null) {
141       writer = new PrintWriter(getOutputStream());
142     }
143     return writer;
144   }
145 }