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
51+ * @author Scott Frederick
4952 */
5053class HttpClientHttp implements Http {
5154
@@ -66,21 +69,19 @@ class HttpClientHttp implements Http {
6669 * Perform a HTTP GET operation.
6770 * @param uri the destination URI
6871 * @return the operation response
69- * @throws IOException on IO error
7072 */
7173 @ Override
72- public Response get (URI uri ) throws IOException {
74+ public Response get (URI uri ) {
7375 return execute (new HttpGet (uri ));
7476 }
7577
7678 /**
7779 * Perform a HTTP POST operation.
7880 * @param uri the destination URI
7981 * @return the operation response
80- * @throws IOException on IO error
8182 */
8283 @ Override
83- public Response post (URI uri ) throws IOException {
84+ public Response post (URI uri ) {
8485 return execute (new HttpPost (uri ));
8586 }
8687
@@ -90,11 +91,10 @@ public Response post(URI uri) throws IOException {
9091 * @param contentType the content type to write
9192 * @param writer a content writer
9293 * @return the operation response
93- * @throws IOException on IO error
9494 */
9595
9696 @ Override
97- public Response post (URI uri , String contentType , IOConsumer <OutputStream > writer ) throws IOException {
97+ public Response post (URI uri , String contentType , IOConsumer <OutputStream > writer ) {
9898 return execute (new HttpPost (uri ), contentType , writer );
9999 }
100100
@@ -104,51 +104,50 @@ public Response post(URI uri, String contentType, IOConsumer<OutputStream> write
104104 * @param contentType the content type to write
105105 * @param writer a content writer
106106 * @return the operation response
107- * @throws IOException on IO error
108107 */
109108
110109 @ Override
111- public Response put (URI uri , String contentType , IOConsumer <OutputStream > writer ) throws IOException {
110+ public Response put (URI uri , String contentType , IOConsumer <OutputStream > writer ) {
112111 return execute (new HttpPut (uri ), contentType , writer );
113112 }
114113
115114 /**
116115 * Perform a HTTP DELETE operation.
117116 * @param uri the destination URI
118117 * @return the operation response
119- * @throws IOException on IO error
120118 */
121119
122120 @ Override
123- public Response delete (URI uri ) throws IOException {
121+ public Response delete (URI uri ) {
124122 return execute (new HttpDelete (uri ));
125123 }
126124
127125 private Response execute (HttpEntityEnclosingRequestBase request , String contentType ,
128- IOConsumer <OutputStream > writer ) throws IOException {
126+ IOConsumer <OutputStream > writer ) {
129127 request .setHeader (HttpHeaders .CONTENT_TYPE , contentType );
130128 request .setEntity (new WritableHttpEntity (writer ));
131129 return execute (request );
132130 }
133131
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 );
132+ private Response execute (HttpUriRequest request ) {
133+ try {
134+ CloseableHttpResponse 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 }
146+ return new HttpClientResponse (response );
147+ }
148+ catch (IOException ioe ) {
149+ throw new DockerException (request .getURI (), 500 , ioe .getMessage (), null );
149150 }
150- EntityUtils .consume (entity );
151- throw new DockerException (request .getURI (), statusCode , statusLine .getReasonPhrase (), errors );
152151 }
153152
154153 /**
@@ -175,7 +174,7 @@ public long getContentLength() {
175174 }
176175
177176 @ Override
178- public InputStream getContent () throws IOException , UnsupportedOperationException {
177+ public InputStream getContent () throws UnsupportedOperationException {
179178 throw new UnsupportedOperationException ();
180179 }
181180
0 commit comments