Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/weasyprint version logging #18

Merged
merged 8 commits into from
Jun 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,20 @@ 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);

boolean hasWeasyPrintVersionChanged = hasVersionChanged(actualWeasyPrintVersion, weasyPrintVersion);
boolean hasPythonVersionChanged = hasVersionChanged(actualPythonVersion, pythonVersion);

if (hasWeasyPrintVersionChanged || hasPythonVersionChanged) {
logger.info(String.format("WeasyPrintService uses WeasyPrint version '%s' and Python version '%s'", actualWeasyPrintVersion, actualPythonVersion));
}
}

public boolean hasVersionChanged(String actualVersion, AtomicReference<String> version) {
return !isEmpty(actualVersion)
&& !actualVersion.equals(version.getAndSet(actualVersion));
}
}
Loading