package at.gv.egiz.moazs.pipeline; import at.gv.egiz.moazs.MsgClient; import at.gv.egiz.moazs.scheme.Mzs2MsgConverter; import at.gv.egiz.moazs.TnvzClient; import at.gv.egiz.moazs.repository.DeliveryRepository; import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; import at.gv.zustellung.msg.xsd.persondata.IdentificationType; import at.gv.zustellung.tnvz.xsd.PersonResultType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Component; import java.util.Collection; import java.util.List; import static java.lang.String.format; import static java.lang.String.join; import static java.util.stream.Collectors.toSet; @Component @Profile("!cluster") public class SameThreadDeliveryPipeline implements DeliveryPipeline { private final DeliveryRepository repository; private final TnvzClient tnvzClient; private final Mzs2MsgConverter converter; private final MsgClient msgClient; public SameThreadDeliveryPipeline(@Autowired DeliveryRepository repository, @Autowired TnvzClient tnvzClient, @Autowired Mzs2MsgConverter converter, @Autowired MsgClient msgClient) { this.repository = repository; this.tnvzClient = tnvzClient; this.converter = converter; this.msgClient = msgClient; } @Override public void processRequest(String appDeliveryId) { var mzsRequest = repository.getDeliveryRequest(appDeliveryId).orElseThrow(); var msgRequest = (mzsRequest.getConfig().isPerformQueryPersonRequest()) ? converter.convert(mzsRequest, queryPerson(mzsRequest)) : converter.convert(mzsRequest); var status = msgClient.send(msgRequest); repository.add(status); } private IdentificationType queryPerson(DeliveryRequestType request) { var result = tnvzClient.queryPerson(request.getSender(), request.getReceiver()); if (result.getError() != null) { var error = result.getError(); var info = error.getErrorInfo(); var noteSent = format("Preadvice note was {0}sent", (error.getPreAdviceNoteSent() != null) ? "" : "not "); var message = format("Receiver is not addressable. Code: {0} ; Text: {1}; {2}", info.getCode(), info.getText(), noteSent); throw new RuntimeException(message); } var mismatchedTypes = findMimeTypeMismatches(result, request); if (!mismatchedTypes.isEmpty()) { var template = "Request contains attachment of type(s) {0}, but receiver only accepts attachments of type(s) {1}"; var acceptedTypesString = join(",", getAcceptedTypes(result)); var mismatchedTypesString = join(",", mismatchedTypes); var message = format(template, mismatchedTypesString, acceptedTypesString); throw new RuntimeException(message); } return result.getSuccess().getIdentification(); } private Collection findMimeTypeMismatches(PersonResultType result, DeliveryRequestType request) { var acceptedTypes = getAcceptedTypes(result); if (acceptedTypes.contains("*/*")) { return List.of(); } var typesInRequest = request.getPayload().stream() .map(payload -> payload.getMIMEType()) .collect(toSet()); typesInRequest.removeAll(acceptedTypes); return typesInRequest; } private List getAcceptedTypes(PersonResultType result) { return result.getSuccess().getMimeTypeList().getMimeType(); } }