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 GZIPResponseStream 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      if (status < 200 || status >= 300) {
44        compress = false;
45      }
46    }
47  
48    @Override
49    public void addHeader(String name, String value) {
50      if (!"content-length".equalsIgnoreCase(name)) {
51        super.addHeader(name, value);
52      }
53    }
54  
55    @Override
56    public void setContentLength(int length) {
57      // do nothing
58    }
59  
60    @Override
61    public void setIntHeader(String name, int value) {
62      if (!"content-length".equalsIgnoreCase(name)) {
63        super.setIntHeader(name, value);
64      }
65    }
66  
67    @Override
68    public void setHeader(String name, String value) {
69      if (!"content-length".equalsIgnoreCase(name)) {
70        super.setHeader(name, value);
71      }
72    }
73  
74    @Override
75    public void flushBuffer() throws IOException {
76      if (writer != null) {
77        writer.flush();
78      }
79      if (os != null) {
80        os.finish();
81      } else {
82        getResponse().flushBuffer();
83      }
84    }
85  
86    @Override
87    public void reset() {
88      super.reset();
89      if (os != null) {
90        os.resetBuffer();
91      }
92      writer = null;
93      os = null;
94      compress = true;
95    }
96  
97    @Override
98    public void resetBuffer() {
99      super.resetBuffer();
100     if (os != null) {
101       os.resetBuffer();
102     }
103     writer = null;
104     os = null;
105   }
106 
107   @Override
108   public void sendError(int status, String msg) throws IOException {
109     resetBuffer();
110     super.sendError(status, msg);
111   }
112 
113   @Override
114   public void sendError(int status) throws IOException {
115     resetBuffer();
116     super.sendError(status);
117   }
118 
119   @Override
120   public void sendRedirect(String location) throws IOException {
121     resetBuffer();
122     super.sendRedirect(location);
123   }
124 
125   @Override
126   public ServletOutputStream getOutputStream() throws IOException {
127     if (!response.isCommitted() && compress) {
128       if (os == null) {
129         os = new GZIPResponseStream(response);
130       }
131       return os;
132     } else {
133       return response.getOutputStream();
134     }
135   }
136 
137   @Override
138   public PrintWriter getWriter() throws IOException {
139     if (writer == null) {
140       writer = new PrintWriter(getOutputStream());
141     }
142     return writer;
143   }
144 }