HtmlConsoleResponse.java
7.75 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* 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
* com.day.jcr.vault.util.HtmlProgressListener
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.servlet.http.HttpServletResponse
* org.apache.commons.lang3.StringEscapeUtils
* org.apache.sling.xss.XSSAPI
*/
package com.day.crx.packaging.impl.response;
import com.day.crx.packaging.impl.response.BaseResponse;
import com.day.crx.packaging.impl.response.Response;
import com.day.jcr.vault.fs.api.ProgressTrackerListener;
import com.day.jcr.vault.util.HtmlProgressListener;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.sling.xss.XSSAPI;
public class HtmlConsoleResponse
extends BaseResponse {
protected Session session;
private final XSSAPI xssapi;
protected PrintWriter out;
protected HtmlProgressListener htmlProgressListener;
public HtmlConsoleResponse(Session session, XSSAPI xssapi) {
this.session = session;
this.xssapi = xssapi;
}
@Override
public void init() throws IOException {
this.getServletResponse().setContentType("text/html; charset=utf-8");
this.out = this.getServletResponse().getWriter();
this.htmlProgressListener = new HtmlProgressListener((Writer)this.out);
this.out.print("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\">\n<html><head>\n <style type=\"text/css\">\n body {\n background-color: white;\n font-family: verdana, arial, sans-serif;\n font-size: 12px;\n white-space: nowrap;\n }\n div {\n font-family: courier, monospace;\n font-size: 11px;\n }\n </style>\n <script type=\"text/javascript\">\n function onStatus(pid, stat, p, max, msg) {\n window.parent.cqPackageShareClient.onStatus(pid, stat, p, max, msg);\n window.scrollTo(0, 1000000);\n }\n </script></head>\n<body>");
}
@Override
public void start(String title) throws IOException {
this.startTimer();
this.out.printf("<h2>%s</h2><div>", this.encode(title));
this.out.flush();
}
@Override
public /* varargs */ void log(String msg, Object ... args) {
this.out.printf(msg + "<br>", this.encode(args));
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("</div><br>%s in %sms.<br>", this.encode(longMessage), this.getElapsedTime());
this.writeScroll();
}
@Override
public void error(String message, Throwable t) throws IOException {
this.setSuccess(false, message, t);
this.out.print("<br><span class=\"error\">Error during processing.</span><br>");
this.out.println("<code><pre>");
StringWriter error = new StringWriter();
t.printStackTrace(new PrintWriter(error));
this.out.print(this.encode(error.toString()));
this.out.print("</pre></code>");
this.writeScroll();
}
@Override
public void onError(ProgressTrackerListener.Mode mode, String path, Exception e) {
this.htmlProgressListener.onError(mode, path, e);
if (mode.equals((Object)ProgressTrackerListener.Mode.PATHS) && path.startsWith("/libs/")) {
String appsPath = "/apps/" + path.substring("/libs/".length());
try {
if (this.session.itemExists(appsPath)) {
this.log("Warning: the file %1$s in this package is overlaid by %2$s. %1$s will not be active. Ignore this message if this behaviour is intended.", path, appsPath);
}
}
catch (RepositoryException ignored) {
// empty catch block
}
}
}
@Override
public void onMessage(ProgressTrackerListener.Mode mode, String action, String path) {
this.htmlProgressListener.onMessage(mode, action, path);
this.checkOverlay(mode, action, path);
}
@Override
public Response.ShareTracker getTracker(String status, String pid) {
return new HtmlShareTracker(this.out, status, pid);
}
@Override
public void send() throws IOException {
this.out.print("</body></html>");
this.out.flush();
}
protected void checkOverlay(ProgressTrackerListener.Mode mode, String action, String path) {
if (path.startsWith("/libs/")) {
String appsPath = "/apps/" + path.substring("/libs/".length());
try {
if (this.session.itemExists(appsPath)) {
this.log("Warning: the file %1$s in this package is overlaid by %2$s. %1$s will not be active. Ignore this message if this behaviour is intended.", path, appsPath);
}
}
catch (RepositoryException ignored) {
// empty catch block
}
}
}
protected void writeScroll() {
this.out.println("<script type=\"text/javascript\">");
this.out.println("window.scrollTo(0, 1000000);");
this.out.println("</script>");
this.out.flush();
}
Object[] encode(Object[] args) {
if (args == null || args.length == 0) {
return args;
}
Object[] result = new Object[args.length];
for (int i = 0; i < args.length; ++i) {
Object arg = args[i];
result[i] = arg instanceof String ? this.encode((String)arg) : arg;
}
return result;
}
String encode(String arg) {
return this.xssapi.encodeForHTML(arg);
}
public class HtmlShareTracker
implements Response.ShareTracker {
private final PrintWriter out;
private String status;
private String callback;
private final String pid;
private long lastPing;
public HtmlShareTracker(PrintWriter out, String status, String pid) {
this.callback = "onStatus";
this.lastPing = 0;
this.out = out;
this.status = status;
this.pid = pid;
}
@Override
public void onStatus(String status) {
}
public void setCallback(String callback) {
this.callback = callback;
}
@Override
public void onProgress(long progress, long max) {
long now = System.currentTimeMillis();
if (now - this.lastPing > 100) {
this.out.printf("%d / %d<br>%n", progress, max);
if (this.status != null) {
this.sendStatus(this.status, progress, max, "");
} else {
HtmlConsoleResponse.this.writeScroll();
}
this.lastPing = now;
}
}
@Override
public void message(String status, String msg) {
this.sendStatus(status, 0, 0, msg);
}
private void sendStatus(String status, long progress, long max, String msg) {
this.status = status;
if (msg == null) {
msg = "";
} else {
StringEscapeUtils.escapeEcmaScript((String)msg);
}
this.out.println("<script>");
this.out.printf("%s('%s', '%s', '%d', '%d', '%s');%n", this.callback, this.pid, status, progress, max, msg);
this.out.println("</script>");
this.out.flush();
}
}
}