1919import java .io .IOException ;
2020import java .io .InputStream ;
2121import java .io .OutputStream ;
22+ import java .io .PrintWriter ;
23+ import java .io .StringWriter ;
2224import java .net .URI ;
2325
2426import org .apache .http .HttpEntity ;
3638import org .apache .http .impl .client .CloseableHttpClient ;
3739import org .apache .http .impl .client .HttpClientBuilder ;
3840import org .apache .http .impl .client .HttpClients ;
39- import org .apache .http .util .EntityUtils ;
4041
4142import org .springframework .boot .buildpack .platform .io .Content ;
4243import org .springframework .boot .buildpack .platform .io .IOConsumer ;
4647 * {@link Http} implementation backed by a {@link HttpClient}.
4748 *
4849 * @author Phillip Webb
50+ * @author Mike Smithson
4951 */
5052class HttpClientHttp implements Http {
5153
@@ -66,21 +68,19 @@ class HttpClientHttp implements Http {
6668 * Perform a HTTP GET operation.
6769 * @param uri the destination URI
6870 * @return the operation response
69- * @throws IOException on IO error
7071 */
7172 @ Override
72- public Response get (URI uri ) throws IOException {
73+ public Response get (URI uri ) {
7374 return execute (new HttpGet (uri ));
7475 }
7576
7677 /**
7778 * Perform a HTTP POST operation.
7879 * @param uri the destination URI
7980 * @return the operation response
80- * @throws IOException on IO error
8181 */
8282 @ Override
83- public Response post (URI uri ) throws IOException {
83+ public Response post (URI uri ) {
8484 return execute (new HttpPost (uri ));
8585 }
8686
@@ -90,11 +90,10 @@ public Response post(URI uri) throws IOException {
9090 * @param contentType the content type to write
9191 * @param writer a content writer
9292 * @return the operation response
93- * @throws IOException on IO error
9493 */
9594
9695 @ Override
97- public Response post (URI uri , String contentType , IOConsumer <OutputStream > writer ) throws IOException {
96+ public Response post (URI uri , String contentType , IOConsumer <OutputStream > writer ) {
9897 return execute (new HttpPost (uri ), contentType , writer );
9998 }
10099
@@ -104,51 +103,55 @@ public Response post(URI uri, String contentType, IOConsumer<OutputStream> write
104103 * @param contentType the content type to write
105104 * @param writer a content writer
106105 * @return the operation response
107- * @throws IOException on IO error
108106 */
109107
110108 @ Override
111- public Response put (URI uri , String contentType , IOConsumer <OutputStream > writer ) throws IOException {
109+ public Response put (URI uri , String contentType , IOConsumer <OutputStream > writer ) {
112110 return execute (new HttpPut (uri ), contentType , writer );
113111 }
114112
115113 /**
116114 * Perform a HTTP DELETE operation.
117115 * @param uri the destination URI
118116 * @return the operation response
119- * @throws IOException on IO error
120117 */
121118
122119 @ Override
123- public Response delete (URI uri ) throws IOException {
120+ public Response delete (URI uri ) {
124121 return execute (new HttpDelete (uri ));
125122 }
126123
127124 private Response execute (HttpEntityEnclosingRequestBase request , String contentType ,
128- IOConsumer <OutputStream > writer ) throws IOException {
125+ IOConsumer <OutputStream > writer ) {
129126 request .setHeader (HttpHeaders .CONTENT_TYPE , contentType );
130127 request .setEntity (new WritableHttpEntity (writer ));
131128 return execute (request );
132129 }
133130
134- private Response execute (HttpUriRequest request ) throws IOException {
135- CloseableHttpResponse response = this .client .execute (request );
136- StatusLine statusLine = response .getStatusLine ();
137- int statusCode = statusLine .getStatusCode ();
138- HttpEntity entity = response .getEntity ();
139- if (statusCode >= 200 && statusCode < 300 ) {
140- return new HttpClientResponse (response );
141- }
142- Errors errors = null ;
143- if (statusCode >= 400 && statusCode < 500 ) {
144- try {
145- errors = SharedObjectMapper .get ().readValue (entity .getContent (), Errors .class );
131+ private Response execute (HttpUriRequest request ) {
132+ CloseableHttpResponse response ;
133+ try {
134+ response = this .client .execute (request );
135+ StatusLine statusLine = response .getStatusLine ();
136+ int statusCode = statusLine .getStatusCode ();
137+ HttpEntity entity = response .getEntity ();
138+
139+ if (statusCode >= 400 && statusCode < 500 ) {
140+ Errors errors = SharedObjectMapper .get ().readValue (entity .getContent (), Errors .class );
141+ throw new DockerException (request .getURI (), statusCode , statusLine .getReasonPhrase (), errors );
146142 }
147- catch (Exception ex ) {
143+ if (statusCode == 500 ) {
144+ throw new DockerException (request .getURI (), statusCode , statusLine .getReasonPhrase (), null );
148145 }
149146 }
150- EntityUtils .consume (entity );
151- throw new DockerException (request .getURI (), statusCode , statusLine .getReasonPhrase (), errors );
147+ catch (IOException ioe ) {
148+ StringWriter stringWriter = new StringWriter ();
149+ PrintWriter printWriter = new PrintWriter (stringWriter );
150+ ioe .printStackTrace (printWriter );
151+ throw new DockerException (request .getURI (), 500 , stringWriter .toString (), null );
152+ }
153+
154+ return new HttpClientResponse (response );
152155 }
153156
154157 /**
@@ -175,7 +178,7 @@ public long getContentLength() {
175178 }
176179
177180 @ Override
178- public InputStream getContent () throws IOException , UnsupportedOperationException {
181+ public InputStream getContent () throws UnsupportedOperationException {
179182 throw new UnsupportedOperationException ();
180183 }
181184
0 commit comments