Skip to content

Commit

Permalink
AGO-8289 | Fixes infinite loop when session is expired Silverpop#20
Browse files Browse the repository at this point in the history
  • Loading branch information
ergin-a1 committed Mar 23, 2017
1 parent 3b26f8e commit 95f081a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 65 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<groupId>com.silverpop</groupId>
<artifactId>spapi-client</artifactId>
<packaging>jar</packaging>
<version>1.2</version>
<version>1.2.1</version>
<dependencies>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
Expand Down
130 changes: 66 additions & 64 deletions src/main/java/com/silverpop/api/client/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.io.IOException;
import java.util.Map;

import com.silverpop.api.client.authentication.LogoutCommand;
import com.silverpop.api.client.xmlapi.XmlApiSession;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethodBase;
Expand All @@ -14,35 +16,35 @@ public abstract class ApiClient<REQUEST extends ApiRequest> {

private Log log = LogFactory.getLog(this.getClass());

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


public ApiSession getSession() {
return session;
}
return session;
}


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

protected ApiClient(ApiCommandProcessor<REQUEST> commandProcessor, HttpClient httpClient, ApiSession session) {
this.commandProcessor = commandProcessor;
this.httpClient = httpClient;
this.session = session;
}
protected ApiClient(ApiCommandProcessor<REQUEST> commandProcessor, HttpClient httpClient, ApiSession session) {
this.commandProcessor = commandProcessor;
this.httpClient = httpClient;
this.session = session;
}

public ApiResult executeCommand(ApiCommand command) throws ApiResultException {
return executeCommand(command, null);
}
public ApiResult executeCommand(ApiCommand command) throws ApiResultException {
return executeCommand(command, null);
}

public ApiResult executeCommand(ApiCommand command, Map<String,String> requestHeaders) throws ApiResultException {
public ApiResult executeCommand(ApiCommand command, Map<String, String> requestHeaders) throws ApiResultException {
try {
return validateSessionAndExecuteCommand(command, requestHeaders);
} catch(ApiResultException e) {
if(retryCommand(e.getErrorResult())) {
} catch (ApiResultException e) {
if (retryCommand(e.getErrorResult(),command)) {
getSession().close();
return validateSessionAndExecuteCommand(command, requestHeaders);
} else {
Expand All @@ -51,71 +53,71 @@ public ApiResult executeCommand(ApiCommand command, Map<String,String> requestHe
}
}

private boolean retryCommand(ApiErrorResult errorResult) {
return errorResult.isSessionLost() && getSession().isReAuthenticate();
}
private boolean retryCommand(ApiErrorResult errorResult, ApiCommand apiCommand) {
return errorResult.isSessionLost() && getSession().isReAuthenticate() && !(apiCommand instanceof LogoutCommand);
}

private ApiResult validateSessionAndExecuteCommand(ApiCommand command, Map<String,String> requestHeaders) throws ApiResultException {
ensureSessionIsOpen();
private ApiResult validateSessionAndExecuteCommand(ApiCommand command, Map<String, String> requestHeaders) throws ApiResultException {
ensureSessionIsOpen();

REQUEST request = commandProcessor.prepareRequest(command);
REQUEST request = commandProcessor.prepareRequest(command);
addAdditionalHeadersToRequest(request, requestHeaders);
addAdditionalHeadersToRequest(request, getSession().getDefaultHeaders());

HttpMethodBase method = commandProcessor.prepareMethod(getSession().getUrl(), request);
String in = executeMethod(method);
String in = executeMethod(method);

ApiResponse response = commandProcessor.processResponse(in, request.getResultType());

return extractResult(command.getClass().getName(), response);
}
ApiResponse response = commandProcessor.processResponse(in, request.getResultType());

private void addAdditionalHeadersToRequest(REQUEST request, Map<String,String> requestHeaders) {
return extractResult(command.getClass().getName(), response);
}

private void addAdditionalHeadersToRequest(REQUEST request, Map<String, String> requestHeaders) {
if (requestHeaders != null) {
for (String key : requestHeaders.keySet()) {
request.addHeader(key, requestHeaders.get(key));
}
}
}

private void ensureSessionIsOpen() {
if(!getSession().isOpen()) {
getSession().open();
}
}
private void ensureSessionIsOpen() {
if (!getSession().isOpen()) {
getSession().open();
}
}

protected String executeMethod(HttpMethodBase method) throws ApiResultException {
try {
protected String executeMethod(HttpMethodBase method) throws ApiResultException {
try {
log.info("executing method:" + method);
int responseCode = httpClient.executeMethod(method);

String responseBody = method.getResponseBodyAsString();
if (responseBody != null && !responseBody.isEmpty()) {
return responseBody;
} else {
StringBuilder buffer = new StringBuilder();
buffer.append("No response body was returned!\nResponse Headers-\n");
Header[] headers = method.getResponseHeaders();
for (Header header : headers) {
buffer.append(header.getName()).append(": ").append(header.getValue()).append("\n");
}
int responseCode = httpClient.executeMethod(method);

String responseBody = method.getResponseBodyAsString();
if (responseBody != null && !responseBody.isEmpty()) {
return responseBody;
} else {
StringBuilder buffer = new StringBuilder();
buffer.append("No response body was returned!\nResponse Headers-\n");
Header[] headers = method.getResponseHeaders();
for (Header header : headers) {
buffer.append(header.getName()).append(": ").append(header.getValue()).append("\n");
}
buffer.append("HTTP-Response-Code: ").append(responseCode).append("\n");
buffer.append("Content length reported as: ").append(method.getResponseContentLength());
buffer.append("Content length reported as: ").append(method.getResponseContentLength());
log.info(buffer.toString());
throw new ApiResultException("Error executing API: " + buffer.toString(), new NoResponseApiErrorResult());
}
} catch(IOException e) {
throw new ApiException("Error executing API: ", e);
}
}

private ApiResult extractResult(String requestName, ApiResponse response) throws ApiResultException {
if(response.isSuccessful()) {
return response.buildResult();
} else {
}
} catch (IOException e) {
throw new ApiException("Error executing API: ", e);
}
}

private ApiResult extractResult(String requestName, ApiResponse response) throws ApiResultException {
if (response.isSuccessful()) {
return response.buildResult();
} else {
log.debug("Got Error Response");
String msg = String.format("API call '%s' unsuccessful.", requestName);
throw new ApiResultException(msg, response.buildErrorResult());
}
}
String msg = String.format("API call '%s' unsuccessful.", requestName);
throw new ApiResultException(msg, response.buildErrorResult());
}
}
}

0 comments on commit 95f081a

Please sign in to comment.