From 8aba1b4f18f5fbfebdf239b4b4945b628e439905 Mon Sep 17 00:00:00 2001 From: Christof Rabensteiner Date: Tue, 9 Jul 2019 14:11:47 +0200 Subject: Refactor Needles Interfaces and Rename "process" to "backend" Reason: - Interfaces with a single method can be replaced with interfaces from java.util.function. - Less interfaces = less code = less maintenance! - Spring can inject beans by name so we name dependencies correctly to prevent ambiguity. Others: - Rename process to backend since backend gives a better description of its components. --- .../at/gv/egiz/moazs/process/DeliveryPipeline.java | 17 ---- .../gv/egiz/moazs/process/MsgResponseHandler.java | 7 -- .../at/gv/egiz/moazs/process/MsgResponseSink.java | 11 --- .../egiz/moazs/process/SaveResponseToFileSink.java | 87 ----------------- .../moazs/process/SingleNodeResponseHandler.java | 26 ------ .../process/SingleThreadedDeliveryPipeline.java | 103 --------------------- 6 files changed, 251 deletions(-) delete mode 100644 src/main/java/at/gv/egiz/moazs/process/DeliveryPipeline.java delete mode 100644 src/main/java/at/gv/egiz/moazs/process/MsgResponseHandler.java delete mode 100644 src/main/java/at/gv/egiz/moazs/process/MsgResponseSink.java delete mode 100644 src/main/java/at/gv/egiz/moazs/process/SaveResponseToFileSink.java delete mode 100644 src/main/java/at/gv/egiz/moazs/process/SingleNodeResponseHandler.java delete mode 100644 src/main/java/at/gv/egiz/moazs/process/SingleThreadedDeliveryPipeline.java (limited to 'src/main/java/at/gv/egiz/moazs/process') diff --git a/src/main/java/at/gv/egiz/moazs/process/DeliveryPipeline.java b/src/main/java/at/gv/egiz/moazs/process/DeliveryPipeline.java deleted file mode 100644 index 506dd1f..0000000 --- a/src/main/java/at/gv/egiz/moazs/process/DeliveryPipeline.java +++ /dev/null @@ -1,17 +0,0 @@ -package at.gv.egiz.moazs.process; - - -public interface DeliveryPipeline { - - /** - * Performs all {@code DeliveryRequest}'s Back-End Tasks. - * - * Fetches {@code DeliveryRequest} referred by appDeliveryId from - * {@code DeliveryRepository}, makes sure that all necessary - * tasks (query tnvz, query msg, verify status) are executed and - * stores the response back to {@code DeliveryRepository}. - * @param appDeliveryId - */ - void processRequest(String appDeliveryId); - -} diff --git a/src/main/java/at/gv/egiz/moazs/process/MsgResponseHandler.java b/src/main/java/at/gv/egiz/moazs/process/MsgResponseHandler.java deleted file mode 100644 index 5a7d39b..0000000 --- a/src/main/java/at/gv/egiz/moazs/process/MsgResponseHandler.java +++ /dev/null @@ -1,7 +0,0 @@ -package at.gv.egiz.moazs.process; - -public interface MsgResponseHandler { - - void handle(String id); - -} diff --git a/src/main/java/at/gv/egiz/moazs/process/MsgResponseSink.java b/src/main/java/at/gv/egiz/moazs/process/MsgResponseSink.java deleted file mode 100644 index 67348fc..0000000 --- a/src/main/java/at/gv/egiz/moazs/process/MsgResponseSink.java +++ /dev/null @@ -1,11 +0,0 @@ -package at.gv.egiz.moazs.process; - -import at.gv.egiz.moazs.scheme.MsgResponse; - -import java.util.concurrent.CompletableFuture; - -public interface MsgResponseSink { - - CompletableFuture handle(MsgResponse response); - -} diff --git a/src/main/java/at/gv/egiz/moazs/process/SaveResponseToFileSink.java b/src/main/java/at/gv/egiz/moazs/process/SaveResponseToFileSink.java deleted file mode 100644 index 09d8570..0000000 --- a/src/main/java/at/gv/egiz/moazs/process/SaveResponseToFileSink.java +++ /dev/null @@ -1,87 +0,0 @@ -package at.gv.egiz.moazs.process; - -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; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import java.io.File; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.concurrent.CompletableFuture; - -import static at.gv.egiz.moazs.MoaZSException.moaZSException; -import static java.lang.String.format; -import static java.util.concurrent.CompletableFuture.allOf; -import static java.util.concurrent.CompletableFuture.supplyAsync; - -@Component -public class SaveResponseToFileSink implements MsgResponseSink { - - private static final Logger log = LoggerFactory.getLogger(SaveResponseToFileSink.class); - private static final String SAVING_FAILED_MSG = "Could not save response with AppDeliveryId=%s."; - - private final SimpleDateFormat isoFormatter; - private final Marshaller msgMarshaller; - private final DeliveryRepository repository; - private final String root; - - @Autowired - public SaveResponseToFileSink(Marshaller msgMarshaller, DeliveryRepository repository, String root) { - this.msgMarshaller = msgMarshaller; - this.repository = repository; - this.root = root; - this.isoFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); - } - - @Override - public CompletableFuture handle(MsgResponse response) { - - var responseID = response.getResponseID(); - - 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)); - - var binaryResponsePath = generatePath(responseID, "binary.xml"); - var storeBinaryResponseToFileSystemFuture = supplyAsync(() -> repository.retrieveBinaryResponse(responseID).get()) - .thenAccept(binaryResponseByteArray -> storeToFile(binaryResponsePath, binaryResponseByteArray)) - .exceptionally(ex -> logException(ex, responseID)); - - return allOf(storeResponseToFileSystemFuture, storeBinaryResponseToFileSystemFuture); - - } - - private String generatePath(String id, String suffix) { - var folder = sanitizeFileString(id); - var nowInIso8601 = isoFormatter.format(new Date()); - return format("%s/%s/%s.%s", root, folder, nowInIso8601, suffix); - } - - private String sanitizeFileString(String fileString) { - return fileString.replaceAll("[^a-zA-Z0-9\\._\\-]", ""); - } - - private Void logException(Throwable ex, String appDeliveryID) { - if(log.isErrorEnabled()) { - log.error(format(SAVING_FAILED_MSG, appDeliveryID), ex); - } - return null; - } - - private void storeToFile(String path, byte[] content) { - try { - FileUtils.writeByteArrayToFile(new File(path), content); - } catch (IOException e) { - throw moaZSException(e.getMessage(), e); - } - } -} diff --git a/src/main/java/at/gv/egiz/moazs/process/SingleNodeResponseHandler.java b/src/main/java/at/gv/egiz/moazs/process/SingleNodeResponseHandler.java deleted file mode 100644 index 65f5eed..0000000 --- a/src/main/java/at/gv/egiz/moazs/process/SingleNodeResponseHandler.java +++ /dev/null @@ -1,26 +0,0 @@ -package at.gv.egiz.moazs.process; - -import at.gv.egiz.moazs.verify.MsgResponseVerifier; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import static java.util.concurrent.CompletableFuture.supplyAsync; - -@Component -public class SingleNodeResponseHandler implements MsgResponseHandler { - - private final MsgResponseVerifier verifier; - private final MsgResponseSink sink; - - @Autowired - public SingleNodeResponseHandler(MsgResponseVerifier verifier, MsgResponseSink sink) { - this.verifier = verifier; - this.sink = sink; - } - - @Override - 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 deleted file mode 100644 index 445f386..0000000 --- a/src/main/java/at/gv/egiz/moazs/process/SingleThreadedDeliveryPipeline.java +++ /dev/null @@ -1,103 +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.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 TnvzHelper tnvzHelper; - private final Mzs2MsgConverter converter; - private final ClientFactory clientFactory; - private final SignatureVerifier verifier; - - @Autowired - public SingleThreadedDeliveryPipeline(DeliveryRepository repository, - TnvzHelper tnvzHelper, - Mzs2MsgConverter converter, - ClientFactory clientFactory, - SignatureVerifier verifier) { - this.repository = repository; - 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.retrieveDeliveryRequest(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(), appDeliveryID, exceptionBuilder); - repository.store(response); - - } catch (MoaZSException exception) { - log.error(format(MZS_PIPELINE_ERROR_MSG, appDeliveryID), exception); - var errorResponse = generateErrorFromException(exception); - repository.store(errorResponse); - } - } - - private void verifySignedStatus(String responseID, String appDeliveryID, MoaZSException.Builder exceptionBuilder) throws MoaZSException { - try { - var signedStatus = repository.retrieveBinaryResponse(responseID).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); - } - } - -} -- cgit v1.2.3