package at.gv.egiz.moazs.pipeline; import at.gv.egiz.moazs.MoaZSException; import at.gv.egiz.moazs.msg.MsgClientFactory; import at.gv.egiz.moazs.verify.SignatureVerifier; import at.gv.egiz.moazs.repository.DeliveryRepository; import at.gv.egiz.moazs.scheme.Mzs2MsgConverter; import at.gv.egiz.moazs.scheme.NameSpace; import at.gv.egiz.moazs.tnvz.TnvzClient; import at.gv.egiz.moazs.tnvz.TnvzResultVerifier; import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; import at.gv.zustellung.msg.xsd.DeliveryAnswerType; import at.gv.zustellung.msg.xsd.DeliveryRequestStatusType; import at.gv.zustellung.tnvz.xsd.PersonResultType; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Component; import static at.gv.egiz.moazs.MoaZSException.moaZSExceptionBuilder; import static at.gv.egiz.moazs.util.NullCoalesce.coalesce; import static at.gv.zustellung.msg.xsd.DeliveryRequestStatusType.Error.errorBuilder; import static at.gv.zustellung.msg.xsd.DeliveryRequestStatusType.deliveryRequestStatusTypeBuilder; import static at.gv.zustellung.msg.xsd.ErrorInfoType.errorInfoTypeBuilder; import static java.lang.String.format; @Component @Profile("!cluster") public class SameThreadDeliveryPipeline implements DeliveryPipeline { private static final Logger log = Logger.getLogger(SameThreadDeliveryPipeline.class); private final DeliveryRepository repository; private final TnvzClient tnvzClient; private final TnvzResultVerifier tnvzVerifier; private final Mzs2MsgConverter converter; private final MsgClientFactory msgClientFactory; private final SignatureVerifier verifier; @Autowired public SameThreadDeliveryPipeline(DeliveryRepository repository, TnvzClient tnvzClient, TnvzResultVerifier tnvzVerifier, Mzs2MsgConverter converter, MsgClientFactory msgClientFactory, SignatureVerifier verifier ) { this.repository = repository; this.tnvzClient = tnvzClient; this.tnvzVerifier = tnvzVerifier; this.converter = converter; this.msgClientFactory = msgClientFactory; this.verifier = verifier; } @Override public void processRequest(String appDeliveryId) { var exceptionBuilder = moaZSExceptionBuilder(""); try { var mzsRequest = repository.getDeliveryRequest(appDeliveryId).orElseThrow(); exceptionBuilder.withMzsRequest(mzsRequest); at.gv.zustellung.msg.xsd.DeliveryRequestType msgRequest; if (mzsRequest.getConfig().isPerformQueryPersonRequest()) { var tnvzResult = performTnvzQuery(mzsRequest); exceptionBuilder.withTnvzResult(tnvzResult); msgRequest = converter.convert(mzsRequest, tnvzResult.getSuccess().getIdentification()); } else { msgRequest = converter.convert(mzsRequest); } exceptionBuilder.withMsgRequest(msgRequest); var status = msgClientFactory.create(mzsRequest.getConfig().getMSGClient()).delivery(msgRequest); exceptionBuilder.withMsgResult(status); verifySignedStatus(appDeliveryId, exceptionBuilder); repository.add(status); } catch (MoaZSException exception) { log.error(format("An error occured while processing the DeliveryRequest with AppDeliveryID=%s. ", appDeliveryId), exception); var errorStatus = generateErrorStatus(exception, appDeliveryId); repository.add(errorStatus); } } private void verifySignedStatus(String appDeliveryId, MoaZSException.Builder exceptionBuilder) throws MoaZSException { try { var signedStatus = repository.getSignedDeliveryRequestStatus(appDeliveryId).get(); verifier.verify(signedStatus); } catch (Exception ex) { throw exceptionBuilder.withMessage(format("Signature of DeliveryRequestStatus with AppDeliveryID=%s " + " is not valid.", appDeliveryId)) .withErrorCode(MoaZSException.ERROR_MOASP_SIGNATURE_INVALID) .withCause(ex) .build(); } } private PersonResultType performTnvzQuery(DeliveryRequestType request) { var result = tnvzClient.query(request.getSender(), request.getReceiver()); tnvzVerifier.verify(request, result); return result; } private DeliveryRequestStatusType generateErrorStatus(MoaZSException exception, String appDeliveryId) { var infoBuilder = errorInfoTypeBuilder() .withText(exception.getMessage()) .withCode(exception.getErrorCode()); var errorBuilder = errorBuilder() .withErrorInfo(infoBuilder.build()) .withAppDeliveryID(appDeliveryId); if (exception.getMzsRequest() != null) { errorBuilder.withDeliverySystem(exception.getMzsRequest().getConfig().getMSGClient().getURL()); } if (exception.getTnvzResult() != null && exception.getTnvzResult().getError() != null) { errorBuilder.withPreAdviceNoteSent(exception.getTnvzResult().getError().getPreAdviceNoteSent()); } if (exception.getMsgResult() != null) { var answer = getAnswerFromResult(exception.getMsgResult()); errorBuilder.withGZ(answer.getGZ()); errorBuilder.withZSDeliveryID(answer.getZSDeliveryID()); } return deliveryRequestStatusTypeBuilder() .withError(errorBuilder.build()) .withVersion(NameSpace.MSG_VERSION) .build(); } private DeliveryAnswerType getAnswerFromResult(DeliveryRequestStatusType msgResult) { return coalesce(msgResult.getSuccess(), msgResult.getPartialSuccess(), msgResult.getError()).get(); } }