View Javadoc

1   /*
2   MadCaching is a Tile Caching solution
3   Copyright (C) 2005  Mac Vu
4   
5   This program is free software; you can redistribute it and/or
6   modify it under the terms of the GNU General Public License
7   as published by the Free Software Foundation; either version 2
8   of the License, or (at your option) any later version.
9   
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  GNU General Public License for more details.
14  
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   */
19  package com.macvu.tiles.capture;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  import javax.servlet.ServletOutputStream;
25  import javax.servlet.http.HttpServletResponse;
26  import javax.servlet.http.HttpServletResponseWrapper;
27  import java.io.IOException;
28  import java.io.PrintWriter;
29  
30  public class CacheHttpServletResponse extends HttpServletResponseWrapper {
31  
32      protected static Log log = LogFactory.getLog(CacheHttpServletResponse.class);
33  
34      private boolean gotStream;
35      private boolean gotWriter;
36  
37      private CacheContent cacheContent;
38      private CacheServletOutputStream out;
39      private PrintWriter writer;
40  
41      public CacheHttpServletResponse(HttpServletResponse delegate) {
42          super(delegate);
43  
44          if (log.isDebugEnabled()) {
45              log.debug("Real Class Name of Delegate is: " + delegate.getClass().getName());
46          }
47  
48          cacheContent = new CacheContent();
49      }
50  
51      public ServletOutputStream getOutputStream() throws IOException {
52          if (log.isDebugEnabled()) {
53              log.debug("CacheHttpServletResponse: getOutputStream().");
54          }
55  
56          if (gotWriter) {
57              throw new IllegalStateException("Cannot get output stream after gotten the writer");
58          }
59  
60          if (out == null) {
61              out = new CacheServletOutputStream(super.getOutputStream(), cacheContent);
62          }
63  
64          gotStream = true;
65  
66          return out;
67      }
68  
69      public PrintWriter getWriter() throws IOException {
70          if (log.isDebugEnabled()) {
71              log.debug("CacheHttpServletResponse: getWriter().");
72          }
73  
74          if (gotStream) {
75              throw new IllegalStateException("Cannot get writer after gotten the output stream");
76          }
77  
78          if (writer == null) {
79              writer = new PrintWriterWrapper(super.getWriter(), cacheContent);
80          }
81  
82          gotWriter = true;
83  
84          return writer;
85      }
86  
87      public String getCache() {
88          if (gotWriter) {
89              writer.flush();
90          }
91  
92          return cacheContent.getCache();
93      }
94  
95  }
96