01: package com.ecyrd.jspwiki.plugin;
02:
03: import java.util.*;
04: import java.io.*;
05: import javax.servlet.*;
06: import javax.servlet.http.*;
07:
08: /**
09: * For capturing the output of a servlet into a buffer
10: */
11: public class BufferedHttpServletResponseWrapper extends
12: HttpServletResponseWrapper {
13:
14: public BufferedHttpServletResponseWrapper(
15: HttpServletResponse response) {
16: super (response);
17: }
18:
19: public ServletOutputStream getOutputStream() {
20: return bsos;
21: }
22:
23: public PrintWriter getWriter() {
24: return bsos.pw;
25: }
26:
27: public void reset() {
28: bsos.reset();
29: }
30:
31: public byte[] getByteContent() {
32: return bsos.getByteContent();
33: }
34:
35: public String getStringContent() {
36: return bsos.getStringContent();
37: }
38:
39: public boolean isCommitted() {
40: return false;
41: }
42:
43: private BufferedServletOutputStream bsos = new BufferedServletOutputStream(
44: getCharacterEncoding());
45:
46: }
47:
48: class BufferedServletOutputStream extends ServletOutputStream {
49:
50: BufferedServletOutputStream(String enc) {
51: try {
52: baos = new ByteArrayOutputStream();
53: pw = new PrintWriter(new OutputStreamWriter(baos, enc));
54: encoding = enc;
55: } catch (java.io.UnsupportedEncodingException e) {
56: // XXX should not happen, checked by servlet methods - assert()
57: }
58: }
59:
60: public void write(int i) {
61: baos.write(i);
62: }
63:
64: public void reset() {
65: baos.reset();
66: }
67:
68: public byte[] getByteContent() {
69: return baos.toByteArray();
70: }
71:
72: public String getStringContent() {
73: try {
74: return baos.toString(encoding);
75: } catch (java.io.UnsupportedEncodingException e) {
76: // XXX should not happen, checked by servlet methods - assert()
77: }
78: return "encoding error";
79: }
80:
81: ByteArrayOutputStream baos;
82: PrintWriter pw;
83: String encoding;
84:
85: }
|