You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2019/08/06 15:03:52 UTC

[camel] branch master updated: CAMEL-12864: camel-rest-swagger producer should remove Host header if its already on the Camel Message as it should use the configured Host on the rest component instead.

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

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new 898c39a  CAMEL-12864: camel-rest-swagger producer should remove Host header if its already on the Camel Message as it should use the configured Host on the rest component instead.
898c39a is described below

commit 898c39aeebd1e8084e0d10cba7a2b8d8aa2ee308
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Aug 6 17:03:20 2019 +0200

    CAMEL-12864: camel-rest-swagger producer should remove Host header if its already on the Camel Message as it should use the configured Host on the rest component instead.
---
 .../rest/swagger/RestSwaggerEndpoint.java          |  9 ++-
 .../rest/swagger/RestSwaggerProducer.java          | 65 ++++++++++++++++++++++
 .../rest/swagger/RestSwaggerEndpointTest.java      | 26 ---------
 3 files changed, 72 insertions(+), 28 deletions(-)

diff --git a/components/camel-rest-swagger/src/main/java/org/apache/camel/component/rest/swagger/RestSwaggerEndpoint.java b/components/camel-rest-swagger/src/main/java/org/apache/camel/component/rest/swagger/RestSwaggerEndpoint.java
index 87d3009..d28a42d 100644
--- a/components/camel-rest-swagger/src/main/java/org/apache/camel/component/rest/swagger/RestSwaggerEndpoint.java
+++ b/components/camel-rest-swagger/src/main/java/org/apache/camel/component/rest/swagger/RestSwaggerEndpoint.java
@@ -61,6 +61,7 @@ import org.apache.camel.spi.UriEndpoint;
 import org.apache.camel.spi.UriParam;
 import org.apache.camel.spi.UriPath;
 import org.apache.camel.support.DefaultEndpoint;
+import org.apache.camel.support.LazyStartProducer;
 import org.apache.camel.support.ResourceHelper;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.StringHelper;
@@ -279,9 +280,13 @@ public final class RestSwaggerEndpoint extends DefaultEndpoint {
 
         final Endpoint endpoint = camelContext.getEndpoint(componentEndpointUri.toString());
 
-        setProperties(endpoint, determineEndpointParameters(swagger, operation));
+        Map<String, Object> params = determineEndpointParameters(swagger, operation);
+        boolean hasHost = params.containsKey("host");
+        setProperties(endpoint, params);
 
-        return endpoint.createProducer();
+        // if there is a host then we should use this hardcoded host instead of any Header that may have an existing
+        // Host header from some other HTTP input, and if so then lets remove it
+        return new RestSwaggerProducer(endpoint.createAsyncProducer(), hasHost);
     }
 
     String determineBasePath(final Swagger swagger) {
diff --git a/components/camel-rest-swagger/src/main/java/org/apache/camel/component/rest/swagger/RestSwaggerProducer.java b/components/camel-rest-swagger/src/main/java/org/apache/camel/component/rest/swagger/RestSwaggerProducer.java
new file mode 100644
index 0000000..43bd588
--- /dev/null
+++ b/components/camel-rest-swagger/src/main/java/org/apache/camel/component/rest/swagger/RestSwaggerProducer.java
@@ -0,0 +1,65 @@
+/*
+ * 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.rest.swagger;
+
+import java.util.concurrent.CompletableFuture;
+
+import org.apache.camel.AsyncCallback;
+import org.apache.camel.AsyncProducer;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.support.processor.DelegateAsyncProcessor;
+
+public class RestSwaggerProducer extends DelegateAsyncProcessor implements AsyncProducer {
+
+    private final AsyncProducer delegate;
+    private final boolean removeHostHeader;
+
+    public RestSwaggerProducer(AsyncProducer delegate, boolean removeHostHeader) {
+        super(delegate);
+        this.delegate = delegate;
+        this.removeHostHeader = removeHostHeader;
+    }
+
+    @Override
+    public boolean process(Exchange exchange, AsyncCallback callback) {
+        if (removeHostHeader) {
+            exchange.getMessage().removeHeader("Host");
+        }
+        return super.process(exchange, callback);
+    }
+
+    @Override
+    public CompletableFuture<Exchange> processAsync(Exchange exchange) {
+        if (removeHostHeader) {
+            exchange.getMessage().removeHeader("Host");
+        }
+        return super.processAsync(exchange);
+    }
+
+    @Override
+    public Endpoint getEndpoint() {
+        return delegate.getEndpoint();
+    }
+
+    @Override
+    public boolean isSingleton() {
+        return delegate.isSingleton();
+    }
+
+
+}
diff --git a/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerEndpointTest.java b/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerEndpointTest.java
index 6b78bd1..33911f9 100644
--- a/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerEndpointTest.java
+++ b/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerEndpointTest.java
@@ -31,18 +31,13 @@ import io.swagger.models.auth.In;
 import io.swagger.models.parameters.Parameter;
 import io.swagger.models.parameters.PathParameter;
 import io.swagger.models.parameters.QueryParameter;
-
 import org.apache.camel.CamelContext;
-import org.apache.camel.Endpoint;
-import org.apache.camel.Producer;
 import org.apache.camel.impl.engine.DefaultClassResolver;
 import org.apache.camel.spi.RestConfiguration;
 import org.junit.Test;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.entry;
-import static org.mockito.AdditionalAnswers.returnsFirstArg;
-import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
@@ -77,27 +72,6 @@ public class RestSwaggerEndpointTest {
     }
 
     @Test
-    public void shouldCreateProducers() throws Exception {
-        final CamelContext camelContext = mock(CamelContext.class);
-        when(camelContext.getClassResolver()).thenReturn(new DefaultClassResolver());
-        final Endpoint endpointDelegate = mock(Endpoint.class);
-        when(camelContext.getEndpoint("rest:GET:/v2:/pet/{petId}")).thenReturn(endpointDelegate);
-        when(camelContext.resolvePropertyPlaceholders(anyString())).then(returnsFirstArg());
-        final Producer delegateProducer = mock(Producer.class);
-        when(endpointDelegate.createProducer()).thenReturn(delegateProducer);
-
-        final RestSwaggerComponent component = new RestSwaggerComponent(camelContext);
-        component.setHost("http://petstore.swagger.io");
-
-        final RestSwaggerEndpoint endpoint = new RestSwaggerEndpoint("rest-swagger:getPetById", "getPetById", component,
-            Collections.emptyMap());
-
-        final Producer producer = endpoint.createProducer();
-
-        assertThat(producer).isSameAs(delegateProducer);
-    }
-
-    @Test
     public void shouldCreateQueryParameterExpressions() {
         assertThat(RestSwaggerEndpoint.queryParameterExpression(new QueryParameter().name("q").required(true)))
             .isEqualTo("q={q}");