JsonResponse.java 1.72 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.servlet.http.HttpServletResponse
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.io.JSONWriter
 */
package com.day.crx.packaging.impl.response;

import com.day.crx.packaging.impl.response.BaseResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import javax.servlet.http.HttpServletResponse;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;

public class JsonResponse
extends BaseResponse {
    private boolean inTextarea;

    public JsonResponse(boolean inTextarea) {
        this.inTextarea = inTextarea;
    }

    @Override
    public void send() throws IOException {
        HttpServletResponse response = this.getServletResponse();
        if (this.inTextarea) {
            response.setContentType("text/html; charset=utf-8");
        } else {
            response.setContentType("application/json; charset=utf-8");
        }
        PrintWriter out = response.getWriter();
        if (this.inTextarea) {
            out.print("<textarea>");
        }
        JSONWriter w = new JSONWriter((Writer)out);
        try {
            w.object();
            w.key("success").value(this.successful());
            if (this.message.length() > 0) {
                w.key("msg").value((Object)this.message);
            }
            if (this.path.length() > 0) {
                w.key("path").value((Object)this.path);
            }
            w.endObject();
        }
        catch (JSONException e) {
            throw new IOException(e.toString());
        }
        if (this.inTextarea) {
            out.print("</textarea>");
        }
    }
}