PostAppBuild.java 5.09 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.HttpPost
 *  org.apache.http.client.methods.HttpPut
 *  org.apache.http.client.methods.HttpUriRequest
 *  org.apache.http.conn.ClientConnectionManager
 *  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.StringBody
 *  org.apache.http.util.EntityUtils
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mobile.phonegap.impl.build.action;

import com.adobe.cq.mobile.phonegap.impl.build.PGBRequest;
import com.adobe.cq.mobile.phonegap.impl.build.metadata.AppInfo;
import com.adobe.cq.mobile.phonegap.impl.build.util.HTTPUtil;
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.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ClientConnectionManager;
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.StringBody;
import org.apache.http.util.EntityUtils;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PostAppBuild {
    private static final Logger logger = LoggerFactory.getLogger(PostAppBuild.class);

    public AppInfo createAppBuild(PGBRequest pgbRequest, ContentBody buildContent, String title) throws Exception {
        logger.info("Creating app build for " + buildContent.getFilename() + ":" + buildContent.getContentLength() + " to " + pgbRequest);
        String jsonMetadata = "{\"title\":\"" + title + "\",\"" + "create_method" + "\":\"file\"}";
        return this.postAppBuild(pgbRequest, null, jsonMetadata, buildContent, 201);
    }

    public AppInfo updateAppBuild(PGBRequest pgbRequest, String appId, ContentBody buildContent, String title) throws Exception {
        logger.info("Updating app build for " + buildContent.getFilename() + ":" + buildContent.getContentLength() + " to " + pgbRequest);
        if (appId == null) {
            throw new Exception("Update request must have an app id: " + appId);
        }
        String jsonMetadata = "{\"title\":\"" + title + "\"}";
        return this.postAppBuild(pgbRequest, appId, jsonMetadata, buildContent, 200);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private AppInfo postAppBuild(PGBRequest pgbRequest, String appId, String jsonMetadata, ContentBody buildContent, int expectedResponseCode) throws Exception {
        AppInfo appInfo;
        block6 : {
            appInfo = null;
            if (null == buildContent) {
                throw new Exception("Build content does not exist: " + (Object)buildContent);
            }
            HttpClient httpclient = null;
            HttpPost submitMethod = null;
            try {
                MultipartEntity post_entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                if (jsonMetadata != null) {
                    post_entity.addPart("data", (ContentBody)new StringBody(jsonMetadata));
                }
                post_entity.addPart("file", buildContent);
                submitMethod = appId == null ? new HttpPost(pgbRequest.getAppsServiceURL()) : new HttpPut(pgbRequest.getAppURL(appId));
                submitMethod.setEntity((HttpEntity)post_entity);
                httpclient = HTTPUtil.getClient(pgbRequest);
                HttpResponse response = httpclient.execute((HttpUriRequest)submitMethod);
                HttpEntity entity = response.getEntity();
                if (response.getStatusLine().getStatusCode() == expectedResponseCode) {
                    String result = EntityUtils.toString((HttpEntity)entity);
                    JSONObject jsonObject = new JSONObject(result);
                    appInfo = new AppInfo(jsonObject);
                    logger.debug("App info " + appInfo);
                    logger.debug("Response [" + (Object)response.getStatusLine() + "] " + jsonObject.toString());
                    break block6;
                }
                String errorDetails = EntityUtils.toString((HttpEntity)entity);
                logger.debug("Response [" + (Object)response.getStatusLine() + "] " + errorDetails);
                throw new Exception("Failed " + submitMethod.getClass().getName() + ": " + (Object)response.getStatusLine() + ". Expected '" + expectedResponseCode + "' but received '" + response.getStatusLine().getStatusCode() + "'. ErrorDetails: " + errorDetails);
            }
            finally {
                httpclient.getConnectionManager().shutdown();
            }
        }
        return appInfo;
    }
}