From c5096b25253eb117d508b5ea1a3a451efb839aed Mon Sep 17 00:00:00 2001 From: Magnus Viernickel Date: Thu, 14 Mar 2024 13:08:19 +0100 Subject: [PATCH] [feat] remove brotli stuff; for some reason http client 5 does something different than the older http client Signed-off-by: Magnus Viernickel --- .../repositories/NixpkgsMetaAnalyzer.java | 46 ++++++++++--------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/dependencytrack/tasks/repositories/NixpkgsMetaAnalyzer.java b/src/main/java/org/dependencytrack/tasks/repositories/NixpkgsMetaAnalyzer.java index 8a162a5423..64e8ba2b98 100644 --- a/src/main/java/org/dependencytrack/tasks/repositories/NixpkgsMetaAnalyzer.java +++ b/src/main/java/org/dependencytrack/tasks/repositories/NixpkgsMetaAnalyzer.java @@ -2,8 +2,6 @@ import alpine.common.logging.Logger; import org.apache.hc.client5.http.classic.methods.HttpGet; -import org.apache.hc.client5.http.entity.BrotliInputStreamFactory; -import org.apache.hc.client5.http.entity.DecompressingEntity; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; import org.apache.hc.client5.http.impl.classic.HttpClients; @@ -32,27 +30,31 @@ private NixpkgsMetaAnalyzer() { this.baseUrl = DEFAULT_CHANNEL_URL; HashMap newLatestVersion = new HashMap<>(); - try (final CloseableHttpResponse packagesResponse = processHttpRequest5()) { - if (packagesResponse != null && packagesResponse.getCode() == HttpStatus.SC_OK) { - final var entity = packagesResponse.getEntity(); - if (entity != null) { - // TODO(mangoiv): is this the fastest way we can do this? - final var entityString = EntityUtils.toString(new DecompressingEntity(entity, new BrotliInputStreamFactory())); - final var packages = new JSONObject(entityString).getJSONObject("packages").toMap().values(); - packages.forEach(pkg -> { - // FUTUREWORK(mangoiv): there are potentially packages with the same pname - if (pkg instanceof JSONObject jsonPkg) { - final var pname = jsonPkg.getString("pname"); - final var version = jsonPkg.getString("version"); - newLatestVersion.putIfAbsent(pname, version); - } - }); - } + try (final CloseableHttpClient client = HttpClients.createDefault()) { + try (final CloseableHttpResponse packagesResponse = processHttpRequest5(client)) { + if (packagesResponse != null && packagesResponse.getCode() == HttpStatus.SC_OK) { + final var entity = packagesResponse.getEntity(); + if (entity != null) { + // TODO(mangoiv): is this the fastest way we can do this? + final var entityString = EntityUtils.toString(entity); + final var packages = new JSONObject(entityString).getJSONObject("packages").toMap().values(); + packages.forEach(pkg -> { + // FUTUREWORK(mangoiv): there are potentially packages with the same pname + if (pkg instanceof HashMap jsonPkg) { + final var pname = jsonPkg.get("pname"); + final var version = jsonPkg.get("version"); + newLatestVersion.putIfAbsent((String)pname, (String)version); + } + }); + } + } } } catch (IOException ex) { + LOGGER.debug(ex.toString()); handleRequestException(LOGGER, ex); } catch (Exception ex) { + LOGGER.debug(ex.toString()); throw new MetaAnalyzerException(ex); } this.latestVersion = newLatestVersion; @@ -63,14 +65,14 @@ public static NixpkgsMetaAnalyzer getNixpkgsMetaAnalyzer() { return nixpkgsMetaAnalyzer; } - private CloseableHttpResponse processHttpRequest5() throws IOException { + private CloseableHttpResponse processHttpRequest5(CloseableHttpClient client) throws IOException { try { URIBuilder uriBuilder = new URIBuilder(baseUrl); final HttpGet request = new HttpGet(uriBuilder.build().toString()); request.addHeader("accept", "application/json"); - try (final CloseableHttpClient client = HttpClients.createDefault()) { - return client.execute(request); - } + + return client.execute(request); + } catch (URISyntaxException ex) { handleRequestException(LOGGER, ex); return null;