private byte[] doRequest(HttpRequestBase request, TokenHolder tokenHolder) {
return doRequest(request, tokenHolder, null);
}
private byte[] doRequest(HttpRequestBase request, TokenHolder tokenHolder, HttpParameters oauthParams) {
CloseableHttpResponse response = null;
try {
OAuthConsumer consumer = tokenHolder.createConsumer();
consumer.setAdditionalParameters(oauthParams);
consumer.sign(request);
response = client.execute(request);
int statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
byte[] content = entity != null ? readBytes(entity.getContent()) : new byte[0];
switch (statusCode) {
case 200:
case 204:
// all is well, return
return content;
// there was some sort of error - throw the right exception
case 400:
case 401:
case 403:
throw buildException(statusCode, content, tokenHolder.hasToken());
case 404:
throw new NotFoundException(request.getRequestLine().getUri());
case 500:
throw new InternalErrorException();
default:
throw new UnexpectedResponseException(statusCode, new String(content));
}
} catch (OAuthException e) {
throw new InternalConfigurationException("Failed to create OAuth signature", e);
} catch (IOException e) {
throw new ConnectionException("Failed to connect to BellaDati", e);
} finally {
try {
if (response != null) {
response.close();
}
} catch (IOException e) {
throw new ConnectionException("Failed to connect to BellaDati", e);
}
request.releaseConnection();