001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.rest.filter;
019
020import java.io.IOException;
021import java.io.PrintWriter;
022import javax.servlet.ServletOutputStream;
023import javax.servlet.http.HttpServletResponse;
024import javax.servlet.http.HttpServletResponseWrapper;
025import org.apache.yetus.audience.InterfaceAudience;
026
027@InterfaceAudience.Private
028public class GZIPResponseWrapper extends HttpServletResponseWrapper {
029  private HttpServletResponse response;
030  private ServletOutputStream os;
031  private PrintWriter writer;
032  private boolean compress = true;
033
034  public GZIPResponseWrapper(HttpServletResponse response) {
035    super(response);
036    this.response = response;
037  }
038
039  @Override
040  public void setStatus(int status) {
041    super.setStatus(status);
042    if (status < 200 || status >= 300) {
043      compress = false;
044    }
045  }
046
047  @Override
048  public void addHeader(String name, String value) {
049    if (!"content-length".equalsIgnoreCase(name)) {
050      super.addHeader(name, value);
051    }
052  }
053
054  @Override
055  public void setContentLength(int length) {
056    // do nothing
057  }
058
059  @Override
060  public void setIntHeader(String name, int value) {
061    if (!"content-length".equalsIgnoreCase(name)) {
062      super.setIntHeader(name, value);
063    }
064  }
065
066  @Override
067  public void setHeader(String name, String value) {
068    if (!"content-length".equalsIgnoreCase(name)) {
069      super.setHeader(name, value);
070    }
071  }
072
073  @Override
074  public void flushBuffer() throws IOException {
075    if (writer != null) {
076      writer.flush();
077    }
078    if (os != null && (os instanceof GZIPResponseStream)) {
079      ((GZIPResponseStream) os).finish();
080    } else {
081      getResponse().flushBuffer();
082    }
083  }
084
085  @Override
086  public void reset() {
087    super.reset();
088    if (os != null && (os instanceof GZIPResponseStream)) {
089      ((GZIPResponseStream) os).resetBuffer();
090    }
091    writer = null;
092    os = null;
093    compress = true;
094  }
095
096  @Override
097  public void resetBuffer() {
098    super.resetBuffer();
099    if (os != null && (os instanceof GZIPResponseStream)) {
100      ((GZIPResponseStream) os).resetBuffer();
101    }
102    writer = null;
103    os = null;
104  }
105
106  @Override
107  public void sendError(int status, String msg) throws IOException {
108    resetBuffer();
109    super.sendError(status, msg);
110  }
111
112  @Override
113  public void sendError(int status) throws IOException {
114    resetBuffer();
115    super.sendError(status);
116  }
117
118  @Override
119  public void sendRedirect(String location) throws IOException {
120    resetBuffer();
121    super.sendRedirect(location);
122  }
123
124  @Override
125  public ServletOutputStream getOutputStream() throws IOException {
126    if (os == null) {
127      if (!response.isCommitted() && compress) {
128        os = (ServletOutputStream) new GZIPResponseStream(response);
129      } else {
130        os = response.getOutputStream();
131      }
132    }
133    return os;
134  }
135
136  @Override
137  public PrintWriter getWriter() throws IOException {
138    if (writer == null) {
139      writer = new PrintWriter(getOutputStream());
140    }
141    return writer;
142  }
143}