[cxf] branch master updated: Handle null contracts

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[cxf] branch master updated: Handle null contracts

amccright
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 <[hidden email]>
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<C extends Configurable<C>> implements Configurable<C> {
+    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<C extends Configurable<C>> 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<C extends Configurable<C>> 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<Message> {
         @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 <code>testClientConfigCopiedOnPathCallWithTemplates</code>,
      * 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<String> 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
[hidden email].