diff options
Diffstat (limited to 'src/main/java/at/gv/egiz/moazs/tnvz')
-rw-r--r-- | src/main/java/at/gv/egiz/moazs/tnvz/TnvzClient.java | 12 | ||||
-rw-r--r-- | src/main/java/at/gv/egiz/moazs/tnvz/TnvzResultVerifier.java | 63 |
2 files changed, 74 insertions, 1 deletions
diff --git a/src/main/java/at/gv/egiz/moazs/tnvz/TnvzClient.java b/src/main/java/at/gv/egiz/moazs/tnvz/TnvzClient.java index 4a69aea..d4049fc 100644 --- a/src/main/java/at/gv/egiz/moazs/tnvz/TnvzClient.java +++ b/src/main/java/at/gv/egiz/moazs/tnvz/TnvzClient.java @@ -1,5 +1,7 @@ package at.gv.egiz.moazs.tnvz; +import at.gv.egiz.moazs.MoaZSException; +import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Sender; import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Receiver; import at.gv.zustellung.tnvz.xsd.PersonResultSuccessType; @@ -7,10 +9,18 @@ import at.gv.zustellung.tnvz.xsd.PersonResultType; import at.gv.zustellung.tnvz.xsd.PersonResultType.PersonResultTypeBuilder; 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 TnvzClient { - public PersonResultType queryPerson(Sender sender, Receiver receiver) { + public PersonResultType query(Sender sender, Receiver receiver) { + //TODO: Replace this stub with actual request. PersonResultSuccessType success = new PersonResultSuccessType.PersonResultSuccessTypeBuilder().build(); return new PersonResultTypeBuilder().withSuccess(success).build(); } 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<String> 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<String> getAcceptedTypes(PersonResultType result) { + return result.getSuccess().getMimeTypeList().getMimeType(); + } +} |