This is an automated email from the ASF dual-hosted git repository. ffang pushed a commit to branch 3.6.x-fixes in repository https://gitbox.apache.org/repos/asf/cxf.git
commit c66a8a2cdd41b4f7eea9f368bf4417e891f438d0 Author: Freeman(Yue) Fang <[email protected]> AuthorDate: Tue Nov 24 11:19:13 2026 -0500 CXF-9277: Keep the reply of a decoupled call when a late message arrives (#3741) * CXF-9277: Keep the reply of a decoupled call when a late message arrives With a decoupled endpoint, the reply and other messages for the same exchange (an HTTP 202 acknowledgement, a partial response) can reach the client in any order. CXF-9275 kept HTTPConduit from replacing a reply that has already finished the exchange, but ClientImpl could still lose it: - onMessage() made every message the in message of the exchange, so a partial response processed after the reply (as WS-RM does), or a 202 passed on with propagate202Response, replaced it, and its finally block could finish the exchange a second time with it; - processResult() read the in message of the exchange, so the callback of an asynchronous call could get the result or the fault of a message that arrived while the reply was being processed. onMessage() now leaves the exchange alone once another message has finished it, and still processes the late message. processResult() uses the response it is called with, for its result and its fault. * CXF-9277: Address review comments Treat an HTTP 202 without content, for a call whose response goes to a decoupled endpoint, as a partial response: the acknowledgement, which the transport passes on with propagate202Response usually before the response arrives, no longer finishes the exchange or completes the callback with no result. Remember the message that finished the exchange, and have a synchronous call read its result and fault from it, as a partial response with a SOAP fault that arrives later still runs the in fault chain, which replaces the exchange's in and in fault messages. Resolve the failure of both synchronous and asynchronous calls through a new getException(Exchange, Message), which keeps using the overridable getException(Exchange) while the response is still the exchange's in or in fault message. * CXF-9277: Address further review comments - A one-way call to a decoupled endpoint gets no other response, so its HTTP 202 is the response again: an asynchronous one-way call is completed. - getException(Exchange, Message) keeps using the overridable getException(Exchange) while the response is the exchange's in message, but discards its result if a late SOAP fault became the exchange's in fault message meanwhile, and uses the response's own failure instead. A message arriving after the exchange is finished still runs the inbound chain while the exchange's in message stays the response. The WS-Addressing, WS-RM, SOAP, WS-Security and WS-Policy interceptors do not read the reply on a client's inbound chain, so this needs no change; it is documented on ClientImpl.receive(). * CXF-9277: Re-check the in message after getException(Exchange) A partial response can replace the exchange's in message while an async reply's failure is resolved, so getException(Exchange) could read the partial's failure, or none, instead of the reply's own. (cherry picked from commit 163b9737dce06e3e145ac84aef0d545212aebde1) (cherry picked from commit f16b73596b69edeb2bd363bcdbbd4de792a38df0) --- .../java/org/apache/cxf/endpoint/ClientImpl.java | 95 +++++- .../endpoint/ClientImplDecoupledResponseTest.java | 378 +++++++++++++++++++++ 2 files changed, 465 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/org/apache/cxf/endpoint/ClientImpl.java b/core/src/main/java/org/apache/cxf/endpoint/ClientImpl.java index 521985c66b0..a747a55bc55 100644 --- a/core/src/main/java/org/apache/cxf/endpoint/ClientImpl.java +++ b/core/src/main/java/org/apache/cxf/endpoint/ClientImpl.java @@ -21,6 +21,7 @@ package org.apache.cxf.endpoint; import java.io.Closeable; import java.io.IOException; +import java.io.InputStream; import java.io.Serializable; import java.net.URI; import java.net.URISyntaxException; @@ -89,6 +90,12 @@ public class ClientImpl public static final String FINISHED = "exchange.finished"; + //REVISIT move the decoupled destination property name into api + private static final String DECOUPLED_DESTINATION = + "org.apache.cxf.ws.addressing.MAPAggregator.decoupledDestination"; + // the message that finished the exchange + private static final String RESPONSE = ClientImpl.class.getName() + ".response"; + private static final Logger LOG = LogUtils.getL7dLogger(ClientImpl.class); protected Bus bus; @@ -624,8 +631,8 @@ public class ClientImpl Integer responseCode = (Integer)exchange.get(Message.RESPONSE_CODE); if (null != responseCode && 202 == responseCode) { Endpoint ep = exchange.getEndpoint(); - if (null != ep && null != ep.getEndpointInfo() && null == ep.getEndpointInfo(). - getProperty("org.apache.cxf.ws.addressing.MAPAggregator.decoupledDestination")) { + if (null != ep && null != ep.getEndpointInfo() + && null == ep.getEndpointInfo().getProperty(DECOUPLED_DESTINATION)) { return null; } } @@ -646,9 +653,11 @@ public class ClientImpl completeExchange(exchange); } - // Grab the response objects if there are any + // Grab the response objects if there are any: from the response this is called with + // (asynchronous calls) or the one that finished the exchange (synchronous calls), as a + // message arriving later (e.g. a partial response with a fault) may replace the exchange's List<Object> resList = null; - Message inMsg = exchange.getInMessage(); + Message inMsg = Boolean.TRUE.equals(message.get(Message.INBOUND_MESSAGE)) ? message : getResponse(exchange); if (inMsg != null) { if (null != resContext) { resContext.putAll(inMsg); @@ -660,7 +669,7 @@ public class ClientImpl } // check for an incoming fault - ex = getException(exchange); + ex = getException(exchange, inMsg); if (ex != null) { throw ex; @@ -770,6 +779,34 @@ public class ClientImpl .collect(Collectors.toList()); } + /** + * Returns the failure of a call whose response is the given inbound message (null if there is none). + * While that message is still the exchange's in (or in fault) message, this is getException(Exchange); + * if a message that arrived later has replaced it, or has become the exchange's in fault message + * meanwhile (e.g. a partial response with a SOAP fault), it is the response's own failure, or else an + * outbound fault. + */ + protected Exception getException(Exchange exchange, Message response) { + Message inFault = exchange.getInFaultMessage(); + if (response == null || response == inFault) { + return getException(exchange); + } + if (inFault == null && response == exchange.getInMessage()) { + Exception ex = getException(exchange); + // the in fault message is only ever set, and a message that arrives later never makes the + // response the in message again, so if both are unchanged, getException(Exchange) cannot + // have read a message that arrived after the response + if (exchange.getInFaultMessage() == null && response == exchange.getInMessage()) { + return ex; + } + } + Exception ex = response.getContent(Exception.class); + if (ex == null && exchange.getOutFaultMessage() != null) { + ex = exchange.getOutFaultMessage().getContent(Exception.class); + } + return ex; + } + protected Exception getException(Exchange exchange) { if (exchange.getInFaultMessage() != null) { return exchange.getInFaultMessage().getContent(Exception.class); @@ -837,7 +874,7 @@ public class ClientImpl message.getExchange().put(Endpoint.class, endpoint); } message = endpoint.getBinding().createMessage(message); - message.getExchange().setInMessage(message); + receive(message); message.put(Message.REQUESTOR_ROLE, Boolean.TRUE); message.put(Message.INBOUND_MESSAGE, Boolean.TRUE); PhaseManager pm = bus.getExtension(PhaseManager.class); @@ -960,9 +997,10 @@ public class ClientImpl BusFactory.setThreadDefaultBus(origBus); } synchronized (message.getExchange()) { - if (!isPartialResponse(message) - || message.getContent(Exception.class) != null) { + if (!isFinished(message.getExchange()) + && (!isPartialResponse(message) || message.getContent(Exception.class) != null)) { message.getExchange().put(FINISHED, Boolean.TRUE); + message.getExchange().put(RESPONSE, message); message.getExchange().setInMessage(message); message.getExchange().notifyAll(); } @@ -1160,6 +1198,47 @@ public class ClientImpl conduitSelector = selector == null ? new UpfrontConduitSelector() : selector; } + private static boolean isFinished(Exchange exchange) { + return Boolean.TRUE.equals(exchange.get(FINISHED)); + } + + // the messages of a decoupled call can arrive in any order. A message that arrives after the exchange + // is finished still runs the inbound chain, but the exchange's in message stays the response: client + // interceptors read the message they are given, not exchange.getInMessage() + private static void receive(Message message) { + if (isDecoupledAcknowledgement(message)) { + // the response arrives separately: the acknowledgement, which can come first, must not + // finish the exchange + message.put(Message.PARTIAL_RESPONSE_MESSAGE, Boolean.TRUE); + } + synchronized (message.getExchange()) { + // a message that arrives after another has finished the exchange (e.g. a partial + // response) is processed, but must not replace the response + if (!isFinished(message.getExchange())) { + message.getExchange().setInMessage(message); + } + } + } + + // the message that finished the exchange, or the exchange's in message + private static Message getResponse(Exchange exchange) { + Message response = (Message)exchange.get(RESPONSE); + return response != null ? response : exchange.getInMessage(); + } + + // an HTTP 202 without content, which acknowledges a call whose response goes to a decoupled endpoint. + // A one-way call gets no other response, so there the 202 is the response + private static boolean isDecoupledAcknowledgement(Message message) { + if (message.getExchange().isOneWay() + || !Integer.valueOf(202).equals(message.get(Message.RESPONSE_CODE)) + || message.getContent(InputStream.class) != null) { + return false; + } + Endpoint ep = message.getExchange().getEndpoint(); + return ep != null && ep.getEndpointInfo() != null + && ep.getEndpointInfo().getProperty(DECOUPLED_DESTINATION) != null; + } + private boolean isPartialResponse(Message in) { return Boolean.TRUE.equals(in.get(Message.PARTIAL_RESPONSE_MESSAGE)); } diff --git a/core/src/test/java/org/apache/cxf/endpoint/ClientImplDecoupledResponseTest.java b/core/src/test/java/org/apache/cxf/endpoint/ClientImplDecoupledResponseTest.java new file mode 100644 index 00000000000..032ee0066e4 --- /dev/null +++ b/core/src/test/java/org/apache/cxf/endpoint/ClientImplDecoupledResponseTest.java @@ -0,0 +1,378 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.cxf.endpoint; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.apache.cxf.Bus; +import org.apache.cxf.binding.Binding; +import org.apache.cxf.bus.extension.ExtensionManagerBus; +import org.apache.cxf.interceptor.InFaultChainInitiatorObserver; +import org.apache.cxf.message.Exchange; +import org.apache.cxf.message.ExchangeImpl; +import org.apache.cxf.message.Message; +import org.apache.cxf.message.MessageImpl; +import org.apache.cxf.phase.AbstractPhaseInterceptor; +import org.apache.cxf.phase.Phase; +import org.apache.cxf.service.Service; +import org.apache.cxf.service.model.EndpointInfo; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * The response of a decoupled call and other messages for its exchange (an HTTP 202 acknowledgement, + * a partial response) can arrive in any order: the response must survive the others. + */ +public class ClientImplDecoupledResponseTest { + + private static final List<Object> RESULT = List.of("Hello Paul!"); + + private Bus bus; + private Endpoint endpoint; + private ConduitSelector selector; + private ClientImpl client; + private Exchange exchange; + private Message outMessage; + + @Before + public void setUp() { + bus = new ExtensionManagerBus(); + Binding binding = mock(Binding.class); + when(binding.createMessage(any(Message.class))).thenAnswer(i -> i.getArgument(0)); + when(binding.getInInterceptors()).thenReturn(new ArrayList<>()); + endpoint = mock(Endpoint.class); + when(endpoint.getBinding()).thenReturn(binding); + when(endpoint.getService()).thenReturn(mock(Service.class)); + when(endpoint.getInInterceptors()).thenReturn(new ArrayList<>()); + selector = mock(ConduitSelector.class); + client = new ClientImpl(bus, endpoint, selector); + + exchange = new ExchangeImpl(); + exchange.put(Endpoint.class, endpoint); + outMessage = new MessageImpl(); + outMessage.put(Message.INBOUND_MESSAGE, Boolean.FALSE); + Map<String, Object> invocationContext = new HashMap<>(); + invocationContext.put(Client.RESPONSE_CONTEXT, new HashMap<String, Object>()); + outMessage.put(Message.INVOCATION_CONTEXT, invocationContext); + exchange.setOutMessage(outMessage); + outMessage.setExchange(exchange); + } + + @After + public void tearDown() { + bus.shutdown(true); + } + + @Test + public void testPartialResponseAfterReply() throws Exception { + Message reply = reply(); + client.onMessage(reply); + client.onMessage(partialResponse()); + + assertReplyKept(reply); + } + + @Test + public void testFaultPartialResponseAfterReply() throws Exception { + Message reply = reply(); + client.onMessage(reply); + Message partial = partialResponse(); + partial.setContent(Exception.class, new IllegalStateException("late")); + client.onMessage(partial); + + assertReplyKept(reply); + } + + @Test + public void testAcknowledgementAfterReply() throws Exception { + // with propagate202Response the transport passes the HTTP 202 on to ClientImpl + Message reply = reply(); + client.onMessage(reply); + client.onMessage(acknowledgement()); + + assertReplyKept(reply); + } + + @Test + public void testAcknowledgementWhileAsyncReplyIsProcessed() throws Exception { + ClientCallback callback = new ClientCallback(); + exchange.put(ClientCallback.class, callback); + // the transport publishes an HTTP 202 for the exchange while the reply is being processed, + // before the reply has finished it + AtomicBoolean acknowledged = new AtomicBoolean(); + doAnswer(i -> { + if (acknowledged.compareAndSet(false, true)) { + exchange.setInMessage(acknowledgement()); + } + return null; + }).when(selector).complete(exchange); + + Message reply = reply(); + client.onMessage(reply); + + assertArrayEquals(RESULT.toArray(), callback.get(5, TimeUnit.SECONDS)); + assertReplyKept(reply); + } + + @Test + public void testPartialFaultWhileAsyncReplyIsProcessed() throws Exception { + ClientCallback callback = new ClientCallback(); + exchange.put(ClientCallback.class, callback); + AtomicBoolean replaced = new AtomicBoolean(); + doAnswer(i -> { + if (replaced.compareAndSet(false, true)) { + Message partial = partialResponse(); + partial.setContent(Exception.class, new IllegalStateException("late partial fault")); + exchange.setInMessage(partial); + } + return null; + }).when(selector).complete(exchange); + client.onMessage(reply()); + assertArrayEquals(RESULT.toArray(), callback.get(5, TimeUnit.SECONDS)); + } + + @Test + public void testAsyncResponseFaultIsReported() throws Exception { + ClientCallback callback = new ClientCallback(); + exchange.put(ClientCallback.class, callback); + Message reply = reply(); + IllegalStateException failure = new IllegalStateException("response fault"); + // Install the exception during completion so the test exercises processResult's + // failure selection after the normal inbound chain has run. + doAnswer(i -> { + reply.setContent(Exception.class, failure); + return null; + }).when(selector).complete(exchange); + client.onMessage(reply); + ExecutionException ex = assertThrows(ExecutionException.class, + () -> callback.get(5, TimeUnit.SECONDS)); + assertSame(failure, ex.getCause()); + } + + @Test + public void testAcknowledgementBeforeReply() throws Exception { + // with propagate202Response the HTTP 202 usually arrives first, on the calling thread + decoupled(); + client.onMessage(acknowledgement()); + assertNull("the acknowledgement must not finish the exchange", exchange.get(ClientImpl.FINISHED)); + + Message reply = reply(); + client.onMessage(reply); + assertReplyKept(reply); + } + + @Test + public void testAcknowledgementBeforeAsyncReply() throws Exception { + decoupled(); + ClientCallback callback = new ClientCallback(); + exchange.put(ClientCallback.class, callback); + client.onMessage(acknowledgement()); + assertFalse("the acknowledgement must not complete the callback", callback.isDone()); + + client.onMessage(reply()); + assertArrayEquals(RESULT.toArray(), callback.get(5, TimeUnit.SECONDS)); + } + + @Test + public void testAcknowledgementWithoutDecoupledDestination() throws Exception { + // without a decoupled endpoint, the acknowledgement is the response + Message ack = acknowledgement(); + client.onMessage(ack); + assertEquals(Boolean.TRUE, exchange.get(ClientImpl.FINISHED)); + assertSame(ack, exchange.getInMessage()); + } + + @Test + public void testAcknowledgementOfAsyncOneWayCall() throws Exception { + // a one-way call gets no reply on the decoupled endpoint: the acknowledgement completes it + decoupled(); + exchange.setOneWay(true); + ClientCallback callback = new ClientCallback(); + exchange.put(ClientCallback.class, callback); + client.onMessage(acknowledgement()); + assertEquals(Boolean.TRUE, exchange.get(ClientImpl.FINISHED)); + assertNull(callback.get(5, TimeUnit.SECONDS)); + } + + @Test + public void testSoapFaultArrivingWhileAsyncFailureIsResolved() throws Exception { + // a partial response with a SOAP fault becomes the exchange's in fault message after the reply + // was found to be the exchange's in message, but before getException(Exchange) reads the exchange + AtomicBoolean arrived = new AtomicBoolean(); + client = new ClientImpl(bus, endpoint, selector) { + @Override + protected Exception getException(Exchange e) { + if (arrived.compareAndSet(false, true)) { + Message partial = partialResponse(); + partial.setContent(Exception.class, new IllegalStateException("late partial fault")); + e.setInMessage(null); + e.setInFaultMessage(partial); + } + return super.getException(e); + } + }; + ClientCallback callback = new ClientCallback(); + exchange.put(ClientCallback.class, callback); + client.onMessage(reply()); + assertArrayEquals(RESULT.toArray(), callback.get(5, TimeUnit.SECONDS)); + } + + @Test + public void testPartialResponseArrivingWhileAsyncFailureIsResolved() throws Exception { + // a partial response with a failure becomes the exchange's in message after the reply was found + // to be the exchange's in message, but before getException(Exchange) reads the exchange + AtomicBoolean arrived = new AtomicBoolean(); + client = new ClientImpl(bus, endpoint, selector) { + @Override + protected Exception getException(Exchange e) { + if (arrived.compareAndSet(false, true)) { + Message partial = partialResponse(); + partial.setContent(Exception.class, new IllegalStateException("late partial failure")); + e.setInMessage(partial); + } + return super.getException(e); + } + }; + ClientCallback callback = new ClientCallback(); + exchange.put(ClientCallback.class, callback); + client.onMessage(reply()); + assertArrayEquals(RESULT.toArray(), callback.get(5, TimeUnit.SECONDS)); + } + + @Test + public void testPartialResponseArrivingWhileAsyncFailureIsResolvedKeepsTheFailure() throws Exception { + // the reply failed, and a partial response without a failure replaces it as the exchange's in + // message before getException(Exchange) reads the exchange + IllegalStateException failure = new IllegalStateException("reply failure"); + AtomicBoolean arrived = new AtomicBoolean(); + client = new ClientImpl(bus, endpoint, selector) { + @Override + protected Exception getException(Exchange e) { + if (arrived.compareAndSet(false, true)) { + e.setInMessage(partialResponse()); + } + return super.getException(e); + } + }; + ClientCallback callback = new ClientCallback(); + exchange.put(ClientCallback.class, callback); + Message reply = reply(); + reply.setContent(Exception.class, failure); + client.onMessage(reply); + ExecutionException ex = assertThrows(ExecutionException.class, + () -> callback.get(5, TimeUnit.SECONDS)); + assertSame(failure, ex.getCause()); + } + + @Test + public void testSoapFaultPartialResponseAfterReply() throws Exception { + // as CheckFaultInterceptor does for a SOAP fault: the in fault chain makes the message the + // in fault message of the exchange, and clears its in message + IllegalStateException lateFault = new IllegalStateException("late partial fault"); + client.getInInterceptors().add(new AbstractPhaseInterceptor<Message>(Phase.POST_PROTOCOL) { + public void handleMessage(Message message) { + if (Boolean.TRUE.equals(message.get(Message.PARTIAL_RESPONSE_MESSAGE))) { + message.getInterceptorChain().abort(); + message.setContent(Exception.class, lateFault); + new InFaultChainInitiatorObserver(bus).onMessage(message); + } + } + }); + Message reply = reply(); + client.onMessage(reply); + client.onMessage(partialResponse()); + + // what a synchronous call returns once the reply has finished the exchange + assertArrayEquals(RESULT.toArray(), client.processResult(outMessage, exchange, null, null)); + } + + @Test + public void testOverriddenGetExceptionForAsyncCalls() throws Exception { + IllegalStateException failure = new IllegalStateException("from the subclass"); + client = new ClientImpl(bus, endpoint, selector) { + @Override + protected Exception getException(Exchange e) { + return failure; + } + }; + ClientCallback callback = new ClientCallback(); + exchange.put(ClientCallback.class, callback); + client.onMessage(reply()); + ExecutionException ex = assertThrows(ExecutionException.class, + () -> callback.get(5, TimeUnit.SECONDS)); + assertSame(failure, ex.getCause()); + } + + private void decoupled() { + EndpointInfo info = new EndpointInfo(); + info.setProperty("org.apache.cxf.ws.addressing.MAPAggregator.decoupledDestination", new Object()); + when(endpoint.getEndpointInfo()).thenReturn(info); + } + + private void assertReplyKept(Message reply) throws Exception { + assertSame(reply, exchange.getInMessage()); + assertEquals(Boolean.TRUE, exchange.get(ClientImpl.FINISHED)); + // what a synchronous call returns once the reply has finished the exchange + assertArrayEquals(RESULT.toArray(), client.processResult(outMessage, exchange, null, null)); + } + + private Message reply() { + Message reply = inbound(); + reply.setContent(List.class, RESULT); + return reply; + } + + private Message partialResponse() { + Message partial = inbound(); + partial.put(Message.PARTIAL_RESPONSE_MESSAGE, Boolean.TRUE); + return partial; + } + + private Message acknowledgement() { + Message ack = inbound(); + ack.put(Message.RESPONSE_CODE, 202); + return ack; + } + + private Message inbound() { + Message m = new MessageImpl(); + m.setExchange(exchange); + return m; + } +}
