diff options
Diffstat (limited to 'src/main/java/com/p4square/ccbapi/ApacheHttpClientImpl.java')
-rw-r--r-- | src/main/java/com/p4square/ccbapi/ApacheHttpClientImpl.java | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/main/java/com/p4square/ccbapi/ApacheHttpClientImpl.java b/src/main/java/com/p4square/ccbapi/ApacheHttpClientImpl.java new file mode 100644 index 0000000..0833c67 --- /dev/null +++ b/src/main/java/com/p4square/ccbapi/ApacheHttpClientImpl.java @@ -0,0 +1,90 @@ +package com.p4square.ccbapi; + +import com.p4square.ccbapi.exception.CCBException; +import com.p4square.ccbapi.exception.CCBRetryableErrorException; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.NameValuePair; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.util.EntityUtils; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * ApacheHttpClientImpl is an implementation of HTTPInterface which uses the Apache HTTP Client library. + */ +public class ApacheHttpClientImpl implements HTTPInterface { + + private final DefaultHttpClient httpClient; + + public ApacheHttpClientImpl(final URI apiBaseUri, final String username, final String password) { + // Create the HTTP client. + this.httpClient = new DefaultHttpClient(); + + // Prepare the CredentialsProvider for the HTTP Client. + int port = apiBaseUri.getPort(); + if (port == -1) { + if ("http".equalsIgnoreCase(apiBaseUri.getScheme())) { + port = 80; + } else if ("https".equalsIgnoreCase(apiBaseUri.getScheme())) { + port = 443; + } else { + throw new IllegalArgumentException("Cannot determine port for unknown scheme."); + } + } + this.httpClient.getCredentialsProvider().setCredentials(new AuthScope(apiBaseUri.getHost(), port), + new UsernamePasswordCredentials(username, password)); + } + + @Override + public void close() throws IOException { + httpClient.getConnectionManager().shutdown(); + } + + @Override + public InputStream sendPostRequest(URI uri, Map<String, String> form) throws IOException { + // Build the request. + final HttpPost httpPost = new HttpPost(uri); + final List<NameValuePair> formParameters = new ArrayList<>(); + for (Map.Entry<String, String> entry : form.entrySet()) { + formParameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); + } + httpPost.setEntity(new UrlEncodedFormEntity(formParameters)); + + // Make the request. + final HttpResponse response = httpClient.execute(httpPost); + + // Process the response. + final int statusCode = response.getStatusLine().getStatusCode(); + if (statusCode != 200) { + // Consume the entity and close the connection. + EntityUtils.consume(response.getEntity()); + + // Determine the type of failure and throw an exception. + if (statusCode >= 400 && statusCode < 500) { + throw new CCBException("Unexpected non-retryable error: " + response.getStatusLine().toString()); + } else if (statusCode >= 500 && statusCode < 600) { + throw new CCBRetryableErrorException("Retryable error: " + response.getStatusLine().toString()); + } else { + throw new CCBException("Unexpected status code: " + response.getStatusLine().toString()); + } + } + + final HttpEntity entity = response.getEntity(); + if (entity != null) { + return entity.getContent(); + } else { + return null; + } + } +} |