RestClientUtil.java 7.88 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.http.HttpEntity
 *  org.apache.http.HttpResponse
 *  org.apache.http.StatusLine
 *  org.apache.http.client.HttpClient
 *  org.apache.http.client.methods.HttpDelete
 *  org.apache.http.client.methods.HttpEntityEnclosingRequestBase
 *  org.apache.http.client.methods.HttpGet
 *  org.apache.http.client.methods.HttpPost
 *  org.apache.http.client.methods.HttpPut
 *  org.apache.http.client.methods.HttpRequestBase
 *  org.apache.http.client.methods.HttpUriRequest
 *  org.apache.http.conn.ClientConnectionManager
 *  org.apache.http.entity.StringEntity
 *  org.apache.http.entity.mime.HttpMultipartMode
 *  org.apache.http.entity.mime.MultipartEntity
 *  org.apache.http.entity.mime.content.ContentBody
 *  org.apache.http.entity.mime.content.FileBody
 *  org.apache.http.entity.mime.content.StringBody
 *  org.apache.http.util.EntityUtils
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mobile.phonegap.impl.build.util;

import com.adobe.cq.mobile.phonegap.impl.PGBuildException;
import com.adobe.cq.mobile.phonegap.impl.PGException;
import com.adobe.cq.mobile.phonegap.impl.build.PGBRequest;
import com.adobe.cq.mobile.phonegap.impl.build.util.HTTPUtil;
import com.adobe.cq.mobile.phonegap.impl.build.util.JSONUtil;
import java.io.File;
import java.nio.charset.Charset;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.util.EntityUtils;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class RestClientUtil {
    private static final Logger logger = LoggerFactory.getLogger(RestClientUtil.class);
    public static final String JSON_KEY_ERROR_DETAIL = "error";
    public static final String BINARY_CHARSET = null;
    public static final String BINARY_MIMETYPE = "application/octet-stream";
    public static final String HTTP_METHOD_GET = "GET";
    public static final String HTTP_METHOD_POST = "POST";
    public static final String HTTP_METHOD_PUT = "PUT";
    public static final String HTTP_METHOD_DELETE = "DELETE";
    public static final String ENCODING_UTF8 = "UTF-8";

    public static JSONObject submitGetRequest(PGBRequest pgbRequest, String serviceURL) throws PGException {
        return RestClientUtil.submitRequest("GET", pgbRequest, serviceURL, null, null, null, 200);
    }

    public static JSONObject submitRequest(String httpMethod, PGBRequest pgbRequest, String serviceURL, String jsonData, File fileData, String filename, int expectedResponseCode) throws PGException {
        JSONObject jsonObject;
        block18 : {
            StringBuilder data = new StringBuilder();
            if (jsonData != null) {
                data.append(jsonData + ":");
            }
            if (fileData != null) {
                data.append(fileData + ":");
            }
            logger.debug(httpMethod + " [" + serviceURL + "][" + data.toString() + expectedResponseCode + "]");
            jsonObject = null;
            HttpClient httpclient = null;
            HttpRequestBase requestMethod = null;
            try {
                requestMethod = RestClientUtil.getMethod(httpMethod, serviceURL);
                if (httpMethod.equalsIgnoreCase("POST") || httpMethod.equalsIgnoreCase("PUT")) {
                    HttpEntityEnclosingRequestBase submitMethod = (HttpEntityEnclosingRequestBase)requestMethod;
                    if (fileData != null) {
                        MultipartEntity post_entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                        FileBody fileBody = new FileBody(fileData);
                        if (filename != null) {
                            fileBody = new FileBody(fileData, filename, "application/octet-stream", BINARY_CHARSET);
                        }
                        post_entity.addPart("file", (ContentBody)fileBody);
                        if (jsonData != null) {
                            post_entity.addPart("request", (ContentBody)new StringBody(jsonData, Charset.forName("UTF-8")));
                        }
                        submitMethod.setEntity((HttpEntity)post_entity);
                    } else if (jsonData != null) {
                        submitMethod.setEntity((HttpEntity)new StringEntity(jsonData, Charset.forName("UTF-8")));
                    }
                }
                httpclient = HTTPUtil.getClient(pgbRequest);
                HttpResponse response = httpclient.execute((HttpUriRequest)requestMethod);
                HttpEntity entity = response.getEntity();
                if (response.getStatusLine().getStatusCode() == expectedResponseCode) {
                    String result = EntityUtils.toString((HttpEntity)entity);
                    jsonObject = new JSONObject(result);
                    logger.debug("Response [" + (Object)response.getStatusLine() + "] " + jsonObject.toString());
                    break block18;
                }
                String rawErrorDetails = EntityUtils.toString((HttpEntity)entity);
                logger.debug("Response [" + (Object)response.getStatusLine() + "] " + rawErrorDetails);
                String errorMsg = "Failed " + httpMethod + ": " + (Object)response.getStatusLine() + ". Expected '" + expectedResponseCode + "' but " + "received '" + response.getStatusLine().getStatusCode() + "'. ErrorDetails: " + rawErrorDetails;
                String errorDetails = null;
                try {
                    JSONObject errorJSON = new JSONObject(rawErrorDetails);
                    errorDetails = errorJSON.getString("error");
                }
                catch (JSONException e) {
                    // empty catch block
                }
                throw new PGBuildException(errorMsg, response.getStatusLine().getStatusCode(), errorDetails);
            }
            catch (PGException ex) {
                throw ex;
            }
            catch (Exception ex) {
                throw new PGException("Failed request: " + httpMethod, ex);
            }
            finally {
                if (httpclient != null) {
                    httpclient.getConnectionManager().shutdown();
                }
            }
        }
        return jsonObject;
    }

    private static HttpRequestBase getMethod(String httpMethod, String url) {
        HttpPost requestMethod = null;
        if (httpMethod.equalsIgnoreCase("POST")) {
            requestMethod = new HttpPost(url);
        } else if (httpMethod.equalsIgnoreCase("GET")) {
            requestMethod = new HttpGet(url);
        } else if (httpMethod.equalsIgnoreCase("PUT")) {
            requestMethod = new HttpPut(url);
        } else if (httpMethod.equalsIgnoreCase("DELETE")) {
            requestMethod = new HttpDelete(url);
        }
        return requestMethod;
    }

    public static String getErrorDetails(JSONObject data) {
        String errorDetails = JSONUtil.getSafeString(data, "error");
        return errorDetails == null ? "" : errorDetails;
    }
}