diff --git a/client-core/src/main/java/nl/vpro/api/client/utils/Swagger.java b/client-core/src/main/java/nl/vpro/api/client/utils/Swagger.java index 13fdf90d..59648932 100644 --- a/client-core/src/main/java/nl/vpro/api/client/utils/Swagger.java +++ b/client-core/src/main/java/nl/vpro/api/client/utils/Swagger.java @@ -4,18 +4,13 @@ import java.io.IOException; import java.io.InputStream; -import java.net.ConnectException; import java.net.URI; +import java.net.http.*; import java.time.Duration; import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.apache.http.client.config.RequestConfig; -import org.apache.http.client.methods.*; -import org.apache.http.conn.ConnectTimeoutException; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClientBuilder; import org.checkerframework.checker.nullness.qual.Nullable; import com.fasterxml.jackson.core.*; @@ -41,50 +36,49 @@ public static VersionResult getVersionFromSwagger(String baseUrl, String default return getVersionFromSwagger(baseUrl, defaultVersion, null); } + private static final HttpClient client = HttpClient.newBuilder() + .version(HttpClient.Version.HTTP_1_1) + .followRedirects(HttpClient.Redirect.NORMAL) + .connectTimeout(Duration.ofSeconds(20)) + .build(); + + public static VersionResult getVersionFromSwagger(String baseUrl, String defaultVersion, @Nullable Duration timeout) { + final URI url = URI.create(baseUrl + "/openapi.json"); try { if (timeout == null) { timeout = Duration.ofSeconds(3); } - ObjectMapper mapper = new ObjectMapper(); - JsonFactory factory = new JsonFactory(); - RequestConfig config = RequestConfig.custom() - .setConnectTimeout((int) timeout.toMillis()) - .setConnectionRequestTimeout((int) timeout.toMillis()) - .setSocketTimeout((int) timeout.toMillis()).build(); - try (CloseableHttpClient client = HttpClientBuilder - .create() - .setDefaultRequestConfig(config) - .build()) { - - URI url = URI.create(baseUrl + "/openapi.json"); - HttpUriRequest request = new HttpGet(url); - - request.addHeader(Headers.NPO_DATE, "CacheBust-" + UUID.randomUUID()); // Cloudfront includes npo date as a cache key header. - try (CloseableHttpResponse response = client.execute(request)) { - if (response.getStatusLine().getStatusCode() == 200) { - try (InputStream stream = response.getEntity().getContent()) { - JsonParser jp = factory.createParser(stream); - JsonNode openapi = mapper.readTree(jp); - String versionString = openapi.get("info").get("version").asText(); - return VersionResult.builder().version(getVersion(versionString, defaultVersion)).available(true).build(); - } - } else { - log.warn("No swagger found at {} -> {}", url, response.getStatusLine()); - } + final ObjectMapper mapper = new ObjectMapper(); + final JsonFactory factory = new JsonFactory(); + + HttpRequest request = HttpRequest.newBuilder() + .uri(url) + .timeout(timeout) + .header("Content-Type", "application/json") + .header(Headers.NPO_DATE, "CacheBust-" + UUID.randomUUID()) // Cloudfront includes npo date as a cache key header. + .GET() + .build(); + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofInputStream()); + if (response.statusCode() == 200) { + try (InputStream stream = response.body()) { + JsonParser jp = factory.createParser(stream); + JsonNode openapi = mapper.readTree(jp); + String versionString = openapi.get("info").get("version").asText(); + return VersionResult.builder().version(getVersion(versionString, defaultVersion)).available(true).build(); } + } else { + log.warn("No swagger found at {} -> {}", url, response.statusCode()); return VersionResult.builder().version(defaultVersion).available(false).build(); } - } catch (JsonParseException jpe) { - log.warn(jpe.getMessage()); - return VersionResult.builder().version(defaultVersion).available(true).build(); - } catch (ConnectException | ConnectTimeoutException e) { - log.warn(e.getMessage()); - return VersionResult.builder().version(defaultVersion).available(false).build(); - } catch (IOException e) { - log.error(e.getMessage(), e); + } catch (JsonParseException ex) { + log.warn(url + ": " + ex.getMessage(), ex); return VersionResult.builder().version(defaultVersion).available(false).build(); + } catch (IOException | InterruptedException ex) { + log.warn(ex.getMessage(), ex); + return VersionResult.builder().version(defaultVersion).available(true).build(); } + } public static String getVersion(String versionString, String defaultVersion) { diff --git a/client-core/src/test/java/nl/vpro/api/client/utils/SwaggerTest.java b/client-core/src/test/java/nl/vpro/api/client/utils/SwaggerTest.java index b91fe3c5..375dcd1b 100644 --- a/client-core/src/test/java/nl/vpro/api/client/utils/SwaggerTest.java +++ b/client-core/src/test/java/nl/vpro/api/client/utils/SwaggerTest.java @@ -1,6 +1,5 @@ package nl.vpro.api.client.utils; -import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; import nl.vpro.util.Version; @@ -16,15 +15,17 @@ public class SwaggerTest { @Test public void getVersion() { - Assertions.assertThat(Swagger.getVersion("5.6-SNAPSHOT.${builder.vcs.number}", null)).isEqualTo("5.6"); + assertThat(Swagger.getVersion("5.6-SNAPSHOT.${builder.vcs.number}", null)).isEqualTo("5.6"); assertThat(Swagger.getVersion("5.6.2.${builder.vcs.number}", null)).isEqualTo("5.6.2"); assertThat(Swagger.getVersionNumber("5.6-SNAPSHOT.${builder.vcs.number}", null)).isEqualTo(Version.of(5, 6)); assertThat(Swagger.getVersionNumber("5.6.2.${builder.vcs.number}", null)).isEqualTo(Version.of(5, 6, 2)); + } - - + @Test + public void getVersionFromSwagger() { + assertThat(Swagger.getVersionFromSwagger("https://rs.poms.omroep.nl/v1/api", "5.6", null).getVersion()).isNotEqualTo("5.6"); } }