01: /**
02: *
03: */package clime.messadmin.filter;
04:
05: import java.io.CharArrayWriter;
06: import java.io.IOException;
07: import java.io.PrintWriter;
08:
09: import javax.servlet.ServletOutputStream;
10: import javax.servlet.http.HttpServletResponse;
11: import javax.servlet.http.HttpServletResponseWrapper;
12:
13: /**
14: * @author Cédrik LIME
15: */
16: class CharResponseWrapper extends HttpServletResponseWrapper {
17: protected CharArrayWriter output;
18: protected ServletOutputStream stream = null;
19: protected PrintWriter writer = null;
20:
21: static class CharOutputStream extends ServletOutputStream {
22: CharArrayWriter outstream;
23:
24: public CharOutputStream(CharArrayWriter out) {
25: outstream = out;
26: }
27:
28: /** {@inheritDoc} */
29: // @Override
30: public void write(int b) {
31: outstream.write(b);
32: }
33:
34: public void write(String str) throws IOException {
35: outstream.write(str);
36: }
37: }
38:
39: /** {@inheritDoc} */
40: // @Override
41: public String toString() {
42: return output.toString();
43: }
44:
45: public char[] toCharArray() {
46: return (output.toCharArray());
47: }
48:
49: public CharResponseWrapper(HttpServletResponse response) {
50: super (response);
51: output = new CharArrayWriter();
52: }
53:
54: /** {@inheritDoc} */
55: // @Override
56: public synchronized PrintWriter getWriter() {
57: if (stream != null) {
58: throw new IllegalStateException(
59: "getOutputStream() has already been called for this response");//$NON-NLS-1$
60: }
61: if (writer == null) {
62: writer = new PrintWriter(output);
63: }
64: return writer;
65: }
66:
67: /** {@inheritDoc} */
68: // @Override
69: public synchronized ServletOutputStream getOutputStream() {
70: if (writer != null) {
71: throw new IllegalStateException(
72: "getWriter() has already been called for this response");//$NON-NLS-1$
73: }
74: if (stream == null) {
75: stream = new CharOutputStream(output);
76: }
77: return stream;
78: }
79:
80: }
|