From 34a0866d02851e21b66e438736cca63723599696 Mon Sep 17 00:00:00 2001 From: FrodeBjerkholt Date: Tue, 4 Feb 2020 14:50:09 +0100 Subject: [PATCH] Issue #74 Sockets not closing properly --- .../oxalis/as4/outbound/As4MessageSender.java | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/main/java/no/difi/oxalis/as4/outbound/As4MessageSender.java b/src/main/java/no/difi/oxalis/as4/outbound/As4MessageSender.java index 6b09d4f..dbc00b2 100644 --- a/src/main/java/no/difi/oxalis/as4/outbound/As4MessageSender.java +++ b/src/main/java/no/difi/oxalis/as4/outbound/As4MessageSender.java @@ -76,14 +76,20 @@ public class As4MessageSender { private PolicyService policyService; public TransmissionResponse send(TransmissionRequest request) throws OxalisAs4TransmissionException { - Dispatch dispatch = createDispatch(request); - Collection attachments = prepareAttachments(request); - dispatch.getRequestContext().put(Message.ATTACHMENTS, attachments); - - Messaging messaging = messagingProvider.createMessagingHeader(request, attachments); - SoapHeader header = getSoapHeader(messaging); - dispatch.getRequestContext().put(Header.HEADER_LIST, new ArrayList<>(Collections.singletonList(header))); + try (DispatchImpl dispatch = createDispatch(request)) { + Collection attachments = prepareAttachments(request); + dispatch.getRequestContext().put(Message.ATTACHMENTS, attachments); + + Messaging messaging = messagingProvider.createMessagingHeader(request, attachments); + SoapHeader header = getSoapHeader(messaging); + dispatch.getRequestContext().put(Header.HEADER_LIST, new ArrayList<>(Collections.singletonList(header))); + return invoke(request, dispatch); + } catch (IOException e) { + throw new OxalisAs4TransmissionException("Failed to send message", e); + } + } + private TransmissionResponse invoke(TransmissionRequest request, DispatchImpl dispatch) throws OxalisAs4TransmissionException { try { SOAPMessage response = dispatch.invoke(null); return transmissionResponseConverter.convert(request, response); @@ -137,14 +143,14 @@ private String getContentID(TransmissionRequest request) { return messageIdGenerator.generate(); } - private Dispatch createDispatch(TransmissionRequest request) throws OxalisAs4TransmissionException { - Dispatch dispatch = getService(request) + private DispatchImpl createDispatch(TransmissionRequest request) throws OxalisAs4TransmissionException { + DispatchImpl dispatch = (DispatchImpl) getService(request) .createDispatch(PORT_NAME, SOAPMessage.class, Service.Mode.MESSAGE); dispatch.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, request.getEndpoint().getAddress().toString()); configureSecurity(request, dispatch); - final Client client = ((DispatchImpl) dispatch).getClient(); + final Client client = dispatch.getClient(); if (CEF_CONFORMANCE.equalsIgnoreCase(as4settings.getString(As4Conf.TYPE))) { client.getInInterceptors().add(getLoggingBeforeSecurityInInterceptor());