aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/gv/egiz/moazs/service
diff options
context:
space:
mode:
authorChristof Rabensteiner <christof.rabensteiner@iaik.tugraz.at>2019-07-08 18:02:37 +0200
committerChristof Rabensteiner <christof.rabensteiner@iaik.tugraz.at>2019-07-08 18:02:37 +0200
commitc1a1a5130a4ecf755da72941ad3525beb919447f (patch)
treeecaca5da80571e10f25186402ee73d8e062d4544 /src/main/java/at/gv/egiz/moazs/service
parent78f0715d86a055aed11138df5f66b0794e72326a (diff)
downloadmoa-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/service')
-rw-r--r--src/main/java/at/gv/egiz/moazs/service/MsgService.java74
-rw-r--r--src/main/java/at/gv/egiz/moazs/service/MzsService.java112
2 files changed, 186 insertions, 0 deletions
diff --git a/src/main/java/at/gv/egiz/moazs/service/MsgService.java b/src/main/java/at/gv/egiz/moazs/service/MsgService.java
new file mode 100644
index 0000000..93f1ed5
--- /dev/null
+++ b/src/main/java/at/gv/egiz/moazs/service/MsgService.java
@@ -0,0 +1,74 @@
+package at.gv.egiz.moazs.service;
+import at.gv.egiz.moazs.process.MsgResponseHandler;
+import at.gv.egiz.moazs.repository.DeliveryRepository;
+import at.gv.egiz.moazs.scheme.MsgResponse;
+import at.gv.egiz.moazs.scheme.NameSpace;
+import at.gv.egiz.moazs.scheme.NotificationResponse;
+import at.gv.egiz.moazs.scheme.RequestStatusResponse;
+import at.gv.zustellung.msg.xsd.*;
+import org.apache.cxf.annotations.SchemaValidation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import static at.gv.zustellung.msg.xsd.DeliveryNotificationACKType.deliveryNotificationACKTypeBuilder;
+import static at.gv.zustellung.msg.xsd.DeliveryRequestStatusACKType.deliveryRequestStatusACKTypeBuilder;
+import static at.gv.zustellung.msg.xsd.GetVersionResponse.getVersionResponseBuilder;
+import static java.util.concurrent.CompletableFuture.runAsync;
+
+@Service
+@SchemaValidation(type = SchemaValidation.SchemaValidationType.BOTH)
+public class MsgService implements Zuse2AppPort {
+
+ private final DeliveryRepository repository;
+ private final MsgResponseHandler handler;
+
+ @Autowired
+ public MsgService(DeliveryRepository repository, MsgResponseHandler handler) {
+ this.repository = repository;
+ this.handler = handler;
+ }
+
+ @Override
+ public GetVersionResponse getVersion(SimpleRequestType getVersionRequest) {
+ return getVersionResponseBuilder()
+ .withVersion(NameSpace.MSG_VERSION)
+ .build();
+ }
+
+ @Override
+ public DeliveryRequestStatusACKType status(DeliveryRequestStatusType status) {
+ var response = new RequestStatusResponse(status);
+ sendToWork(response);
+ return statusAck(response.getAppDeliveryID(), response.getZSDeliveryID());
+ }
+
+ private DeliveryRequestStatusACKType statusAck(String appDeliveryID, String zsDeliveryID) {
+ return deliveryRequestStatusACKTypeBuilder()
+ .withAppDeliveryID(appDeliveryID)
+ .withZSDeliveryID(zsDeliveryID)
+ .withVersion(NameSpace.MSG_VERSION)
+ .build();
+ }
+
+ @Override
+ public DeliveryNotificationACKType notification(DeliveryNotificationType notification) {
+ var response = new NotificationResponse(notification);
+ sendToWork(response);
+ return notificationAck(response.getAppDeliveryID(), response.getZSDeliveryID());
+ }
+
+ private void sendToWork(MsgResponse response) {
+ runAsync(() -> repository.add(response))
+ .thenRunAsync(() -> handler.handle(response.getResponseID()));
+ }
+
+ private DeliveryNotificationACKType notificationAck(String appDeliveryID, String zsDeliveryID) {
+ return deliveryNotificationACKTypeBuilder()
+ .withAppDeliveryID(appDeliveryID)
+ .withZSDeliveryID(zsDeliveryID)
+ .withVersion(NameSpace.MSG_VERSION)
+ .build();
+ }
+
+
+}
diff --git a/src/main/java/at/gv/egiz/moazs/service/MzsService.java b/src/main/java/at/gv/egiz/moazs/service/MzsService.java
new file mode 100644
index 0000000..c41740f
--- /dev/null
+++ b/src/main/java/at/gv/egiz/moazs/service/MzsService.java
@@ -0,0 +1,112 @@
+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;
+import at.gv.egiz.moazs.scheme.RequestStatusResponse;
+import at.gv.zustellung.app2mzs.xsd.App2MzsPortType;
+import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType;
+import at.gv.zustellung.app2mzs.xsd.DeliveryResponseType;
+import at.gv.zustellung.msg.xsd.DeliveryRequestStatusType;
+import org.apache.cxf.annotations.SchemaValidation;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import javax.jws.WebParam;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+import static at.gv.egiz.moazs.MoaZSException.moaZSException;
+import static at.gv.egiz.moazs.MoaZSException.moaZSExceptionBuilder;
+import static at.gv.zustellung.app2mzs.xsd.PartialSuccessType.partialSuccessTypeBuilder;
+import static java.util.concurrent.CompletableFuture.supplyAsync;
+
+//todo : validate Schema in both directions.
+@Service
+@SchemaValidation(type = SchemaValidation.SchemaValidationType.IN)
+public class MzsService implements App2MzsPortType {
+
+ private static final Logger logger = LoggerFactory.getLogger(MzsService.class);
+
+ //TODO move timeout and namespaces to config
+ private static final int TIMEOUT_FOR_ANWSER = 10;
+
+ private final DeliveryRepository repository;
+ private final DeliveryPipeline pipeline;
+ 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) {
+ this.repository = repository;
+ this.pipeline = pipeline;
+ this.appClient = appClient;
+ this.augmenter = augmenter;
+ this.converter = converter;
+ this.binaryStatusRepository = binaryStatusRepository;
+ }
+
+ @Override
+ public DeliveryResponseType app2Mzs(
+ @WebParam(partName = "DeliveryRequest",
+ name = "DeliveryRequest")
+ DeliveryRequestType deliveryRequest) {
+
+ var appDeliveryID = deliveryRequest.getMetaData().getAppDeliveryID();
+ var responseID = RequestStatusResponse.getResponseID(appDeliveryID);
+
+ var future = supplyAsync(() -> augmenter.augment(deliveryRequest))
+ .thenApply(this::process)
+ .thenApply(status -> converter.convert(status, binaryStatusRepository.get(responseID)));
+
+ try {
+ return future.get(TIMEOUT_FOR_ANWSER, TimeUnit.SECONDS);
+ } catch (TimeoutException e) {
+ logger.info("Answer Timed Out", e);
+
+ future.thenAccept(appClient::sendNotification);
+ return generatePartialSuccessResponse(appDeliveryID);
+
+ } catch (Exception e) {
+ throw moaZSExceptionBuilder("An error occurred while processing DeliveryRequest " +
+ "with AppDeliveryID=%s.", appDeliveryID)
+ .withCause(e)
+ .build();
+ }
+
+ }
+
+ private DeliveryRequestStatusType process(DeliveryRequestType deliveryRequest) {
+
+ var appDeliveryID = deliveryRequest.getMetaData().getAppDeliveryID();
+ logger.info("Receive request with appDeliveryID = {}.", appDeliveryID);
+
+ repository.add(deliveryRequest);
+ pipeline.processRequest(appDeliveryID);
+
+ var statusId = RequestStatusResponse.getResponseID(appDeliveryID);
+ var response = repository.getResponse(statusId)
+ .orElseThrow(() -> moaZSException("Could not get a response for AppDeliveryID=%s.", appDeliveryID));
+ return (DeliveryRequestStatusType) response.getResponse();
+ }
+
+ private DeliveryResponseType generatePartialSuccessResponse(String appDeliveryId) {
+
+ var partial = partialSuccessTypeBuilder()
+ .withAppDeliveryID(appDeliveryId)
+ .build();
+
+ return DeliveryResponseType.deliveryResponseTypeBuilder()
+ .withPartialSuccess(partial)
+ .build();
+ }
+
+}