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