PostAppBuild.java
5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* 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;
}
}