From 8e36fa01ea42d282eacdc78d6749daf9ae1ede9c Mon Sep 17 00:00:00 2001 From: Christof Rabensteiner Date: Tue, 9 Jul 2019 11:13:35 +0200 Subject: Refactor: Merge BinaryRepository into DeliveryRepository - Reason: Components, which rely on one of those, usually also rely on the other, so merging them reduces amount of dependencies. - Frame operations in DeliveryRepository API as "store" and "retrieve" operations. - Rename: Convert *Id in local variable names to upper case. --- .../at/gv/egiz/moazs/config/RepositoryConfig.java | 10 ++---- .../egiz/moazs/process/SafeResponseToFileSink.java | 20 ++++++------ .../process/SingleThreadedDeliveryPipeline.java | 22 ++++++------- .../egiz/moazs/repository/DeliveryRepository.java | 12 ++++--- .../repository/InMemoryDeliveryRepository.java | 37 +++++++++++++++------- .../moazs/repository/RedisDeliveryRepository.java | 20 ++++++++---- .../java/at/gv/egiz/moazs/service/MsgService.java | 2 +- .../java/at/gv/egiz/moazs/service/MzsService.java | 11 +++---- ...StoreSOAPBodyBinaryInRepositoryInterceptor.java | 10 +++--- .../gv/egiz/moazs/verify/MsgResponseVerifier.java | 15 +++------ 10 files changed, 84 insertions(+), 75 deletions(-) (limited to 'src/main/java/at/gv') 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 01a4cc3..59bfb88 100644 --- a/src/main/java/at/gv/egiz/moazs/config/RepositoryConfig.java +++ b/src/main/java/at/gv/egiz/moazs/config/RepositoryConfig.java @@ -1,7 +1,5 @@ package at.gv.egiz.moazs.config; -import at.gv.egiz.moazs.repository.BinaryRepository; -import at.gv.egiz.moazs.repository.InMemoryBinaryRepository; import at.gv.egiz.moazs.scheme.NameSpace; import at.gv.egiz.moazs.scheme.NotificationResponse; import at.gv.egiz.moazs.scheme.RequestStatusResponse; @@ -17,17 +15,13 @@ import java.util.function.Function; @Profile("!cluster") public class RepositoryConfig { - @Value("${repository.expiresAfterWrite}") - private int expiresAfterWrite; - @Bean - public BinaryRepository binaryRepository() { - return new InMemoryBinaryRepository(expiresAfterWrite); + public int expiresAfterWrite(@Value("${repository.expiresAfterWrite}") int exp) { + return exp; } @Bean 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/process/SafeResponseToFileSink.java b/src/main/java/at/gv/egiz/moazs/process/SafeResponseToFileSink.java index ee32768..12a9fe3 100644 --- a/src/main/java/at/gv/egiz/moazs/process/SafeResponseToFileSink.java +++ b/src/main/java/at/gv/egiz/moazs/process/SafeResponseToFileSink.java @@ -1,6 +1,6 @@ package at.gv.egiz.moazs.process; -import at.gv.egiz.moazs.repository.BinaryRepository; +import at.gv.egiz.moazs.repository.DeliveryRepository; import at.gv.egiz.moazs.scheme.Marshaller; import at.gv.egiz.moazs.scheme.MsgResponse; import org.apache.commons.io.FileUtils; @@ -28,32 +28,32 @@ public class SafeResponseToFileSink implements MsgResponseSink { private static final SimpleDateFormat ISO_FORMATTER = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); private final Marshaller msgMarshaller; - private final BinaryRepository binaryRepository; + private final DeliveryRepository respository; private final String root; @Autowired - public SafeResponseToFileSink(Marshaller msgMarshaller, BinaryRepository binaryRepository, String root) { + public SafeResponseToFileSink(Marshaller msgMarshaller, DeliveryRepository binaryRepository, String root) { this.msgMarshaller = msgMarshaller; - this.binaryRepository = binaryRepository; + this.respository = binaryRepository; this.root = root; } @Override public CompletableFuture handle(MsgResponse response) { - var responseId = response.getResponseID(); + var responseID = response.getResponseID(); - var responsePath = generatePath(responseId, "xml"); + var responsePath = generatePath(responseID, "xml"); var storeResponseToFileSystemFuture = supplyAsync(() -> msgMarshaller.marshallXml(response.getResponse())) .thenApply(responseString -> responseString.getBytes(StandardCharsets.UTF_8)) .thenAccept(responseByteArray -> storeToFile(responsePath, responseByteArray)) - .exceptionally((ex) -> logException(ex, responseId)); + .exceptionally((ex) -> logException(ex, responseID)); - var binaryResponsePath = generatePath(responseId, "binary.xml"); - var storeBinaryResponseToFileSystemFuture = supplyAsync(() -> binaryRepository.get(responseId).get()) + var binaryResponsePath = generatePath(responseID, "binary.xml"); + var storeBinaryResponseToFileSystemFuture = supplyAsync(() -> respository.retrieveBinaryResponse(responseID).get()) .thenAccept(binaryResponseByteArray -> storeToFile(binaryResponsePath, binaryResponseByteArray)) - .exceptionally((ex) -> logException(ex, responseId)); + .exceptionally((ex) -> logException(ex, responseID)); return allOf(storeResponseToFileSystemFuture, storeBinaryResponseToFileSystemFuture); diff --git a/src/main/java/at/gv/egiz/moazs/process/SingleThreadedDeliveryPipeline.java b/src/main/java/at/gv/egiz/moazs/process/SingleThreadedDeliveryPipeline.java index 69065d7..445f386 100644 --- a/src/main/java/at/gv/egiz/moazs/process/SingleThreadedDeliveryPipeline.java +++ b/src/main/java/at/gv/egiz/moazs/process/SingleThreadedDeliveryPipeline.java @@ -4,7 +4,6 @@ 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; @@ -31,7 +30,6 @@ public class SingleThreadedDeliveryPipeline implements DeliveryPipeline { "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; @@ -39,13 +37,11 @@ public class SingleThreadedDeliveryPipeline implements DeliveryPipeline { @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; @@ -53,12 +49,12 @@ public class SingleThreadedDeliveryPipeline implements DeliveryPipeline { } @Override - public void processRequest(String appDeliveryId) { + public void processRequest(String appDeliveryID) { var exceptionBuilder = moaZSExceptionBuilder(); try { - var mzsRequest = repository.getDeliveryRequest(appDeliveryId).orElseThrow(); + var mzsRequest = repository.retrieveDeliveryRequest(appDeliveryID).orElseThrow(); exceptionBuilder.withDeliverySystem(mzsRequest); at.gv.zustellung.msg.xsd.DeliveryRequestType msgRequest = buildMsgRequest(mzsRequest, exceptionBuilder); @@ -70,22 +66,22 @@ public class SingleThreadedDeliveryPipeline implements DeliveryPipeline { var response = new RequestStatusResponse(status); exceptionBuilder.withAllParametersInAnswer(response.getAnswer()); - verifySignedStatus(response.getResponseID(), exceptionBuilder); - repository.add(response); + verifySignedStatus(response.getResponseID(), appDeliveryID, exceptionBuilder); + repository.store(response); } catch (MoaZSException exception) { - log.error(format(MZS_PIPELINE_ERROR_MSG, appDeliveryId), exception); + log.error(format(MZS_PIPELINE_ERROR_MSG, appDeliveryID), exception); var errorResponse = generateErrorFromException(exception); - repository.add(errorResponse); + repository.store(errorResponse); } } - private void verifySignedStatus(String appDeliveryId, MoaZSException.Builder exceptionBuilder) throws MoaZSException { + private void verifySignedStatus(String responseID, String appDeliveryID, MoaZSException.Builder exceptionBuilder) throws MoaZSException { try { - var signedStatus = binaryRepository.get(appDeliveryId).get(); + var signedStatus = repository.retrieveBinaryResponse(responseID).get(); verifier.verify(signedStatus); } catch (MoaZSException ex) { - throw exceptionBuilder.withMessage(format(MsgResponseVerifier.MOASP_SIGNATURE_INVALID_ERROR_MSG, appDeliveryId)) + throw exceptionBuilder.withMessage(format(MsgResponseVerifier.MOASP_SIGNATURE_INVALID_ERROR_MSG, appDeliveryID)) .withErrorCode(MoaZSException.ERROR_MOASP_SIGNATURE_INVALID) .withCause(ex) .build(); 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 57d2d07..e9b0466 100644 --- a/src/main/java/at/gv/egiz/moazs/repository/DeliveryRepository.java +++ b/src/main/java/at/gv/egiz/moazs/repository/DeliveryRepository.java @@ -11,13 +11,17 @@ import java.util.Optional; @Repository public interface DeliveryRepository { - void add(DeliveryRequestType request); + void store(DeliveryRequestType request); - Optional getDeliveryRequest(String appDeliveryID); + Optional retrieveDeliveryRequest(String appDeliveryID); - void add(MsgResponse response); + void store(MsgResponse response); - Optional getResponse(String responseID); + Optional retrieveResponse(String responseID); + + void store(String responseID, byte[] data); + + Optional retrieveBinaryResponse(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 3b5a020..41b742b 100644 --- a/src/main/java/at/gv/egiz/moazs/repository/InMemoryDeliveryRepository.java +++ b/src/main/java/at/gv/egiz/moazs/repository/InMemoryDeliveryRepository.java @@ -4,46 +4,61 @@ package at.gv.egiz.moazs.repository; import at.gv.egiz.moazs.scheme.MsgResponse; import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; import com.google.common.cache.Cache; -import com.google.common.cache.CacheBuilder; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Repository; import java.util.Optional; import java.util.concurrent.TimeUnit; +import static com.google.common.cache.CacheBuilder.newBuilder; import static java.util.Optional.ofNullable; @Repository @Profile("!cluster") public class InMemoryDeliveryRepository implements DeliveryRepository { - private final Cache requestRepository = CacheBuilder.newBuilder() - .expireAfterWrite(30, TimeUnit.MINUTES) - .build(); + private final Cache requestRepository; + private final Cache responseRepository; + private final Cache binaryResponseRepository; - private final Cache responseRepository = CacheBuilder.newBuilder() - .expireAfterWrite(30, TimeUnit.MINUTES) - .build(); + public InMemoryDeliveryRepository(int expiresAfterWrite) { + var builder = newBuilder().expireAfterWrite(expiresAfterWrite, TimeUnit.MINUTES); + this.requestRepository = builder.build(); + this.responseRepository = builder.build(); + this.binaryResponseRepository = builder.build(); + } @Override - public void add(DeliveryRequestType request) { + public void store(DeliveryRequestType request) { var key = request.getMetaData().getAppDeliveryID(); requestRepository.put(key, request); } @Override - public Optional getDeliveryRequest(String appDeliveryID) { + public Optional retrieveDeliveryRequest(String appDeliveryID) { return ofNullable(requestRepository.getIfPresent(appDeliveryID)); } @Override - public void add(MsgResponse response) { + public void store(MsgResponse response) { String key = response.getResponseID(); responseRepository.put(key, response); } @Override - public Optional getResponse(String responseID) { + public Optional retrieveResponse(String responseID) { return ofNullable(responseRepository.getIfPresent(responseID)); } + + @Override + public void store(String id, byte[] data) { + binaryResponseRepository.put(id, data); + + } + + @Override + public Optional retrieveBinaryResponse(String responseID) { + return ofNullable(binaryResponseRepository.getIfPresent(responseID)); + } + } diff --git a/src/main/java/at/gv/egiz/moazs/repository/RedisDeliveryRepository.java b/src/main/java/at/gv/egiz/moazs/repository/RedisDeliveryRepository.java index 7ccd236..68e96d9 100644 --- a/src/main/java/at/gv/egiz/moazs/repository/RedisDeliveryRepository.java +++ b/src/main/java/at/gv/egiz/moazs/repository/RedisDeliveryRepository.java @@ -3,8 +3,6 @@ package at.gv.egiz.moazs.repository; import at.gv.egiz.moazs.scheme.Marshaller; import at.gv.egiz.moazs.scheme.MsgResponse; import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; -import at.gv.zustellung.msg.xsd.DeliveryNotificationType; -import at.gv.zustellung.msg.xsd.DeliveryRequestStatusType; import at.gv.zustellung.app2mzs.xsd.ObjectFactory; import org.apache.tools.ant.filters.StringInputStream; import org.slf4j.Logger; @@ -42,7 +40,7 @@ public class RedisDeliveryRepository implements DeliveryRepository { } @Override - public void add(DeliveryRequestType request) { + public void store(DeliveryRequestType request) { var hashKey = request.getMetaData().getAppDeliveryID(); var serializedRequest = marshaller.marshallXml(factory.createDeliveryRequest(request)); @@ -51,7 +49,7 @@ public class RedisDeliveryRepository implements DeliveryRepository { } @Override - public Optional getDeliveryRequest(String appDeliveryID) { + public Optional retrieveDeliveryRequest(String appDeliveryID) { String serializedRequest = hashOperations.get(KEY, appDeliveryID); if (serializedRequest == null) { @@ -64,14 +62,24 @@ public class RedisDeliveryRepository implements DeliveryRepository { } @Override - public void add(MsgResponse notification) { + public void store(MsgResponse notification) { throw new UnsupportedOperationException(NOT_IMPLEMENTED_MESSAGE); } @Override - public Optional getResponse(String id) { + public Optional retrieveResponse(String id) { throw new UnsupportedOperationException(NOT_IMPLEMENTED_MESSAGE); } + @Override + public void store(String responseID, byte[] data) { + + } + + @Override + public Optional retrieveBinaryResponse(String responseID) { + return Optional.empty(); + } + } diff --git a/src/main/java/at/gv/egiz/moazs/service/MsgService.java b/src/main/java/at/gv/egiz/moazs/service/MsgService.java index 93f1ed5..87644a0 100644 --- a/src/main/java/at/gv/egiz/moazs/service/MsgService.java +++ b/src/main/java/at/gv/egiz/moazs/service/MsgService.java @@ -58,7 +58,7 @@ public class MsgService implements Zuse2AppPort { } private void sendToWork(MsgResponse response) { - runAsync(() -> repository.add(response)) + runAsync(() -> repository.store(response)) .thenRunAsync(() -> handler.handle(response.getResponseID())); } diff --git a/src/main/java/at/gv/egiz/moazs/service/MzsService.java b/src/main/java/at/gv/egiz/moazs/service/MzsService.java index c41740f..2755a37 100644 --- a/src/main/java/at/gv/egiz/moazs/service/MzsService.java +++ b/src/main/java/at/gv/egiz/moazs/service/MzsService.java @@ -2,7 +2,6 @@ package at.gv.egiz.moazs.service; import at.gv.egiz.moazs.preprocess.DeliveryRequestAugmenter; import at.gv.egiz.moazs.process.DeliveryPipeline; -import at.gv.egiz.moazs.repository.BinaryRepository; import at.gv.egiz.moazs.repository.DeliveryRepository; import at.gv.egiz.moazs.scheme.Msg2MzsConverter; import at.gv.egiz.moazs.client.MzsClient; @@ -41,17 +40,15 @@ public class MzsService implements App2MzsPortType { private final MzsClient appClient; private final DeliveryRequestAugmenter augmenter; private final Msg2MzsConverter converter; - private final BinaryRepository binaryStatusRepository; @Autowired public MzsService(DeliveryRepository repository, DeliveryPipeline pipeline, MzsClient appClient, - DeliveryRequestAugmenter augmenter, Msg2MzsConverter converter, BinaryRepository binaryStatusRepository) { + DeliveryRequestAugmenter augmenter, Msg2MzsConverter converter) { this.repository = repository; this.pipeline = pipeline; this.appClient = appClient; this.augmenter = augmenter; this.converter = converter; - this.binaryStatusRepository = binaryStatusRepository; } @Override @@ -65,7 +62,7 @@ public class MzsService implements App2MzsPortType { var future = supplyAsync(() -> augmenter.augment(deliveryRequest)) .thenApply(this::process) - .thenApply(status -> converter.convert(status, binaryStatusRepository.get(responseID))); + .thenApply(status -> converter.convert(status, repository.retrieveBinaryResponse(responseID))); try { return future.get(TIMEOUT_FOR_ANWSER, TimeUnit.SECONDS); @@ -89,11 +86,11 @@ public class MzsService implements App2MzsPortType { var appDeliveryID = deliveryRequest.getMetaData().getAppDeliveryID(); logger.info("Receive request with appDeliveryID = {}.", appDeliveryID); - repository.add(deliveryRequest); + repository.store(deliveryRequest); pipeline.processRequest(appDeliveryID); var statusId = RequestStatusResponse.getResponseID(appDeliveryID); - var response = repository.getResponse(statusId) + var response = repository.retrieveResponse(statusId) .orElseThrow(() -> moaZSException("Could not get a response for AppDeliveryID=%s.", appDeliveryID)); return (DeliveryRequestStatusType) response.getResponse(); } diff --git a/src/main/java/at/gv/egiz/moazs/util/StoreSOAPBodyBinaryInRepositoryInterceptor.java b/src/main/java/at/gv/egiz/moazs/util/StoreSOAPBodyBinaryInRepositoryInterceptor.java index 39566fe..88ab7e0 100644 --- a/src/main/java/at/gv/egiz/moazs/util/StoreSOAPBodyBinaryInRepositoryInterceptor.java +++ b/src/main/java/at/gv/egiz/moazs/util/StoreSOAPBodyBinaryInRepositoryInterceptor.java @@ -1,6 +1,6 @@ package at.gv.egiz.moazs.util; -import at.gv.egiz.moazs.repository.BinaryRepository; +import at.gv.egiz.moazs.repository.DeliveryRepository; import at.gv.egiz.moazs.scheme.SOAPUtils; import org.apache.cxf.message.Message; import org.apache.cxf.phase.AbstractPhaseInterceptor; @@ -28,12 +28,12 @@ public class StoreSOAPBodyBinaryInRepositoryInterceptor extends AbstractPhaseInt private final CXFMessageUtils messageUtils; private final SOAPUtils soapUtils; private final Map> idGenerators; - private final BinaryRepository repository; + private final DeliveryRepository repository; @Autowired public StoreSOAPBodyBinaryInRepositoryInterceptor(CXFMessageUtils extractor, SOAPUtils soapUtils, Map> idGenerators, - BinaryRepository repository) { + DeliveryRepository repository) { super(Phase.RECEIVE); this.messageUtils = extractor; this.soapUtils = soapUtils; @@ -60,12 +60,12 @@ public class StoreSOAPBodyBinaryInRepositoryInterceptor extends AbstractPhaseInt String rootTag = document.getTagName(); if (!idGenerators.containsKey(rootTag)) { - log.trace("Will not add message of type {}. ", rootTag); + log.trace("Will not store message of type {}. ", rootTag); return; } var id = idGenerators.get(rootTag).apply(appDeliveryID); - repository.add(id, response); + repository.store(id, response); if(log.isTraceEnabled()) { log.trace("Msg Response with AppDeliveryID={} unwrapped and stored: {}. ", 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 6b4ef7a..9c8b17c 100644 --- a/src/main/java/at/gv/egiz/moazs/verify/MsgResponseVerifier.java +++ b/src/main/java/at/gv/egiz/moazs/verify/MsgResponseVerifier.java @@ -1,7 +1,6 @@ package at.gv.egiz.moazs.verify; import at.gv.egiz.moazs.MoaZSException; -import at.gv.egiz.moazs.repository.BinaryRepository; import at.gv.egiz.moazs.repository.DeliveryRepository; import at.gv.egiz.moazs.scheme.MsgResponse; import at.gv.egiz.moazs.service.MsgService; @@ -20,25 +19,21 @@ public class MsgResponseVerifier { public static final String MOASP_SIGNATURE_INVALID_ERROR_MSG = "Signature of Msg Response " + "with AppDeliveryID=%s is not valid."; - private final DeliveryRepository repo; - private final BinaryRepository binaryRepo; + private final DeliveryRepository repository; private final SignatureVerifier verifier; @Autowired - public MsgResponseVerifier(DeliveryRepository repo, - BinaryRepository binaryStatusRepository, - SignatureVerifier verifier) { - this.repo = repo; - this.binaryRepo = binaryStatusRepository; + public MsgResponseVerifier(DeliveryRepository repository, SignatureVerifier verifier) { + this.repository = repository; this.verifier = verifier; } public MsgResponse verify(String responseID) { - var response = repo.getResponse(responseID).get(); + var response = repository.retrieveResponse(responseID).get(); var builder = moaZSExceptionBuilder().withAllParametersInAnswer(response.getAnswer()); - var binaryResponse = binaryRepo.get(responseID).get(); + var binaryResponse = repository.retrieveBinaryResponse(responseID).get(); try { verifier.verify(binaryResponse); -- cgit v1.2.3