Support of spring initializr meta-data v2.1

Update the `init` command to support the latest meta-data format. Recent
Spring Initializr version also supports Spring Boot CLI now and generates
a textual service capabilities when requested. The command no longer
generates the capabilities of the service unless said service does not
support it.

Closes gh-2515
This commit is contained in:
Stephane Nicoll
2015-02-18 17:38:13 +01:00
parent 9af30450c4
commit 9d0e50c6ac
8 changed files with 345 additions and 47 deletions

View File

@@ -48,6 +48,19 @@ class InitializrService {
private static final Charset UTF_8 = Charset.forName("UTF-8");
/**
* Accept header to use to retrieve the json meta-data.
*/
public static final String ACCEPT_META_DATA =
"application/vnd.initializr.v2.1+json,application/vnd.initializr.v2+json";
/**
* Accept header to use to retrieve the service capabilities of the service. If the
* service does not offer such feature, the json meta-data are retrieved instead.
*/
public static final String ACCEPT_SERVICE_CAPABILITIES =
"text/plain," + ACCEPT_META_DATA;
/**
* Late binding HTTP client.
*/
@@ -80,12 +93,7 @@ class InitializrService {
URI url = request.generateUrl(metadata);
CloseableHttpResponse httpResponse = executeProjectGenerationRequest(url);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity == null) {
throw new ReportableException("No content received from server '" + url + "'");
}
if (httpResponse.getStatusLine().getStatusCode() != 200) {
throw createException(request.getServiceUrl(), httpResponse);
}
validateResponse(httpResponse, request.getServiceUrl());
return createResponse(httpResponse, httpEntity);
}
@@ -97,6 +105,42 @@ class InitializrService {
*/
public InitializrServiceMetadata loadMetadata(String serviceUrl) throws IOException {
CloseableHttpResponse httpResponse = executeInitializrMetadataRetrieval(serviceUrl);
validateResponse(httpResponse, serviceUrl);
return parseJsonMetadata(httpResponse.getEntity());
}
/**
* Loads the service capabilities of the service at the specified url.
* <p>If the service supports generating a textual representation of the
* capabilities, it is returned. Otherwhise the json meta-data as a
* {@link JSONObject} is returned.
* @param serviceUrl to url of the initializer service
* @return the service capabilities (as a String) or the metadata describing the service
* @throws IOException if the service capabilities cannot be loaded
*/
public Object loadServiceCapabilities(String serviceUrl) throws IOException {
CloseableHttpResponse httpResponse = executeServiceCapabilitiesRetrieval(serviceUrl);
validateResponse(httpResponse, serviceUrl);
HttpEntity httpEntity = httpResponse.getEntity();
ContentType contentType = ContentType.getOrDefault(httpEntity);
if (contentType.getMimeType().equals("text/plain")) {
return getContent(httpEntity);
} else {
return parseJsonMetadata(httpEntity);
}
}
private InitializrServiceMetadata parseJsonMetadata(HttpEntity httpEntity) throws IOException {
try {
return new InitializrServiceMetadata(getContentAsJson(httpEntity));
}
catch (JSONException ex) {
throw new ReportableException("Invalid content received from server ("
+ ex.getMessage() + ")", ex);
}
}
private void validateResponse(CloseableHttpResponse httpResponse, String serviceUrl) {
if (httpResponse.getEntity() == null) {
throw new ReportableException("No content received from server '"
+ serviceUrl + "'");
@@ -104,14 +148,6 @@ class InitializrService {
if (httpResponse.getStatusLine().getStatusCode() != 200) {
throw createException(serviceUrl, httpResponse);
}
try {
HttpEntity httpEntity = httpResponse.getEntity();
return new InitializrServiceMetadata(getContentAsJson(httpEntity));
}
catch (JSONException ex) {
throw new ReportableException("Invalid content received from server ("
+ ex.getMessage() + ")", ex);
}
}
private ProjectGenerationResponse createResponse(CloseableHttpResponse httpResponse,
@@ -139,11 +175,19 @@ class InitializrService {
*/
private CloseableHttpResponse executeInitializrMetadataRetrieval(String url) {
HttpGet request = new HttpGet(url);
request.setHeader(new BasicHeader(HttpHeaders.ACCEPT,
"application/vnd.initializr.v2+json"));
request.setHeader(new BasicHeader(HttpHeaders.ACCEPT, ACCEPT_META_DATA));
return execute(request, url, "retrieve metadata");
}
/**
* Retrieves the service capabilities of the service at the specified URL
*/
private CloseableHttpResponse executeServiceCapabilitiesRetrieval(String url) {
HttpGet request = new HttpGet(url);
request.setHeader(new BasicHeader(HttpHeaders.ACCEPT, ACCEPT_SERVICE_CAPABILITIES));
return execute(request, url, "retrieve help");
}
private CloseableHttpResponse execute(HttpUriRequest request, Object url,
String description) {
try {
@@ -188,11 +232,15 @@ class InitializrService {
}
private JSONObject getContentAsJson(HttpEntity entity) throws IOException {
return new JSONObject(getContent(entity));
}
private String getContent(HttpEntity entity) throws IOException {
ContentType contentType = ContentType.getOrDefault(entity);
Charset charset = contentType.getCharset();
charset = (charset != null ? charset : UTF_8);
byte[] content = FileCopyUtils.copyToByteArray(entity.getContent());
return new JSONObject(new String(content, charset));
return new String(content, charset);
}
private String extractFileName(Header header) {

View File

@@ -54,7 +54,15 @@ class ServiceCapabilitiesReportGenerator {
* @throws IOException if the report cannot be generated
*/
public String generate(String url) throws IOException {
InitializrServiceMetadata metadata = this.initializrService.loadMetadata(url);
Object content = this.initializrService.loadServiceCapabilities(url);
if (content instanceof InitializrServiceMetadata) {
return generateHelp(url, (InitializrServiceMetadata) content);
} else {
return content.toString();
}
}
private String generateHelp(String url, InitializrServiceMetadata metadata) {
String header = "Capabilities of " + url;
StringBuilder report = new StringBuilder();
report.append(StringUtils.repeat("=", header.length()) + NEW_LINE);