diff options
-rw-r--r-- | pdf-as-lib/src/main/java/at/gv/egiz/sl/util/BKUSLConnector.java | 73 | ||||
-rw-r--r-- | pdf-as-lib/src/test/java/at/gv/egiz/pdfas/lib/test/mains/LegacySLExtractor.java | 74 |
2 files changed, 141 insertions, 6 deletions
diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/BKUSLConnector.java b/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/BKUSLConnector.java index 15d2fb38..84bd7d29 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/BKUSLConnector.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/sl/util/BKUSLConnector.java @@ -27,6 +27,8 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.Charset; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; @@ -62,7 +64,10 @@ public class BKUSLConnector extends BaseSLConnector { .getLogger(BKUSLConnector.class); public static final String SIGNATURE_DEVICE = "BKU"; - + + public static final String PATTERN_ERROR_CODE = "<.*:?ErrorCode>\\s*([0-9]+)\\s*</.*:?ErrorCode>"; + public static final String PATTERN_ERROR_INFO = "<.*:?Info>\\s*(.*)\\s*</.*:?Info>"; + private String bkuUrl; public BKUSLConnector(Configuration config) { @@ -74,6 +79,50 @@ public class BKUSLConnector extends BaseSLConnector { return builder.build(); } + public static SLPdfAsException generateLegacySLException(String xmlResponse) { + if (xmlResponse != null) { + if (xmlResponse.contains("ErrorResponse")) { + int errorCode = -1; + String errorInfo = null; + // Probably an ErrorResponse + Pattern patternErrorCode = Pattern.compile(PATTERN_ERROR_CODE, + Pattern.CASE_INSENSITIVE); + Matcher matcherErrorCode = patternErrorCode + .matcher(xmlResponse); + if (matcherErrorCode.find()) { + if (matcherErrorCode.groupCount() == 1) { + String errorCodeString = matcherErrorCode.group(1); + try { + errorCode = Integer.parseInt(errorCodeString); + } catch (NumberFormatException e) { + // Ignore + logger.trace( + "Failed to convert ErrorCode [{}] into number", + errorCodeString); + } + } + } + + if (errorCode > 0) { + + Pattern patternErrorInfo = Pattern.compile( + PATTERN_ERROR_INFO, Pattern.CASE_INSENSITIVE); + Matcher matcherErrorInfo = patternErrorInfo + .matcher(xmlResponse); + + if (matcherErrorInfo.find()) { + if (matcherErrorInfo.groupCount() == 1) { + errorInfo = matcherErrorInfo.group(1); + } + } + + return new SLPdfAsException(errorCode, errorInfo); + } + } + } + return null; + } + private String performHttpRequestToBKU(String xmlRequest, RequestPackage pack, SignParameter parameter) throws ClientProtocolException, IOException, IllegalStateException { @@ -118,9 +167,9 @@ public class BKUSLConnector extends BaseSLConnector { holder.getProcessInfo().add(hdr); } } - - BKUHeader hdr = new BKUHeader(ErrorConstants.STATUS_INFO_SIGDEVICE, - SIGNATURE_DEVICE); + + BKUHeader hdr = new BKUHeader( + ErrorConstants.STATUS_INFO_SIGDEVICE, SIGNATURE_DEVICE); logger.debug("Response Header : {}", hdr.toString()); holder.getProcessInfo().add(hdr); } @@ -153,18 +202,25 @@ public class BKUSLConnector extends BaseSLConnector { throws PdfAsException { JAXBElement<?> element = null; String slRequest; + String slResponse = null; try { slRequest = SLMarschaller.marshalToString(of .createInfoboxReadRequest(request)); logger.trace(slRequest); - String slResponse = performHttpRequestToBKU(slRequest, null, + slResponse = performHttpRequestToBKU(slRequest, null, parameter); element = (JAXBElement<?>) SLMarschaller .unmarshalFromString(slResponse); } catch (JAXBException e) { + + SLPdfAsException slError = generateLegacySLException(slResponse); + if(slError != null) { + throw slError; + } + throw new PDFIOException("error.pdf.io.03", e); } catch (ClientProtocolException e) { throw new PDFIOException("error.pdf.io.03", e); @@ -193,17 +249,22 @@ public class BKUSLConnector extends BaseSLConnector { SignParameter parameter) throws PdfAsException { JAXBElement<?> element = null; String slRequest; + String slResponse = null; try { slRequest = SLMarschaller.marshalToString(of .createCreateCMSSignatureRequest(pack.getRequestType())); logger.debug(slRequest); - String slResponse = performHttpRequestToBKU(slRequest, pack, + slResponse = performHttpRequestToBKU(slRequest, pack, parameter); element = (JAXBElement<?>) SLMarschaller .unmarshalFromString(slResponse); } catch (JAXBException e) { + SLPdfAsException slError = generateLegacySLException(slResponse); + if(slError != null) { + throw slError; + } throw new PDFIOException("error.pdf.io.03", e); } catch (ClientProtocolException e) { throw new PDFIOException("error.pdf.io.03", e); diff --git a/pdf-as-lib/src/test/java/at/gv/egiz/pdfas/lib/test/mains/LegacySLExtractor.java b/pdf-as-lib/src/test/java/at/gv/egiz/pdfas/lib/test/mains/LegacySLExtractor.java new file mode 100644 index 00000000..d31d6b1e --- /dev/null +++ b/pdf-as-lib/src/test/java/at/gv/egiz/pdfas/lib/test/mains/LegacySLExtractor.java @@ -0,0 +1,74 @@ +package at.gv.egiz.pdfas.lib.test.mains; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import at.gv.egiz.pdfas.common.exceptions.SLPdfAsException; +import at.gv.egiz.sl.util.BKUSLConnector; + +public class LegacySLExtractor { + + public static final String TEST_LEGACY_RESPONSE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + + "<sl10:ErrorResponse xmlns:sl10=\"http://www.buergerkarte.at/namespaces/securitylayer/20020225#\">" + + "<sl10:ErrorCode>1501</sl10:ErrorCode>" + + "<sl10:Info>Fehler in XML-Struktur der Anfrage. (Element content is invalid according to the DTD/Schema.)</sl10:Info>" + + "</sl10:ErrorResponse>"; + + public static final String TEST_LEGACY_RESPONSE_NOINFO = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + + "<sl10:ErrorResponse xmlns:sl10=\"http://www.buergerkarte.at/namespaces/securitylayer/20020225#\">" + + "<sl10:ErrorCode>1501</sl10:ErrorCode>" + + "</sl10:ErrorResponse>"; + + public static final String TEST_LEGACY_RESPONSE_PLAIN = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + + "<sl10:ErrorResponse xmlns:sl10=\"http://www.buergerkarte.at/namespaces/securitylayer/20020225#\">" + + "</sl10:ErrorResponse>"; + + public static final String TEST_LEGACY_SOMETHING_ELSE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + + "<sl10:Errorresponse xmlns:sl10=\"http://www.buergerkarte.at/namespaces/securitylayer/20020225#\">" + + "<sl10:ErrorCode>1501</sl10:ErrorCode>" + + "<sl10:Info>Fehler in XML-Struktur der Anfrage. (Element content is invalid according to the DTD/Schema.)</sl10:Info>" + + "</sl10:Errorresponse>"; + + @Test + public void test() { + SLPdfAsException e = BKUSLConnector.generateLegacySLException(TEST_LEGACY_RESPONSE); + + if(e == null) { + fail("Failed to extract SL Error"); + } + + if(e.getCode() != 1501 || !e.getInfo().equals("Fehler in XML-Struktur der Anfrage. (Element content is invalid according to the DTD/Schema.)")) { + fail("Failed to extract SL Error"); + } + + SLPdfAsException e1 = BKUSLConnector.generateLegacySLException(TEST_LEGACY_RESPONSE_NOINFO); + + if(e1 == null) { + fail("Failed to extract SL Error"); + } + + if(e1.getCode() != 1501 || e1.getInfo() != null) { + fail("Failed to extract SL Error"); + } + + SLPdfAsException e2 = BKUSLConnector.generateLegacySLException(TEST_LEGACY_RESPONSE_PLAIN); + + if(e2 != null) { + fail("Extracted invalid error info"); + } + + SLPdfAsException e3 = BKUSLConnector.generateLegacySLException(TEST_LEGACY_SOMETHING_ELSE); + + if(e3 != null) { + fail("Extracted invalid error info"); + } + + SLPdfAsException e4 = BKUSLConnector.generateLegacySLException(null); + + if(e4 != null) { + fail("Extracted invalid error info"); + } + } + +} |