You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by on...@apache.org on 2019/02/13 07:02:14 UTC

[camel] branch camel-2.x updated: CAMEL-13037 - SimpleBinding ignores annotations on interface, Thanks Jens Kleine-Herzbruch

This is an automated email from the ASF dual-hosted git repository.

onders pushed a commit to branch camel-2.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-2.x by this push:
     new 3e7fe40  CAMEL-13037 - SimpleBinding ignores annotations on interface, Thanks Jens Kleine-Herzbruch
3e7fe40 is described below

commit 3e7fe40534ed2b2f228ab02d6593dcac397ddd30
Author: onders <on...@apache.org>
AuthorDate: Tue Feb 12 19:15:21 2019 +0300

    CAMEL-13037 - SimpleBinding ignores annotations on interface, Thanks Jens Kleine-Herzbruch
---
 .../component/cxf/jaxrs/SimpleCxfRsBinding.java    |   2 +
 .../CxfRsConsumerSimpleBindingImplTest.java        | 120 +++++++++++++++++++++
 .../simplebinding/testbean/CustomerService.java    |  59 ++++++++++
 .../testbean/CustomerServiceImpl.java              |  67 ++++++++++++
 4 files changed, 248 insertions(+)

diff --git a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/SimpleCxfRsBinding.java b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/SimpleCxfRsBinding.java
index e30471a..69c6cab 100644
--- a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/SimpleCxfRsBinding.java
+++ b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/SimpleCxfRsBinding.java
@@ -49,6 +49,7 @@ import org.apache.cxf.jaxrs.ext.multipart.Attachment;
 import org.apache.cxf.jaxrs.ext.multipart.InputStreamDataSource;
 import org.apache.cxf.jaxrs.ext.multipart.Multipart;
 import org.apache.cxf.jaxrs.model.URITemplate;
+import org.apache.cxf.jaxrs.utils.AnnotationUtils;
 import org.apache.cxf.message.Exchange;
 import org.apache.cxf.message.MessageContentsList;
 
