diff options
author | Christof Rabensteiner <christof.rabensteiner@iaik.tugraz.at> | 2019-07-08 18:02:37 +0200 |
---|---|---|
committer | Christof Rabensteiner <christof.rabensteiner@iaik.tugraz.at> | 2019-07-08 18:02:37 +0200 |
commit | c1a1a5130a4ecf755da72941ad3525beb919447f (patch) | |
tree | ecaca5da80571e10f25186402ee73d8e062d4544 /src/main/java/at/gv/egiz/moazs/process | |
parent | 78f0715d86a055aed11138df5f66b0794e72326a (diff) | |
download | moa-zs-c1a1a5130a4ecf755da72941ad3525beb919447f.tar.gz moa-zs-c1a1a5130a4ecf755da72941ad3525beb919447f.tar.bz2 moa-zs-c1a1a5130a4ecf755da72941ad3525beb919447f.zip |
Implement Zuse2App Services
- Add zuse2app.wsdl contract.
- Add MsgResponse as an type-agnostic view for DeliveryRequestStatus
and DeliveryNotification messages. Reason: Both DeliveryNotification
and DeliveryRequestStatus messages have similar fields and need to
be treated similarly (e.g.: receive from msg service, store to
repository, verify signature, store to file...). In order to prevent
duplicated code, the wrapper interface provides a type-agnostic view
onto these messages for depending components to operate on.
- Add MsgResponseHandler interface; decides how to process
MsgResponse. Also implement this handler with a multi-threaded
single-node implementation.
- Add MsgResponseSink interface; decides how to archive MsgResponse.
- Implement and test SafeResponseToFileSink.
Change Identifier for MsgResponses:
- Before, DeliveryRequestStatus and DeliveryNotifications had their
own repositories. Now, both types are stored in the same repository
(the MsgResponse repository) to streamline the handling of
MsgResponses. We need to change the identification of MsgReponses,
otherwise the identifiers (AppDeliveryID) clash.
- MsgResponses are not identified by:
<AppDeliveryId>+<typeSpecificSuffix>
- Rewrite StoreSOAPInterceptor to accommodate fact that, both
DeliveryRequestStatus and DeliveryNotification messages have
different IDs upon storage / retrieval.
Restructure packages and components as follows:
- client: All components that are involved when consuming a web service.
- process: "fabric" of MoaZS; contains business logic that
orchestrates back-end tasks of MoaZS's operational services, e.g.:
by processing a delivery request.
- service: Implementation of MoaZS's front-end services.
Refactoring:
- MoaZSException: Remove unused fields. Before: Store mzsrequest,
tnvzresult, msgrequest and msgresult as members. Now: Only keep the
fields that are needed later, e.g for generating a
msg:DeliveryRequestStatus element. Add copy constructor to Builder.
- Put storage of byte[] into a dedicated "BinaryRepository". Reason:
This was useful in a former design. Now it's not really needed
anymore.
- Put "create Endpoint" code into EndpointFactory. Reason: Eliminate
duplicated code when configuring a service.
Testing:
- Activate Stacktraces in surefire.
Diffstat (limited to 'src/main/java/at/gv/egiz/moazs/process')
5 files changed, 145 insertions, 0 deletions
diff --git a/src/main/java/at/gv/egiz/moazs/process/DeliveryPipeline.java b/src/main/java/at/gv/egiz/moazs/process/DeliveryPipeline.java new file mode 100644 index 0000000..506dd1f --- /dev/null +++ b/src/main/java/at/gv/egiz/moazs/process/DeliveryPipeline.java @@ -0,0 +1,17 @@ +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 new file mode 100644 index 0000000..5a7d39b --- /dev/null +++ b/src/main/java/at/gv/egiz/moazs/process/MsgResponseHandler.java @@ -0,0 +1,7 @@ +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 new file mode 100644 index 0000000..67348fc --- /dev/null +++ b/src/main/java/at/gv/egiz/moazs/process/MsgResponseSink.java @@ -0,0 +1,11 @@ +package at.gv.egiz.moazs.process; + +import at.gv.egiz.moazs.scheme.MsgResponse; + +import java.util.concurrent.CompletableFuture; + +public interface MsgResponseSink { + + CompletableFuture<Void> handle(MsgResponse response); + +} diff --git a/src/main/java/at/gv/egiz/moazs/process/SafeResponseToFileSink.java b/src/main/java/at/gv/egiz/moazs/process/SafeResponseToFileSink.java new file mode 100644 index 0000000..ee32768 --- /dev/null +++ b/src/main/java/at/gv/egiz/moazs/process/SafeResponseToFileSink.java @@ -0,0 +1,84 @@ +package at.gv.egiz.moazs.process; + +import at.gv.egiz.moazs.repository.BinaryRepository; +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 java.lang.String.format; +import static java.util.concurrent.CompletableFuture.allOf; +import static java.util.concurrent.CompletableFuture.supplyAsync; + +@Component +public class SafeResponseToFileSink implements MsgResponseSink { + + private static final Logger log = LoggerFactory.getLogger(SafeResponseToFileSink.class); + private static final String SAFING_FAILED_MSG = "Could not save response with AppDeliveryId=%s."; + 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 String root; + + + @Autowired + public SafeResponseToFileSink(Marshaller msgMarshaller, BinaryRepository binaryRepository, String root) { + this.msgMarshaller = msgMarshaller; + this.binaryRepository = binaryRepository; + this.root = root; + } + + @Override + public CompletableFuture<Void> 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(() -> binaryRepository.get(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 iso8601_now = ISO_FORMATTER.format(new Date()); + return format("%s/%s/%s.%s", root, folder, iso8601_now, suffix); + } + + private String sanitizeFileString(String fileString) { + return fileString.replaceAll("[^a-zA-Z0-9\\._\\-]", ""); + } + + private Void logException(Throwable ex, String appDeliveryID) { + log.error(format(SAFING_FAILED_MSG, appDeliveryID), ex); + return null; + } + + private void storeToFile(String path, byte[] content) { + try { + FileUtils.writeByteArrayToFile(new File(path), content); + } catch (IOException e) { + throw new RuntimeException(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 new file mode 100644 index 0000000..ec609cb --- /dev/null +++ b/src/main/java/at/gv/egiz/moazs/process/SingleNodeResponseHandler.java @@ -0,0 +1,26 @@ +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 id) { + supplyAsync(() -> verifier.verify(id)) + .thenAcceptAsync((response) -> sink.handle(response)); + } +} |