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