From e1f365955aa22cdf8e44429af2b744388ce0c05b Mon Sep 17 00:00:00 2001 From: Christof Rabensteiner Date: Tue, 28 May 2019 10:48:20 +0200 Subject: Integrate Sign.Verification and Improve Error Handling of Pipeline - Ensure proper communication of errors between pipeline and mzs service by converting MoaZSExceptions into DeliveryRequestStatus messages. - Revise MoaZSException: Add optional fields; those fields are a) helpful to construct meaningful error messages and b) optional because, depending on where an exception appears, either existent or non-existent and thus optional. Add inner-class Builder. - Integrate Signature Verification into pipeline and add Stub for SignatureVerification. - Move TNVZResponse's Mimetype check into dedicated class (Reason: separate abstration layers). - Update api changes in testcases. --- .../at/gv/egiz/moazs/tnvz/TnvzResultVerifier.java | 63 ++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/main/java/at/gv/egiz/moazs/tnvz/TnvzResultVerifier.java (limited to 'src/main/java/at/gv/egiz/moazs/tnvz/TnvzResultVerifier.java') diff --git a/src/main/java/at/gv/egiz/moazs/tnvz/TnvzResultVerifier.java b/src/main/java/at/gv/egiz/moazs/tnvz/TnvzResultVerifier.java new file mode 100644 index 0000000..9992246 --- /dev/null +++ b/src/main/java/at/gv/egiz/moazs/tnvz/TnvzResultVerifier.java @@ -0,0 +1,63 @@ +package at.gv.egiz.moazs.tnvz; + +import at.gv.egiz.moazs.MoaZSException; +import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; +import at.gv.zustellung.tnvz.xsd.PersonResultType; +import org.springframework.stereotype.Component; + +import java.util.Collection; +import java.util.List; + +import static at.gv.egiz.moazs.MoaZSException.moaZSExceptionBuilder; +import static java.lang.String.join; +import static java.util.stream.Collectors.toSet; + +@Component +public class TnvzResultVerifier { + + public void verify(DeliveryRequestType request, PersonResultType result) { + + if (result.getError() != null) { + var info = result.getError().getErrorInfo(); + throw moaZSExceptionBuilder("Receiver is not addressable. Reason: %s", info.getText()) + .withErrorCode(info.getCode()) + .withMzsRequest(request) + .withTnvzResult(result) + .build(); + } + + var mismatchedTypes = findMimeTypeMismatches(result, request); + + if (!mismatchedTypes.isEmpty()) { + var template = "Request contains attachment of type(s) %s, but receiver only accepts attachments" + + " of type(s) %s."; + var acceptedTypesString = join(",", getAcceptedTypes(result)); + var mismatchedTypesString = join(",", mismatchedTypes); + throw moaZSExceptionBuilder(template, mismatchedTypesString, acceptedTypesString) + .withErrorCode(MoaZSException.ERROR_MZS_MIMETYPE_MISSMATCH) + .withMzsRequest(request) + .withTnvzResult(result) + .build(); + } + } + + private Collection findMimeTypeMismatches(PersonResultType result, DeliveryRequestType request) { + var acceptedTypes = getAcceptedTypes(result); + + if (acceptedTypes.contains("*/*")) { + return List.of(); + } + + var typesInRequest = request.getPayload().stream() + .map(DeliveryRequestType.Payload::getMIMEType) + .collect(toSet()); + + typesInRequest.removeAll(acceptedTypes); + + return typesInRequest; + } + + private List getAcceptedTypes(PersonResultType result) { + return result.getSuccess().getMimeTypeList().getMimeType(); + } +} -- cgit v1.2.3