From 6dbe979e0b6bd6236db304b034ad592a4aeacabc Mon Sep 17 00:00:00 2001 From: Christof Rabensteiner Date: Fri, 29 Nov 2019 15:56:44 +0100 Subject: Refactor & Fix Client Creation in Client Factory - Refactor: Replace createSoap1{1,2} with create{Tnvz,App,Msg}Client. Configure each method with zusespec's specific parameters. Reason: Each service requires different parameters (e.g. with / without MTOM? soap11 / soap12? store responses in binary respository - yes/no?). These parameters could be placed in the client configuration of application.yaml. Since the parameters are tied to zusespecs, they do not need to be configurable via application.yaml. Another benefit for this refactoring is to improve readability. - Fix: Disable "Store Response" for TNVZ Client and App Client --- .../egiz/moazs/backend/DeliveryRequestBackend.java | 4 +- .../backend/ForwardResponseToServiceSink.java | 2 +- .../at/gv/egiz/moazs/client/ClientFactory.java | 45 +++++++++++----------- .../gv/egiz/moazs/DeliveryRequestBackendTest.java | 4 +- src/test/java/at/gv/egiz/moazs/ITEndToEndTest.java | 6 +-- .../java/at/gv/egiz/moazs/ITSSLMsgClientTest.java | 2 +- 6 files changed, 32 insertions(+), 31 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 9cbdd81..7e1d2f7 100644 --- a/src/main/java/at/gv/egiz/moazs/backend/DeliveryRequestBackend.java +++ b/src/main/java/at/gv/egiz/moazs/backend/DeliveryRequestBackend.java @@ -121,7 +121,7 @@ public class DeliveryRequestBackend implements Consumer { var msgRequest = buildMsgRequest(mzsRequest); traceMsgRequest(msgRequest); var msgClientParams = mzsRequest.getConfig().getMSGClient(); - App2ZusePort client = clientFactory.createSOAP12(msgClientParams, App2ZusePort.class); + App2ZusePort client = clientFactory.createMsgClient(msgClientParams); status = client.delivery(msgRequest); traceMsgResponse(status); @@ -159,7 +159,7 @@ public class DeliveryRequestBackend implements Consumer { if (mzsRequest.getConfig().isPerformQueryPersonRequest()) { log.trace("Perform QueryPersonRequest. "); var tnvzClientParams = mzsRequest.getConfig().getTNVZClient(); - TNVZServicePort tnvzPort = clientFactory.createSOAP12(tnvzClientParams, TNVZServicePort.class); + TNVZServicePort tnvzPort = clientFactory.createTnvzClient(tnvzClientParams); var identification = tnvzHelper.performQueryPersonRequest(mzsRequest, tnvzPort); return converter.convert(mzsRequest, identification); } else { diff --git a/src/main/java/at/gv/egiz/moazs/backend/ForwardResponseToServiceSink.java b/src/main/java/at/gv/egiz/moazs/backend/ForwardResponseToServiceSink.java index b6f9b44..1312a5f 100644 --- a/src/main/java/at/gv/egiz/moazs/backend/ForwardResponseToServiceSink.java +++ b/src/main/java/at/gv/egiz/moazs/backend/ForwardResponseToServiceSink.java @@ -58,7 +58,7 @@ public class ForwardResponseToServiceSink { log.info("Forward msg:{} with AppDeliveryID={} to service at {}.", msgResponse.getRootElementLocalPart(), msgResponse.getAppDeliveryID(), params.getURL()); var binaryResponse = repository.retrieveBinaryResponse(msgResponse.getResponseID()); - Mzs2AppPortType client = factory.createSOAP11(params, Mzs2AppPortType.class); + Mzs2AppPortType client = factory.createAppClient(params); return msgResponse.sendToAppClient(converter, binaryResponse, client); } } diff --git a/src/main/java/at/gv/egiz/moazs/client/ClientFactory.java b/src/main/java/at/gv/egiz/moazs/client/ClientFactory.java index c294aa3..18fcdd9 100644 --- a/src/main/java/at/gv/egiz/moazs/client/ClientFactory.java +++ b/src/main/java/at/gv/egiz/moazs/client/ClientFactory.java @@ -26,7 +26,10 @@ import at.gv.egiz.moazs.util.FileUtils; import at.gv.egiz.moazs.util.StoreSOAPBodyBinaryInRepositoryInterceptor; import at.gv.zustellung.app2mzs.xsd.ClientType; import at.gv.zustellung.app2mzs.xsd.KeyStoreType; +import at.gv.zustellung.app2mzs.xsd.Mzs2AppPortType; import at.gv.zustellung.app2mzs.xsd.SSLType; +import at.gv.zustellung.msg.xsd.App2ZusePort; +import at.gv.zustellung.tnvz.xsd.TNVZServicePort; import org.apache.cxf.configuration.jsse.TLSClientParameters; import org.apache.cxf.endpoint.Client; import org.apache.cxf.ext.logging.LoggingInInterceptor; @@ -78,48 +81,46 @@ public class ClientFactory { this.configUtil = configUtil; } - /** - * Creates a client that communicates with a soap 1.2 service. - * - * @param params for the client, such as service url and ssl parameters. - * @return the client - */ - public T createSOAP12(ClientType params, Class clazz) { - return createSOAPClient(params, clazz, true); + public TNVZServicePort createTnvzClient(ClientType params) { + return createSOAPClient(params, TNVZServicePort.class, true, false, false); } - /** - * Creates a client that communicates with a soap 1.1 service. - * - * @param params for the client, such as service url and ssl parameters. - * @return the client - */ - public T createSOAP11(ClientType params, Class clazz) { - return createSOAPClient(params, clazz, false); + public Mzs2AppPortType createAppClient(ClientType params) { + return createSOAPClient(params, Mzs2AppPortType.class, false, false, true); + } + + public App2ZusePort createMsgClient(ClientType params) { + return createSOAPClient(params, App2ZusePort.class, true, true, true); } - private T createSOAPClient(ClientType params, Class clazz, boolean isSoap12) { + private T createSOAPClient(ClientType params, Class clazz, boolean isSoap12, boolean storeResponse, boolean mtomEnabled) { log.info("Create client for service {}", params.getURL()); var factory = new JaxWsClientFactoryBean(); factory.setServiceClass(clazz); factory.setAddress(params.getURL()); - factory.getInInterceptors().add(storeResponseInterceptor); + + if (storeResponse) { + factory.getInInterceptors().add(storeResponseInterceptor); + } if (isSoap12) { - factory.setBindingId(SOAPBinding.SOAP12HTTP_MTOM_BINDING); + factory.setBindingId(SOAPBinding.SOAP12HTTP_BINDING); } + setupLoggingInterceptors(factory); var proxy = new JaxWsProxyFactoryBean(factory).create(); Client client = ClientProxy.getClient(proxy); HTTPConduit http = (HTTPConduit) client.getConduit(); - var bindingProvider = (BindingProvider) proxy; - SOAPBinding binding = (SOAPBinding) bindingProvider.getBinding(); - binding.setMTOMEnabled(true); + if (mtomEnabled) { + var bindingProvider = (BindingProvider) proxy; + SOAPBinding binding = (SOAPBinding) bindingProvider.getBinding(); + binding.setMTOMEnabled(true); + } var httpClientPolicy = new HTTPClientPolicy(); httpClientPolicy.setConnectionTimeout(params.getConnectionTimeout().longValueExact()); diff --git a/src/test/java/at/gv/egiz/moazs/DeliveryRequestBackendTest.java b/src/test/java/at/gv/egiz/moazs/DeliveryRequestBackendTest.java index e1859b5..f2e33ab 100644 --- a/src/test/java/at/gv/egiz/moazs/DeliveryRequestBackendTest.java +++ b/src/test/java/at/gv/egiz/moazs/DeliveryRequestBackendTest.java @@ -165,11 +165,11 @@ public class DeliveryRequestBackendTest { when(converter.convert(eq(mzsRequest) )).thenReturn(msgRequest); when(converter.convert(eq(mzsRequest), any())).thenReturn(msgRequest); - when(clientFactory.createSOAP12(any(), same(App2ZusePort.class))).thenReturn(msgClient); + when(clientFactory.createMsgClient(any())).thenReturn(msgClient); when(msgClient.delivery(msgRequest)).thenReturn(status); if (tnvzRequest) { - when(clientFactory.createSOAP12(any(), same(TNVZServicePort.class))).thenReturn(tnvzClient); + when(clientFactory.createTnvzClient(any())).thenReturn(tnvzClient); when(tnvzHelper.performQueryPersonRequest(any(), any())).thenReturn(identification); } return status; diff --git a/src/test/java/at/gv/egiz/moazs/ITEndToEndTest.java b/src/test/java/at/gv/egiz/moazs/ITEndToEndTest.java index 7e03a13..f350681 100644 --- a/src/test/java/at/gv/egiz/moazs/ITEndToEndTest.java +++ b/src/test/java/at/gv/egiz/moazs/ITEndToEndTest.java @@ -112,9 +112,9 @@ public class ITEndToEndTest { var tnvz = mockTnvz(); var msg = mockMsg(); APP = mockApp(); - when(factory.createSOAP12(any(), same(TNVZServicePort.class))).thenReturn(tnvz); - when(factory.createSOAP12(any(), same(App2ZusePort.class))).thenReturn(msg); - when(factory.createSOAP11(any(), same(Mzs2AppPortType.class))).thenReturn(APP); + when(factory.createTnvzClient(any())).thenReturn(tnvz); + when(factory.createMsgClient(any())).thenReturn(msg); + when(factory.createAppClient(any())).thenReturn(APP); return factory; } diff --git a/src/test/java/at/gv/egiz/moazs/ITSSLMsgClientTest.java b/src/test/java/at/gv/egiz/moazs/ITSSLMsgClientTest.java index 107ede5..4158aec 100644 --- a/src/test/java/at/gv/egiz/moazs/ITSSLMsgClientTest.java +++ b/src/test/java/at/gv/egiz/moazs/ITSSLMsgClientTest.java @@ -86,7 +86,7 @@ public class ITSSLMsgClientTest { } private App2ZusePort createPort(ClientType params) { - return factory.createSOAP12(params, App2ZusePort.class); + return factory.createMsgClient(params); } // Requirements: -- cgit v1.2.3