Skip to content

Commit 1ef619e

Browse files
committed
Merge branch 'feature/intermediate-host' of https://github.com/CyberSource/cybersource-rest-client-java into feature/mar23-rel-updates
2 parents f14fc99 + ac49b8a commit 1ef619e

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import javax.net.ssl.*;
4444
import com.google.gson.Gson;
4545
import com.google.gson.reflect.TypeToken;
4646

47+
import org.apache.commons.lang.StringUtils;
4748
import org.apache.logging.log4j.LogManager;
4849
import org.apache.logging.log4j.Logger;
4950

@@ -734,7 +735,9 @@ public class ApiClient {
734735
* @return ApiClient
735736
*/
736737
public ApiClient addDefaultHeader(String key, String value) {
737-
defaultHeaderMap.put(key, value);
738+
if (!defaultHeaderMap.containsKey(key)) {
739+
defaultHeaderMap.put(key, value);
740+
}
738741
return this;
739742
}
740743

@@ -1324,6 +1327,13 @@ public class ApiClient {
13241327
public Call buildCall(String path, String method, List<Pair> queryParams, Object body,
13251328
Map<String, String> headerParams, Map<String, Object> formParams, String[] authNames,
13261329
ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
1330+
1331+
if(merchantConfig.getDefaultHeaders() != null && !merchantConfig.getDefaultHeaders().isEmpty()) {
1332+
for (Entry<String, String> header : merchantConfig.getDefaultHeaders().entrySet()) {
1333+
addDefaultHeader(header.getKey(), header.getValue());
1334+
}
1335+
}
1336+
13271337
callAuthenticationHeader(method, path, body, queryParams);
13281338

13291339
if (merchantConfig.isEnableClientCert()) {
@@ -1336,8 +1346,10 @@ public class ApiClient {
13361346
headerParams.remove("Accept");
13371347
headerParams.put("Accept", defaultAcceptHeader);
13381348
}
1339-
1349+
13401350
headerParams.putAll(defaultHeaderMap);
1351+
1352+
13411353
logger.info("Request Header Parameters:\n{}", new PrettyPrintingMap<String, String>(headerParams));
13421354
Request request = buildRequest(path, method, queryParams, body, headerParams, formParams, authNames,
13431355
progressRequestListener);
@@ -1502,8 +1514,18 @@ public class ApiClient {
15021514
*/
15031515
public String buildUrl(String path, List<Pair> queryParams) {
15041516
final StringBuilder url = new StringBuilder();
1505-
url.append(GlobalLabelParameters.URL_PREFIX).append(merchantConfig.getRequestHost().trim()).append(path);
1506-
1517+
if (StringUtils.isNotBlank(merchantConfig.getIntermediateHost())) {
1518+
if (merchantConfig.getIntermediateHost().startsWith(GlobalLabelParameters.URL_PREFIX)
1519+
|| merchantConfig.getIntermediateHost().startsWith("http://")) {
1520+
url.append(merchantConfig.getIntermediateHost().trim()).append(path);
1521+
} else {
1522+
url.append(GlobalLabelParameters.URL_PREFIX).append(merchantConfig.getIntermediateHost().trim())
1523+
.append(path);
1524+
}
1525+
} else {
1526+
url.append(GlobalLabelParameters.URL_PREFIX).append(merchantConfig.getRequestHost().trim()).append(path);
1527+
}
1528+
15071529
if (queryParams != null && !queryParams.isEmpty()) {
15081530
// support (constant) query string in `path`, e.g. "/posts?draft=1"
15091531
String prefix = path.contains("?") ? "&" : "?";

src/main/java/Invokers/ApiClient.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import com.google.gson.Gson;
4545
import com.google.gson.reflect.TypeToken;
4646

47+
import org.apache.commons.lang.StringUtils;
4748
import org.apache.logging.log4j.LogManager;
4849
import org.apache.logging.log4j.Logger;
4950

@@ -734,7 +735,9 @@ public ApiClient setUserAgent(String userAgent) {
734735
* @return ApiClient
735736
*/
736737
public ApiClient addDefaultHeader(String key, String value) {
737-
defaultHeaderMap.put(key, value);
738+
if (!defaultHeaderMap.containsKey(key)) {
739+
defaultHeaderMap.put(key, value);
740+
}
738741
return this;
739742
}
740743

@@ -1323,7 +1326,14 @@ public <T> T handleResponse(Response response, Type returnType) throws ApiExcept
13231326
*/
13241327
public Call buildCall(String path, String method, List<Pair> queryParams, Object body,
13251328
Map<String, String> headerParams, Map<String, Object> formParams, String[] authNames,
1326-
ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
1329+
ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
1330+
1331+
if(merchantConfig.getDefaultHeaders() != null && !merchantConfig.getDefaultHeaders().isEmpty()) {
1332+
for (Entry<String, String> header : merchantConfig.getDefaultHeaders().entrySet()) {
1333+
addDefaultHeader(header.getKey(), header.getValue());
1334+
}
1335+
}
1336+
13271337
callAuthenticationHeader(method, path, body, queryParams);
13281338

13291339
if (merchantConfig.isEnableClientCert()) {
@@ -1338,6 +1348,7 @@ public Call buildCall(String path, String method, List<Pair> queryParams, Object
13381348
}
13391349

13401350
headerParams.putAll(defaultHeaderMap);
1351+
13411352
logger.info("Request Header Parameters:\n{}", new PrettyPrintingMap<String, String>(headerParams));
13421353
Request request = buildRequest(path, method, queryParams, body, headerParams, formParams, authNames,
13431354
progressRequestListener);
@@ -1502,7 +1513,17 @@ public Request buildRequest(String path, String method, List<Pair> queryParams,
15021513
*/
15031514
public String buildUrl(String path, List<Pair> queryParams) {
15041515
final StringBuilder url = new StringBuilder();
1505-
url.append(GlobalLabelParameters.URL_PREFIX).append(merchantConfig.getRequestHost().trim()).append(path);
1516+
if (StringUtils.isNotBlank(merchantConfig.getIntermediateHost())) {
1517+
if (merchantConfig.getIntermediateHost().startsWith(GlobalLabelParameters.URL_PREFIX)
1518+
|| merchantConfig.getIntermediateHost().startsWith("http://")) {
1519+
url.append(merchantConfig.getIntermediateHost().trim()).append(path);
1520+
} else {
1521+
url.append(GlobalLabelParameters.URL_PREFIX).append(merchantConfig.getIntermediateHost().trim())
1522+
.append(path);
1523+
}
1524+
} else {
1525+
url.append(GlobalLabelParameters.URL_PREFIX).append(merchantConfig.getRequestHost().trim()).append(path);
1526+
}
15061527

15071528
if (queryParams != null && !queryParams.isEmpty()) {
15081529
// support (constant) query string in `path`, e.g. "/posts?draft=1"

0 commit comments

Comments
 (0)