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.OutputStream;
24  import java.util.HashSet;
25  import java.util.Set;
26  import java.util.StringTokenizer;
27  
28  import javax.servlet.Filter;
29  import javax.servlet.FilterChain;
30  import javax.servlet.FilterConfig;
31  import javax.servlet.ServletException;
32  import javax.servlet.ServletRequest;
33  import javax.servlet.ServletResponse;
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpServletResponse;
36  
37  import org.apache.hadoop.classification.InterfaceAudience;
38  
39  @InterfaceAudience.Private
40  public class GzipFilter implements Filter {
41    private Set<String> mimeTypes = new HashSet<String>();
42  
43    @Override
44    public void init(FilterConfig filterConfig) throws ServletException {
45      String s = filterConfig.getInitParameter("mimeTypes");
46      if (s != null) {
47        StringTokenizer tok = new StringTokenizer(s, ",", false);
48        while (tok.hasMoreTokens()) {
49          mimeTypes.add(tok.nextToken());
50        }
51      }
52    }
53  
54    @Override
55    public void destroy() {
56    }
57  
58    @Override
59    public void doFilter(ServletRequest req, ServletResponse rsp,
60        FilterChain chain) throws IOException, ServletException {
61      HttpServletRequest request = (HttpServletRequest)req;
62      HttpServletResponse response = (HttpServletResponse)rsp;
63      String contentEncoding = request.getHeader("content-encoding");
64      String acceptEncoding = request.getHeader("accept-encoding");
65      String contentType = request.getHeader("content-type");
66      if ((contentEncoding != null) &&
67          (contentEncoding.toLowerCase().indexOf("gzip") > -1)) {
68        request = new GZIPRequestWrapper(request);
69      }
70      if (((acceptEncoding != null) &&
71            (acceptEncoding.toLowerCase().indexOf("gzip") > -1)) ||
72          ((contentType != null) && mimeTypes.contains(contentType))) {
73        response = new GZIPResponseWrapper(response);
74      }
75      chain.doFilter(request, response);
76      if (response instanceof GZIPResponseWrapper) {
77        OutputStream os = response.getOutputStream();
78        if (os instanceof GZIPResponseStream) {
79          ((GZIPResponseStream)os).finish();
80        }
81      }
82    }
83  
84  }