@@ -319,6 +320,7 @@ public class SimpleCxfRsBinding extends DefaultCxfRsBinding {
          * @return A MethodSpec instance representing the method metadata relevant to the Camel binding process.
          */
         public static MethodSpec fromMethod(Method method) {
+            method = AnnotationUtils.getAnnotatedMethod(method.getDeclaringClass(), method);
             MethodSpec answer = new MethodSpec();
             
             Annotation[][] annotations = method.getParameterAnnotations();
diff --git a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/simplebinding/CxfRsConsumerSimpleBindingImplTest.java b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/simplebinding/CxfRsConsumerSimpleBindingImplTest.java
new file mode 100644
index 0000000..38881a1
--- /dev/null
+++ b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/simplebinding/CxfRsConsumerSimpleBindingImplTest.java
@@ -0,0 +1,120 @@
+/**
+ * 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.camel.component.cxf.jaxrs.simplebinding;
+
+import java.io.StringWriter;
+
+import javax.xml.bind.JAXBContext;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.cxf.CXFTestSupport;
+import org.apache.camel.component.cxf.jaxrs.simplebinding.testbean.Customer;
+import org.apache.camel.component.cxf.jaxrs.simplebinding.testbean.CustomerList;
+import org.apache.camel.component.cxf.jaxrs.simplebinding.testbean.Order;
+import org.apache.camel.component.cxf.jaxrs.simplebinding.testbean.Product;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Tests for the Simple Binding style of CXF JAX-RS consumers.
+ */
+public class CxfRsConsumerSimpleBindingImplTest extends CamelTestSupport {
+    private static final String PORT_PATH = CXFTestSupport.getPort1() + "/CxfRsConsumerTest";
+    private static final String CXF_RS_ENDPOINT_URI = "cxfrs://http://localhost:" + PORT_PATH
+        + "/rest?resourceClasses=org.apache.camel.component.cxf.jaxrs.simplebinding.testbean.CustomerServiceImpl&bindingStyle=SimpleConsumer";
+
+    private JAXBContext jaxb;
+    private CloseableHttpClient httpclient;
+
+    @Override
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+        httpclient = HttpClientBuilder.create().build();
+        jaxb = JAXBContext.newInstance(CustomerList.class, Customer.class, Order.class, Product.class);
+    }
+
+    @Override
+    @After
+    public void tearDown() throws Exception {
+        super.tearDown();
+        httpclient.close();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from(CXF_RS_ENDPOINT_URI)
+                    .recipientList(simple("direct:${header.operationName}"));
+
+                from("direct:getCustomer").process(new Processor() {
+                    @Override
+                    public void process(Exchange exchange) throws Exception {
+                        assertEquals("123", exchange.getIn().getHeader("id"));
+                        exchange.getOut().setBody(new Customer(123, "Raul"));
+                        exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 200);
+                    }
+                });
+
+                from("direct:newCustomer").process(new Processor() {
+                    @Override
+                    public void process(Exchange exchange) throws Exception {
+                        Customer c = exchange.getIn().getBody(Customer.class);
+                        assertNotNull(c);
+                        assertEquals(123, c.getId());
+                        assertEquals(12, exchange.getIn().getHeader("age"));
+                        exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 200);
+                    }
+                });
+            }
+        };
+    }
+
+    @Test
+    public void testGetCustomerOnlyHeaders() throws Exception {
+        HttpGet get = new HttpGet("http://localhost:" + PORT_PATH + "/rest/customerservice/customers/123");
+        get.addHeader("Accept", "text/xml");
+        HttpResponse response = httpclient.execute(get);
+        assertEquals(200, response.getStatusLine().getStatusCode());
+        Customer entity = (Customer) jaxb.createUnmarshaller().unmarshal(response.getEntity().getContent());
+        assertEquals(123, entity.getId());
+    }
+
+    @Test
+    public void testNewCustomerWithQueryParam() throws Exception {
+        HttpPost post = new HttpPost("http://localhost:" + PORT_PATH + "/rest/customerservice/customers?age=12");
+        StringWriter sw = new StringWriter();
+        jaxb.createMarshaller().marshal(new Customer(123, "Raul"), sw);
+        post.setEntity(new StringEntity(sw.toString()));
+        post.addHeader("Content-Type", "text/xml");
+        post.addHeader("Accept", "text/xml");
+        HttpResponse response = httpclient.execute(post);
+        assertEquals(200, response.getStatusLine().getStatusCode());
+    }
+}
diff --git a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/simplebinding/testbean/CustomerService.java b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/simplebinding/testbean/CustomerService.java
new file mode 100644
index 0000000..d6f8e48
--- /dev/null
+++ b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/simplebinding/testbean/CustomerService.java
@@ -0,0 +1,59 @@
+/**
+ * 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.camel.component.cxf.jaxrs.simplebinding.testbean;
+
+import java.io.InputStream;
+
+import javax.activation.DataHandler;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.Response;
+
+import org.apache.camel.component.cxf.jaxrs.testbean.Customer;
+
+@Path("/customerservice/")
+public interface CustomerService {
+
+    @GET @Path("/customers/{id}/")
+    Customer getCustomer(@PathParam("id") String id, @QueryParam("test") String test);
+
+    @PUT @Path("/customers/{id}")
+    Response updateCustomer(Customer customer, @PathParam("id") String id);
+
+    @POST @Path("/customers/")
+    Response newCustomer(Customer customer, @PathParam("type") String type, @QueryParam("age") int age);
+
+    @Path("/customers/vip/{status}")
+    VipCustomerResource vipCustomer(@PathParam("status") String status);
+
+    @Consumes("image/jpeg")
+    @POST @Path("/customers/{id}/image_inputstream")
+    Response uploadImageInputStream(InputStream is);
+
+    @Consumes("image/jpeg")
+    @POST @Path("/customers/{id}/image_datahandler")
+    Response uploadImageDataHandler(DataHandler dh);
+
+    @Path("/customers/multipart")
+    MultipartCustomer multipart();
+
+}
diff --git a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/simplebinding/testbean/CustomerServiceImpl.java b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/simplebinding/testbean/CustomerServiceImpl.java
new file mode 100644
index 0000000..fdd893f
--- /dev/null
+++ b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/simplebinding/testbean/CustomerServiceImpl.java
@@ -0,0 +1,67 @@
+/**
+ * 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.camel.component.cxf.jaxrs.simplebinding.testbean;
+
+import java.io.InputStream;
+
+import javax.activation.DataHandler;
+import javax.ws.rs.core.Response;
+
+import org.apache.camel.component.cxf.jaxrs.testbean.Customer;
+
+public class CustomerServiceImpl implements CustomerService {
+
+    public Customer getCustomer(String id) {
+        return null;
+    }
+
+    @Override
+    public Response updateCustomer(Customer customer, String id) {
+        return null;
+    }
+
+    @Override
+    public Response newCustomer(Customer customer, String type, int age) {
+        return null;
+    }
+
+    @Override
+    public VipCustomerResource vipCustomer(String status) {
+        return new VipCustomerResource();
+    }
+
+    @Override
+    public Response uploadImageInputStream(InputStream is) {
+        return null;
+    }
+
+    @Override
+    public Response uploadImageDataHandler(DataHandler dh) {
+        return null;
+    }
+
+    @Override
+    public MultipartCustomer multipart() {
+        return new MultipartCustomer();
+    }
+
+    @Override
+    public Customer getCustomer(String id, String test) {
+        return null;
+    }
+
+}