From commits-return-48637-archive-asf-public=cust-asf.ponee.io@cxf.apache.org Wed Feb 7 05:18:40 2018 Return-Path: X-Original-To: archive-asf-public@eu.ponee.io Delivered-To: archive-asf-public@eu.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by mx-eu-01.ponee.io (Postfix) with ESMTP id 0DECD180657 for ; Wed, 7 Feb 2018 05:18:40 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id F17B8160C5B; Wed, 7 Feb 2018 04:18:39 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 1CE0E160C45 for ; Wed, 7 Feb 2018 05:18:38 +0100 (CET) Received: (qmail 81341 invoked by uid 500); 7 Feb 2018 04:18:38 -0000 Mailing-List: contact commits-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list commits@cxf.apache.org Received: (qmail 81332 invoked by uid 99); 7 Feb 2018 04:18:38 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 07 Feb 2018 04:18:38 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id 2B64B82234; Wed, 7 Feb 2018 04:18:37 +0000 (UTC) Date: Wed, 07 Feb 2018 04:18:37 +0000 To: "commits@cxf.apache.org" Subject: [cxf] branch master updated: Handle null contracts MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Message-ID: <151797711709.24737.16674291143925305257@gitbox.apache.org> From: amccright@apache.org X-Git-Host: gitbox.apache.org X-Git-Repo: cxf X-Git-Refname: refs/heads/master X-Git-Reftype: branch X-Git-Oldrev: e1b76aacfda66cbb8e36485e9d8220babdc00ac1 X-Git-Newrev: 9e9da1ec63f5c30d99ceb6cd662fe89b789447dd X-Git-Rev: 9e9da1ec63f5c30d99ceb6cd662fe89b789447dd X-Git-NotificationType: ref_changed_plus_diff X-Git-Multimail-Version: 1.5.dev Auto-Submitted: auto-generated This is an automated email from the ASF dual-hosted git repository. amccright pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/cxf.git The following commit(s) were added to refs/heads/master by this push: new 9e9da1e Handle null contracts 9e9da1e is described below commit 9e9da1ec63f5c30d99ceb6cd662fe89b789447dd Author: Andy McCright AuthorDate: Tue Feb 6 22:18:02 2018 -0600 Handle null contracts Per javadoc for Client.register(Class|Object, Class[]), the impl must handle null/empty class arrays - ignore the register call and should log a message. --- .../apache/cxf/jaxrs/impl/ConfigurableImpl.java | 9 ++- .../cxf/jaxrs/client/spec/ClientImplTest.java | 76 +++++++++++++++++++--- 2 files changed, 76 insertions(+), 9 deletions(-) diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ConfigurableImpl.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ConfigurableImpl.java index 200a597..3685a87 100644 --- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ConfigurableImpl.java +++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ConfigurableImpl.java @@ -20,6 +20,7 @@ package org.apache.cxf.jaxrs.impl; import java.util.Map; +import java.util.logging.Logger; import javax.ws.rs.Priorities; import javax.ws.rs.RuntimeType; @@ -27,9 +28,11 @@ import javax.ws.rs.core.Configurable; import javax.ws.rs.core.Configuration; import javax.ws.rs.core.Feature; +import org.apache.cxf.common.logging.LogUtils; import org.apache.cxf.jaxrs.utils.AnnotationUtils; public class ConfigurableImpl> implements Configurable { + private static final Logger LOG = LogUtils.getL7dLogger(ConfigurableImpl.class); private ConfigurationImpl config; private final C configurable; private final Class[] supportedProviderClasses; @@ -113,6 +116,10 @@ public class ConfigurableImpl> implements Configurable } private C doRegister(Object provider, int bindingPriority, Class... contracts) { + if (contracts == null || contracts.length == 0) { + LOG.warning("Null or empty contracts specified for " + provider + "; ignoring."); + return configurable; + } return doRegister(provider, ConfigurationImpl.initContractsMap(bindingPriority, contracts)); } @@ -125,6 +132,6 @@ public class ConfigurableImpl> implements Configurable return configurable; } config.register(provider, contracts); - return configurable; + return configurable; } } diff --git a/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/spec/ClientImplTest.java b/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/spec/ClientImplTest.java index c2f1857..f45c2ea 100644 --- a/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/spec/ClientImplTest.java +++ b/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/spec/ClientImplTest.java @@ -18,17 +18,23 @@ */ package org.apache.cxf.jaxrs.client.spec; +import java.util.ArrayList; import java.util.Arrays; +import java.util.List; +import java.util.logging.Handler; +import java.util.logging.LogRecord; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.WebTarget; +import org.apache.cxf.common.logging.LogUtils; import org.apache.cxf.interceptor.Fault; import org.apache.cxf.interceptor.Interceptor; import org.apache.cxf.jaxrs.client.ClientConfiguration; import org.apache.cxf.jaxrs.client.WebClient; import org.apache.cxf.jaxrs.client.spec.ClientImpl.WebTargetImpl; +import org.apache.cxf.jaxrs.impl.ConfigurableImpl; import org.apache.cxf.message.Message; import org.junit.Assert; @@ -37,7 +43,7 @@ import org.junit.Test; public class ClientImplTest extends Assert { private static final String MY_INTERCEPTOR_NAME = "MyInterceptor"; - + private static class MyInterceptor implements Interceptor { @Override public String toString() { @@ -55,7 +61,7 @@ public class ClientImplTest extends Assert { } } - + /** * This test checks that we do not lose track of registered interceptors * on the original client implementation after we create a new impl with @@ -72,15 +78,13 @@ public class ClientImplTest extends Assert { clientConfig.setOutInterceptors(Arrays.asList(new MyInterceptor())); assertTrue("Precondition failed - original WebTarget is missing expected interceptor", doesClientConfigHaveMyInterceptor(webClient)); - + WebTarget webTargetAfterPath = webTarget.path("/rest/{key}/").resolveTemplate("key", "myKey"); WebClient webClientAfterPath = getWebClient(webTargetAfterPath); assertTrue("New WebTarget is missing expected interceptor specified on 'parent' WebTarget's client impl", doesClientConfigHaveMyInterceptor(webClientAfterPath)); - - } - + private WebClient getWebClient(WebTarget webTarget) { webTarget.request(); WebTargetImpl webTargetImpl = (WebTargetImpl) webTarget; @@ -88,7 +92,7 @@ public class ClientImplTest extends Assert { assertNotNull("No WebClient is associated with this WebTargetImpl", webClient); return webClient; } - + private boolean doesClientConfigHaveMyInterceptor(WebClient webClient) { ClientConfiguration clientConfigAfterPath = WebClient.getConfig(webClient); boolean foundMyInterceptor = false; @@ -100,7 +104,7 @@ public class ClientImplTest extends Assert { } return foundMyInterceptor; } - + /** * Similar to testClientConfigCopiedOnPathCallWithTemplates, * this test uses a template, but in the initial call to target(). At this @@ -127,4 +131,60 @@ public class ClientImplTest extends Assert { doesClientConfigHaveMyInterceptor(webClientAfterPath)); } + + static class TestHandler extends Handler { + + List messages = new ArrayList<>(); + + /** {@inheritDoc}*/ + @Override + public void publish(LogRecord record) { + messages.add(record.getLevel().toString() + ": " + record.getMessage()); + } + + /** {@inheritDoc}*/ + @Override + public void flush() { + // no-op + } + + /** {@inheritDoc}*/ + @Override + public void close() throws SecurityException { + // no-op + } + } + @Test + public void testRegisterNullComponentClass() { + // Per register's javadoc, "Implementations MUST ignore attempts to register a component class for an empty + // or null collection of contracts via this method and SHOULD raise a warning about such event." + TestHandler handler = new TestHandler(); + LogUtils.getL7dLogger(ConfigurableImpl.class).addHandler(handler); + + ClientBuilder.newClient().register(MyInterceptor.class, (Class[]) null); + + for (String message : handler.messages) { + if (message.startsWith("WARN") && message.contains("Null or empty contracts")) { + return; // success + } + } + fail("did not log expected message"); + } + + @Test + public void testRegisterNullComponentObject() { + // Per register's javadoc, "Implementations MUST ignore attempts to register a component class for an empty + // or null collection of contracts via this method and SHOULD raise a warning about such event." + TestHandler handler = new TestHandler(); + LogUtils.getL7dLogger(ConfigurableImpl.class).addHandler(handler); + + ClientBuilder.newClient().register(new MyInterceptor(), (Class[]) null); + + for (String message : handler.messages) { + if (message.startsWith("WARN") && message.contains("Null or empty contracts")) { + return; // success + } + } + fail("did not log expected message"); + } } -- To stop receiving notification emails like this one, please contact amccright@apache.org.