View Javadoc

1   package com.atlassian.gzipfilter;
2   
3   import com.atlassian.gzipfilter.util.IOUtils;
4   import org.slf4j.Logger;
5   import org.slf4j.LoggerFactory;
6   
7   import java.io.IOException;
8   import java.io.OutputStreamWriter;
9   import java.io.PrintWriter;
10  
11  import javax.servlet.ServletOutputStream;
12  import javax.servlet.http.HttpServletResponse;
13  import javax.servlet.http.HttpServletResponseWrapper;
14  
15  public class GzipResponseWrapper extends HttpServletResponseWrapper
16  {
17      private static final Logger log = LoggerFactory.getLogger(GzipResponseWrapper.class);
18      protected HttpServletResponse origResponse = null;
19      protected ServletOutputStream stream = null;
20      protected PrintWriter writer = null;
21      private String encoding;
22  
23      public GzipResponseWrapper(HttpServletResponse response, String encoding)
24      {
25          super(response);
26          this.encoding = encoding;
27          origResponse = response;
28      }
29  
30      protected ServletOutputStream createOutputStream() throws IOException
31      {
32          return new GzipResponseStream(origResponse);
33      }
34  
35      public void finishResponse()
36      {
37  
38          //JRA-29795 If stream was not created - let's create it and close it right after. GzipOutputStream while construction is
39          //writing some metadata into stream and since in this place "Content-Encoding:zip" was set and cannot be removed
40          //we have to produce valid GZIP stream with appropriate gzip metadata header/footers.
41          if(stream == null)
42          {
43              try
44              {
45                  stream = createOutputStream();
46                  stream.flush();
47              }
48              catch (IOException e)
49              {
50                  log.warn("Was unable to create GzipResponseStream. Invalid gzip stream was sent in response body!", e);
51              }
52          }
53          IOUtils.closeQuietly(writer);
54          IOUtils.closeQuietly(stream);
55      }
56  
57      public void flushBuffer() throws IOException
58      {
59          if (stream != null)
60              stream.flush();
61      }
62  
63      public ServletOutputStream getOutputStream() throws IOException
64      {
65          if (writer != null)
66              throw new IllegalStateException("getWriter() has already been called!");
67  
68          if (stream == null)
69              stream = createOutputStream();
70  
71          return stream;
72      }
73  
74      public PrintWriter getWriter() throws IOException
75      {
76          if (writer != null)
77              return writer;
78  
79          if (stream != null)
80              throw new IllegalStateException("getOutputStream() has already been called!");
81  
82          stream = createOutputStream();
83          writer = new PrintWriter(new OutputStreamWriter(stream, encoding));
84  
85          return writer;
86      }
87  
88      public void setEncoding(String encoding)
89      {
90          this.encoding = encoding;
91      }
92  }