KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > portletcontainer > impl > portletAPIImp > helpers > CustomResponseWrapper


1 /**
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  **/

5
6 /**
7  * Created by The eXo Platform SARL
8  * Author : Mestrallet Benjamin
9  * benjmestrallet@users.sourceforge.net
10  * Date: Jul 30, 2003
11  * Time: 8:22:50 PM
12  */

13 package org.exoplatform.services.portletcontainer.impl.portletAPIImp.helpers;
14
15
16 import javax.servlet.ServletOutputStream JavaDoc;
17 import javax.servlet.http.HttpServletResponse JavaDoc;
18 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
19 import org.exoplatform.services.portletcontainer.helper.URLEncoder;
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.CharArrayWriter JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24
25 public class CustomResponseWrapper extends HttpServletResponseWrapper JavaDoc {
26   
27   private PrintWriter JavaDoc tmpWriter;
28   private ByteArrayOutputStream JavaDoc output;
29   private ByteArrayServletOutputStream servletOutput;
30   private URLEncoder urlEncoder_ ;
31   private CharArrayWriter JavaDoc charArrayWriter;
32   private boolean writerAlreadyCalled;
33   private boolean outputStreamAlreadyCalled;
34
35
36   public CustomResponseWrapper(HttpServletResponse JavaDoc httpServletResponse) {
37     super(httpServletResponse);
38   }
39
40   public void fillResponseWrapper(HttpServletResponse JavaDoc httpServletResponse) {
41     super.setResponse(httpServletResponse);
42     output = new ByteArrayOutputStream JavaDoc();
43     servletOutput = new ByteArrayServletOutputStream(output);
44
45     charArrayWriter = new CharArrayWriter JavaDoc();
46     tmpWriter = new PrintWriter JavaDoc(charArrayWriter);
47     writerAlreadyCalled = false;
48     outputStreamAlreadyCalled = false;
49   }
50
51   public void emptyResponseWrapper() {
52     output = null;
53     tmpWriter = null;
54     servletOutput = null;
55     writerAlreadyCalled = false;
56     outputStreamAlreadyCalled = false;
57   }
58
59   public void finalize() throws Throwable JavaDoc {
60     super.finalize();
61     servletOutput.close();
62     output.close();
63     tmpWriter.close();
64   }
65
66   public char[] getPortletContent() {
67     return charArrayWriter.toCharArray();
68   }
69
70   public PrintWriter JavaDoc getWriter() throws IOException JavaDoc {
71     if(outputStreamAlreadyCalled)
72       throw new IllegalStateException JavaDoc("the PrintWriter object has already been called");
73     writerAlreadyCalled = true;
74     return tmpWriter;
75   }
76
77   public ServletOutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
78     if(writerAlreadyCalled)
79       throw new IllegalStateException JavaDoc("the output stream has already been called");
80     outputStreamAlreadyCalled = true;
81     return servletOutput;
82   }
83
84   public byte[] toByteArray() {
85     return output.toByteArray();
86   }
87
88   public void flushBuffer() throws IOException JavaDoc {
89     tmpWriter.flush();
90     servletOutput.flush();
91   }
92
93   public void reset() {
94     output.reset();
95   }
96
97   public void close() throws IOException JavaDoc {
98     tmpWriter.close();
99   }
100   
101   public int getBufferSize() {
102     return 0;
103   }
104
105   public String JavaDoc encodeURL(String JavaDoc url) {
106     if (urlEncoder_ == null) return super.encodeURL(url) ;
107     return urlEncoder_.encodeURL(url) ;
108   }
109   
110   public void setURLEncoder(URLEncoder encoder) {
111     urlEncoder_ = encoder ;
112   }
113   
114   private static class ByteArrayServletOutputStream extends ServletOutputStream JavaDoc {
115     ByteArrayOutputStream JavaDoc baos;
116     public ByteArrayServletOutputStream(ByteArrayOutputStream JavaDoc baos) {
117       this.baos = baos;
118     }
119     public void write(int i) throws IOException JavaDoc {
120       baos.write(i);
121     }
122   }
123 }
Popular Tags