This is an automated email from the ASF dual-hosted git repository. reta pushed a commit to branch 3.6.x-fixes in repository https://gitbox.apache.org/repos/asf/cxf.git
commit 840bd0a46293ceda5c4920678f4eaa7dfacc543a Author: Konrad Windszus <[email protected]> AuthorDate: Sun Jun 26 19:55:58 2022 +0200 CXF-8725: Optionally don't set (#964) "org.apache.cxf.transport.service_not_available" for specific HTTP response codes --- .../java/org/apache/cxf/message/MessageUtils.java | 20 ++++++++++++++++++++ .../org/apache/cxf/message/MessageUtilsTest.java | 12 +++++++++++- .../org/apache/cxf/transport/http/HTTPConduit.java | 13 ++++++++++++- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/apache/cxf/message/MessageUtils.java b/core/src/main/java/org/apache/cxf/message/MessageUtils.java index e7f75233c5..38d29b5708 100644 --- a/core/src/main/java/org/apache/cxf/message/MessageUtils.java +++ b/core/src/main/java/org/apache/cxf/message/MessageUtils.java @@ -21,6 +21,8 @@ package org.apache.cxf.message; import java.lang.reflect.Method; import java.net.HttpURLConnection; +import java.util.ArrayList; +import java.util.Collection; import java.util.Optional; import java.util.logging.Logger; @@ -153,6 +155,24 @@ public final class MessageUtils { return defaultValue; } + public static Collection<Integer> getContextualIntegers(Message m, String key, Collection<Integer> defaultValue) { + if (m != null) { + Object o = m.getContextualProperty(key); + if (o instanceof String) { + Collection<Integer> intValues = new ArrayList<>(); + for (String value : ((String) o).split(",")) { + try { + intValues.add(Integer.parseInt(value)); + } catch (NumberFormatException ex) { + LOG.warning("Incorrect integer value of " + value + " specified for: " + key); + } + } + return intValues; + } + } + return defaultValue; + } + public static int getContextualInteger(Message m, String key, int defaultValue) { if (m != null) { Object o = m.getContextualProperty(key); diff --git a/core/src/test/java/org/apache/cxf/message/MessageUtilsTest.java b/core/src/test/java/org/apache/cxf/message/MessageUtilsTest.java index 298aaa54ab..65a09d8935 100644 --- a/core/src/test/java/org/apache/cxf/message/MessageUtilsTest.java +++ b/core/src/test/java/org/apache/cxf/message/MessageUtilsTest.java @@ -19,6 +19,7 @@ package org.apache.cxf.message; import java.lang.reflect.Method; +import java.util.List; import java.util.Optional; import javax.xml.namespace.QName; @@ -29,7 +30,8 @@ import org.apache.cxf.service.factory.SimpleMethodDispatcher; import org.apache.cxf.service.invoker.MethodDispatcher; import org.apache.cxf.service.model.BindingOperationInfo; import org.apache.cxf.service.model.OperationInfo; - +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -80,4 +82,12 @@ public class MessageUtilsTest { assertFalse(MessageUtils.getTargetMethod(message).isPresent()); } + + @Test + public void getContextualIntegers() { + Message message = new MessageImpl(); + message.put("key1", "1,2,invalid,3"); + MatcherAssert.assertThat(MessageUtils.getContextualIntegers(message, "key1", List.of(0)), Matchers.contains(1,2,3)); + MatcherAssert.assertThat(MessageUtils.getContextualIntegers(message, "invalid-key", List.of(0, 1)), Matchers.contains(0,1)); + } } diff --git a/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HTTPConduit.java b/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HTTPConduit.java index 8db3a3a2e3..cf2517f6a6 100644 --- a/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HTTPConduit.java +++ b/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HTTPConduit.java @@ -32,6 +32,7 @@ import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.util.Arrays; +import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -164,6 +165,15 @@ public abstract class HTTPConduit public static final String PROCESS_FAULT_ON_HTTP_400 = "org.apache.cxf.transport.process_fault_on_http_400"; public static final String NO_IO_EXCEPTIONS = "org.apache.cxf.transport.no_io_exceptions"; + + /** + * The HTTP status codes as contextual property (comma-separated integers as String) on the outgoing {@link Message} which lead to + * setting {@code org.apache.cxf.transport.service_not_available} for all responses with those status codes. + * This is used e.g. by the {@code org.apache.cxf.clustering.FailoverTargetSelector} to determine if it should do the fail-over. + * Default: {@code 404,429,503} + */ + public static final String SERVICE_NOT_AVAILABLE_ON_HTTP_STATUS_CODES = "org.apache.cxf.transport.service_not_available_on_http_status_codes"; + /** * The Logger for this class. */ @@ -1600,7 +1610,8 @@ public abstract class HTTPConduit } if (exchange != null) { exchange.put(Message.RESPONSE_CODE, rc); - if (rc == 404 || rc == 503 || rc == 429) { + Collection<Integer> serviceNotAvailableOnHttpStatusCodes = MessageUtils.getContextualIntegers(outMessage, SERVICE_NOT_AVAILABLE_ON_HTTP_STATUS_CODES, List.of(404, 429, 503)); + if (serviceNotAvailableOnHttpStatusCodes.contains(rc)) { exchange.put("org.apache.cxf.transport.service_not_available", true); } }
