From d806bcfaa31da78bb98d85f25c0ac6b123e1561a Mon Sep 17 00:00:00 2001 From: e550448 Date: Mon, 10 Jun 2024 16:25:25 +0200 Subject: [PATCH] feat: Logging Weasyprint service versions Refs: #DEV-11775 --- .../service/WeasyPrintServiceConnector.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/main/java/ch/sbb/polarion/extension/pdf/exporter/weasyprint/service/WeasyPrintServiceConnector.java b/src/main/java/ch/sbb/polarion/extension/pdf/exporter/weasyprint/service/WeasyPrintServiceConnector.java index 2ee3c81..7860829 100644 --- a/src/main/java/ch/sbb/polarion/extension/pdf/exporter/weasyprint/service/WeasyPrintServiceConnector.java +++ b/src/main/java/ch/sbb/polarion/extension/pdf/exporter/weasyprint/service/WeasyPrintServiceConnector.java @@ -6,6 +6,7 @@ import ch.sbb.polarion.extension.pdf.exporter.weasyprint.service.model.WeasyPrintServiceVersion; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import com.polarion.core.util.logging.Logger; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; @@ -16,8 +17,16 @@ import java.io.IOException; import java.io.InputStream; import java.lang.module.ModuleDescriptor; +import java.util.concurrent.atomic.AtomicReference; + +import static com.polarion.core.util.StringUtils.isEmpty; public class WeasyPrintServiceConnector implements WeasyPrintConverter { + private static final Logger logger = Logger.getLogger(WeasyPrintServiceConnector.class); + private static final String WEASYPRINT_VERSION_HEADER = "Weasyprint-Version"; + private static final String PYTHON_VERSION_HEADER = "Python-Version"; + private static final AtomicReference weasyprintVersion = new AtomicReference<>(); + private static final AtomicReference pythonVersion = new AtomicReference<>(); @Override public byte[] convertToPdf(String htmlPage, WeasyPrintOptions weasyPrintOptions) { @@ -36,6 +45,7 @@ public byte[] convertToPdf(String htmlPage, WeasyPrintOptions weasyPrintOptions) if (response.getStatus() == Response.Status.OK.getStatusCode()) { InputStream inputStream = response.readEntity(InputStream.class); try { + logWeasyprintVersionFromHeader(response); return inputStream.readAllBytes(); } catch (IOException e) { throw new IllegalStateException("Could not read response stream", e); @@ -83,4 +93,18 @@ private String getWeasyPrintServiceBaseUrl() { return PdfExporterExtensionConfiguration.getInstance().getWeasyprintService(); } + private void logWeasyprintVersionFromHeader(Response response) { + String actualWeasyprintVersion = response.getHeaderString(WEASYPRINT_VERSION_HEADER); + String actualPythonVersion = response.getHeaderString(PYTHON_VERSION_HEADER); + + logWeasyprintVersion(actualWeasyprintVersion, weasyprintVersion, "Weasyprint"); + logWeasyprintVersion(actualPythonVersion, pythonVersion, "Python"); + } + + private void logWeasyprintVersion(String actualVersion, AtomicReference version, String nameInMessage) { + if (!isEmpty(actualVersion) + && !actualVersion.equals(version.getAndSet(actualVersion))) { + logger.info(String.format("Using Weasyprint Service with %s version: %s", nameInMessage, actualVersion)); + } + } }