diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitCommand.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitCommand.java index b0090efc318..add75306fd4 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitCommand.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitCommand.java @@ -22,7 +22,6 @@ import java.util.Arrays; import joptsimple.OptionSet; import joptsimple.OptionSpec; -import org.apache.http.impl.client.HttpClientBuilder; import org.springframework.boot.cli.command.Command; import org.springframework.boot.cli.command.OptionParsingCommand; import org.springframework.boot.cli.command.options.OptionHandler; @@ -38,7 +37,7 @@ import org.springframework.boot.cli.util.Log; public class InitCommand extends OptionParsingCommand { public InitCommand() { - this(new InitOptionHandler(getInitializrService())); + this(new InitOptionHandler(new InitializrService())); } public InitCommand(InitOptionHandler handler) { @@ -46,10 +45,6 @@ public class InitCommand extends OptionParsingCommand { + "Initialzr (start.spring.io)", handler); } - private static InitializrService getInitializrService() { - return new InitializrService(HttpClientBuilder.create().build()); - } - static class InitOptionHandler extends OptionHandler { private final ServiceCapabilitiesReportGenerator serviceCapabilitiesReport; diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitializrService.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitializrService.java index 2be1cd7f83a..d4a3e28070f 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitializrService.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitializrService.java @@ -28,6 +28,7 @@ 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.json.JSONException; import org.json.JSONObject; @@ -47,15 +48,25 @@ class InitializrService { private static final Charset UTF_8 = Charset.forName("UTF-8"); - private final CloseableHttpClient http; - /** - * Create a new instance with the given {@link CloseableHttpClient HTTP client}. + * Late binding HTTP client. */ - public InitializrService(CloseableHttpClient http) { + private CloseableHttpClient http; + + public InitializrService() { + } + + InitializrService(CloseableHttpClient http) { this.http = http; } + protected CloseableHttpClient getHttp() { + if (this.http == null) { + this.http = HttpClientBuilder.create().build(); + } + return this.http; + } + /** * Generate a project based on the specified {@link ProjectGenerationRequest} * @return an entity defining the project @@ -130,7 +141,7 @@ class InitializrService { private CloseableHttpResponse execute(HttpUriRequest request, Object url, String description) { try { - return this.http.execute(request); + return getHttp().execute(request); } catch (IOException ex) { throw new ReportableException("Failed to " + description