|
|
|
@ -21,16 +21,18 @@ import java.net.URI;
|
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
|
|
|
|
|
import org.apache.http.Header;
|
|
|
|
|
import org.apache.http.HttpEntity;
|
|
|
|
|
import org.apache.http.HttpHeaders;
|
|
|
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
|
|
import org.apache.http.client.methods.HttpGet;
|
|
|
|
|
import org.apache.http.client.methods.HttpUriRequest;
|
|
|
|
|
import org.apache.http.entity.ContentType;
|
|
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
|
|
import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
|
|
import org.apache.http.message.BasicHeader;
|
|
|
|
|
import org.apache.hc.client5.http.classic.HttpClient;
|
|
|
|
|
import org.apache.hc.client5.http.classic.methods.HttpGet;
|
|
|
|
|
import org.apache.hc.client5.http.classic.methods.HttpUriRequest;
|
|
|
|
|
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
|
|
|
|
|
import org.apache.hc.core5.http.ClassicHttpResponse;
|
|
|
|
|
import org.apache.hc.core5.http.ContentType;
|
|
|
|
|
import org.apache.hc.core5.http.Header;
|
|
|
|
|
import org.apache.hc.core5.http.HttpEntity;
|
|
|
|
|
import org.apache.hc.core5.http.HttpHeaders;
|
|
|
|
|
import org.apache.hc.core5.http.HttpHost;
|
|
|
|
|
import org.apache.hc.core5.http.message.BasicHeader;
|
|
|
|
|
import org.apache.hc.core5.http.message.StatusLine;
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
|
@ -62,16 +64,16 @@ class InitializrService {
|
|
|
|
|
/**
|
|
|
|
|
* Late binding HTTP client.
|
|
|
|
|
*/
|
|
|
|
|
private CloseableHttpClient http;
|
|
|
|
|
private HttpClient http;
|
|
|
|
|
|
|
|
|
|
InitializrService() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
InitializrService(CloseableHttpClient http) {
|
|
|
|
|
InitializrService(HttpClient http) {
|
|
|
|
|
this.http = http;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected CloseableHttpClient getHttp() {
|
|
|
|
|
protected HttpClient getHttp() {
|
|
|
|
|
if (this.http == null) {
|
|
|
|
|
this.http = HttpClientBuilder.create().useSystemProperties().build();
|
|
|
|
|
}
|
|
|
|
@ -88,7 +90,7 @@ class InitializrService {
|
|
|
|
|
Log.info("Using service at " + request.getServiceUrl());
|
|
|
|
|
InitializrServiceMetadata metadata = loadMetadata(request.getServiceUrl());
|
|
|
|
|
URI url = request.generateUrl(metadata);
|
|
|
|
|
CloseableHttpResponse httpResponse = executeProjectGenerationRequest(url);
|
|
|
|
|
ClassicHttpResponse httpResponse = executeProjectGenerationRequest(url);
|
|
|
|
|
HttpEntity httpEntity = httpResponse.getEntity();
|
|
|
|
|
validateResponse(httpResponse, request.getServiceUrl());
|
|
|
|
|
return createResponse(httpResponse, httpEntity);
|
|
|
|
@ -101,7 +103,7 @@ class InitializrService {
|
|
|
|
|
* @throws IOException if the service's metadata cannot be loaded
|
|
|
|
|
*/
|
|
|
|
|
InitializrServiceMetadata loadMetadata(String serviceUrl) throws IOException {
|
|
|
|
|
CloseableHttpResponse httpResponse = executeInitializrMetadataRetrieval(serviceUrl);
|
|
|
|
|
ClassicHttpResponse httpResponse = executeInitializrMetadataRetrieval(serviceUrl);
|
|
|
|
|
validateResponse(httpResponse, serviceUrl);
|
|
|
|
|
return parseJsonMetadata(httpResponse.getEntity());
|
|
|
|
|
}
|
|
|
|
@ -118,10 +120,10 @@ class InitializrService {
|
|
|
|
|
Object loadServiceCapabilities(String serviceUrl) throws IOException {
|
|
|
|
|
HttpGet request = new HttpGet(serviceUrl);
|
|
|
|
|
request.setHeader(new BasicHeader(HttpHeaders.ACCEPT, ACCEPT_SERVICE_CAPABILITIES));
|
|
|
|
|
CloseableHttpResponse httpResponse = execute(request, serviceUrl, "retrieve help");
|
|
|
|
|
ClassicHttpResponse httpResponse = execute(request, URI.create(serviceUrl), "retrieve help");
|
|
|
|
|
validateResponse(httpResponse, serviceUrl);
|
|
|
|
|
HttpEntity httpEntity = httpResponse.getEntity();
|
|
|
|
|
ContentType contentType = ContentType.getOrDefault(httpEntity);
|
|
|
|
|
ContentType contentType = ContentType.create(httpEntity.getContentType());
|
|
|
|
|
if (contentType.getMimeType().equals("text/plain")) {
|
|
|
|
|
return getContent(httpEntity);
|
|
|
|
|
}
|
|
|
|
@ -137,18 +139,19 @@ class InitializrService {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void validateResponse(CloseableHttpResponse httpResponse, String serviceUrl) {
|
|
|
|
|
private void validateResponse(ClassicHttpResponse httpResponse, String serviceUrl) {
|
|
|
|
|
if (httpResponse.getEntity() == null) {
|
|
|
|
|
throw new ReportableException("No content received from server '" + serviceUrl + "'");
|
|
|
|
|
}
|
|
|
|
|
if (httpResponse.getStatusLine().getStatusCode() != 200) {
|
|
|
|
|
if (httpResponse.getCode() != 200) {
|
|
|
|
|
throw createException(serviceUrl, httpResponse);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ProjectGenerationResponse createResponse(CloseableHttpResponse httpResponse, HttpEntity httpEntity)
|
|
|
|
|
private ProjectGenerationResponse createResponse(ClassicHttpResponse httpResponse, HttpEntity httpEntity)
|
|
|
|
|
throws IOException {
|
|
|
|
|
ProjectGenerationResponse response = new ProjectGenerationResponse(ContentType.getOrDefault(httpEntity));
|
|
|
|
|
ProjectGenerationResponse response = new ProjectGenerationResponse(
|
|
|
|
|
ContentType.create(httpEntity.getContentType()));
|
|
|
|
|
response.setContent(FileCopyUtils.copyToByteArray(httpEntity.getContent()));
|
|
|
|
|
String fileName = extractFileName(httpResponse.getFirstHeader("Content-Disposition"));
|
|
|
|
|
if (fileName != null) {
|
|
|
|
@ -162,7 +165,7 @@ class InitializrService {
|
|
|
|
|
* @param url the URL
|
|
|
|
|
* @return the response
|
|
|
|
|
*/
|
|
|
|
|
private CloseableHttpResponse executeProjectGenerationRequest(URI url) {
|
|
|
|
|
private ClassicHttpResponse executeProjectGenerationRequest(URI url) {
|
|
|
|
|
return execute(new HttpGet(url), url, "generate project");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -171,16 +174,17 @@ class InitializrService {
|
|
|
|
|
* @param url the URL
|
|
|
|
|
* @return the response
|
|
|
|
|
*/
|
|
|
|
|
private CloseableHttpResponse executeInitializrMetadataRetrieval(String url) {
|
|
|
|
|
private ClassicHttpResponse executeInitializrMetadataRetrieval(String url) {
|
|
|
|
|
HttpGet request = new HttpGet(url);
|
|
|
|
|
request.setHeader(new BasicHeader(HttpHeaders.ACCEPT, ACCEPT_META_DATA));
|
|
|
|
|
return execute(request, url, "retrieve metadata");
|
|
|
|
|
return execute(request, URI.create(url), "retrieve metadata");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private CloseableHttpResponse execute(HttpUriRequest request, Object url, String description) {
|
|
|
|
|
private ClassicHttpResponse execute(HttpUriRequest request, URI url, String description) {
|
|
|
|
|
try {
|
|
|
|
|
HttpHost host = HttpHost.create(url);
|
|
|
|
|
request.addHeader("User-Agent", "SpringBootCli/" + getClass().getPackage().getImplementationVersion());
|
|
|
|
|
return getHttp().execute(request);
|
|
|
|
|
return getHttp().execute(host, request);
|
|
|
|
|
}
|
|
|
|
|
catch (IOException ex) {
|
|
|
|
|
throw new ReportableException(
|
|
|
|
@ -188,15 +192,16 @@ class InitializrService {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ReportableException createException(String url, CloseableHttpResponse httpResponse) {
|
|
|
|
|
private ReportableException createException(String url, ClassicHttpResponse httpResponse) {
|
|
|
|
|
StatusLine statusLine = new StatusLine(httpResponse);
|
|
|
|
|
String message = "Initializr service call failed using '" + url + "' - service returned "
|
|
|
|
|
+ httpResponse.getStatusLine().getReasonPhrase();
|
|
|
|
|
+ statusLine.getReasonPhrase();
|
|
|
|
|
String error = extractMessage(httpResponse.getEntity());
|
|
|
|
|
if (StringUtils.hasText(error)) {
|
|
|
|
|
message += ": '" + error + "'";
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
int statusCode = httpResponse.getStatusLine().getStatusCode();
|
|
|
|
|
int statusCode = statusLine.getStatusCode();
|
|
|
|
|
message += " (unexpected " + statusCode + " error)";
|
|
|
|
|
}
|
|
|
|
|
throw new ReportableException(message);
|
|
|
|
@ -222,7 +227,7 @@ class InitializrService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String getContent(HttpEntity entity) throws IOException {
|
|
|
|
|
ContentType contentType = ContentType.getOrDefault(entity);
|
|
|
|
|
ContentType contentType = ContentType.create(entity.getContentType());
|
|
|
|
|
Charset charset = contentType.getCharset();
|
|
|
|
|
charset = (charset != null) ? charset : StandardCharsets.UTF_8;
|
|
|
|
|
byte[] content = FileCopyUtils.copyToByteArray(entity.getContent());
|
|
|
|
|