11package com .github .dockerjava .client ;
22
3- import static com .google .common .base .Preconditions .checkNotNull ;
4- import static org .apache .commons .io .IOUtils .closeQuietly ;
5-
6- import java .io .*;
7-
3+ import com .fasterxml .jackson .jaxrs .json .JacksonJsonProvider ;
84import com .github .dockerjava .client .command .*;
9-
5+ import com .github .dockerjava .client .model .AuthConfig ;
6+ import com .github .dockerjava .client .utils .JsonClientFilter ;
107import org .apache .commons .io .IOUtils ;
118import org .apache .commons .io .LineIterator ;
12- import org .apache .http .client .HttpClient ;
13- import org .apache .http .conn .scheme .PlainSocketFactory ;
14- import org .apache .http .conn .scheme .Scheme ;
15- import org .apache .http .conn .scheme .SchemeRegistry ;
16- import org .apache .http .conn .ssl .SSLSocketFactory ;
17- import org .apache .http .impl .client .DefaultHttpClient ;
18- import org .apache .http .impl .conn .PoolingClientConnectionManager ;
9+ import org .glassfish .jersey .client .ClientConfig ;
10+ import org .glassfish .jersey .client .ClientProperties ;
1911
20- import com .github .dockerjava .client .model .AuthConfig ;
12+ import javax .ws .rs .client .Client ;
13+ import javax .ws .rs .client .ClientBuilder ;
14+ import javax .ws .rs .client .WebTarget ;
15+ import javax .ws .rs .core .Response ;
16+ import java .io .*;
2117
22- import com .github .dockerjava .client .utils .JsonClientFilter ;
23- import com .sun .jersey .api .client .Client ;
24- import com .sun .jersey .api .client .ClientResponse ;
25- import com .sun .jersey .api .client .WebResource ;
26- import com .sun .jersey .api .client .config .ClientConfig ;
27- import com .sun .jersey .api .client .config .DefaultClientConfig ;
28- import com .sun .jersey .client .apache4 .ApacheHttpClient4 ;
29- import com .sun .jersey .client .apache4 .ApacheHttpClient4Handler ;
18+ import static com .google .common .base .Preconditions .checkNotNull ;
3019
3120/**
3221 * @author Konstantin Pelykh (kpelykh@gmail.com)
@@ -36,7 +25,7 @@ public class DockerClient implements Closeable {
3625 private final Client client ;
3726
3827 private final CommandFactory cmdFactory ;
39- private final WebResource baseResource ;
28+ private final WebTarget baseResource ;
4029 private final DockerClientConfig dockerClientConfig ;
4130
4231 public DockerClient () {
@@ -61,51 +50,35 @@ public DockerClient(DockerClientConfig dockerClientConfig, CommandFactory cmdFac
6150 this .cmdFactory = cmdFactory ;
6251 this .dockerClientConfig = dockerClientConfig ;
6352
64- HttpClient httpClient = getPoolingHttpClient (dockerClientConfig );
65- ClientConfig clientConfig = new DefaultClientConfig ();
66- client = new ApacheHttpClient4 (new ApacheHttpClient4Handler (httpClient ,
67- null , false ), clientConfig );
68-
69- if (dockerClientConfig .getReadTimeout () != null ) {
70- client .setReadTimeout (dockerClientConfig .getReadTimeout ());
71- }
72-
73- client .addFilter (new JsonClientFilter ());
53+ ClientConfig clientConfig = new ClientConfig ();
7454
75- if (dockerClientConfig .isLoggingFilterEnabled ())
76- client .addFilter (new SelectiveLoggingFilter ());
55+ if (dockerClientConfig .getReadTimeout () != null ) {
56+ clientConfig .property (ClientProperties .READ_TIMEOUT , dockerClientConfig .getReadTimeout ());
57+ }
7758
78- WebResource webResource = client .resource (dockerClientConfig .getUri ());
79-
80- if (dockerClientConfig .getVersion () != null ) {
81- baseResource = webResource .path ("v" + dockerClientConfig .getVersion ());
82- } else {
83- baseResource = webResource ;
84- }
85- }
59+ clientConfig .register (JsonClientFilter .class );
60+ clientConfig .register (JacksonJsonProvider .class );
8661
87- private HttpClient getPoolingHttpClient (DockerClientConfig dockerClientConfig ) {
88- SchemeRegistry schemeRegistry = new SchemeRegistry ();
89- schemeRegistry .register (new Scheme ("http" , dockerClientConfig .getUri ().getPort (),
90- PlainSocketFactory .getSocketFactory ()));
91- schemeRegistry .register (new Scheme ("https" , 443 , SSLSocketFactory
92- .getSocketFactory ()));
62+ if (dockerClientConfig .isLoggingFilterEnabled ()) {
63+ clientConfig .register (SelectiveLoggingFilter .class );
64+ }
9365
94- PoolingClientConnectionManager cm = new PoolingClientConnectionManager (schemeRegistry );
95- // Increase max total connection
96- cm .setMaxTotal (1000 );
97- // Increase default max connection per route
98- cm .setDefaultMaxPerRoute (1000 );
66+ client = ClientBuilder .newBuilder ().withConfig (clientConfig ).build ();
67+ WebTarget webResource = client .target (dockerClientConfig .getUri ());
9968
100- return new DefaultHttpClient (cm );
69+ if (dockerClientConfig .getVersion () != null ) {
70+ baseResource = webResource .path ("v" + dockerClientConfig .getVersion ());
71+ } else {
72+ baseResource = webResource ;
73+ }
10174 }
10275
10376 public <RES_T > RES_T execute (AbstrDockerCmd <?, RES_T > command )
10477 throws DockerException {
10578 return command .withBaseResource (baseResource ).exec ();
10679 }
10780
108- public AuthConfig authConfig () throws DockerException {
81+ public AuthConfig authConfig () throws DockerException {
10982 checkNotNull (dockerClientConfig .getUsername (), "Configured username is null." );
11083 checkNotNull (dockerClientConfig .getPassword (), "Configured password is null." );
11184 checkNotNull (dockerClientConfig .getEmail (), "Configured email is null." );
@@ -263,28 +236,29 @@ public TagImageCmd tagImageCmd(String imageId, String repository, String tag) {
263236 }
264237
265238 // TODO This is only being used by the test code for logging. Is it really necessary?
266- /**
267- * @return The output slurped into a string.
268- */
269- public static String asString (ClientResponse response ) throws IOException {
270-
271- StringWriter out = new StringWriter ();
272- try {
273- LineIterator itr = IOUtils .lineIterator (
274- response .getEntityInputStream (), "UTF-8" );
275- while (itr .hasNext ()) {
276- String line = itr .next ();
277- out .write (line + (itr .hasNext () ? "\n " : "" ));
278- }
279- } finally {
280- closeQuietly (response .getEntityInputStream ());
281- }
282- return out .toString ();
283- }
239+
240+ /**
241+ * @return The output slurped into a string.
242+ */
243+ public static String asString (Response response ) throws IOException {
244+
245+ StringWriter out = new StringWriter ();
246+ InputStream is = response .readEntity (InputStream .class );
247+ try {
248+ LineIterator itr = IOUtils .lineIterator (is , "UTF-8" );
249+ while (itr .hasNext ()) {
250+ String line = itr .next ();
251+ out .write (line + (itr .hasNext () ? "\n " : "" ));
252+ }
253+ } finally {
254+ IOUtils .closeQuietly (is );
255+ }
256+ return out .toString ();
257+ }
284258
285259 @ Override
286260 public void close () throws IOException {
287- client .destroy ();
261+ client .close ();
288262 }
289263
290264}
0 commit comments