diff --git a/src/main/java/com/salesforce/dataloader/client/HttpClientTransport.java b/src/main/java/com/salesforce/dataloader/client/HttpClientTransport.java index 3d570950..136bbc30 100644 --- a/src/main/java/com/salesforce/dataloader/client/HttpClientTransport.java +++ b/src/main/java/com/salesforce/dataloader/client/HttpClientTransport.java @@ -26,8 +26,6 @@ package com.salesforce.dataloader.client; import java.io.*; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.net.*; import java.nio.charset.StandardCharsets; import java.util.HashMap; @@ -115,24 +113,9 @@ public synchronized void setConfig(ConnectorConfig newConfig) { } currentConfig = newConfig; } - - @Override - public OutputStream connect(String url, String soapAction) throws IOException { - if (soapAction == null) { - soapAction = ""; - } - - HashMap header = new HashMap(); - - header.put("SOAPAction", "\"" + soapAction + "\""); - header.put("Content-Type", "text/xml; charset=" + StandardCharsets.UTF_8.name()); - header.put("Accept", "text/xml"); - - return connect(url, header); - } private boolean areEquivalentConfigs(ConnectorConfig config1, ConnectorConfig config2) { - if (config1 == null && config2 == null) { + if (config1 == config2) { return true; } else if (config1 == null || config2 == null) { // one of the configs is null, other isn't. They can't be equal. @@ -185,19 +168,6 @@ private boolean areEquivalentConfigs(ConnectorConfig config1, ConnectorConfig co private boolean isHttpClientInitialized = false; - private void setAuthAndClientHeadersForHttpMethod() { - if (this.httpMethod != null - && currentConfig.getSessionId() != null - && !currentConfig.getSessionId().isBlank()) { - this.httpMethod.removeHeaders(AUTH_HEADER_FOR_JSON); - this.httpMethod.removeHeaders(AUTH_HEADER_FOR_XML); - this.httpMethod.removeHeaders(USER_AGENT_HEADER); - this.httpMethod.addHeader(AUTH_HEADER_FOR_JSON, AUTH_HEADER_VALUE_PREFIX + currentConfig.getSessionId()); - this.httpMethod.addHeader(AUTH_HEADER_FOR_XML, AUTH_HEADER_VALUE_PREFIX + currentConfig.getSessionId()); - this.httpMethod.addHeader(USER_AGENT_HEADER, VersionInfo.info()); - } - } - private synchronized void initializeHttpClient() throws UnknownHostException { if (isHttpClientInitialized) { // already initialized. @@ -238,7 +208,7 @@ private synchronized void initializeHttpClient() throws UnknownHostException { } isHttpClientInitialized = true; } - + @Override public synchronized InputStream getContent() throws IOException { serverInvocationCount++; @@ -302,7 +272,22 @@ public HttpResponse getHttpResponse() { public boolean isSuccessful() { return successful; } + + @Override + public OutputStream connect(String url, String soapAction) throws IOException { + if (soapAction == null) { + soapAction = ""; + } + + HashMap header = new HashMap(); + header.put("SOAPAction", "\"" + soapAction + "\""); + header.put("Content-Type", "text/xml; charset=" + StandardCharsets.UTF_8.name()); + header.put("Accept", "text/xml"); + + return connect(url, header); + } + @Override public OutputStream connect(String endpoint, HashMap httpHeaders) throws IOException { return connect(endpoint, httpHeaders, true); @@ -404,6 +389,31 @@ private OutputStream doConnect(String endpoint, return output; } + private void setAuthAndClientHeadersForHttpMethod() { + if (this.httpMethod != null + && currentConfig.getSessionId() != null + && !currentConfig.getSessionId().isBlank()) { + String authSessionId = currentConfig.getSessionId(); + Header authHeaderValForXML = this.httpMethod.getFirstHeader(AUTH_HEADER_FOR_XML); + Header authHeaderValForJSON = this.httpMethod.getFirstHeader(AUTH_HEADER_FOR_XML); + + if (authHeaderValForXML != null) { + authSessionId = authHeaderValForXML.getValue(); + } else if (authHeaderValForJSON != null) { + authSessionId = authHeaderValForJSON.getValue(); + } + if (authHeaderValForXML == null) { + this.httpMethod.addHeader(AUTH_HEADER_FOR_XML, AUTH_HEADER_VALUE_PREFIX + authSessionId); + } + if (authHeaderValForJSON == null) { + this.httpMethod.addHeader(AUTH_HEADER_FOR_JSON, AUTH_HEADER_VALUE_PREFIX + authSessionId); + } + } + Header userAgentHeaderVal = this.httpMethod.getFirstHeader(USER_AGENT_HEADER); + if (userAgentHeaderVal == null) { + this.httpMethod.addHeader(USER_AGENT_HEADER, VersionInfo.info()); + } + } public static void closeConnections() { if (currentHttpClient != null) { try {