Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor code refactoring of HttpClientTransport #1281

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, String> header = new HashMap<String, String>();

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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -238,7 +208,7 @@ private synchronized void initializeHttpClient() throws UnknownHostException {
}
isHttpClientInitialized = true;
}

@Override
public synchronized InputStream getContent() throws IOException {
serverInvocationCount++;
Expand Down Expand Up @@ -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<String, String> header = new HashMap<String, String>();

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<String, String> httpHeaders) throws IOException {
return connect(endpoint, httpHeaders, true);
Expand Down Expand Up @@ -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 {
Expand Down
Loading