cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: [CXF-7262] ClientConfig lost when using templates in the path method, patch from Andy McCright is applied with minor updates, This closes #240
Date Mon, 06 Mar 2017 12:38:59 GMT
Repository: cxf
Updated Branches:
  refs/heads/master cbd4e7fa6 -> b1c6125f5


[CXF-7262] ClientConfig lost when using templates in the path method, patch from Andy McCright
is applied with minor updates, This closes #240


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/b1c6125f
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/b1c6125f
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/b1c6125f

Branch: refs/heads/master
Commit: b1c6125f50b294b0e2ebc927671e2ff53b5001bf
Parents: cbd4e7f
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Mon Mar 6 12:38:38 2017 +0000
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Mon Mar 6 12:38:38 2017 +0000

----------------------------------------------------------------------
 .../cxf/jaxrs/client/spec/ClientImpl.java       |  15 +--
 .../cxf/jaxrs/client/spec/ClientImplTest.java   | 130 +++++++++++++++++++
 .../jaxrs/JAXRS20ClientServerBookTest.java      |  16 +++
 3 files changed, 150 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/b1c6125f/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/spec/ClientImpl.java
----------------------------------------------------------------------
diff --git a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/spec/ClientImpl.java b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/spec/ClientImpl.java
index e88a76f..ec20e6c 100644
--- a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/spec/ClientImpl.java
+++ b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/spec/ClientImpl.java
@@ -462,19 +462,12 @@ public class ClientImpl implements Client {
         }
 
         private WebTarget newWebTarget(UriBuilder newBuilder) {
-            boolean complete = false;
+            WebClient newClient;
             if (targetClient != null) {
-                try {
-                    newBuilder.build();
-                    complete = true;
-                } catch (IllegalArgumentException ex) {
-                    //the builder still has unresolved vars
-                }
-            }
-            if (!complete) {
-                return new WebTargetImpl(newBuilder, getConfiguration());
+                newClient = WebClient.fromClient(targetClient);
+            } else {
+                newClient = null;
             }
-            WebClient newClient = WebClient.fromClient(targetClient);
             return new WebTargetImpl(newBuilder, getConfiguration(), newClient);
         }
 

http://git-wip-us.apache.org/repos/asf/cxf/blob/b1c6125f/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/spec/ClientImplTest.java
----------------------------------------------------------------------
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
new file mode 100644
index 0000000..c2f1857
--- /dev/null
+++ b/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/spec/ClientImplTest.java
@@ -0,0 +1,130 @@
+/**
+ * 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.jaxrs.client.spec;
+
+import java.util.Arrays;
+
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.WebTarget;
+
+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.message.Message;
+
+import org.junit.Assert;
+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() {
+            return MY_INTERCEPTOR_NAME;
+        }
+        @Override
+        public void handleMessage(Message message) throws Fault {
+            // no-op
+            
+        }
+
+        @Override
+        public void handleFault(Message message) {
+            // no-op
+            
+        }
+    }
+    
+    /**
+     * This test checks that we do not lose track of registered interceptors
+     * on the original client implementation after we create a new impl with
+     * the path(...) method - particularly when the path passed in to the
+     * path(...) method contains a template.
+     */
+    @Test
+    public void testClientConfigCopiedOnPathCallWithTemplates() {
+        Client client = ClientBuilder.newClient();
+        WebTarget webTarget = client.target("http://localhost:8080/");
+        WebClient webClient = getWebClient(webTarget);
+        
+        ClientConfiguration clientConfig = WebClient.getConfig(webClient);
+        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;
+        WebClient webClient = webTargetImpl.getWebClient();
+        assertNotNull("No WebClient is associated with this WebTargetImpl", webClient);
+        return webClient;
+    }
+    
+    private boolean doesClientConfigHaveMyInterceptor(WebClient webClient) {
+        ClientConfiguration clientConfigAfterPath = WebClient.getConfig(webClient);
+        boolean foundMyInterceptor = false;
+        for (Interceptor<?> i : clientConfigAfterPath.getOutInterceptors()) {
+            if (MY_INTERCEPTOR_NAME.equals(i.toString())) {
+                foundMyInterceptor = true;
+                break;
+            }
+        }
+        return foundMyInterceptor;
+    }
+    
+    /**
+     * Similar to <code>testClientConfigCopiedOnPathCallWithTemplates</code>,
+     * this test uses a template, but in the initial call to target().  At this
+     * point, the WebTargetImpl's targetClient field will be null, so we need
+     * this test to ensure that there are no null pointers when creating and
+     * using a template on the first call to target().
+     */
+    @Test
+    public void testTemplateInInitialTarget() {
+        String address = "http://localhost:8080/bookstore/{a}/simple";
+        Client client = ClientBuilder.newClient();
+        WebTarget webTarget = client.target(address).resolveTemplate("a", "bookheaders");
+        webTarget.request("application/xml").header("a", "b");
+        WebClient webClient = getWebClient(webTarget);
+        
+        ClientConfiguration clientConfig = WebClient.getConfig(webClient);
+        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));
+
+    }
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/b1c6125f/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
index e4eb762..d3b63af 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
@@ -150,6 +150,22 @@ public class JAXRS20ClientServerBookTest extends AbstractBusClientServerTestBase
     }
 
     @Test
+    public void testGetBookSpecTemplate() {
+        String address = "http://localhost:" + PORT + "/bookstore/{a}";
+        Client client = ClientBuilder.newClient();
+        client.register((Object)ClientFilterClientAndConfigCheck.class);
+        client.register(new BTypeParamConverterProvider());
+        client.property("clientproperty", "somevalue");
+        WebTarget webTarget = client.target(address).path("{b}")
+            .resolveTemplate("a", "bookheaders").resolveTemplate("b", "simple");
+        Invocation.Builder builder = webTarget.request("application/xml").header("a", new
BType());
+
+        Response r = builder.get();
+        Book book = r.readEntity(Book.class);
+        assertEquals(124L, book.getId());
+        assertEquals("b", r.getHeaderString("a"));
+    }
+    @Test
     public void testGetBookSpec() {
         String address = "http://localhost:" + PORT + "/bookstore/bookheaders/simple";
         Client client = ClientBuilder.newClient();


Mime
View raw message