SirenResponse.java 4.17 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.servlet.http.HttpServletResponse
 *  org.apache.sling.servlets.post.AbstractPostResponse
 *  org.json.JSONArray
 *  org.json.JSONException
 *  org.json.JSONObject
 */
package com.adobe.granite.rest.impl.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import javax.servlet.http.HttpServletResponse;
import org.apache.sling.servlets.post.AbstractPostResponse;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class SirenResponse
extends AbstractPostResponse {
    private static final String PROP_PROPERTIES = "properties";
    private static final String PROP_CHANGES = "changes";
    private static final String PROP_TYPE = "type";
    private static final String PROP_ARGUMENT = "argument";
    private JSONObject json = new JSONObject();
    private JSONObject properties = new JSONObject();
    private JSONArray changes = new JSONArray();
    private Throwable error;

    public SirenResponse() throws JSONResponseException {
        try {
            this.json = new JSONObject();
            this.json.put("class", (Object)new String[]{"core/response"});
            this.properties = new JSONObject();
            this.properties.put("changes", (Object)this.changes);
            this.json.put("properties", (Object)this.properties);
        }
        catch (Exception e) {
            throw new JSONResponseException(this, e);
        }
    }

    public /* varargs */ void onChange(String type, String ... arguments) throws JSONResponseException {
        try {
            JSONObject change = new JSONObject();
            change.put("type", (Object)type);
            for (String argument : arguments) {
                change.accumulate("argument", (Object)argument);
            }
            this.changes.put((Object)change);
        }
        catch (JSONException e) {
            throw new JSONResponseException(this, (Throwable)e);
        }
    }

    public void setError(Throwable error) {
        try {
            this.error = error;
            JSONObject jsonError = new JSONObject();
            jsonError.put("class", (Object)error.getClass().getName());
            jsonError.put("message", (Object)error.getMessage());
            this.properties.put("error", (Object)jsonError);
        }
        catch (JSONException e) {
            throw new JSONResponseException(this, (Throwable)e);
        }
    }

    public Throwable getError() {
        return this.error;
    }

    public void setProperty(String name, Object value) {
        try {
            this.properties.put(name, value);
        }
        catch (Exception e) {
            throw new JSONResponseException(this, "Error setting JSON property '" + name + "' to '" + value + "'", e);
        }
    }

    public Object getProperty(String name) throws JSONResponseException {
        try {
            if (this.properties.has(name)) {
                return this.properties.get(name);
            }
            return null;
        }
        catch (JSONException e) {
            throw new JSONResponseException(this, "Error getting JSON property '" + name + "'", (Throwable)e);
        }
    }

    protected void doSend(HttpServletResponse response) throws IOException {
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        try {
            this.json.write((Writer)response.getWriter());
        }
        catch (JSONException e) {
            IOException ioe = new IOException("Error creating JSON response");
            ioe.initCause((Throwable)e);
            throw ioe;
        }
    }

    JSONObject getJson() {
        return this.json;
    }

    public class JSONResponseException
    extends RuntimeException {
        final /* synthetic */ SirenResponse this$0;

        public JSONResponseException(SirenResponse this$0, String message, Throwable exception) {
            this.this$0 = this$0;
            super(message, exception);
        }

        public JSONResponseException(SirenResponse this$0, Throwable e) {
            this.this$0 = this$0;
            super("Error building JSON response", e);
        }
    }

}