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(); } }