BufferedResponse.java
2.94 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* 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.adobe.cq.mcm.campaign.impl;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
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 BufferedResponse
extends SlingHttpServletResponseWrapper {
private BufferingOutputStream os;
private BufferingWriter pw;
public BufferedResponse(SlingHttpServletResponse response) {
super(response);
}
public PrintWriter getWriter() throws IOException {
if (this.pw == null) {
if (this.os != null) {
throw new IllegalStateException("Output stream already obtained.");
}
this.pw = new BufferingWriter(new StringWriter());
}
return this.pw;
}
public ServletOutputStream getOutputStream() throws IOException {
if (this.os == null) {
if (this.pw != null) {
throw new IllegalStateException("Print writer already obtained.");
}
this.os = new BufferingOutputStream();
}
return this.os;
}
public String getString() throws UnsupportedEncodingException {
if (this.pw != null) {
return this.pw.getString();
}
if (this.os != null) {
return this.os.getString();
}
return "";
}
public void forward() throws IOException {
SlingHttpServletResponse wrapped = this.getSlingResponse();
String toForward = this.pw.getString();
PrintWriter writer = wrapped.getWriter();
writer.print(toForward);
writer.flush();
}
class BufferingWriter
extends PrintWriter {
private StringWriter actualWriter;
public BufferingWriter(StringWriter actualWriter) {
super(actualWriter);
this.actualWriter = actualWriter;
}
public String getString() {
return this.actualWriter.toString();
}
}
class BufferingOutputStream
extends ServletOutputStream {
private ByteArrayOutputStream bos;
public BufferingOutputStream() {
this.bos = new ByteArrayOutputStream();
}
public void write(int b) throws IOException {
this.bos.write(b);
}
public String getString() throws UnsupportedEncodingException {
return this.bos.toString(BufferedResponse.this.getCharacterEncoding());
}
public void close() throws IOException {
this.bos.close();
super.close();
}
}
}