StreamedScriptResponse.java
6.56 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.jcr.vault.fs.api.ProgressTrackerListener
* com.day.jcr.vault.fs.api.ProgressTrackerListener$Mode
* javax.jcr.Session
* javax.servlet.http.HttpServletResponse
* org.apache.commons.lang3.StringEscapeUtils
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.io.JSONWriter
* org.apache.sling.xss.XSSAPI
*/
package com.day.crx.packaging.impl.response;
import com.day.crx.packaging.impl.response.HtmlConsoleResponse;
import com.day.crx.packaging.impl.response.Response;
import com.day.jcr.vault.fs.api.ProgressTrackerListener;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Arrays;
import javax.jcr.Session;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.apache.sling.xss.XSSAPI;
public class StreamedScriptResponse
extends HtmlConsoleResponse {
private PrintWriter realOut;
private static final String FILL_BUFFER = StreamedScriptResponse.createString(1024, ' ');
private static final String SHARE_CB = "shareTrackerCallback";
private String pid = "null";
private String callback = "console.log";
private long logFlushDelay = 1000;
private long lastFlushed = 0;
private long maxLogsPerFlush = 20;
private long logCount = 0;
public StreamedScriptResponse(String pid, String callback, Session session, XSSAPI xssapi) {
super(session, xssapi);
this.pid = pid;
if (callback != null) {
this.callback = callback;
}
}
@Override
public void init() throws IOException {
this.getServletResponse().setContentType("text/html; charset=utf-8");
this.realOut = this.getServletResponse().getWriter();
this.out = new PrintWriter(new StringWriter(){
@Override
public void flush() {
StreamedScriptResponse.this.writeCallback(this.toString());
this.getBuffer().setLength(0);
}
});
this.realOut.print("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\">\n<html><head>\n <script type=\"text/javascript\">\n function callback(msg) {\n " + this.callback + "({msg: msg, pid: '" + this.pid + "'});\n" + " }\n" + " function " + "shareTrackerCallback" + "(pid, stat, p, max, msg) {\n" + " " + this.callback + "({share: true, pid: '" + this.pid + "', status: stat, progress: p, max: max, msg: msg});\n" + " }\n" + " </script>\n" + "</head>\n" + "<body>");
this.realOut.println(FILL_BUFFER);
}
@Override
public Response.ShareTracker getTracker(String status, String pid) {
HtmlConsoleResponse.HtmlShareTracker tracker = new HtmlConsoleResponse.HtmlShareTracker(this.out, status, pid);
tracker.setCallback("shareTrackerCallback");
return tracker;
}
@Override
protected void writeScroll() {
this.out.flush();
}
@Override
public void start(String title) throws IOException {
this.startTimer();
this.out.printf("<h2>%s</h2>", title);
this.out.flush();
}
@Override
public void onError(ProgressTrackerListener.Mode mode, String path, Exception e) {
this.print("E", path, e.toString());
}
@Override
public void onMessage(ProgressTrackerListener.Mode mode, String action, String path) {
this.print(action, path, null);
this.checkOverlay(mode, action, path);
}
private void print(String action, String path, String msg) {
this.out.write("<span class=\"");
this.out.write(action);
this.out.write("\">");
this.out.write("<b>");
this.out.write(action);
this.out.write("</b> ");
this.out.write(path);
if (msg != null) {
this.out.write(" (");
this.out.write(msg);
this.out.write(")");
}
this.out.write("</span><br>\r\n");
++this.logCount;
long now = System.currentTimeMillis();
if (now > this.lastFlushed + this.logFlushDelay || this.logCount > this.maxLogsPerFlush) {
this.lastFlushed = now;
this.logCount = 0;
this.out.flush();
}
}
@Override
public void success(String message, String longMessage) throws IOException {
this.setSuccess(true, message, null);
if (longMessage == null) {
longMessage = message;
}
this.out.printf("<br>%s in %sms.<br>", longMessage, this.getElapsedTime());
this.out.flush();
}
@Override
public void error(String message, Throwable t) throws IOException {
this.setSuccess(false, message, t);
this.out.print("<span class=\"error\">Error during processing:</span><br><br>");
this.out.println("<pre>");
t.printStackTrace(this.out);
this.out.print("</pre>");
this.out.flush();
}
@Override
public void send() throws IOException {
this.realOut.print("<textarea>");
JSONWriter w = new JSONWriter((Writer)this.realOut);
try {
w.object();
w.key("success").value(this.successful());
if (this.message != null && this.message.length() > 0) {
w.key("msg").value((Object)this.message);
}
if (this.path != null && this.path.length() > 0) {
w.key("path").value((Object)this.path);
}
if (this.alertMsg != null && this.alertMsg.length() > 0) {
w.key("alertMsg").value((Object)this.alertMsg);
}
w.endObject();
}
catch (JSONException e) {
throw new IOException(e.toString());
}
this.realOut.print("</textarea>");
this.realOut.print("</body></html>");
this.realOut.flush();
}
private void writeCallback(String msg) {
if (msg == null) {
msg = "";
} else {
msg = msg.trim();
msg = StringEscapeUtils.escapeEcmaScript((String)msg);
}
this.realOut.printf("<script>%ncallback('", new Object[0]);
this.realOut.print(msg);
this.realOut.printf("');%n</script>%n", new Object[0]);
this.realOut.flush();
}
public static String createString(int number, char ch) {
char[] chars = new char[number];
Arrays.fill(chars, ch);
return new String(chars);
}
}