From 636ae0270fe0d05cc07baf7acd37cd535cb98a1a Mon Sep 17 00:00:00 2001 From: devatherock Date: Mon, 26 Oct 2020 09:35:52 -0500 Subject: [PATCH] Closes #13 and #19 ## Changes: ### Added - https://github.com/devatherock/artifactory-badge/issues/19: A `BlockingHttpClient` bean ### Removed - https://github.com/devatherock/artifactory-badge/issues/13: Apache HTTP Client --- .circleci/config.yml | 2 +- CHANGELOG.md | 11 +++++--- build.gradle | 4 +-- .../artifactory/config/AppConfig.java | 9 ++++--- .../config/ShieldsIOProperties.java | 3 --- .../service/DockerBadgeService.java | 13 ++------- .../artifactory/util/BadgeGenerator.java | 27 +------------------ 7 files changed, 17 insertions(+), 52 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index bfd109c..d3718e4 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -72,7 +72,7 @@ workflows: - gradle/run image: devatherock/artifactory-badge attach-at: ~/artifactory-badge - tag: "0.1.6,latest" + tag: "0.1.7,latest" use-remote-docker: true docker-username: DOCKER_USERNAME - notify_success: diff --git a/CHANGELOG.md b/CHANGELOG.md index 95d6001..4eb93b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,11 +6,14 @@ - `/health` endpoint - Custom badge generator - [Issue 16](https://github.com/devatherock/artifactory-badge/issues/16): Environment variables to configure log levels +- https://github.com/devatherock/artifactory-badge/issues/19: A `BlockingHttpClient` bean ### Changed - Caught exception when JSON processing fails -- Used an explicit HttpClient so as to not URL encode slashes -- Enabled SSL. Used injected HttpClient -- Used Apache HTTP client to generate the badge +- Used an explicit, injected HttpClient for artifactory so as to not URL encode slashes - Used the IO thread pool instead of the default event loop -- Set the number of threads on the IO pool to `200`, to match spring boot's tomcat \ No newline at end of file +- Set the number of threads on the IO pool to `200`, to match spring boot's tomcat +- Upgraded JRE base image to `11.0.8` from `11.0.1` to fix SSL errors with TLS v1.3 + +### Removed +- https://github.com/devatherock/artifactory-badge/issues/13: Apache HTTP Client \ No newline at end of file diff --git a/build.gradle b/build.gradle index e63f2d4..6890c7f 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { id "application" } -version "0.1.6" +version "0.1.7" group "io.github.devatherock" repositories { @@ -39,8 +39,6 @@ dependencies { implementation "io.micronaut.cache:micronaut-cache-core" implementation "io.micronaut.cache:micronaut-cache-caffeine" - implementation group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.13' - runtimeOnly "ch.qos.logback:logback-classic:1.2.3" testAnnotationProcessor platform("io.micronaut:micronaut-bom:$micronautVersion") testAnnotationProcessor "io.micronaut:micronaut-inject-java" diff --git a/src/main/java/io/github/devatherock/artifactory/config/AppConfig.java b/src/main/java/io/github/devatherock/artifactory/config/AppConfig.java index 66662f4..a6e6328 100644 --- a/src/main/java/io/github/devatherock/artifactory/config/AppConfig.java +++ b/src/main/java/io/github/devatherock/artifactory/config/AppConfig.java @@ -1,8 +1,9 @@ package io.github.devatherock.artifactory.config; import io.micronaut.context.annotation.Factory; -import org.apache.http.client.HttpClient; -import org.apache.http.impl.client.HttpClientBuilder; +import io.micronaut.http.client.BlockingHttpClient; +import io.micronaut.http.client.HttpClient; +import io.micronaut.http.client.annotation.Client; import javax.inject.Singleton; @@ -10,7 +11,7 @@ public class AppConfig { @Singleton - public HttpClient httpClient() { - return HttpClientBuilder.create().build(); + public BlockingHttpClient httpClient(@Client("${artifactory.url}") HttpClient artifactoryClient) { + return artifactoryClient.toBlocking(); } } diff --git a/src/main/java/io/github/devatherock/artifactory/config/ShieldsIOProperties.java b/src/main/java/io/github/devatherock/artifactory/config/ShieldsIOProperties.java index 2e88177..92314b6 100644 --- a/src/main/java/io/github/devatherock/artifactory/config/ShieldsIOProperties.java +++ b/src/main/java/io/github/devatherock/artifactory/config/ShieldsIOProperties.java @@ -9,7 +9,4 @@ @ConfigurationProperties("artifactory.badge.shields-io") public class ShieldsIOProperties { private boolean enabled = true; - private boolean customHttp = true; - - private String url = "https://img.shields.io/badge"; } diff --git a/src/main/java/io/github/devatherock/artifactory/service/DockerBadgeService.java b/src/main/java/io/github/devatherock/artifactory/service/DockerBadgeService.java index b45e4d7..00be8f3 100644 --- a/src/main/java/io/github/devatherock/artifactory/service/DockerBadgeService.java +++ b/src/main/java/io/github/devatherock/artifactory/service/DockerBadgeService.java @@ -13,16 +13,15 @@ import io.micronaut.http.HttpMethod; import io.micronaut.http.HttpRequest; import io.micronaut.http.client.BlockingHttpClient; -import io.micronaut.http.client.HttpClient; -import io.micronaut.http.client.annotation.Client; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import javax.inject.Inject; import javax.inject.Singleton; @Slf4j @Blocking @Singleton +@RequiredArgsConstructor public class DockerBadgeService { private static final double BYTES_IN_MB = 1024 * 1024; private static final String FILE_NAME_MANIFEST = "/manifest.json"; @@ -32,14 +31,6 @@ public class DockerBadgeService { private final BadgeGenerator badgeGenerator; private final ArtifactoryProperties artifactoryConfig; - @Inject - public DockerBadgeService(@Client("${artifactory.url}") HttpClient artifactoryClient, - BadgeGenerator badgeGenerator, - ArtifactoryProperties artifactoryConfig) { - this.artifactoryClient = artifactoryClient.toBlocking(); - this.badgeGenerator = badgeGenerator; - this.artifactoryConfig = artifactoryConfig; - } @Cacheable(cacheNames = "size-cache") public String getImageSizeBadge(String packageName, String tag, String badgeLabel) { diff --git a/src/main/java/io/github/devatherock/artifactory/util/BadgeGenerator.java b/src/main/java/io/github/devatherock/artifactory/util/BadgeGenerator.java index 9d5a386..8a407f2 100644 --- a/src/main/java/io/github/devatherock/artifactory/util/BadgeGenerator.java +++ b/src/main/java/io/github/devatherock/artifactory/util/BadgeGenerator.java @@ -2,20 +2,11 @@ import io.github.devatherock.artifactory.config.ShieldsIOProperties; import io.github.devatherock.artifactory.service.ShieldsIOClient; -import io.micronaut.core.io.IOUtils; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; -import org.apache.http.HttpResponse; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpGet; import javax.inject.Singleton; -import java.io.BufferedReader; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.URI; -import java.net.URL; @Slf4j @Singleton @@ -24,7 +15,6 @@ public class BadgeGenerator { private static final int WIDTH_ONE_CHAR = 8; private final ShieldsIOClient shieldsIOClient; - private final HttpClient httpClient; private final ShieldsIOProperties config; @SneakyThrows @@ -34,22 +24,7 @@ public String generateBadge(String label, String value) { if (config.isEnabled()) { String shieldIoImageName = String.format("%s-%s-blue.svg", label, value); - - if (config.isCustomHttp()) { - // For URL encoding spaces in the path. URLEncoder.encode encodes them into + instead of %20 - URL url = new URL(config.getUrl() + "/" + shieldIoImageName); - URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), - url.getQuery(), url.getRef()); - HttpGet httpGet = new HttpGet(uri); - HttpResponse httpResponse = httpClient.execute(httpGet); - - try (InputStream stream = httpResponse.getEntity().getContent(); - BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) { - badge = IOUtils.readText(reader); - } - } else { - badge = shieldsIOClient.downloadBadge(shieldIoImageName); - } + badge = shieldsIOClient.downloadBadge(shieldIoImageName); } else { badge = generateSvg(label, value); }