From d8c668bbca31a835c387a3d52293c9938c424f58 Mon Sep 17 00:00:00 2001 From: Christof Rabensteiner Date: Tue, 9 Jul 2019 10:32:54 +0200 Subject: Refactor MsgResponse and its IDs StoreSOAPBodyBinaryInRepositoryInterceptor: - Replace "generate body's id via concatenation" with "give the right generator function the app delivery id and let the generator function do the work". Reason: Prevent the logistics of deriving IDs to spill into unrelated components. MsgResponse refactor: - Make MsgResponse an abstract class. - Derive ResponseID's ONLY in MsgResponse::createResponseID. Others: - Ensure that all invocations to DeliveryRepository.getResponse and BinaryRepository.get use "responseID" instead of ambiguous "id" or incorrect "appDeliveryID". - Move SingleThreadedDeliveryPipeline into process package. --- .../at/gv/egiz/moazs/config/RepositoryConfig.java | 8 +- .../pipeline/SingleThreadedDeliveryPipeline.java | 107 --------------------- .../moazs/process/SingleNodeResponseHandler.java | 4 +- .../process/SingleThreadedDeliveryPipeline.java | 107 +++++++++++++++++++++ .../egiz/moazs/repository/DeliveryRepository.java | 2 +- .../repository/InMemoryDeliveryRepository.java | 5 +- .../java/at/gv/egiz/moazs/scheme/MsgResponse.java | 29 ++++-- .../gv/egiz/moazs/scheme/NotificationResponse.java | 20 +--- .../egiz/moazs/scheme/RequestStatusResponse.java | 13 +-- ...StoreSOAPBodyBinaryInRepositoryInterceptor.java | 12 ++- .../gv/egiz/moazs/verify/MsgResponseVerifier.java | 6 +- .../moazs/SingleThreadedDeliveryPipelineTest.java | 4 +- 12 files changed, 157 insertions(+), 160 deletions(-) delete mode 100644 src/main/java/at/gv/egiz/moazs/pipeline/SingleThreadedDeliveryPipeline.java create mode 100644 src/main/java/at/gv/egiz/moazs/process/SingleThreadedDeliveryPipeline.java diff --git a/src/main/java/at/gv/egiz/moazs/config/RepositoryConfig.java b/src/main/java/at/gv/egiz/moazs/config/RepositoryConfig.java index ee73b9e..01a4cc3 100644 --- a/src/main/java/at/gv/egiz/moazs/config/RepositoryConfig.java +++ b/src/main/java/at/gv/egiz/moazs/config/RepositoryConfig.java @@ -11,6 +11,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import java.util.Map; +import java.util.function.Function; @Configuration @Profile("!cluster") @@ -25,8 +26,9 @@ public class RepositoryConfig { } @Bean - public Map idSuffixes() { - return Map.of(NameSpace.MSG_DELIVERY_REQUEST_STATUS, RequestStatusResponse.getIdSuffix(), - NameSpace.MSG_DELIVERY_NOTIFICATION, NotificationResponse.getIdSuffix()); + public Map> idGenerators() { + + return Map.of(NameSpace.MSG_DELIVERY_REQUEST_STATUS, RequestStatusResponse::getResponseID, + NameSpace.MSG_DELIVERY_NOTIFICATION, NotificationResponse::getResponseID); } } diff --git a/src/main/java/at/gv/egiz/moazs/pipeline/SingleThreadedDeliveryPipeline.java b/src/main/java/at/gv/egiz/moazs/pipeline/SingleThreadedDeliveryPipeline.java deleted file mode 100644 index 69065d7..0000000 --- a/src/main/java/at/gv/egiz/moazs/pipeline/SingleThreadedDeliveryPipeline.java +++ /dev/null @@ -1,107 +0,0 @@ -package at.gv.egiz.moazs.process; - - -import at.gv.egiz.moazs.MoaZSException; -import at.gv.egiz.moazs.client.ClientFactory; -import at.gv.egiz.moazs.client.TnvzHelper; -import at.gv.egiz.moazs.repository.BinaryRepository; -import at.gv.egiz.moazs.repository.DeliveryRepository; -import at.gv.egiz.moazs.scheme.Mzs2MsgConverter; -import at.gv.egiz.moazs.scheme.RequestStatusResponse; -import at.gv.egiz.moazs.verify.MsgResponseVerifier; -import at.gv.egiz.moazs.verify.SignatureVerifier; -import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; -import at.gv.zustellung.msg.xsd.App2ZusePort; -import at.gv.zustellung.tnvz.xsd.TNVZServicePort; -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.scheme.RequestStatusResponse.generateErrorFromException; -import static java.lang.String.format; - -@Component -@Profile("!cluster") -public class SingleThreadedDeliveryPipeline implements DeliveryPipeline { - - private static final Logger log = Logger.getLogger(SingleThreadedDeliveryPipeline.class); - private static final String MZS_PIPELINE_ERROR_MSG = - "An error occured while processing the DeliveryRequest with AppDeliveryID=%s. "; - - private final DeliveryRepository repository; - private final BinaryRepository binaryRepository; - private final TnvzHelper tnvzHelper; - private final Mzs2MsgConverter converter; - private final ClientFactory clientFactory; - private final SignatureVerifier verifier; - - @Autowired - public SingleThreadedDeliveryPipeline(DeliveryRepository repository, - BinaryRepository binaryStatusRepository, - TnvzHelper tnvzHelper, - Mzs2MsgConverter converter, - ClientFactory clientFactory, - SignatureVerifier verifier) { - this.repository = repository; - this.binaryRepository = binaryStatusRepository; - this.tnvzHelper = tnvzHelper; - this.converter = converter; - this.clientFactory = clientFactory; - this.verifier = verifier; - } - - @Override - public void processRequest(String appDeliveryId) { - - var exceptionBuilder = moaZSExceptionBuilder(); - - try { - var mzsRequest = repository.getDeliveryRequest(appDeliveryId).orElseThrow(); - exceptionBuilder.withDeliverySystem(mzsRequest); - - at.gv.zustellung.msg.xsd.DeliveryRequestType msgRequest = buildMsgRequest(mzsRequest, exceptionBuilder); - - var msgClientParams = mzsRequest.getConfig().getMSGClient(); - App2ZusePort client = clientFactory.create(msgClientParams, App2ZusePort.class); - var status = client.delivery(msgRequest); - - var response = new RequestStatusResponse(status); - exceptionBuilder.withAllParametersInAnswer(response.getAnswer()); - - verifySignedStatus(response.getResponseID(), exceptionBuilder); - repository.add(response); - - } catch (MoaZSException exception) { - log.error(format(MZS_PIPELINE_ERROR_MSG, appDeliveryId), exception); - var errorResponse = generateErrorFromException(exception); - repository.add(errorResponse); - } - } - - private void verifySignedStatus(String appDeliveryId, MoaZSException.Builder exceptionBuilder) throws MoaZSException { - try { - var signedStatus = binaryRepository.get(appDeliveryId).get(); - verifier.verify(signedStatus); - } catch (MoaZSException ex) { - throw exceptionBuilder.withMessage(format(MsgResponseVerifier.MOASP_SIGNATURE_INVALID_ERROR_MSG, appDeliveryId)) - .withErrorCode(MoaZSException.ERROR_MOASP_SIGNATURE_INVALID) - .withCause(ex) - .build(); - } - } - - private at.gv.zustellung.msg.xsd.DeliveryRequestType buildMsgRequest(DeliveryRequestType mzsRequest, - MoaZSException.Builder exceptionBuilder) throws MoaZSException { - if (mzsRequest.getConfig().isPerformQueryPersonRequest()) { - var tnvzClientParams = mzsRequest.getConfig().getTNVZClient(); - TNVZServicePort tvnzPort = clientFactory.create(tnvzClientParams, TNVZServicePort.class); - var identification = tnvzHelper.performQueryPersonRequest(mzsRequest, tvnzPort, exceptionBuilder); - return converter.convert(mzsRequest, identification); - } else { - return converter.convert(mzsRequest); - } - } - -} diff --git a/src/main/java/at/gv/egiz/moazs/process/SingleNodeResponseHandler.java b/src/main/java/at/gv/egiz/moazs/process/SingleNodeResponseHandler.java index ec609cb..b115e27 100644 --- a/src/main/java/at/gv/egiz/moazs/process/SingleNodeResponseHandler.java +++ b/src/main/java/at/gv/egiz/moazs/process/SingleNodeResponseHandler.java @@ -19,8 +19,8 @@ public class SingleNodeResponseHandler implements MsgResponseHandler { } @Override - public void handle(String id) { - supplyAsync(() -> verifier.verify(id)) + public void handle(String responseID) { + supplyAsync(() -> verifier.verify(responseID)) .thenAcceptAsync((response) -> sink.handle(response)); } } diff --git a/src/main/java/at/gv/egiz/moazs/process/SingleThreadedDeliveryPipeline.java b/src/main/java/at/gv/egiz/moazs/process/SingleThreadedDeliveryPipeline.java new file mode 100644 index 0000000..69065d7 --- /dev/null +++ b/src/main/java/at/gv/egiz/moazs/process/SingleThreadedDeliveryPipeline.java @@ -0,0 +1,107 @@ +package at.gv.egiz.moazs.process; + + +import at.gv.egiz.moazs.MoaZSException; +import at.gv.egiz.moazs.client.ClientFactory; +import at.gv.egiz.moazs.client.TnvzHelper; +import at.gv.egiz.moazs.repository.BinaryRepository; +import at.gv.egiz.moazs.repository.DeliveryRepository; +import at.gv.egiz.moazs.scheme.Mzs2MsgConverter; +import at.gv.egiz.moazs.scheme.RequestStatusResponse; +import at.gv.egiz.moazs.verify.MsgResponseVerifier; +import at.gv.egiz.moazs.verify.SignatureVerifier; +import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; +import at.gv.zustellung.msg.xsd.App2ZusePort; +import at.gv.zustellung.tnvz.xsd.TNVZServicePort; +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.scheme.RequestStatusResponse.generateErrorFromException; +import static java.lang.String.format; + +@Component +@Profile("!cluster") +public class SingleThreadedDeliveryPipeline implements DeliveryPipeline { + + private static final Logger log = Logger.getLogger(SingleThreadedDeliveryPipeline.class); + private static final String MZS_PIPELINE_ERROR_MSG = + "An error occured while processing the DeliveryRequest with AppDeliveryID=%s. "; + + private final DeliveryRepository repository; + private final BinaryRepository binaryRepository; + private final TnvzHelper tnvzHelper; + private final Mzs2MsgConverter converter; + private final ClientFactory clientFactory; + private final SignatureVerifier verifier; + + @Autowired + public SingleThreadedDeliveryPipeline(DeliveryRepository repository, + BinaryRepository binaryStatusRepository, + TnvzHelper tnvzHelper, + Mzs2MsgConverter converter, + ClientFactory clientFactory, + SignatureVerifier verifier) { + this.repository = repository; + this.binaryRepository = binaryStatusRepository; + this.tnvzHelper = tnvzHelper; + this.converter = converter; + this.clientFactory = clientFactory; + this.verifier = verifier; + } + + @Override + public void processRequest(String appDeliveryId) { + + var exceptionBuilder = moaZSExceptionBuilder(); + + try { + var mzsRequest = repository.getDeliveryRequest(appDeliveryId).orElseThrow(); + exceptionBuilder.withDeliverySystem(mzsRequest); + + at.gv.zustellung.msg.xsd.DeliveryRequestType msgRequest = buildMsgRequest(mzsRequest, exceptionBuilder); + + var msgClientParams = mzsRequest.getConfig().getMSGClient(); + App2ZusePort client = clientFactory.create(msgClientParams, App2ZusePort.class); + var status = client.delivery(msgRequest); + + var response = new RequestStatusResponse(status); + exceptionBuilder.withAllParametersInAnswer(response.getAnswer()); + + verifySignedStatus(response.getResponseID(), exceptionBuilder); + repository.add(response); + + } catch (MoaZSException exception) { + log.error(format(MZS_PIPELINE_ERROR_MSG, appDeliveryId), exception); + var errorResponse = generateErrorFromException(exception); + repository.add(errorResponse); + } + } + + private void verifySignedStatus(String appDeliveryId, MoaZSException.Builder exceptionBuilder) throws MoaZSException { + try { + var signedStatus = binaryRepository.get(appDeliveryId).get(); + verifier.verify(signedStatus); + } catch (MoaZSException ex) { + throw exceptionBuilder.withMessage(format(MsgResponseVerifier.MOASP_SIGNATURE_INVALID_ERROR_MSG, appDeliveryId)) + .withErrorCode(MoaZSException.ERROR_MOASP_SIGNATURE_INVALID) + .withCause(ex) + .build(); + } + } + + private at.gv.zustellung.msg.xsd.DeliveryRequestType buildMsgRequest(DeliveryRequestType mzsRequest, + MoaZSException.Builder exceptionBuilder) throws MoaZSException { + if (mzsRequest.getConfig().isPerformQueryPersonRequest()) { + var tnvzClientParams = mzsRequest.getConfig().getTNVZClient(); + TNVZServicePort tvnzPort = clientFactory.create(tnvzClientParams, TNVZServicePort.class); + var identification = tnvzHelper.performQueryPersonRequest(mzsRequest, tvnzPort, exceptionBuilder); + return converter.convert(mzsRequest, identification); + } else { + return converter.convert(mzsRequest); + } + } + +} diff --git a/src/main/java/at/gv/egiz/moazs/repository/DeliveryRepository.java b/src/main/java/at/gv/egiz/moazs/repository/DeliveryRepository.java index d0a8148..57d2d07 100644 --- a/src/main/java/at/gv/egiz/moazs/repository/DeliveryRepository.java +++ b/src/main/java/at/gv/egiz/moazs/repository/DeliveryRepository.java @@ -17,7 +17,7 @@ public interface DeliveryRepository { void add(MsgResponse response); - Optional getResponse(String id); + Optional getResponse(String responseID); } diff --git a/src/main/java/at/gv/egiz/moazs/repository/InMemoryDeliveryRepository.java b/src/main/java/at/gv/egiz/moazs/repository/InMemoryDeliveryRepository.java index d9abb7b..3b5a020 100644 --- a/src/main/java/at/gv/egiz/moazs/repository/InMemoryDeliveryRepository.java +++ b/src/main/java/at/gv/egiz/moazs/repository/InMemoryDeliveryRepository.java @@ -39,12 +39,11 @@ public class InMemoryDeliveryRepository implements DeliveryRepository { @Override public void add(MsgResponse response) { String key = response.getResponseID(); - System.out.println("storing with response id " + key); responseRepository.put(key, response); } @Override - public Optional getResponse(String id) { - return ofNullable(responseRepository.getIfPresent(id)); + public Optional getResponse(String responseID) { + return ofNullable(responseRepository.getIfPresent(responseID)); } } diff --git a/src/main/java/at/gv/egiz/moazs/scheme/MsgResponse.java b/src/main/java/at/gv/egiz/moazs/scheme/MsgResponse.java index fe46451..699819f 100644 --- a/src/main/java/at/gv/egiz/moazs/scheme/MsgResponse.java +++ b/src/main/java/at/gv/egiz/moazs/scheme/MsgResponse.java @@ -3,13 +3,26 @@ package at.gv.egiz.moazs.scheme; import at.gv.egiz.moazs.MoaZSException; import at.gv.zustellung.msg.xsd.DeliveryAnswerType; -public interface MsgResponse { - - String getResponseID(); - T getResponse(); - String getAppDeliveryID(); - String getZSDeliveryID(); - DeliveryAnswerType getAnswer(); - MsgResponse generateError(MoaZSException exception); +public abstract class MsgResponse { + + protected String id; + + protected MsgResponse(){ + this.id = ""; + } + + public static String createResponseId(String appDeliveryID, String idSuffix) { + return appDeliveryID + idSuffix; + } + + public String getResponseID() { + return id; + } + + abstract public T getResponse(); + abstract public String getAppDeliveryID(); + abstract public String getZSDeliveryID(); + abstract public DeliveryAnswerType getAnswer(); + abstract public MsgResponse generateError(MoaZSException exception); } diff --git a/src/main/java/at/gv/egiz/moazs/scheme/NotificationResponse.java b/src/main/java/at/gv/egiz/moazs/scheme/NotificationResponse.java index f465a28..7670ab7 100644 --- a/src/main/java/at/gv/egiz/moazs/scheme/NotificationResponse.java +++ b/src/main/java/at/gv/egiz/moazs/scheme/NotificationResponse.java @@ -6,24 +6,18 @@ import at.gv.zustellung.msg.xsd.DeliveryNotificationType; import static at.gv.zustellung.msg.xsd.DeliveryNotificationType.deliveryNotificationTypeBuilder; -public class NotificationResponse implements MsgResponse { +public class NotificationResponse extends MsgResponse { private final DeliveryNotificationType notification; - private final String id; - private static final String ID_SUFFIX = ".NO"; + private final static String ID_SUFFIX = ".NO"; public NotificationResponse(DeliveryNotificationType notification) { + super.id = createResponseId(notification.getAppDeliveryID(), ID_SUFFIX); this.notification = notification; - this.id = getId(notification.getAppDeliveryID()); } - @Override - public String getResponseID() { - return this.id; - } - - public static String getId(String appDeliveryID) { - return appDeliveryID + ID_SUFFIX; + public static String getResponseID(String appDeliveryID) { + return createResponseId(appDeliveryID, ID_SUFFIX); } @Override @@ -60,8 +54,4 @@ public class NotificationResponse implements MsgResponse { +public class RequestStatusResponse extends MsgResponse { private final DeliveryRequestStatusType status; private final DeliveryAnswerType answer; - private final String responseID; private static final String ID_SUFFIX = ".RS"; public RequestStatusResponse(DeliveryRequestStatusType status) { this.status = status; this.answer = coalesce(status.getSuccess(), status.getPartialSuccess(), status.getError()).get(); - this.responseID = getResponseID(answer.getAppDeliveryID()); + super.id = createResponseId(answer.getAppDeliveryID(), ID_SUFFIX); } public static String getResponseID(String appDeliveryID) { return appDeliveryID + ID_SUFFIX; } - @Override - public String getResponseID() { - return this.responseID; - } - @Override public DeliveryRequestStatusType getResponse() { return status; @@ -81,7 +75,4 @@ public class RequestStatusResponse implements MsgResponse idSuffixes; + private final Map> idGenerators; private final BinaryRepository repository; @Autowired public StoreSOAPBodyBinaryInRepositoryInterceptor(CXFMessageUtils extractor, SOAPUtils soapUtils, - Map idSuffixes, BinaryRepository repository) { + Map> idGenerators, + BinaryRepository repository) { super(Phase.RECEIVE); this.messageUtils = extractor; this.soapUtils = soapUtils; - this.idSuffixes = idSuffixes; + this.idGenerators = idGenerators; this.repository = repository; } @@ -57,12 +59,12 @@ public class StoreSOAPBodyBinaryInRepositoryInterceptor extends AbstractPhaseInt String appDeliveryID = soapUtils.getAppDeliveryIDFrom(document); String rootTag = document.getTagName(); - if (!idSuffixes.containsKey(rootTag)) { + if (!idGenerators.containsKey(rootTag)) { log.trace("Will not add message of type {}. ", rootTag); return; } - var id = appDeliveryID + idSuffixes.get(rootTag); + var id = idGenerators.get(rootTag).apply(appDeliveryID); repository.add(id, response); if(log.isTraceEnabled()) { diff --git a/src/main/java/at/gv/egiz/moazs/verify/MsgResponseVerifier.java b/src/main/java/at/gv/egiz/moazs/verify/MsgResponseVerifier.java index c5e04d9..6b4ef7a 100644 --- a/src/main/java/at/gv/egiz/moazs/verify/MsgResponseVerifier.java +++ b/src/main/java/at/gv/egiz/moazs/verify/MsgResponseVerifier.java @@ -33,12 +33,12 @@ public class MsgResponseVerifier { this.verifier = verifier; } - public MsgResponse verify(String id) { + public MsgResponse verify(String responseID) { - var response = repo.getResponse(id).get(); + var response = repo.getResponse(responseID).get(); var builder = moaZSExceptionBuilder().withAllParametersInAnswer(response.getAnswer()); - var binaryResponse = binaryRepo.get(id).get(); + var binaryResponse = binaryRepo.get(responseID).get(); try { verifier.verify(binaryResponse); diff --git a/src/test/java/at/gv/egiz/moazs/SingleThreadedDeliveryPipelineTest.java b/src/test/java/at/gv/egiz/moazs/SingleThreadedDeliveryPipelineTest.java index e145cd9..4154db9 100644 --- a/src/test/java/at/gv/egiz/moazs/SingleThreadedDeliveryPipelineTest.java +++ b/src/test/java/at/gv/egiz/moazs/SingleThreadedDeliveryPipelineTest.java @@ -97,8 +97,8 @@ public class SingleThreadedDeliveryPipelineTest { pipeline.processRequest(appDeliveryID); - var responseId = getResponseID(appDeliveryID); - var response = repository.getResponse(responseId).get(); + var responseID = getResponseID(appDeliveryID); + var response = repository.getResponse(responseID).get(); var status = (DeliveryRequestStatusType) response.getResponse(); var actualCode = status.getError().getErrorInfo().getCode(); -- cgit v1.2.3