From 1d2aafe0cd6607a48d880c007881768079e49060 Mon Sep 17 00:00:00 2001 From: Christof Rabensteiner Date: Mon, 9 Dec 2019 10:25:02 +0100 Subject: Refactor & Fixes - Handle InterruptedExceptions as discussed in https://stackoverflow.com/questions/3976344/handling-interruptedexception-in-java - Remove unused imports. --- .../gv/egiz/moazs/backend/MsgResponseBackend.java | 6 ++--- .../gv/egiz/moazs/backend/MsgResponseSinkHub.java | 4 ---- .../java/at/gv/egiz/moazs/service/MsgService.java | 27 +++++++++++++--------- .../java/at/gv/egiz/moazs/service/MzsService.java | 10 ++++++-- 4 files changed, 27 insertions(+), 20 deletions(-) (limited to 'src/main') diff --git a/src/main/java/at/gv/egiz/moazs/backend/MsgResponseBackend.java b/src/main/java/at/gv/egiz/moazs/backend/MsgResponseBackend.java index 45a4000..cc20cc9 100644 --- a/src/main/java/at/gv/egiz/moazs/backend/MsgResponseBackend.java +++ b/src/main/java/at/gv/egiz/moazs/backend/MsgResponseBackend.java @@ -58,7 +58,7 @@ public class MsgResponseBackend { "with AppDeliveryID=%s is not valid."; public static final String BINARY_RESPONSE_MISSING_ERROR_MSG = "Binary Response is not in repository."; public static final String RESPONSE_MISSING_ERROR_MSG = "Response with ResponseID=%s is not in repository."; - public static final String REQUEST_MISSING_ERROR_MSG = "Request with AppDeliveryID=%s is not in repository. " + + public static final String REQUEST_MISSING_ERROR_MSG = "Request with AppDeliveryID={} is not in repository. " + "Falling Back to Default Configuration."; private final DeliveryRepository repository; @@ -96,7 +96,7 @@ public class MsgResponseBackend { public CompletableFuture accept(String responseID) { log.info("Backend accepts MsgResponse with responseID={}.", responseID); return supplyAsync(() -> verifySignature(responseID), taskExecutor) - .thenCompose(msgResponse -> applySinks(msgResponse)); + .thenCompose(this::applySinks); } private MsgResponse verifySignature(String responseID) { @@ -138,7 +138,7 @@ public class MsgResponseBackend { private Supplier supplyRequestWithDefaultConfig(String appDeliveryID) { return () -> { - log.warn(format(REQUEST_MISSING_ERROR_MSG, appDeliveryID)); + log.warn(REQUEST_MISSING_ERROR_MSG, appDeliveryID); var defaultConfig = deliveryRequestConfigs.get(DEFAULT_CONFIG_KEY); validator.areSinksConfigured(defaultConfig.getMsgResponseSinks()); return deliveryRequestTypeBuilder() diff --git a/src/main/java/at/gv/egiz/moazs/backend/MsgResponseSinkHub.java b/src/main/java/at/gv/egiz/moazs/backend/MsgResponseSinkHub.java index 64b559f..9869e2e 100644 --- a/src/main/java/at/gv/egiz/moazs/backend/MsgResponseSinkHub.java +++ b/src/main/java/at/gv/egiz/moazs/backend/MsgResponseSinkHub.java @@ -22,21 +22,17 @@ package at.gv.egiz.moazs.backend; import at.gv.egiz.moazs.scheme.MsgResponse; -import at.gv.egiz.moazs.scheme.RequestStatusResponse; import at.gv.zustellung.app2mzs.xsd.MsgResponseSinksType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.core.task.TaskExecutor; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CompletionStage; import static java.util.concurrent.CompletableFuture.allOf; -import static java.util.concurrent.CompletableFuture.supplyAsync; /** * @author Christof Rabensteiner 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 6fce6ae..3de7b38 100644 --- a/src/main/java/at/gv/egiz/moazs/service/MsgService.java +++ b/src/main/java/at/gv/egiz/moazs/service/MsgService.java @@ -43,6 +43,7 @@ import static at.gv.zustellung.msg.xsd.DeliveryNotificationACKType.deliveryNotif import static at.gv.zustellung.msg.xsd.DeliveryRequestStatusACKType.deliveryRequestStatusACKTypeBuilder; import static at.gv.zustellung.msg.xsd.GetVersionResponse.getVersionResponseBuilder; import static java.lang.String.format; +import static java.lang.Thread.currentThread; import static java.util.concurrent.CompletableFuture.runAsync; /** @@ -81,10 +82,11 @@ public class MsgService implements Zuse2AppPort { try { sendToBackend(response).get(); return statusAck(response.getAppDeliveryID(), response.getZSDeliveryID()); - } catch (InterruptedException | ExecutionException e) { - var message = format(BACKEND_ERROR_MSG, "request status"); - log.error(message, e); - throw toFault(e, message); + } catch (ExecutionException e) { + throw logAndCreateFault("delivery request status", e); + } catch (InterruptedException e) { + currentThread().interrupt(); + throw logAndCreateFault("delivery request status", e); } } @@ -97,21 +99,24 @@ public class MsgService implements Zuse2AppPort { } @Override - public DeliveryNotificationACKType notification(DeliveryNotificationType notification) { + public DeliveryNotificationACKType notification(DeliveryNotificationType notification){ var response = new NotificationResponse(notification); try { sendToBackend(response).get(); return notificationAck(response.getAppDeliveryID(), response.getZSDeliveryID()); - } catch (InterruptedException | ExecutionException e) { - var message = format(BACKEND_ERROR_MSG, "delivery notification"); - log.error(message, e); - throw toFault(e, message); + } catch (ExecutionException e) { + throw logAndCreateFault("delivery notification", e); + } catch (InterruptedException e) { + currentThread().interrupt(); + throw logAndCreateFault("delivery notification", e); } } - private RuntimeException toFault(Exception e, String fallbackMessage) { + private RuntimeException logAndCreateFault(String responseType, Exception e) { + var message = format(BACKEND_ERROR_MSG, responseType); + log.error(message, e); Throwable cause = (e.getCause() != null) ? e.getCause() : e; - return new SoapFault(fallbackMessage, cause, new QName("faultcode")); + return new SoapFault(message, cause, new QName("faultcode")); } private CompletableFuture sendToBackend(MsgResponse response) { 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 caaa251..565e374 100644 --- a/src/main/java/at/gv/egiz/moazs/service/MzsService.java +++ b/src/main/java/at/gv/egiz/moazs/service/MzsService.java @@ -39,6 +39,7 @@ import org.springframework.stereotype.Service; import javax.jws.WebParam; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.function.Consumer; @@ -46,6 +47,7 @@ import java.util.function.Consumer; import static at.gv.egiz.moazs.MoaZSException.moaZSException; import static at.gv.zustellung.app2mzs.xsd.PartialSuccessType.partialSuccessTypeBuilder; import static java.lang.String.format; +import static java.lang.Thread.currentThread; import static java.util.concurrent.CompletableFuture.supplyAsync; /** @@ -116,11 +118,15 @@ public class MzsService implements App2MzsPortType { } catch (TimeoutException e) { return applySinkHubToAsyncResponse(appDeliveryID, completeRequest, requestProcessed); - } catch (Exception e) { + } catch (InterruptedException e) { + currentThread().interrupt(); var message = format(MZS_SERVICE_ERROR_MSG, appDeliveryID); throw moaZSException(message, e); - } + } catch (ExecutionException e) { + var message = format(MZS_SERVICE_ERROR_MSG, appDeliveryID); + throw moaZSException(message, e); + } } private RequestStatusResponse process(DeliveryRequestType deliveryRequest) { -- cgit v1.2.3