View Javadoc

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