StreamedScriptResponse.java 6.56 KB
/*
 * 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>&nbsp;");
        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);
    }

}