StringResponseWrapper.java
2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.servlet.ServletOutputStream
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.wrappers.SlingHttpServletResponseWrapper
*/
package com.day.cq.commons.feed;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import javax.servlet.ServletOutputStream;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.wrappers.SlingHttpServletResponseWrapper;
public class StringResponseWrapper
extends SlingHttpServletResponseWrapper {
private StringWriter sw;
private PrintWriter pw;
private ByteArrayOutputStream bos;
private ServletOutputStream sos;
public StringResponseWrapper(SlingHttpServletResponse slingHttpServletResponse) {
super(slingHttpServletResponse);
}
public ServletOutputStream getOutputStream() throws IOException {
if (this.sos == null) {
if (this.pw != null) {
throw new IllegalStateException("Writer already obtained.");
}
this.bos = new ByteArrayOutputStream();
this.sos = new AtomFeedOutputStream(this.bos);
}
return this.sos;
}
public PrintWriter getWriter() throws IOException {
if (this.pw == null) {
if (this.sos != null) {
throw new IllegalStateException("Output stream already obtained.");
}
this.sw = new StringWriter();
this.pw = new PrintWriter(this.sw);
}
return this.pw;
}
public String getString() throws UnsupportedEncodingException {
if (this.sw != null) {
return this.sw.toString();
}
if (this.bos != null) {
return this.bos.toString(super.getCharacterEncoding());
}
return "";
}
public class AtomFeedOutputStream
extends ServletOutputStream {
private final OutputStream out;
public AtomFeedOutputStream(OutputStream out) {
this.out = out;
}
public void write(int b) throws IOException {
this.out.write(b);
}
public void write(byte[] b, int off, int len) throws IOException {
this.out.write(b, off, len);
}
public void write(byte[] b) throws IOException {
this.out.write(b);
}
}
}