httpRequest.disconnect();
throw new RemoteCommunicationException("Unable to post request: " + e.getMessage(), e);
}
// Get response
JaxbExceptionResponse exceptionResponse = null;
JaxbCommandsResponse commandResponse = null;
int responseStatus = httpResponse.code();
try {
String content = httpResponse.body();
if( responseStatus < 300 ) {
commandResponse = deserializeResponseContent(content, JaxbCommandsResponse.class);
} else {
String contentType = httpResponse.contentType();
if( contentType.equals(MediaType.APPLICATION_XML) ) {
exceptionResponse = deserializeResponseContent(content, JaxbExceptionResponse.class);
} else if( contentType.startsWith(MediaType.TEXT_HTML) ) {
exceptionResponse = new JaxbExceptionResponse();
Document doc = Jsoup.parse(content);
String body = doc.body().text();
exceptionResponse.setMessage(body);
exceptionResponse.setUrl(httpRequest.getUri().toString());
exceptionResponse.setStackTrace("");
}
else {
throw new RemoteCommunicationException("Unable to deserialize response with content type '" + contentType + "'");
}
}
} catch( Exception e ) {
logger.error("Unable to retrieve response content from request with status {}: {}", e.getMessage(), e);
throw new RemoteCommunicationException("Unable to retrieve content from response!", e);
} finally {
httpRequest.disconnect();
}
if( commandResponse != null ) {
List<JaxbCommandResponse<?>> responses = commandResponse.getResponses();
if( responses.size() == 0 ) {
return null;
} else if( responses.size() == 1 ) {
JaxbCommandResponse<?> responseObject = responses.get(0);
if( responseObject instanceof JaxbExceptionResponse ) {
exceptionResponse = (JaxbExceptionResponse) responseObject;
} else {
return (T) responseObject.getResult();
}
} else {
throw new RemoteCommunicationException("Unexpected number of results from " + command.getClass().getSimpleName()
+ ":" + responses.size() + " results instead of only 1");
}
}
logger.error("Response with status {} returned.", responseStatus);
// Process exception response
switch ( responseStatus ) {
case 409:
throw new RemoteTaskException(exceptionResponse.getMessage() + ":\n" + exceptionResponse.getStackTrace());
default:
if( exceptionResponse != null ) {
throw new RemoteApiException(exceptionResponse.getMessage() + ":\n" + exceptionResponse.getStackTrace());
} else {
throw new RemoteCommunicationException("Unable to communicate with remote API via URL "
+ "'" + httpRequest.getUri().toString() + "'");
}
}