davsclaus commented on a change in pull request #3777: URL: https://github.com/apache/camel/pull/3777#discussion_r415835535
########## File path: components/camel-resteasy/pom.xml ########## @@ -0,0 +1,313 @@ +<?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> + + <!-- + <properties> + <resteasy-version>4.4.1.Final</resteasy-version> + <version.org.eclipse.jetty>9.4.17.v20190418</version.org.eclipse.jetty> + <arquillian-version>1.4.1.Final</arquillian-version> + <shrinkwrap-resolver-version>2.2.6</shrinkwrap-resolver-version> + <shrinkwrap-version>1.2.6</shrinkwrap-version> + </properties> + --> + + <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> + </dependency> + <dependency> + <groupId>org.jboss.resteasy</groupId> + <artifactId>resteasy-servlet-initializer</artifactId> + </dependency> + <dependency> + <groupId>org.jboss.resteasy</groupId> + <artifactId>resteasy-client</artifactId> + </dependency> + <dependency> + <groupId>org.jboss.resteasy</groupId> + <artifactId>resteasy-validator-provider</artifactId> + </dependency> + + <!-- smx dependencies --> + <!--dependency> + <groupId>org.apache.servicemix.bundles</groupId> + <artifactId>org.apache.servicemix.bundles.reflections</artifactId> + <version>${reflections-bundle-version}</version> + </dependency--> + + <!-- camel dependencies --> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-support</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-http-common</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-http</artifactId> + </dependency> + + <!-- common dependencies --> + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-lang3</artifactId> + </dependency> + + <dependency> + <groupId>commons-io</groupId> + <artifactId>commons-io</artifactId> + </dependency> + + <!-- jboss jaxb/jaxrs dependencies needed by resteasy --> + <dependency> + <groupId>org.jboss.spec.javax.ws.rs</groupId> + <artifactId>jboss-jaxrs-api_2.1_spec</artifactId> + <version>${jboss-jaxrs-api_2.1_spec-version}</version> + </dependency> + <dependency> + <groupId>org.jboss.spec.javax.xml.bind</groupId> + <artifactId>jboss-jaxb-api_2.3_spec</artifactId> + <version>${jboss-jaxb-api_2.3_spec-version}</version> + </dependency> + <dependency> + <groupId>org.glassfish</groupId> + <artifactId>jakarta.el</artifactId> + <version>${jakarta.el-version}</version> + </dependency> + <dependency> Review comment: Can we avoid JAXB? Or at least get rid of this old clunky glassfish thingy. There are some newer JAXB JARs, see in core/camel-xml-jaxb. ########## 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); Review comment: This forces the body to be string based. Wonder if we should in the future support inputstream and byte[] too ########## File path: components/camel-resteasy/src/main/java/org/apache/camel/component/resteasy/servlet/ResteasyCamelServlet.java ########## @@ -0,0 +1,359 @@ +/* + * 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.IOException; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Proxy; +import java.util.Arrays; +import java.util.Collections; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.camel.Exchange; +import org.apache.camel.ExchangePattern; +import org.apache.camel.component.resteasy.DefaultHttpRegistry; +import org.apache.camel.component.resteasy.HttpRegistry; +import org.apache.camel.component.resteasy.ResteasyComponent; +import org.apache.camel.component.resteasy.ResteasyConstants; +import org.apache.camel.component.resteasy.ResteasyEndpoint; +import org.apache.camel.http.common.HttpConsumer; +import org.apache.camel.http.common.HttpHelper; +import org.apache.camel.http.common.HttpMessage; +import org.apache.camel.support.DefaultExchange; +import org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Class extending HttpServletDispatcher from Resteasy and representing servlet used as Camel Consumer. This servlet + * needs to be used in application if you want to use Camel Resteasy consumer in your camel routes. + */ +public class ResteasyCamelServlet extends HttpServletDispatcher { + + private static final long serialVersionUID = 1L; + + private static final Logger LOG = LoggerFactory.getLogger(ResteasyCamelServlet.class); + + private HttpRegistry httpRegistry; + + private String servletName; + + private final ConcurrentMap<String, HttpConsumer> consumers = new ConcurrentHashMap<String, HttpConsumer>(); + + + /** + * Init method for ResteasyCamelServlet, which registering servlets to HttpRegistry and it is also registering + * proxy classes to Resteasy dispatcher + * + * @param servletConfig configuration of the servlet + * @throws ServletException exception thrown from the super method + */ + @SuppressWarnings("rawtypes") + @Override + public void init(ServletConfig servletConfig) throws ServletException { + super.init(servletConfig); + + String name = servletConfig.getServletName(); + if (httpRegistry == null) { + httpRegistry = DefaultHttpRegistry.getHttpRegistry(name); + ResteasyCamelServlet existing = httpRegistry.getCamelServlet(name); + if (existing != null) { + String msg = "Duplicate ServletName detected: " + name + ". Existing: " + existing + " This: " + this.toString() + + ". Its advised to use unique ServletName per Camel application."; + // always log so people can see it easier + LOG.info(msg); + } + httpRegistry.register(this); + } + + + for (Map.Entry<String, HttpConsumer> entry : consumers.entrySet()) { + String proxyClasses = ((ResteasyComponent)getServletEndpoint(entry.getValue()).getComponent()).getProxyConsumersClasses(); + if (proxyClasses != null) { + String[] classes = proxyClasses.split(","); + LOG.debug("Proxy classes defined in the component {}", Arrays.asList(classes)); + + for (String clazz : classes) { + try { + Class realClazz = Class.forName(clazz); + // Create dynamic proxy class implementing interface + InvocationHandler handler = new ResteasyInvocationHandler(); + Object proxy = Proxy.newProxyInstance(realClazz.getClassLoader(), new Class[]{realClazz}, handler); + + // register new created proxy to the resteasy registry + getDispatcher().getRegistry().addSingletonResource(proxy); + } catch (ClassNotFoundException e) { + e.printStackTrace(); Review comment: Dont ignore exception, but log via logger or fail fast or something ########## 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: Dont load classes via Class.forName. What is this proxyClassName use-case? ########## File path: components/camel-resteasy/pom.xml ########## @@ -0,0 +1,313 @@ +<?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> + + <!-- + <properties> + <resteasy-version>4.4.1.Final</resteasy-version> + <version.org.eclipse.jetty>9.4.17.v20190418</version.org.eclipse.jetty> + <arquillian-version>1.4.1.Final</arquillian-version> + <shrinkwrap-resolver-version>2.2.6</shrinkwrap-resolver-version> + <shrinkwrap-version>1.2.6</shrinkwrap-version> + </properties> + --> + + <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> + </dependency> + <dependency> + <groupId>org.jboss.resteasy</groupId> + <artifactId>resteasy-servlet-initializer</artifactId> + </dependency> + <dependency> + <groupId>org.jboss.resteasy</groupId> + <artifactId>resteasy-client</artifactId> + </dependency> + <dependency> + <groupId>org.jboss.resteasy</groupId> + <artifactId>resteasy-validator-provider</artifactId> + </dependency> + + <!-- smx dependencies --> + <!--dependency> + <groupId>org.apache.servicemix.bundles</groupId> + <artifactId>org.apache.servicemix.bundles.reflections</artifactId> + <version>${reflections-bundle-version}</version> + </dependency--> + + <!-- camel dependencies --> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-support</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-http-common</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-http</artifactId> + </dependency> + + <!-- common dependencies --> + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-lang3</artifactId> + </dependency> + + <dependency> + <groupId>commons-io</groupId> + <artifactId>commons-io</artifactId> + </dependency> + + <!-- jboss jaxb/jaxrs dependencies needed by resteasy --> + <dependency> + <groupId>org.jboss.spec.javax.ws.rs</groupId> + <artifactId>jboss-jaxrs-api_2.1_spec</artifactId> + <version>${jboss-jaxrs-api_2.1_spec-version}</version> + </dependency> + <dependency> + <groupId>org.jboss.spec.javax.xml.bind</groupId> + <artifactId>jboss-jaxb-api_2.3_spec</artifactId> + <version>${jboss-jaxb-api_2.3_spec-version}</version> + </dependency> + <dependency> + <groupId>org.glassfish</groupId> Review comment: Can we avoid this old EL that nobody ever uses - some old clunk from J2EE days ########## 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); Review comment: Use {} placeholders in logger and not + concat ########## 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: Hmm I would rather have no surprise, and that we have a autoMethod=Basic you configure on the endpoint or component. ########## File path: components/camel-resteasy/src/main/java/org/apache/camel/component/resteasy/ResteasyEndpoint.java ########## @@ -0,0 +1,274 @@ +/* + * 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.net.URI; +import java.net.URISyntaxException; + +import org.apache.camel.Consumer; +import org.apache.camel.Processor; +import org.apache.camel.Producer; +import org.apache.camel.component.http.HttpClientConfigurer; +import org.apache.camel.component.http.HttpEndpoint; +import org.apache.camel.spi.HeaderFilterStrategy; +import org.apache.camel.spi.HeaderFilterStrategyAware; +import org.apache.camel.spi.UriEndpoint; +import org.apache.camel.spi.UriParam; +import org.apache.http.conn.HttpClientConnectionManager; +import org.apache.http.impl.client.HttpClientBuilder; + +/** + * Defines the Resteasy Endpoint. + * It contains a list of properties for Resteasy endpoint including {@link org.apache.camel.component.resteasy.ResteasyHttpBinding}, + * and {@link HeaderFilterStrategy}. + * + */ +@UriEndpoint(firstVersion = "3.3.0", scheme = "resteasy", extendsScheme = "http", + title = "Resteasy", syntax = "resteasy:contextPath", consumerOnly = true, label = "rest") Review comment: is it consumer only or was there also a producer? ---------------------------------------------------------------- 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