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



##########
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:
       in earlier versions the client framework was a bit limited in resteasy. 
there are other ways but looks like this is one of the prior methods 
considering API support coming from resteasy's history. to consider adding / 
removing such features, engagement with resteasy community might be good but 
can be said of being not strong ATM




----------------------------------------------------------------
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