Skip to content

Commit

Permalink
Merge pull request #302 from solver-it-sro/AG-152/eform-visualization
Browse files Browse the repository at this point in the history
AG-152 Eform visualization
  • Loading branch information
celuchmarek authored Nov 7, 2023
2 parents 4c232a8 + 7c6d2dd commit 9527ca6
Show file tree
Hide file tree
Showing 86 changed files with 2,638 additions and 1,473 deletions.
2 changes: 2 additions & 0 deletions src/main/java/digital/slovensko/autogram/core/Autogram.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public void startVisualization(SigningJob job) {
try {
var visualization = DocumentVisualizationBuilder.fromJob(job);
ui.onUIThreadDo(() -> ui.showVisualization(visualization, this));
} catch (AutogramException e) {
ui.onUIThreadDo(() -> ui.showError(e));
} catch (Exception e) {
Runnable onContinue = () -> ui.showVisualization(new UnsupportedVisualization(job), this);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
Expand All @@ -21,6 +20,7 @@
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import digital.slovensko.autogram.util.XMLUtils;
import eu.europa.esig.dss.enumerations.SignatureLevel;
import eu.europa.esig.dss.model.DSSDocument;
import eu.europa.esig.dss.model.DSSException;
Expand Down Expand Up @@ -134,26 +134,21 @@ public synchronized ValidationReports getSignatureValidationReport(SigningJob jo
}

public static String getSignatureValidationReportHTML(Reports signatureValidationReport) {
var builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true);

try {
builderFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);

var document = builderFactory.newDocumentBuilder().parse(new InputSource(new StringReader(signatureValidationReport.getXmlSimpleReport())));
var document = XMLUtils.getSecureDocumentBuilder().parse(new InputSource(new StringReader(signatureValidationReport.getXmlSimpleReport())));
var xmlSource = new DOMSource(document);

var xsltFile = SignatureValidator.class.getResourceAsStream("simple-report-bootstrap4.xslt");
var xsltSource = new StreamSource(xsltFile);

var outputTarget = new StreamResult(new StringWriter());
var transformer = TransformerFactory.newInstance().newTransformer(xsltSource);
var transformer = XMLUtils.getSecureTransformerFactory().newTransformer(xsltSource);
transformer.transform(xmlSource, outputTarget);

var r = outputTarget.getWriter().toString().trim();

var templateFile = SignatureValidator.class.getResourceAsStream("simple-report-template.html");
var templateString = new String(templateFile.readAllBytes());
var templateString = new String(templateFile.readAllBytes(), StandardCharsets.UTF_8);
return templateString.replace("{{content}}", r);

} catch (SAXException | IOException | ParserConfigurationException | TransformerException e) {
Expand Down
72 changes: 44 additions & 28 deletions src/main/java/digital/slovensko/autogram/core/SigningJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,35 @@

import java.io.File;

import digital.slovensko.autogram.core.eforms.EFormUtils;
import digital.slovensko.autogram.core.eforms.XDCBuilder;
import digital.slovensko.autogram.core.eforms.XDCValidator;
import digital.slovensko.autogram.core.errors.AutogramException;
import digital.slovensko.autogram.util.Logging;
import eu.europa.esig.dss.asic.cades.signature.ASiCWithCAdESService;
import eu.europa.esig.dss.asic.xades.signature.ASiCWithXAdESService;
import eu.europa.esig.dss.cades.signature.CAdESService;
import eu.europa.esig.dss.enumerations.SignatureLevel;
import eu.europa.esig.dss.model.CommonDocument;
import eu.europa.esig.dss.model.DSSDocument;
import eu.europa.esig.dss.model.FileDocument;
import eu.europa.esig.dss.pades.signature.PAdESService;
import eu.europa.esig.dss.validation.CommonCertificateVerifier;
import eu.europa.esig.dss.xades.signature.XAdESService;

import static digital.slovensko.autogram.core.AutogramMimeType.isPDF;
import static digital.slovensko.autogram.core.AutogramMimeType.*;

public class SigningJob {
private final Responder responder;
private final CommonDocument document;
private final DSSDocument document;
private final SigningParameters parameters;

public SigningJob(CommonDocument document, SigningParameters parameters, Responder responder) {
private SigningJob(DSSDocument document, SigningParameters parameters, Responder responder) {
this.document = document;
this.parameters = parameters;
this.responder = responder;
}

public CommonDocument getDocument() {
public DSSDocument getDocument() {
return this.document;
}

Expand All @@ -40,10 +42,6 @@ public int getVisualizationWidth() {
return parameters.getVisualizationWidth();
}

private boolean isDocumentXDC() {
return document.getMimeType().equals(AutogramMimeType.XML_DATACONTAINER);
}

public void signWithKeyAndRespond(SigningKey key) throws InterruptedException, AutogramException {

Logging.log("Signing Job: " + this.hashCode() + " file " + getDocument().getName());
Expand Down Expand Up @@ -79,13 +77,6 @@ private DSSDocument signDocumentAsCAdeS(SigningKey key) {
}

private DSSDocument signDocumentAsAsiCWithXAdeS(SigningKey key) {
DSSDocument doc = getDocument();
if (getParameters().shouldCreateDatacontainer() && !isDocumentXDC()) {
var transformer = XDCTransformer.buildFromSigningParameters(getParameters());
doc = transformer.transform(doc);
doc.setMimeType(AutogramMimeType.XML_DATACONTAINER);
}

var commonCertificateVerifier = new CommonCertificateVerifier();
var service = new ASiCWithXAdESService(commonCertificateVerifier);
var signatureParameters = getParameters().getASiCWithXAdESSignatureParameters();
Expand All @@ -94,10 +85,10 @@ private DSSDocument signDocumentAsAsiCWithXAdeS(SigningKey key) {
signatureParameters.setCertificateChain(key.getCertificateChain());
signatureParameters.setSignWithExpiredCertificate(true);

var dataToSign = service.getDataToSign(doc, signatureParameters);
var dataToSign = service.getDataToSign(getDocument(), signatureParameters);
var signatureValue = key.sign(dataToSign, getParameters().getDigestAlgorithm());

return service.signDocument(doc, signatureParameters, signatureValue);
return service.signDocument(getDocument(), signatureParameters, signatureValue);
}

private DSSDocument signDocumentAsXAdeS(SigningKey key) {
Expand Down Expand Up @@ -148,14 +139,39 @@ private DSSDocument signDocumentAsPAdeS(SigningKey key) {
return service.signDocument(getDocument(), signatureParameters, signatureValue);
}

public static FileDocument createDSSFileDocumentFromFile(File file) {
var fileDocument = new FileDocument(file);

if (isXDC(fileDocument.getMimeType()) || isXML(fileDocument.getMimeType()) && XDCValidator.isXDCContent(fileDocument))
fileDocument.setMimeType(AutogramMimeType.XML_DATACONTAINER);

return fileDocument;
}

private static SigningJob build(DSSDocument document, SigningParameters params, Responder responder) {
if (params.shouldCreateXdc()) {
var mimeType = document.getMimeType();
if (!isXDC(mimeType) && !isAsice(mimeType)) {
document = XDCBuilder.transform(params, document.getName(), EFormUtils.getXmlFromDocument(document));
document.setMimeType(AutogramMimeType.XML_DATACONTAINER);
}
}

return new SigningJob(document, params, responder);
}

public static SigningJob buildFromRequest(DSSDocument document, SigningParameters params, Responder responder) {
return build(document, params, responder);
}

public static SigningJob buildFromFile(File file, Responder responder, boolean checkPDFACompliance, SignatureLevel signatureType, boolean isEn319132) {
var document = new FileDocument(file);
SigningParameters parameters = getParametersForFile(document, checkPDFACompliance, signatureType, isEn319132);
var document = createDSSFileDocumentFromFile(file);
var parameters = getParametersForFile(document, checkPDFACompliance, signatureType, isEn319132);
return new SigningJob(document, parameters, responder);
}

public static SigningJob buildFromFileBatch(File file, Autogram autogram, Responder responder, boolean checkPDFACompliance, SignatureLevel signatureType, boolean isEn319132) {
var document = new FileDocument(file);
var document = createDSSFileDocumentFromFile(file);
var parameters = getParametersForFile(document, checkPDFACompliance, signatureType, isEn319132);
return new SigningJob(document, parameters, responder);
}
Expand All @@ -164,28 +180,28 @@ private static SigningParameters getParametersForFile(FileDocument document, boo
var level = SignatureValidator.getSignedDocumentSignatureLevel(document);
if (level != null) switch (level) {
case PAdES_BASELINE_B:
return SigningParameters.buildForPDF(document.getName(), checkPDFACompliance, isEn319132);
return SigningParameters.buildForPDF(document.getName(), document, checkPDFACompliance, isEn319132);
case XAdES_BASELINE_B:
return SigningParameters.buildForASiCWithXAdES(document.getName(), isEn319132);
return SigningParameters.buildForASiCWithXAdES(document.getName(), document, isEn319132);
case CAdES_BASELINE_B:
return SigningParameters.buildForASiCWithCAdES(document.getName(), isEn319132);
return SigningParameters.buildForASiCWithCAdES(document.getName(), document, isEn319132);
default:
;
}

var filename = document.getName();
if (isPDF(document.getMimeType())) switch (signatureType) {
case PAdES_BASELINE_B:
return SigningParameters.buildForPDF(filename, checkPDFACompliance, isEn319132);
return SigningParameters.buildForPDF(filename, document, checkPDFACompliance, isEn319132);
case XAdES_BASELINE_B:
return SigningParameters.buildForASiCWithXAdES(filename, isEn319132);
return SigningParameters.buildForASiCWithXAdES(filename, document, isEn319132);
case CAdES_BASELINE_B:
return SigningParameters.buildForASiCWithCAdES(filename, isEn319132);
return SigningParameters.buildForASiCWithCAdES(filename, document, isEn319132);
default:
;
}

return SigningParameters.buildForASiCWithXAdES(filename, isEn319132);
return SigningParameters.buildForASiCWithXAdES(filename, document, isEn319132);
}

public boolean shouldCheckPDFCompliance() {
Expand Down
Loading

0 comments on commit 9527ca6

Please sign in to comment.