View Javadoc
1   /*
2    * Copyright (C) 2010 eXo Platform SAS.
3    *
4    * This is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU Lesser General Public License as
6    * published by the Free Software Foundation; either version 2.1 of
7    * the License, or (at your option) any later version.
8    *
9    * This software is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this software; if not, write to the Free
16   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
18   */
19  
20  package org.exoplatform.services.portletcache;
21  
22  import java.io.ByteArrayOutputStream;
23  import java.io.IOException;
24  import java.io.OutputStream;
25  import java.io.PrintWriter;
26  
27  import javax.portlet.MimeResponse;
28  import javax.portlet.RenderResponse;
29  import javax.portlet.filter.RenderResponseWrapper;
30  
31  /**
32   * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
33   * @version $Revision$
34   */
35  class BufferedRenderResponse extends RenderResponseWrapper
36  {
37  
38     /** . */
39     private ByteArrayOutputStream buffer;
40  
41     /** . */
42     private String expirationCache;
43  
44     BufferedRenderResponse(RenderResponse response)
45     {
46        super(response);
47     }
48  
49     public byte[] getBytes()
50     {
51        return buffer != null ? buffer.toByteArray() : new byte[0];
52     }
53  
54     public String getExpirationCache()
55     {
56        return expirationCache;
57     }
58  
59     @Override
60     public PrintWriter getWriter() throws IOException
61     {
62        throw new UnsupportedOperationException("Not yet implemented");
63     }
64  
65     @Override
66     public OutputStream getPortletOutputStream() throws IOException
67     {
68        if (buffer == null)
69        {
70           buffer = new ByteArrayOutputStream();
71        }
72        return buffer;
73     }
74  
75     @Override
76     public void addProperty(String key, String value)
77     {
78        if (MimeResponse.EXPIRATION_CACHE.equals(key))
79        {
80           if (expirationCache != null)
81           {
82              expirationCache = value;
83           }
84        }
85        else
86        {
87           super.addProperty(key, value);
88        }
89     }
90  
91     @Override
92     public void setProperty(String key, String value)
93     {
94        if (MimeResponse.EXPIRATION_CACHE.equals(key))
95        {
96           expirationCache = value;
97        }
98        else
99        {
100          super.addProperty(key, value);
101       }
102    }
103 }