Skip to content

Commit 9425c92

Browse files
committed
adding changes for multipart form data in mustache file
1 parent 18b0f2a commit 9425c92

File tree

2 files changed

+40
-31
lines changed

2 files changed

+40
-31
lines changed

generator/cybersource-java-template/libraries/okhttp-gson/ApiClient.mustache

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import okhttp3.Route;
5757
import okhttp3.internal.http.HttpMethod;
5858
import okhttp3.logging.HttpLoggingInterceptor;
5959
import okhttp3.logging.HttpLoggingInterceptor.Level;
60+
import okio.Buffer;
6061
import okio.BufferedSink;
6162
import okio.Okio;
6263
import org.bouncycastle.jce.provider.BouncyCastleProvider;
@@ -1339,8 +1340,15 @@ public class ApiClient {
13391340
}
13401341
}
13411342
}
1342-
1343-
callAuthenticationHeader(method, path, body, queryParams, requestHeaderMap);
1343+
1344+
String contentType = headerParams.get("Content-Type");
1345+
// ensuring a default content type
1346+
if (contentType == null) {
1347+
contentType = "application/json";
1348+
}
1349+
RequestBody requestbody = createRequestBody(method, body, formParams, contentType);
1350+
1351+
callAuthenticationHeader(method, path, requestbody, queryParams, requestHeaderMap);
13441352

13451353
if (merchantConfig.isEnableClientCert()) {
13461354
addClientCertToKeyStore();
@@ -1357,17 +1365,27 @@ public class ApiClient {
13571365

13581366

13591367
logger.info("Request Header Parameters:\n{}", new PrettyPrintingMap<String, String>(headerParams));
1360-
Request request = buildRequest(path, method, queryParams, body, headerParams, formParams, authNames,
1368+
Request request = buildRequest(path, method, queryParams, requestbody, headerParams, formParams, authNames,
13611369
progressRequestListener);
13621370
return httpClient.newCall(request);
13631371
}
1372+
1373+
private String getRequestContentSendOverNetwork(RequestBody requestBody) throws IOException {
1374+
if(requestBody!=null) {
1375+
Buffer buffer = new Buffer();
1376+
requestBody.writeTo(buffer);
1377+
String payload = buffer.readUtf8();
1378+
return payload;
1379+
}
1380+
return null;
1381+
}
13641382

13651383
/*
13661384
* Purpose : This function calling the Authentication and making an Auth Header
13671385
*
13681386
*/
13691387

1370-
public void callAuthenticationHeader(String method, String path, Object body, List<Pair> queryParams, Map<String, String> requestHeaderMap) {
1388+
public void callAuthenticationHeader(String method, String path, RequestBody reqBody, List<Pair> queryParams, Map<String, String> requestHeaderMap) {
13711389
13721390
try {
13731391
String requestTarget = null;
@@ -1399,14 +1417,7 @@ public class ApiClient {
13991417

14001418
Authorization authorization = new Authorization();
14011419

1402-
String requestBody = null;
1403-
if ((method.equalsIgnoreCase("POST") || method.equalsIgnoreCase("PUT") ||
1404-
method.equalsIgnoreCase("PATCH"))
1405-
&& body.equals("{}")) {
1406-
requestBody = "{}";
1407-
} else {
1408-
requestBody = json.serialize(body);
1409-
}
1420+
String requestBody = getRequestContentSendOverNetwork(reqBody);
14101421

14111422
logger.debug("HTTP Request Body:\n" + requestBody);
14121423
boolean isMerchantDetails = merchantConfig.validateMerchantDetails(method);
@@ -1443,7 +1454,7 @@ public class ApiClient {
14431454
requestHeaderMap.put("v-c-client-id", "cybs-rest-sdk-java-" + versionInfo);
14441455
}
14451456

1446-
} catch (ConfigException e) {
1457+
} catch (ConfigException | IOException e) {
14471458
logger.error(e.getMessage());
14481459
}
14491460

@@ -1465,7 +1476,7 @@ public class ApiClient {
14651476
* @throws ApiException If fail to serialize the request body object
14661477
*/
14671478
@SuppressWarnings({ "unchecked" })
1468-
public Request buildRequest(String path, String method, List<Pair> queryParams, Object body,
1479+
public Request buildRequest(String path, String method, List<Pair> queryParams, RequestBody reqBody,
14691480
Map<String, String> headerParams, Map<String, Object> formParams, String[] authNames,
14701481
ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
14711482
updateParamsForAuth(authNames, queryParams, headerParams);
@@ -1474,12 +1485,20 @@ public class ApiClient {
14741485
final Request.Builder reqBuilder = new Request.Builder().url(url);
14751486
processHeaderParams(headerParams, reqBuilder);
14761487
1477-
String contentType = headerParams.get("Content-Type");
1478-
// ensuring a default content type
1479-
if (contentType == null) {
1480-
contentType = "application/json";
1488+
Request request = null;
1489+
1490+
if (progressRequestListener != null && reqBody != null) {
1491+
ProgressRequestBody progressRequestBody = new ProgressRequestBody(reqBody, progressRequestListener);
1492+
request = reqBuilder.method(method, progressRequestBody).build();
1493+
} else {
1494+
request = reqBuilder.method(method, reqBody).build();
14811495
}
14821496

1497+
return request;
1498+
}
1499+
1500+
private RequestBody createRequestBody(String method, Object body, Map<String, Object> formParams,
1501+
String contentType) throws ApiException {
14831502
RequestBody reqBody;
14841503
if (!HttpMethod.permitsRequestBody(method)) {
14851504
reqBody = null;
@@ -1506,17 +1525,7 @@ public class ApiClient {
15061525
reqBody = serialize(body, contentType);
15071526
}
15081527
}
1509-
1510-
Request request = null;
1511-
1512-
if (progressRequestListener != null && reqBody != null) {
1513-
ProgressRequestBody progressRequestBody = new ProgressRequestBody(reqBody, progressRequestListener);
1514-
request = reqBuilder.method(method, progressRequestBody).build();
1515-
} else {
1516-
request = reqBuilder.method(method, reqBody).build();
1517-
}
1518-
1519-
return request;
1528+
return reqBody;
15201529
}
15211530

15221531
/**

src/main/java/Invokers/ApiClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,7 +1347,7 @@ public Call buildCall(String path, String method, List<Pair> queryParams, Object
13471347
contentType = "application/json";
13481348
}
13491349
RequestBody requestbody = createRequestBody(method, body, formParams, contentType);
1350-
1350+
13511351
callAuthenticationHeader(method, path, requestbody, queryParams, requestHeaderMap);
13521352

13531353
if (merchantConfig.isEnableClientCert()) {
@@ -1496,7 +1496,7 @@ public Request buildRequest(String path, String method, List<Pair> queryParams,
14961496

14971497
return request;
14981498
}
1499-
1499+
15001500
private RequestBody createRequestBody(String method, Object body, Map<String, Object> formParams,
15011501
String contentType) throws ApiException {
15021502
RequestBody reqBody;

0 commit comments

Comments
 (0)