RestClientUtil.java
7.88 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* 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;
}
}