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