21
21
import java .nio .charset .Charset ;
22
22
import java .nio .charset .StandardCharsets ;
23
23
24
- import org .apache .http .Header ;
25
- import org .apache .http .HttpEntity ;
26
- import org .apache .http .HttpHeaders ;
27
- import org .apache .http .client .methods .CloseableHttpResponse ;
28
- import org .apache .http .client .methods .HttpGet ;
29
- import org .apache .http .client .methods .HttpUriRequest ;
30
- import org .apache .http .entity .ContentType ;
31
- import org .apache .http .impl .client .CloseableHttpClient ;
32
- import org .apache .http .impl .client .HttpClientBuilder ;
33
- import org .apache .http .message .BasicHeader ;
24
+ import org .apache .hc .client5 .http .classic .HttpClient ;
25
+ import org .apache .hc .client5 .http .classic .methods .HttpGet ;
26
+ import org .apache .hc .client5 .http .classic .methods .HttpUriRequest ;
27
+ import org .apache .hc .client5 .http .impl .classic .HttpClientBuilder ;
28
+ import org .apache .hc .core5 .http .ClassicHttpResponse ;
29
+ import org .apache .hc .core5 .http .ContentType ;
30
+ import org .apache .hc .core5 .http .Header ;
31
+ import org .apache .hc .core5 .http .HttpEntity ;
32
+ import org .apache .hc .core5 .http .HttpHeaders ;
33
+ import org .apache .hc .core5 .http .HttpHost ;
34
+ import org .apache .hc .core5 .http .message .BasicHeader ;
35
+ import org .apache .hc .core5 .http .message .StatusLine ;
34
36
import org .json .JSONException ;
35
37
import org .json .JSONObject ;
36
38
@@ -62,16 +64,16 @@ class InitializrService {
62
64
/**
63
65
* Late binding HTTP client.
64
66
*/
65
- private CloseableHttpClient http ;
67
+ private HttpClient http ;
66
68
67
69
InitializrService () {
68
70
}
69
71
70
- InitializrService (CloseableHttpClient http ) {
72
+ InitializrService (HttpClient http ) {
71
73
this .http = http ;
72
74
}
73
75
74
- protected CloseableHttpClient getHttp () {
76
+ protected HttpClient getHttp () {
75
77
if (this .http == null ) {
76
78
this .http = HttpClientBuilder .create ().useSystemProperties ().build ();
77
79
}
@@ -88,7 +90,7 @@ ProjectGenerationResponse generate(ProjectGenerationRequest request) throws IOEx
88
90
Log .info ("Using service at " + request .getServiceUrl ());
89
91
InitializrServiceMetadata metadata = loadMetadata (request .getServiceUrl ());
90
92
URI url = request .generateUrl (metadata );
91
- CloseableHttpResponse httpResponse = executeProjectGenerationRequest (url );
93
+ ClassicHttpResponse httpResponse = executeProjectGenerationRequest (url );
92
94
HttpEntity httpEntity = httpResponse .getEntity ();
93
95
validateResponse (httpResponse , request .getServiceUrl ());
94
96
return createResponse (httpResponse , httpEntity );
@@ -101,7 +103,7 @@ ProjectGenerationResponse generate(ProjectGenerationRequest request) throws IOEx
101
103
* @throws IOException if the service's metadata cannot be loaded
102
104
*/
103
105
InitializrServiceMetadata loadMetadata (String serviceUrl ) throws IOException {
104
- CloseableHttpResponse httpResponse = executeInitializrMetadataRetrieval (serviceUrl );
106
+ ClassicHttpResponse httpResponse = executeInitializrMetadataRetrieval (serviceUrl );
105
107
validateResponse (httpResponse , serviceUrl );
106
108
return parseJsonMetadata (httpResponse .getEntity ());
107
109
}
@@ -118,10 +120,10 @@ InitializrServiceMetadata loadMetadata(String serviceUrl) throws IOException {
118
120
Object loadServiceCapabilities (String serviceUrl ) throws IOException {
119
121
HttpGet request = new HttpGet (serviceUrl );
120
122
request .setHeader (new BasicHeader (HttpHeaders .ACCEPT , ACCEPT_SERVICE_CAPABILITIES ));
121
- CloseableHttpResponse httpResponse = execute (request , serviceUrl , "retrieve help" );
123
+ ClassicHttpResponse httpResponse = execute (request , URI . create ( serviceUrl ) , "retrieve help" );
122
124
validateResponse (httpResponse , serviceUrl );
123
125
HttpEntity httpEntity = httpResponse .getEntity ();
124
- ContentType contentType = ContentType .getOrDefault (httpEntity );
126
+ ContentType contentType = ContentType .create (httpEntity . getContentType () );
125
127
if (contentType .getMimeType ().equals ("text/plain" )) {
126
128
return getContent (httpEntity );
127
129
}
@@ -137,18 +139,19 @@ private InitializrServiceMetadata parseJsonMetadata(HttpEntity httpEntity) throw
137
139
}
138
140
}
139
141
140
- private void validateResponse (CloseableHttpResponse httpResponse , String serviceUrl ) {
142
+ private void validateResponse (ClassicHttpResponse httpResponse , String serviceUrl ) {
141
143
if (httpResponse .getEntity () == null ) {
142
144
throw new ReportableException ("No content received from server '" + serviceUrl + "'" );
143
145
}
144
- if (httpResponse .getStatusLine (). getStatusCode () != 200 ) {
146
+ if (httpResponse .getCode () != 200 ) {
145
147
throw createException (serviceUrl , httpResponse );
146
148
}
147
149
}
148
150
149
- private ProjectGenerationResponse createResponse (CloseableHttpResponse httpResponse , HttpEntity httpEntity )
151
+ private ProjectGenerationResponse createResponse (ClassicHttpResponse httpResponse , HttpEntity httpEntity )
150
152
throws IOException {
151
- ProjectGenerationResponse response = new ProjectGenerationResponse (ContentType .getOrDefault (httpEntity ));
153
+ ProjectGenerationResponse response = new ProjectGenerationResponse (
154
+ ContentType .create (httpEntity .getContentType ()));
152
155
response .setContent (FileCopyUtils .copyToByteArray (httpEntity .getContent ()));
153
156
String fileName = extractFileName (httpResponse .getFirstHeader ("Content-Disposition" ));
154
157
if (fileName != null ) {
@@ -162,7 +165,7 @@ private ProjectGenerationResponse createResponse(CloseableHttpResponse httpRespo
162
165
* @param url the URL
163
166
* @return the response
164
167
*/
165
- private CloseableHttpResponse executeProjectGenerationRequest (URI url ) {
168
+ private ClassicHttpResponse executeProjectGenerationRequest (URI url ) {
166
169
return execute (new HttpGet (url ), url , "generate project" );
167
170
}
168
171
@@ -171,32 +174,34 @@ private CloseableHttpResponse executeProjectGenerationRequest(URI url) {
171
174
* @param url the URL
172
175
* @return the response
173
176
*/
174
- private CloseableHttpResponse executeInitializrMetadataRetrieval (String url ) {
177
+ private ClassicHttpResponse executeInitializrMetadataRetrieval (String url ) {
175
178
HttpGet request = new HttpGet (url );
176
179
request .setHeader (new BasicHeader (HttpHeaders .ACCEPT , ACCEPT_META_DATA ));
177
- return execute (request , url , "retrieve metadata" );
180
+ return execute (request , URI . create ( url ) , "retrieve metadata" );
178
181
}
179
182
180
- private CloseableHttpResponse execute (HttpUriRequest request , Object url , String description ) {
183
+ private ClassicHttpResponse execute (HttpUriRequest request , URI url , String description ) {
181
184
try {
185
+ HttpHost host = HttpHost .create (url );
182
186
request .addHeader ("User-Agent" , "SpringBootCli/" + getClass ().getPackage ().getImplementationVersion ());
183
- return getHttp ().execute (request );
187
+ return getHttp ().execute (host , request );
184
188
}
185
189
catch (IOException ex ) {
186
190
throw new ReportableException (
187
191
"Failed to " + description + " from service at '" + url + "' (" + ex .getMessage () + ")" );
188
192
}
189
193
}
190
194
191
- private ReportableException createException (String url , CloseableHttpResponse httpResponse ) {
195
+ private ReportableException createException (String url , ClassicHttpResponse httpResponse ) {
196
+ StatusLine statusLine = new StatusLine (httpResponse );
192
197
String message = "Initializr service call failed using '" + url + "' - service returned "
193
- + httpResponse . getStatusLine () .getReasonPhrase ();
198
+ + statusLine .getReasonPhrase ();
194
199
String error = extractMessage (httpResponse .getEntity ());
195
200
if (StringUtils .hasText (error )) {
196
201
message += ": '" + error + "'" ;
197
202
}
198
203
else {
199
- int statusCode = httpResponse . getStatusLine () .getStatusCode ();
204
+ int statusCode = statusLine .getStatusCode ();
200
205
message += " (unexpected " + statusCode + " error)" ;
201
206
}
202
207
throw new ReportableException (message );
@@ -222,7 +227,7 @@ private JSONObject getContentAsJson(HttpEntity entity) throws IOException, JSONE
222
227
}
223
228
224
229
private String getContent (HttpEntity entity ) throws IOException {
225
- ContentType contentType = ContentType .getOrDefault (entity );
230
+ ContentType contentType = ContentType .create (entity . getContentType () );
226
231
Charset charset = contentType .getCharset ();
227
232
charset = (charset != null ) ? charset : StandardCharsets .UTF_8 ;
228
233
byte [] content = FileCopyUtils .copyToByteArray (entity .getContent ());
0 commit comments