Skip to content
Open
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
20 changes: 15 additions & 5 deletions src/main/java/com/silverpop/api/client/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand All @@ -16,19 +17,26 @@ public abstract class ApiClient<REQUEST extends ApiRequest> {
private Log log = LogFactory.getLog(this.getClass());

private ApiCommandProcessor<REQUEST> commandProcessor;
protected HttpClient httpClient;
protected ApiSession session;

private final HttpClient httpClient;


public ApiSession getSession() {
return session;
}


protected ApiClient(ApiCommandProcessor<REQUEST> commandProcessor, ApiSession session) {
this(commandProcessor, new HttpClient(), session);
this(commandProcessor, new HttpClient(new MultiThreadedHttpConnectionManager()), session);
}

/**
* Constructor.
* @param commandProcessor can not be {@code null}.
* @param httpClient this should be configured with a {@code MultiThreadedHttpConnectionManager}, otherwise the ApiClient constructed will not be thread-safe.
* @param session can not be {@code null}.
*/
protected ApiClient(ApiCommandProcessor<REQUEST> commandProcessor, HttpClient httpClient, ApiSession session) {
this.commandProcessor = commandProcessor;
this.httpClient = httpClient;
Expand Down Expand Up @@ -84,7 +92,7 @@ private void ensureSessionIsOpen() {
}
}

protected String executeMethod(HttpMethodBase method) throws ApiResultException {
private String executeMethod(HttpMethodBase method) throws ApiResultException {
try {
log.info("executing method:" + method);
int responseCode = httpClient.executeMethod(method);
Expand All @@ -106,8 +114,10 @@ protected String executeMethod(HttpMethodBase method) throws ApiResultException
}
} catch(IOException e) {
throw new ApiException("Error executing API: ", e);
}
}
} finally {
method.releaseConnection();
}
}

private ApiResult extractResult(String requestName, ApiResponse response) throws ApiResultException {
if(response.isSuccessful()) {
Expand Down