Skip to content

Commit

Permalink
Closes #13 and #19
Browse files Browse the repository at this point in the history
## Changes:
### Added
- #19: A
`BlockingHttpClient` bean

### Removed
- #13: Apache
HTTP Client
  • Loading branch information
devatherock committed Nov 14, 2020
1 parent 1342a6c commit efbc339
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 7 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
- 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
4 changes: 1 addition & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
id "application"
}

version "0.1.6"
version "0.1.7"
group "io.github.devatherock"

repositories {
Expand Down Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
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;

@Factory
public class AppConfig {

@Singleton
public HttpClient httpClient() {
return HttpClientBuilder.create().build();
public BlockingHttpClient httpClient(@Client("${artifactory.url}") HttpClient artifactoryClient) {
return artifactoryClient.toBlocking();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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);
}
Expand Down

0 comments on commit efbc339

Please sign in to comment.