Skip to content

Commit

Permalink
feat: Logging Weasyprint service versions
Browse files Browse the repository at this point in the history
Refs: #DEV-11775
  • Loading branch information
nirikash committed Jun 10, 2024
1 parent e6aa41a commit d806bcf
Showing 1 changed file with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> weasyprintVersion = new AtomicReference<>();
private static final AtomicReference<String> pythonVersion = new AtomicReference<>();

@Override
public byte[] convertToPdf(String htmlPage, WeasyPrintOptions weasyPrintOptions) {
Expand All @@ -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);
Expand Down Expand Up @@ -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<String> version, String nameInMessage) {
if (!isEmpty(actualVersion)
&& !actualVersion.equals(version.getAndSet(actualVersion))) {
logger.info(String.format("Using Weasyprint Service with %s version: %s", nameInMessage, actualVersion));
}
}
}

0 comments on commit d806bcf

Please sign in to comment.