Skip to content

Commit

Permalink
Merge pull request #489 from xhyrom-forks/fix/handle-unavailable-reso…
Browse files Browse the repository at this point in the history
…urces-properly

fix: handle unavailable resources properly during maintenance
  • Loading branch information
celuchmarek authored Jul 18, 2024
2 parents 45f5aa4 + fafaa45 commit 82aa5ae
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import digital.slovensko.autogram.core.eforms.dto.ManifestXsltEntry;
import digital.slovensko.autogram.core.errors.*;
import eu.europa.esig.dss.spi.exception.DSSExternalResourceException;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
Expand Down Expand Up @@ -220,23 +221,32 @@ public static String getNamespaceFromEformXml(Document xml) {
return null;
}

public static byte[] getResource(String url) {
public static byte[] getResource(String url) throws ServiceUnavailableException {
var offlineFileLoader = new FileCacheDataLoader();
offlineFileLoader.setCacheExpirationTime(21600000); // 6 hours
offlineFileLoader.setDataLoader(new CommonsDataLoader());

DSSDocument xsltDoc;
DSSDocument document;
try {
xsltDoc = offlineFileLoader.getDocument(url);
document = offlineFileLoader.getDocument(url);
} catch (DSSExternalResourceException e) {
var matcher = Pattern.compile("HTTP status code : (\\d{3})").matcher(e.getCause().getMessage());
if (!matcher.find())
return null;

if (matcher.group(1).startsWith("5"))
throw new ServiceUnavailableException(url, e);

return null;
} catch (DSSException e) {
return null;
}

if (xsltDoc == null)
if (document == null)
return null;

try {
return xsltDoc.openStream().readAllBytes();
try (var inputStream = document.openStream()) {
return inputStream.readAllBytes();
} catch (IOException e) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package digital.slovensko.autogram.core.eforms;

import digital.slovensko.autogram.core.eforms.dto.XsltParams;
import digital.slovensko.autogram.core.errors.ServiceUnavailableException;
import digital.slovensko.autogram.core.errors.XMLValidationException;
import eu.europa.esig.dss.enumerations.DigestAlgorithm;
import eu.europa.esig.dss.model.InMemoryDocument;
Expand Down Expand Up @@ -30,7 +31,7 @@ public String getXsdIdentifier() {

@Override
public boolean findResources() throws XMLValidationException {
var manifest_xml = getResource(SOURCE_URL + url + "/META-INF/manifest.xml");
var manifest_xml = getRemoteResource(SOURCE_URL + url + "/META-INF/manifest.xml");
if (manifest_xml == null)
throw new XMLValidationException("Zlyhala príprava elektronického formulára", "Nepodarilo sa nájsť manifest elektronického formulára");

Expand All @@ -45,7 +46,7 @@ public boolean findResources() throws XMLValidationException {
if (entry == null)
return false;

var xsltString = getResource(SOURCE_URL + url + "/" + entry.fullPath());
var xsltString = getRemoteResource(SOURCE_URL + url + "/" + entry.fullPath());
if (xsltString == null)
return false;

Expand All @@ -61,7 +62,7 @@ public boolean findResources() throws XMLValidationException {
if (this.xsltIdentifier == null)
this.xsltIdentifier = "http://schemas.gov.sk/form/" + url + "/form.xslt";

var xsdString = getResource(SOURCE_URL + url + "/schema.xsd");
var xsdString = getRemoteResource(SOURCE_URL + url + "/schema.xsd");
if (xsdString == null)
return false;

Expand All @@ -73,4 +74,16 @@ public boolean findResources() throws XMLValidationException {

return true;
}

private byte[] getRemoteResource(String url) {
var bytes = getResource(url);
if (bytes == null)
return null;

var s = new String(bytes, ENCODING);
if (s.contains("<!DOCTYPE html>") || (s.contains("<html>") && s.contains("<title>Nedostupnosť portálu</title>")))
throw new ServiceUnavailableException(url);

return bytes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package digital.slovensko.autogram.core.errors;

public class ServiceUnavailableException extends AutogramException {
public ServiceUnavailableException(String url) {
this(url, null);
}

public ServiceUnavailableException(String url, Throwable e) {
super("Chyba pripojenia", "Spojenie so serverom zlyhalo", "Nepodarilo sa nadviazať spojenie so serverom na adrese: " + url + "\n\nSkúste podpisovanie opakovať neskôr. Ak problém pretrváva dlhodobo, ozvite sa nám, prosím, emailom na [email protected]", e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static ErrorResponse buildFromException(Exception e) {
// TODO maybe replace with pattern matching someday
return switch (e.getClass().getSimpleName()) {
case "SigningCanceledByUserException" -> new ErrorResponse(204, "USER_CANCELLED", (AutogramException) e);
case "ServiceUnavailableException" -> new ErrorResponse(503, "SERVICE_UNAVAILABLE", (AutogramException) e);
case "UnrecognizedException" -> new ErrorResponse(502, "UNRECOGNIZED_DSS_ERROR", (AutogramException) e);
case "UnsupportedSignatureLevelException" -> new ErrorResponse(422, "UNSUPPORTED_SIGNATURE_LEVEL", (AutogramException) e);
case "RequestValidationException",
Expand Down

0 comments on commit 82aa5ae

Please sign in to comment.