diff options
author | Christof Rabensteiner <christof.rabensteiner@iaik.tugraz.at> | 2019-07-17 13:17:21 +0200 |
---|---|---|
committer | Christof Rabensteiner <christof.rabensteiner@iaik.tugraz.at> | 2019-07-17 13:27:40 +0200 |
commit | f2e1263702901581512131ea587fad7a2ba45baa (patch) | |
tree | b92a38f758fecb039de184963116017921d2e314 /src/main/java/at/gv/egiz/moazs/backend | |
parent | 896195cc9b287a3f41008cc85997b9c2209120b8 (diff) | |
download | moa-zs-f2e1263702901581512131ea587fad7a2ba45baa.tar.gz moa-zs-f2e1263702901581512131ea587fad7a2ba45baa.tar.bz2 moa-zs-f2e1263702901581512131ea587fad7a2ba45baa.zip |
Put MoaZSException on Diet & Handle Edge Cases
Reason: MoaZSException (and: its builder) were used to collect
intermediary results while stepping through the delivery request
backend. These results were needed to generate meaningful responses
towards the sender application in case of error. However, the builder
sprawled over too many interfaces (e.g. DeliveryRequestBackend and
TNVZHelper) and it became difficult to understand from where
intermediary results originated.
Solution: Put MoaZSException on diet:
- Remove all DeliveryAnswer fields from MoaZSException and refactor
code base to ensure that the removed fields get sourced by other
means.
- Remove Builder since amount of parameters is manageable.
Refactor DeliveryRequestBackend:
- Instead of passing down the builder and using MoaZSException as the
only source for intermediary results, we collect available results
at the outermost stack frame (DeliveryRequestBackend.accept) and
only retrieve results via exception if those results appear
somewhere down the stack frame (E.g. PredviceNoteSent). We collect
available results with the "fallbackAnswerBuilder" and switch to the
msg client response, once received.
Refactor MsgResponseBackend:
- Handle cases (response missing, binary response missing) properly.
- Integrate changes from MsgResponse.generateError()
Refactor TVNZHelper:
- Remove MoaZSExceptionBuilder from all interfaces.
Refactor MsgResponse.generateError:
- Change interface such that it's more intuitive.
- Implement NotificationResponse.generateError.
- Implement RequestStatusResponse.generateError.
Others:
- Replace all invocations against MoaZSException.Builder.
Diffstat (limited to 'src/main/java/at/gv/egiz/moazs/backend')
3 files changed, 52 insertions, 39 deletions
diff --git a/src/main/java/at/gv/egiz/moazs/backend/DeliveryRequestBackend.java b/src/main/java/at/gv/egiz/moazs/backend/DeliveryRequestBackend.java index 06eba80..6a1e0fd 100644 --- a/src/main/java/at/gv/egiz/moazs/backend/DeliveryRequestBackend.java +++ b/src/main/java/at/gv/egiz/moazs/backend/DeliveryRequestBackend.java @@ -9,6 +9,7 @@ import at.gv.egiz.moazs.scheme.Mzs2MsgConverter; import at.gv.egiz.moazs.scheme.RequestStatusResponse; import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; import at.gv.zustellung.msg.xsd.App2ZusePort; +import at.gv.zustellung.msg.xsd.DeliveryRequestStatusType; import at.gv.zustellung.tnvz.xsd.TNVZServicePort; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; @@ -17,8 +18,10 @@ import org.springframework.stereotype.Component; import java.util.function.Consumer; -import static at.gv.egiz.moazs.MoaZSException.moaZSExceptionBuilder; -import static at.gv.egiz.moazs.scheme.RequestStatusResponse.generateErrorFromException; +import static at.gv.egiz.moazs.scheme.RequestStatusResponse.*; +import static at.gv.egiz.moazs.scheme.RequestStatusResponse.generateError; +import static at.gv.zustellung.msg.xsd.DeliveryRequestStatusType.Error.errorBuilder; +import static at.gv.zustellung.msg.xsd.DeliveryRequestStatusType.deliveryRequestStatusTypeBuilder; import static java.lang.String.format; @Component @@ -28,6 +31,10 @@ public class DeliveryRequestBackend implements Consumer<String> { private static final Logger log = Logger.getLogger(DeliveryRequestBackend.class); private static final String MZS_PIPELINE_ERROR_MSG = "An error occured while processing the DeliveryRequest with AppDeliveryID=%s. "; + private static final String DELIVERY_REQUEST_MISSING_ERROR_MSG = + "DeliveryRequest for AppDeliveryID=%s is not in repository. " ; + private static final String BINARY_RESPONSE_MISSING_ERROR_MSG = + "Binary DeliveryRequestStatus for AppDeliveryID=%s is not in repository. " ; private final DeliveryRepository repository; private final TnvzHelper tnvzHelper; @@ -60,49 +67,53 @@ public class DeliveryRequestBackend implements Consumer<String> { @Override public void accept(String appDeliveryID) { - var exceptionBuilder = moaZSExceptionBuilder(); + DeliveryRequestStatusType status = null; + var fallbackAnswerBuilder = errorBuilder().withAppDeliveryID(appDeliveryID); try { - var mzsRequest = repository.retrieveDeliveryRequest(appDeliveryID).orElseThrow(); - exceptionBuilder.withDeliverySystem(mzsRequest); - - at.gv.zustellung.msg.xsd.DeliveryRequestType msgRequest = buildMsgRequest(mzsRequest, exceptionBuilder); + var mzsRequest = repository.retrieveDeliveryRequest(appDeliveryID).orElseThrow( + () -> MoaZSException.moaZSException(format(DELIVERY_REQUEST_MISSING_ERROR_MSG, appDeliveryID))); + fallbackAnswerBuilder.withDeliverySystem(mzsRequest.getConfig().getMSGClient().getURL()); + var msgRequest = buildMsgRequest(mzsRequest); var msgClientParams = mzsRequest.getConfig().getMSGClient(); App2ZusePort client = clientFactory.create(msgClientParams, App2ZusePort.class); - var status = client.delivery(msgRequest); + status = client.delivery(msgRequest); var response = new RequestStatusResponse(status); - exceptionBuilder.withAllParametersInAnswer(response.getAnswer()); - verifySignedStatus(response.getResponseID(), appDeliveryID, exceptionBuilder); + verifySignedStatus(response.getResponseID(), appDeliveryID); repository.store(response); } catch (MoaZSException exception) { log.error(format(MZS_PIPELINE_ERROR_MSG, appDeliveryID), exception); - var errorResponse = generateErrorFromException(exception); - repository.store(errorResponse); + + var templateAnswer = (status == null) + ? fallbackAnswerBuilder.build() + : getAnswer(status); + + var errorStatus = generateError(exception, templateAnswer); + repository.store(errorStatus); } } - private void verifySignedStatus(String responseID, String appDeliveryID, MoaZSException.Builder exceptionBuilder) throws MoaZSException { + private void verifySignedStatus(String responseID, String appDeliveryID) throws MoaZSException { try { - var signedStatus = repository.retrieveBinaryResponse(responseID).get(); + var signedStatus = repository.retrieveBinaryResponse(responseID).orElseThrow( + () -> MoaZSException.moaZSException(format(BINARY_RESPONSE_MISSING_ERROR_MSG, responseID))); signatureVerifier.accept(signedStatus); } catch (MoaZSException ex) { - throw exceptionBuilder.withMessage(format(MsgResponseBackend.MOASP_SIGNATURE_INVALID_ERROR_MSG, appDeliveryID)) - .withErrorCode(MoaZSException.ERROR_MOASP_SIGNATURE_INVALID) - .withCause(ex) - .build(); + var message = format(MsgResponseBackend.MOASP_SIGNATURE_INVALID_ERROR_MSG, appDeliveryID); + var code = MoaZSException.ERROR_MOASP_SIGNATURE_INVALID; + throw MoaZSException.moaZSException(message, code, ex); } } - private at.gv.zustellung.msg.xsd.DeliveryRequestType buildMsgRequest(DeliveryRequestType mzsRequest, - MoaZSException.Builder exceptionBuilder) throws MoaZSException { + private at.gv.zustellung.msg.xsd.DeliveryRequestType buildMsgRequest(DeliveryRequestType mzsRequest) 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); + var identification = tnvzHelper.performQueryPersonRequest(mzsRequest, tvnzPort); return converter.convert(mzsRequest, identification); } else { return converter.convert(mzsRequest); 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 9e3cd36..8649a32 100644 --- a/src/main/java/at/gv/egiz/moazs/backend/MsgResponseBackend.java +++ b/src/main/java/at/gv/egiz/moazs/backend/MsgResponseBackend.java @@ -12,7 +12,7 @@ import org.springframework.stereotype.Component; import java.util.function.Consumer; -import static at.gv.egiz.moazs.MoaZSException.moaZSExceptionBuilder; +import static at.gv.egiz.moazs.MoaZSException.*; import static java.lang.String.format; import static java.util.concurrent.CompletableFuture.supplyAsync; @@ -23,6 +23,8 @@ public class MsgResponseBackend implements Consumer<String> { public static final String MOASP_SIGNATURE_INVALID_ERROR_MSG = "Signature of Msg Response " + "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."; private final DeliveryRepository repository; private final Consumer<byte[]> signatureVerifier; @@ -58,28 +60,28 @@ public class MsgResponseBackend implements Consumer<String> { public void accept(String responseID) { supplyAsync(() -> verify(responseID)) - .thenAcceptAsync(msgResponse -> applySinks(msgResponse)); + .thenAcceptAsync(msgResponse -> applySinks(msgResponse)) + .exceptionally(ex -> { + log.error(ex.getMessage(), ex); + return null; + }); } private MsgResponse verify(String responseID) { - var response = repository.retrieveResponse(responseID).get(); - var builder = moaZSExceptionBuilder().withAllParametersInAnswer(response.getAnswer()); - - var binaryResponse = repository.retrieveBinaryResponse(responseID).get(); + var response = repository.retrieveResponse(responseID).orElseThrow( + ()-> moaZSException(format(RESPONSE_MISSING_ERROR_MSG, responseID))); try { + var binaryResponse = repository.retrieveBinaryResponse(responseID).orElseThrow( + () -> moaZSException(BINARY_RESPONSE_MISSING_ERROR_MSG, MoaZSException.ERROR_MZS_BINARY_RESPONSE_MISSING)); signatureVerifier.accept(binaryResponse); return response; } catch (MoaZSException ex) { log.error(ex.getMessage(), ex); - var wrappingEx = builder - .withMessage(format(MOASP_SIGNATURE_INVALID_ERROR_MSG, response.getAppDeliveryID())) - .withErrorCode(MoaZSException.ERROR_MOASP_SIGNATURE_INVALID) - .withCause(ex) - .build(); - - return response.generateError(wrappingEx); + var text = format(MOASP_SIGNATURE_INVALID_ERROR_MSG, response.getAppDeliveryID()); + var code = ERROR_MOASP_SIGNATURE_INVALID; + return response.generateError(text, code); } } diff --git a/src/main/java/at/gv/egiz/moazs/backend/SignatureVerifier.java b/src/main/java/at/gv/egiz/moazs/backend/SignatureVerifier.java index f9bbeb3..874e4f4 100644 --- a/src/main/java/at/gv/egiz/moazs/backend/SignatureVerifier.java +++ b/src/main/java/at/gv/egiz/moazs/backend/SignatureVerifier.java @@ -3,13 +3,13 @@ package at.gv.egiz.moazs.backend; import at.gv.egiz.eid.authhandler.modules.sigverify.moasig.api.ISignatureVerificationService; import at.gv.egiz.eid.authhandler.modules.sigverify.moasig.api.data.IXMLSignatureVerificationResponse; import at.gv.egiz.eid.authhandler.modules.sigverify.moasig.exceptions.MOASigServiceException; +import at.gv.egiz.moazs.MoaZSException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.function.Consumer; import static at.gv.egiz.moazs.MoaZSException.moaZSException; -import static at.gv.egiz.moazs.MoaZSException.moaZSExceptionBuilder; import static java.lang.String.format; public class SignatureVerifier implements Consumer<byte[]> { @@ -21,6 +21,8 @@ public class SignatureVerifier implements Consumer<byte[]> { private static final String CERT_CODE_ERROR_MSG = "Certificate chain is not valid: Check code was %d. "; private static final String MANIFEST_CODE_ERROR_MSG = "Signature Manifest is not valid: Check code was %d. "; private static final String XMLMANIFEST_CODE_ERROR_MSG = "XmlDSIGManifest is not valid: Check code was %d. "; + + //TODO: Dont make this multiline! private static final String XML_SIGNATURE_RESPONSE_TEMPLATE = " XmlDsigSubjectName: %s\n" + " SignatureManifestCheckCode: %s\n" + @@ -93,14 +95,12 @@ public class SignatureVerifier implements Consumer<byte[]> { var msg = builder.toString(); - if(msg.length() > 0) { + if(!msg.isEmpty()) { throw moaZSException(msg); } } catch (MOASigServiceException e) { - throw moaZSExceptionBuilder(MOASIG_SERVICE_ERROR_MSG) - .withCause(e) - .build(); + throw moaZSException(MOASIG_SERVICE_ERROR_MSG, e); } } |