davsclaus commented on a change in pull request #3777:
URL: https://github.com/apache/camel/pull/3777#discussion_r418445078



##########
File path: 
components/camel-resteasy/src/main/java/org/apache/camel/component/resteasy/servlet/ResteasyServletInputStreamCopier.java
##########
@@ -0,0 +1,70 @@
+/*
+ * 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.resteasy.servlet;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.servlet.ReadListener;
+import javax.servlet.ServletInputStream;
+
+/**
+ * Class for copying input stream from HttpRequest
+ */
+public class ResteasyServletInputStreamCopier extends ServletInputStream {
+    
+    private InputStream input;
+    private ByteArrayOutputStream copy;
+
+    public ResteasyServletInputStreamCopier(InputStream inputStream) {
+        /* create a new input stream from the cached request body */
+        this.input = inputStream;
+        this.copy = new ByteArrayOutputStream();

Review comment:
       This will read the entire stream into memory.
   
   Camel has a stream caching feature. Maybe we should revisit this later.

##########
File path: 
components/camel-resteasy/src/main/java/org/apache/camel/component/resteasy/DefaultResteasyHttpBinding.java
##########
@@ -0,0 +1,218 @@
+/*
+ * 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.resteasy;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.client.Invocation;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.Response;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.spi.HeaderFilterStrategy;
+import org.apache.camel.support.MessageHelper;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.commons.lang3.exception.ExceptionUtils;

Review comment:
       Can you avoid using this - Camel has api for working with exceptions. We 
should avoid adding a 3rd party JAR just to use a single api which we have 
ourselves or easily can write a few lines of code.

##########
File path: 
components/camel-resteasy/src/main/java/org/apache/camel/component/resteasy/DefaultResteasyHttpBinding.java
##########
@@ -0,0 +1,219 @@
+/*
+ * 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.resteasy;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.client.Invocation;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.Response;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.spi.HeaderFilterStrategy;
+import org.apache.camel.support.MessageHelper;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.commons.lang3.exception.ExceptionUtils;
+import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
+import org.jboss.resteasy.client.jaxrs.internal.BasicAuthentication;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The default Resteasy binding implementation
+ */
+public class DefaultResteasyHttpBinding implements ResteasyHttpBinding {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(DefaultResteasyHttpBinding.class);
+
+    private HeaderFilterStrategy headerFilterStrategy;
+
+    public HeaderFilterStrategy getHeaderFilterStrategy() {
+        return headerFilterStrategy;
+    }
+
+    @Override
+    public void setHeaderFilterStrategy(HeaderFilterStrategy 
headerFilterStrategy) {
+        this.headerFilterStrategy = headerFilterStrategy;
+    }
+
+    @Override
+    public Response populateResteasyRequestFromExchangeAndExecute(String uri, 
Exchange exchange, Map<String, String> parameters) {
+        Client client = ClientBuilder.newBuilder().build();
+        String body = exchange.getIn().getBody(String.class);
+
+        LOG.debug("Body in producer: " + body);
+
+        String mediaType = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, 
String.class);
+
+        WebTarget target = client.target(uri);
+
+        LOG.debug("Populate Resteasy request from exchange body: {} using 
media type {}", body, mediaType);
+
+        Invocation.Builder builder;
+        if (mediaType != null) {
+            builder = target.request(mediaType);
+        } else {
+            builder = target.request();
+        }
+
+
+        for (Map.Entry<String, Object> entry : 
exchange.getIn().getHeaders().entrySet()) {
+            String key = entry.getKey();
+            Object value = entry.getValue();
+            if (headerFilterStrategy != null
+                    && !headerFilterStrategy.applyFilterToCamelHeaders(key, 
value, exchange)) {
+                builder.header(key, value);
+                LOG.debug("Populate Resteasy request from exchange header: {} 
value: {}", key, value);
+            }
+        }
+
+        if (parameters.get("username") != null && parameters.get("password") 
!= null) {
+            target.register(new 
BasicAuthentication(parameters.get("username"), parameters.get("password")));
+        }
+        LOG.debug("Basic authentication was applied");
+        String method = parameters.get("method");
+
+        if (method.equals("GET")) {
+            return builder.get();
+        }
+        if (method.equals("POST")) {
+            return  builder.post(Entity.entity(body, mediaType));
+        }
+        if (method.equals("PUT")) {
+            return  builder.put(Entity.entity(body, mediaType));
+        }
+        if (method.equals("DELETE")) {
+            return  builder.delete();
+        }
+        if (method.equals("OPTIONS")) {
+            return  builder.options();
+        }
+        if (method.equals("TRACE")) {
+            return  builder.trace();
+        }
+        if (method.equals("HEAD")) {
+            return  builder.head();
+        }
+
+        // maybe throw exception because not method was correct
+        throw new IllegalArgumentException("Method '" + method + "' is not 
supported method");
+    }
+
+
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    @Override
+    public void populateProxyResteasyRequestAndExecute(String uri, Exchange 
exchange, Map<String, String> parameters) {
+        Client client = ClientBuilder.newBuilder().build();
+
+        WebTarget target = client.target(uri);
+
+        if (parameters.get("username") != null && parameters.get("password") 
!= null) {
+            target.register(new 
BasicAuthentication(parameters.get("username"), parameters.get("password")));
+        }
+
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("Basic authentication was applied");
+        }
+
+        Class realClazz;
+        Object object = null;
+        try {
+            realClazz = Class.forName(parameters.get("proxyClassName"));

Review comment:
       So is this mandatory? Do I really need to write a class with annotations 
to call a remote HTTP REST service? 
   
   

##########
File path: components/camel-resteasy/pom.xml
##########
@@ -0,0 +1,287 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+    xmlns="http://maven.apache.org/POM/4.0.0";
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.apache.camel</groupId>
+        <artifactId>components</artifactId>
+        <version>3.3.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>camel-resteasy</artifactId>
+    <packaging>jar</packaging>
+    <name>Camel :: Resteasy</name>
+    <description>Camel Resteasy support</description>
+
+    <dependencyManagement>
+        <dependencies>
+            <dependency>
+                <groupId>org.jboss.arquillian</groupId>
+                <artifactId>arquillian-bom</artifactId>
+                <version>${arquillian-version}</version>
+                <scope>import</scope>
+                <type>pom</type>
+            </dependency>
+            <dependency>
+                <groupId>org.jboss.resteasy</groupId>
+                <artifactId>resteasy-bom</artifactId>
+                <version>${resteasy-version}</version>
+                <type>pom</type>
+                <scope>import</scope>
+            </dependency>
+            <dependency>
+                <groupId>org.jboss.resteasy</groupId>
+                <artifactId>resteasy-core</artifactId>
+                <version>${resteasy-version}</version>
+            </dependency>
+            <dependency>
+                <groupId>javax.servlet</groupId>
+                <artifactId>javax.servlet-api</artifactId>
+                <version>${javax-servlet-api-version}</version>
+            </dependency>
+        </dependencies>
+    </dependencyManagement>
+
+    <dependencies>
+        <!-- resteasy dependencies -->
+        <dependency>
+            <groupId>org.jboss.resteasy</groupId>
+            <artifactId>resteasy-core-spi</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.jboss.resteasy</groupId>
+            <artifactId>resteasy-spring</artifactId>

Review comment:
       What do you use spring for?

##########
File path: components/camel-resteasy/pom.xml
##########
@@ -0,0 +1,287 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+    xmlns="http://maven.apache.org/POM/4.0.0";
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.apache.camel</groupId>
+        <artifactId>components</artifactId>
+        <version>3.3.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>camel-resteasy</artifactId>
+    <packaging>jar</packaging>
+    <name>Camel :: Resteasy</name>
+    <description>Camel Resteasy support</description>
+
+    <dependencyManagement>
+        <dependencies>
+            <dependency>
+                <groupId>org.jboss.arquillian</groupId>
+                <artifactId>arquillian-bom</artifactId>

Review comment:
       Its a bit shame to use arquillian for testing but its good for those WAR 
archives. But the project is dead just mind that.

##########
File path: 
components/camel-resteasy/src/main/java/org/apache/camel/component/resteasy/DefaultResteasyHttpBinding.java
##########
@@ -0,0 +1,219 @@
+/*
+ * 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.resteasy;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.client.Invocation;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.Response;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.spi.HeaderFilterStrategy;
+import org.apache.camel.support.MessageHelper;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.commons.lang3.exception.ExceptionUtils;
+import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
+import org.jboss.resteasy.client.jaxrs.internal.BasicAuthentication;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The default Resteasy binding implementation
+ */
+public class DefaultResteasyHttpBinding implements ResteasyHttpBinding {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(DefaultResteasyHttpBinding.class);
+
+    private HeaderFilterStrategy headerFilterStrategy;
+
+    public HeaderFilterStrategy getHeaderFilterStrategy() {
+        return headerFilterStrategy;
+    }
+
+    @Override
+    public void setHeaderFilterStrategy(HeaderFilterStrategy 
headerFilterStrategy) {
+        this.headerFilterStrategy = headerFilterStrategy;
+    }
+
+    @Override
+    public Response populateResteasyRequestFromExchangeAndExecute(String uri, 
Exchange exchange, Map<String, String> parameters) {
+        Client client = ClientBuilder.newBuilder().build();
+        String body = exchange.getIn().getBody(String.class);
+
+        LOG.debug("Body in producer: " + body);
+
+        String mediaType = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, 
String.class);
+
+        WebTarget target = client.target(uri);
+
+        LOG.debug("Populate Resteasy request from exchange body: {} using 
media type {}", body, mediaType);
+
+        Invocation.Builder builder;
+        if (mediaType != null) {
+            builder = target.request(mediaType);
+        } else {
+            builder = target.request();
+        }
+
+
+        for (Map.Entry<String, Object> entry : 
exchange.getIn().getHeaders().entrySet()) {
+            String key = entry.getKey();
+            Object value = entry.getValue();
+            if (headerFilterStrategy != null
+                    && !headerFilterStrategy.applyFilterToCamelHeaders(key, 
value, exchange)) {
+                builder.header(key, value);
+                LOG.debug("Populate Resteasy request from exchange header: {} 
value: {}", key, value);
+            }
+        }
+
+        if (parameters.get("username") != null && parameters.get("password") 
!= null) {
+            target.register(new 
BasicAuthentication(parameters.get("username"), parameters.get("password")));
+        }
+        LOG.debug("Basic authentication was applied");
+        String method = parameters.get("method");
+
+        if (method.equals("GET")) {
+            return builder.get();
+        }
+        if (method.equals("POST")) {
+            return  builder.post(Entity.entity(body, mediaType));
+        }
+        if (method.equals("PUT")) {
+            return  builder.put(Entity.entity(body, mediaType));
+        }
+        if (method.equals("DELETE")) {
+            return  builder.delete();
+        }
+        if (method.equals("OPTIONS")) {
+            return  builder.options();
+        }
+        if (method.equals("TRACE")) {
+            return  builder.trace();
+        }
+        if (method.equals("HEAD")) {
+            return  builder.head();
+        }
+
+        // maybe throw exception because not method was correct
+        throw new IllegalArgumentException("Method '" + method + "' is not 
supported method");
+    }
+
+
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    @Override
+    public void populateProxyResteasyRequestAndExecute(String uri, Exchange 
exchange, Map<String, String> parameters) {
+        Client client = ClientBuilder.newBuilder().build();
+
+        WebTarget target = client.target(uri);
+
+        if (parameters.get("username") != null && parameters.get("password") 
!= null) {
+            target.register(new 
BasicAuthentication(parameters.get("username"), parameters.get("password")));

Review comment:
       You just assume username/password requires basic auth. But they may just 
be query parameters for something else. Instead we should have an option to 
turn on authentication first. And so only do this if that option is turned on.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to