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