Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change docker http client to use handlers instead of results to avoid leaving open connections #232

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ private DockerAccess createDockerAccess(String baseUrl) throws MojoExecutionExce

return client;
}
catch (IOException | DockerAccessException e) {
catch (IOException e) {
throw new MojoExecutionException("Cannot create docker access object ", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.jolokia.docker.maven.access;

import java.io.IOException;

/**
* Exception thrown if access to the docker host fails
*
* @author roland
* @since 20.10.14
*/
public class DockerAccessException extends Exception {
public class DockerAccessException extends IOException {

/**
* Constructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.jolokia.docker.maven.access.chunked;

import org.jolokia.docker.maven.access.DockerAccessException;

import java.io.IOException;
import java.io.InputStream;

Expand All @@ -15,7 +13,7 @@ public ChunkedResponseReader(InputStream stream, ChunkedResponseHandler<String>
this.handler = handler;
}

public void process() throws IOException, DockerAccessException {
public void process() throws IOException {
int len;
int size = 8129;
byte[] buf = new byte[size];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package org.jolokia.docker.maven.access.hc;

import java.io.*;
import com.google.common.net.MediaType;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.*;
import org.apache.http.client.methods.*;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
Expand All @@ -15,126 +22,167 @@

public class ApacheHttpClientDelegate {

private static final String HEADER_ACCEPT = "Accept";
private static final String HEADER_ACCEPT_ALL = "*/*";
private final CloseableHttpClient httpClient;

private final CloseableHttpClient httpClient;
public ApacheHttpClientDelegate(CloseableHttpClient httpClient) {
this.httpClient = httpClient;
}

public ApacheHttpClientDelegate(CloseableHttpClient httpClient) {
this.httpClient = httpClient;
}
public int delete(String url, int... statusCodes) throws IOException {
return delete(url, new StatusCodeResponseHandler(), statusCodes);
}

public Result delete(String url, int statusCode, int... additional) throws IOException, HttpRequestException {
return parseResponse(httpClient.execute(newDelete(url)), statusCode, additional);
}
public static class StatusCodeResponseHandler implements ResponseHandler<Integer> {

public Result get(String url, int statusCode, int... additional) throws IOException, HttpRequestException {
return parseResponse(httpClient.execute(newGet(url)), statusCode, additional);
@Override
public Integer handleResponse(HttpResponse response)
throws IOException {
return response.getStatusLine().getStatusCode();
}
}

public <T> T delete(String url, ResponseHandler<T> responseHandler, int... statusCodes)
throws IOException {
return httpClient.execute(newDelete(url),
new StatusCodeCheckerResponseHandler<>(responseHandler,
statusCodes));
}

public String get(String url, int... statusCodes) throws IOException {
return httpClient.execute(newGet(url), new StatusCodeCheckerResponseHandler<>(
new BodyResponseHandler(), statusCodes));
}

public <T> T get(String url, ResponseHandler<T> responseHandler, int... statusCodes)
throws IOException {
return httpClient
.execute(newGet(url), new StatusCodeCheckerResponseHandler<>(responseHandler, statusCodes));
}

public static class BodyResponseHandler implements ResponseHandler<String> {

@Override
public String handleResponse(HttpResponse response)
throws IOException {
return getResponseMessage(response);
}
}

private static String getResponseMessage(HttpResponse response) throws IOException {
return (response.getEntity() == null) ? null
: EntityUtils.toString(response.getEntity()).trim();
}

public <T> T post(String url, Object body, Map<String, String> headers,
ResponseHandler<T> responseHandler, int... statusCodes) throws IOException {
HttpUriRequest request = newPost(url, body);
for (Entry<String, String> entry : headers.entrySet()) {
request.addHeader(entry.getKey(), entry.getValue());
}

public Result post(String url, Object body, Map<String, String> headers, int statusCode, int... additional) throws IOException,
HttpRequestException {
return httpClient.execute(request, new StatusCodeCheckerResponseHandler<>(responseHandler,
statusCodes));
}

HttpUriRequest request = newPost(url, body);
for (Entry<String, String> entry : headers.entrySet()) {
request.addHeader(entry.getKey(), entry.getValue());
}
public <T> T post(String url, Object body, ResponseHandler<T> responseHandler,
int... statusCodes) throws IOException {
return httpClient.execute(newPost(url, body),
new StatusCodeCheckerResponseHandler<>(responseHandler,
statusCodes));
}

return parseResponse(httpClient.execute(request), statusCode, additional);
}
public int post(String url, Object body,
int... statusCodes) throws IOException {
return post(url, body, new StatusCodeResponseHandler(), statusCodes);
}

public Result post(String url, Object body, int statusCode, int... additional) throws IOException, HttpRequestException {
return parseResponse(httpClient.execute(newPost(url, body)), statusCode, additional);
}

public CloseableHttpClient getHttpClient() {
return httpClient;
}
public CloseableHttpClient getHttpClient() {
return httpClient;
}

// =========================================================================================
// =========================================================================================

private HttpUriRequest addDefaultHeaders(HttpUriRequest req) {
req.addHeader(HEADER_ACCEPT, HEADER_ACCEPT_ALL);
req.addHeader("Content-Type", "application/json");
return req;
}
private HttpUriRequest addDefaultHeaders(HttpUriRequest req) {
req.addHeader(HttpHeaders.ACCEPT, "*/*");
req.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.JSON_UTF_8.toString());
return req;
}


private HttpUriRequest newDelete(String url) {
return addDefaultHeaders(new HttpDelete(url));
}

private HttpUriRequest newDelete(String url) {
return addDefaultHeaders(new HttpDelete(url));
}
private HttpUriRequest newGet(String url) {
return addDefaultHeaders(new HttpGet(url));
}

private HttpUriRequest newPost(String url, Object body) {
HttpPost post = new HttpPost(url);

private HttpUriRequest newGet(String url) {
return addDefaultHeaders(new HttpGet(url));
if (body != null) {
if (body instanceof File) {
post.setEntity(new FileEntity((File) body));
} else {
post.setEntity(new StringEntity((String) body, Charset.defaultCharset()));
}
}
return addDefaultHeaders(post);
}

private HttpUriRequest newPost(String url, Object body) {
HttpPost post = new HttpPost(url);
public static class StatusCodeCheckerResponseHandler<T> implements ResponseHandler<T> {

if (body != null) {
if (body instanceof File) {
post.setEntity(new FileEntity((File) body));
}
else {
post.setEntity(new StringEntity((String) body, Charset.defaultCharset()));
}
}
return addDefaultHeaders(post);
private int[] statusCodes;
private ResponseHandler<T> delegate;

public StatusCodeCheckerResponseHandler(ResponseHandler<T> delegate, int... statusCodes) {
this.statusCodes = statusCodes;
this.delegate = delegate;
}

private Result parseResponse(HttpResponse response, int successCode, int... additional) throws HttpRequestException {
HttpEntity entity = response.getEntity();
@Override
public T handleResponse(HttpResponse response) throws IOException {
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
for (int code : statusCodes) {
if (statusCode == code) {
return delegate.handleResponse(response);
}
}

StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
String reason = statusLine.getReasonPhrase().trim();
String reason = statusLine.getReasonPhrase().trim();
throw new HttpRequestException(String.format("%s (%s: %d)", getResponseMessage(response),
reason, statusCode));
}

if (statusCode == successCode) {
return new Result(statusCode, entity);
}
}

for (int code : additional) {
if (statusCode == code) {
return new Result(code, entity);
}
}
public static class BodyAndStatusResponseHandler implements ResponseHandler<HttpBodyAndStatus> {

Result result = new Result(statusCode, entity);
throw new HttpRequestException(String.format("%s (%s: %d)", result.getMessage(), reason, statusCode));
@Override
public HttpBodyAndStatus handleResponse(HttpResponse response)
throws IOException {
return new HttpBodyAndStatus(response.getStatusLine().getStatusCode(),
getResponseMessage(response));
}
}

public static class Result
{
private final int code;
private final HttpEntity entity;
private String message;
public static class HttpBodyAndStatus {

public Result(int code, HttpEntity entity)
{
this.code = code;
this.entity = entity;
}
private final int statusCode;
private final String body;

public int getCode() {
return code;
}
public HttpBodyAndStatus(int statusCode, String body) {
this.statusCode = statusCode;
this.body = body;
}

public InputStream getInputStream() throws IOException {
return entity.getContent();
}
public int getStatusCode() {
return statusCode;
}

public String getMessage() {
try {
if (message != null) {
return message;
}
message = (entity == null) ? null : EntityUtils.toString(entity).trim();
return message;
}
catch (IOException e) {
return "Unknown error - failed to read response content";
}
}
public String getBody() {
return body;
}
}
}
Loading