You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by GitBox <gi...@apache.org> on 2020/07/21 18:07:57 UTC

[GitHub] [camel] jamesnetherton opened a new pull request #4029: CAMEL-15283: Add Vert.x HTTP client component

jamesnetherton opened a new pull request #4029:
URL: https://github.com/apache/camel/pull/4029


   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [camel] jamesnetherton merged pull request #4029: CAMEL-15283: Add Vert.x HTTP client component

Posted by GitBox <gi...@apache.org>.
jamesnetherton merged pull request #4029:
URL: https://github.com/apache/camel/pull/4029


   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [camel] lburgazzoli commented on a change in pull request #4029: CAMEL-15283: Add Vert.x HTTP client component

Posted by GitBox <gi...@apache.org>.
lburgazzoli commented on a change in pull request #4029:
URL: https://github.com/apache/camel/pull/4029#discussion_r458634314



##########
File path: components/camel-vertx-http/src/main/java/org/apache/camel/component/vertx/http/VertxHttpComponent.java
##########
@@ -0,0 +1,227 @@
+/*
+ * 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.vertx.http;
+
+import java.net.URI;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import io.vertx.core.Vertx;
+import io.vertx.core.VertxOptions;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Producer;
+import org.apache.camel.SSLContextParametersAware;
+import org.apache.camel.spi.Metadata;
+import org.apache.camel.spi.RestConfiguration;
+import org.apache.camel.spi.RestProducerFactory;
+import org.apache.camel.spi.annotations.Component;
+import org.apache.camel.support.CamelContextHelper;
+import org.apache.camel.support.HeaderFilterStrategyComponent;
+import org.apache.camel.support.RestProducerFactoryHelper;
+import org.apache.camel.support.service.ServiceHelper;
+import org.apache.camel.util.FileUtil;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.URISupport;
+import org.apache.camel.util.UnsafeUriCharactersEncoder;
+
+@Component("vertx-http")
+public class VertxHttpComponent extends HeaderFilterStrategyComponent implements RestProducerFactory, SSLContextParametersAware {
+
+    @Metadata(label = "advanced")
+    private Vertx vertx;
+    @Metadata(label = "advanced")
+    private VertxOptions vertxOptions;
+    @Metadata(label = "advanced")
+    private VertxHttpBinding vertxHttpBinding;
+    @Metadata(label = "security", defaultValue = "false")
+    private boolean useGlobalSslContextParameters;
+    @Metadata(label = "advanced")
+    private boolean allowJavaSerializedObject;
+
+    @Override
+    protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
+        VertxHttpConfiguration configuration = new VertxHttpConfiguration();
+
+        URI uriHttpUriAddress = new URI(UnsafeUriCharactersEncoder.encodeHttpURI(remaining));
+
+        VertxHttpEndpoint endpoint = new VertxHttpEndpoint(uri, this, configuration);
+        setProperties(endpoint, parameters);
+
+        if (configuration.getSslContextParameters() == null) {
+            configuration.setSslContextParameters(retrieveGlobalSslContextParameters());
+        }
+
+        if (configuration.getVertxHttpBinding() == null) {
+            configuration.setVertxHttpBinding(getVertxHttpBinding());
+        }
+
+        if (configuration.getHeaderFilterStrategy() == null) {
+            configuration.setHeaderFilterStrategy(getHeaderFilterStrategy());
+        }
+
+        // Recreate the http uri with the remaining parameters which the endpoint did not use
+        URI httpUri = URISupport.createRemainingURI(
+                new URI(uriHttpUriAddress.getScheme(),
+                        uriHttpUriAddress.getUserInfo(),
+                        uriHttpUriAddress.getHost(),
+                        uriHttpUriAddress.getPort(),
+                        uriHttpUriAddress.getPath(),
+                        uriHttpUriAddress.getQuery(),
+                        uriHttpUriAddress.getFragment()),
+                        parameters);
+
+        configuration.setHttpUri(httpUri);
+
+        return endpoint;
+    }
+
+    @Override
+    public Producer createProducer(CamelContext camelContext, String host,
+            String verb, String basePath, String uriTemplate, String queryParameters, String consumes,
+            String produces, RestConfiguration configuration, Map<String, Object> parameters) throws Exception {
+        // avoid leading slash
+        basePath = FileUtil.stripLeadingSeparator(basePath);
+        uriTemplate = FileUtil.stripLeadingSeparator(uriTemplate);
+
+        // get the endpoint
+        String scheme = "vertx-http:";
+        String url = scheme + host;
+        if (!ObjectHelper.isEmpty(basePath)) {
+            url += "/" + basePath;
+        }
+        if (!ObjectHelper.isEmpty(uriTemplate)) {
+            url += "/" + uriTemplate;
+        }
+
+        RestConfiguration config = configuration;
+        if (config == null) {
+            config = CamelContextHelper.getRestConfiguration(getCamelContext(), null, scheme);
+        }
+
+        Map<String, Object> map = new HashMap<>();
+        // build query string, and append any endpoint configuration properties
+        if (config.getProducerComponent() == null || config.getProducerComponent().equals(scheme)) {
+            // setup endpoint options
+            if (config.getEndpointProperties() != null && !config.getEndpointProperties().isEmpty()) {
+                map.putAll(config.getEndpointProperties());
+            }
+        }
+
+        // get the endpoint
+        String query = URISupport.createQueryString(map);
+        if (!query.isEmpty()) {
+            url = url + "?" + query;
+        }
+
+        parameters = parameters != null ? new HashMap<>(parameters) : new HashMap<>();
+
+        // there are cases where we might end up here without component being created beforehand
+        // we need to abide by the component properties specified in the parameters when creating
+        // the component, one such case is when we switch from "http" to "https" component name
+        RestProducerFactoryHelper.setupComponentFor(url, camelContext, (Map<String, Object>) parameters.remove("component"));
+
+        VertxHttpEndpoint endpoint = camelContext.getEndpoint(url, VertxHttpEndpoint.class);
+        setProperties(endpoint, parameters);
+        String path = uriTemplate != null ? uriTemplate : basePath;
+        endpoint.getConfiguration().setHeaderFilterStrategy(new VertxHttpRestHeaderFilterStrategy(path, queryParameters));
+
+        // the endpoint must be started before creating the producer
+        ServiceHelper.startService(endpoint);
+
+        return endpoint.createProducer();
+    }
+
+    public Vertx getVertx() {
+        if (vertx == null) {
+            Set<Vertx> vertxes = getCamelContext().getRegistry().findByType(Vertx.class);
+            if (vertxes.size() == 1) {
+                vertx  = vertxes.iterator().next();
+            }
+        }
+
+        if (vertx == null) {

Review comment:
       I think in this case, we need to close/stop the vertx instance once the component stops




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [camel] lburgazzoli commented on a change in pull request #4029: CAMEL-15283: Add Vert.x HTTP client component

Posted by GitBox <gi...@apache.org>.
lburgazzoli commented on a change in pull request #4029:
URL: https://github.com/apache/camel/pull/4029#discussion_r458631567



##########
File path: components/camel-vertx-http/src/main/java/org/apache/camel/component/vertx/http/VertxHttpEndpoint.java
##########
@@ -0,0 +1,147 @@
+/*
+ * 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.vertx.http;
+
+import io.vertx.core.Vertx;
+import io.vertx.core.net.ProxyOptions;
+import io.vertx.ext.web.client.WebClient;
+import io.vertx.ext.web.client.WebClientOptions;
+import io.vertx.ext.web.client.WebClientSession;
+import io.vertx.ext.web.client.spi.CookieStore;
+import org.apache.camel.Category;
+import org.apache.camel.Consumer;
+import org.apache.camel.Processor;
+import org.apache.camel.Producer;
+import org.apache.camel.spi.UriEndpoint;
+import org.apache.camel.spi.UriParam;
+import org.apache.camel.support.DefaultEndpoint;
+import org.apache.camel.support.jsse.SSLContextParameters;
+import org.apache.camel.util.ObjectHelper;
+
+@UriEndpoint(firstVersion = "3.5.0", scheme = "vertx-http", title = "Vert.x HTTP Client", syntax = "vertx-http:httpUri", category = {Category.HTTP}, producerOnly = true, lenientProperties = true)
+public class VertxHttpEndpoint extends DefaultEndpoint {
+
+    @UriParam
+    private VertxHttpConfiguration configuration;
+
+    private WebClient webClient;
+
+    public VertxHttpEndpoint(String uri, VertxHttpComponent component, VertxHttpConfiguration configuration) {
+        super(uri, component);
+        this.configuration = configuration;
+    }
+
+    @Override
+    public VertxHttpComponent getComponent() {
+        return (VertxHttpComponent) super.getComponent();
+    }
+
+    @Override
+    public Producer createProducer() throws Exception {
+        return new VertxHttpProducer(this);
+    }
+
+    @Override
+    public Consumer createConsumer(Processor processor) throws Exception {
+        throw new UnsupportedOperationException("vertx-http consumers are not supported");
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+        if (webClient == null) {
+            WebClientOptions options = configuration.getWebClientOptions();
+            if (options == null) {
+                options = new WebClientOptions();
+                options.setTryUseCompression(configuration.isUseCompression());
+                options.setConnectTimeout(configuration.getConnectTimeout());
+                configureProxyOptionsIfRequired(options);
+            }
+
+            SSLContextParameters sslContextParameters = configuration.getSslContextParameters();
+            if (sslContextParameters != null) {
+                VertxHttpHelper.setupSSLOptions(sslContextParameters, options);
+            }
+
+            webClient = WebClient.create(getVertx(), options);
+            if (configuration.isSessionManagement()) {
+                CookieStore cookieStore = configuration.getCookieStore() == null ? CookieStore.build() : configuration.getCookieStore();
+                webClient = WebClientSession.create(webClient, cookieStore);
+            }
+        }
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        super.doStop();
+        webClient.close();

Review comment:
       maybe we need to check for null here ? I don't remember if in case of failures setting up the routes, the doStop method would get invoked




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [camel] lburgazzoli commented on a change in pull request #4029: CAMEL-15283: Add Vert.x HTTP client component

Posted by GitBox <gi...@apache.org>.
lburgazzoli commented on a change in pull request #4029:
URL: https://github.com/apache/camel/pull/4029#discussion_r458633221



##########
File path: components/camel-vertx-http/src/main/java/org/apache/camel/component/vertx/http/VertxHttpComponent.java
##########
@@ -0,0 +1,227 @@
+/*
+ * 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.vertx.http;
+
+import java.net.URI;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import io.vertx.core.Vertx;
+import io.vertx.core.VertxOptions;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Producer;
+import org.apache.camel.SSLContextParametersAware;
+import org.apache.camel.spi.Metadata;
+import org.apache.camel.spi.RestConfiguration;
+import org.apache.camel.spi.RestProducerFactory;
+import org.apache.camel.spi.annotations.Component;
+import org.apache.camel.support.CamelContextHelper;
+import org.apache.camel.support.HeaderFilterStrategyComponent;
+import org.apache.camel.support.RestProducerFactoryHelper;
+import org.apache.camel.support.service.ServiceHelper;
+import org.apache.camel.util.FileUtil;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.URISupport;
+import org.apache.camel.util.UnsafeUriCharactersEncoder;
+
+@Component("vertx-http")
+public class VertxHttpComponent extends HeaderFilterStrategyComponent implements RestProducerFactory, SSLContextParametersAware {
+
+    @Metadata(label = "advanced")
+    private Vertx vertx;
+    @Metadata(label = "advanced")
+    private VertxOptions vertxOptions;
+    @Metadata(label = "advanced")
+    private VertxHttpBinding vertxHttpBinding;
+    @Metadata(label = "security", defaultValue = "false")
+    private boolean useGlobalSslContextParameters;
+    @Metadata(label = "advanced")
+    private boolean allowJavaSerializedObject;
+
+    @Override
+    protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
+        VertxHttpConfiguration configuration = new VertxHttpConfiguration();
+
+        URI uriHttpUriAddress = new URI(UnsafeUriCharactersEncoder.encodeHttpURI(remaining));
+
+        VertxHttpEndpoint endpoint = new VertxHttpEndpoint(uri, this, configuration);
+        setProperties(endpoint, parameters);
+
+        if (configuration.getSslContextParameters() == null) {
+            configuration.setSslContextParameters(retrieveGlobalSslContextParameters());
+        }
+
+        if (configuration.getVertxHttpBinding() == null) {
+            configuration.setVertxHttpBinding(getVertxHttpBinding());
+        }
+
+        if (configuration.getHeaderFilterStrategy() == null) {
+            configuration.setHeaderFilterStrategy(getHeaderFilterStrategy());
+        }
+
+        // Recreate the http uri with the remaining parameters which the endpoint did not use
+        URI httpUri = URISupport.createRemainingURI(
+                new URI(uriHttpUriAddress.getScheme(),
+                        uriHttpUriAddress.getUserInfo(),
+                        uriHttpUriAddress.getHost(),
+                        uriHttpUriAddress.getPort(),
+                        uriHttpUriAddress.getPath(),
+                        uriHttpUriAddress.getQuery(),
+                        uriHttpUriAddress.getFragment()),
+                        parameters);
+
+        configuration.setHttpUri(httpUri);
+
+        return endpoint;
+    }
+
+    @Override
+    public Producer createProducer(CamelContext camelContext, String host,
+            String verb, String basePath, String uriTemplate, String queryParameters, String consumes,
+            String produces, RestConfiguration configuration, Map<String, Object> parameters) throws Exception {
+        // avoid leading slash
+        basePath = FileUtil.stripLeadingSeparator(basePath);
+        uriTemplate = FileUtil.stripLeadingSeparator(uriTemplate);
+
+        // get the endpoint
+        String scheme = "vertx-http:";
+        String url = scheme + host;
+        if (!ObjectHelper.isEmpty(basePath)) {
+            url += "/" + basePath;
+        }
+        if (!ObjectHelper.isEmpty(uriTemplate)) {
+            url += "/" + uriTemplate;
+        }
+
+        RestConfiguration config = configuration;
+        if (config == null) {
+            config = CamelContextHelper.getRestConfiguration(getCamelContext(), null, scheme);
+        }
+
+        Map<String, Object> map = new HashMap<>();
+        // build query string, and append any endpoint configuration properties
+        if (config.getProducerComponent() == null || config.getProducerComponent().equals(scheme)) {
+            // setup endpoint options
+            if (config.getEndpointProperties() != null && !config.getEndpointProperties().isEmpty()) {
+                map.putAll(config.getEndpointProperties());
+            }
+        }
+
+        // get the endpoint
+        String query = URISupport.createQueryString(map);
+        if (!query.isEmpty()) {
+            url = url + "?" + query;
+        }
+
+        parameters = parameters != null ? new HashMap<>(parameters) : new HashMap<>();
+
+        // there are cases where we might end up here without component being created beforehand
+        // we need to abide by the component properties specified in the parameters when creating
+        // the component, one such case is when we switch from "http" to "https" component name
+        RestProducerFactoryHelper.setupComponentFor(url, camelContext, (Map<String, Object>) parameters.remove("component"));
+
+        VertxHttpEndpoint endpoint = camelContext.getEndpoint(url, VertxHttpEndpoint.class);
+        setProperties(endpoint, parameters);
+        String path = uriTemplate != null ? uriTemplate : basePath;
+        endpoint.getConfiguration().setHeaderFilterStrategy(new VertxHttpRestHeaderFilterStrategy(path, queryParameters));
+
+        // the endpoint must be started before creating the producer
+        ServiceHelper.startService(endpoint);
+
+        return endpoint.createProducer();
+    }
+
+    public Vertx getVertx() {

Review comment:
       I think in this case, we need to close/stop the vertx instance once the component stops




----------------------------------------------------------------
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:
users@infra.apache.org