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 2016/11/29 15:49:39 UTC
[2/2] camel git commit: CAMEL-10539: Jetty http producer bridge
endpoint should avoid NPE error and detect invalid uri and provide exception
with suggestions what to do.
CAMEL-10539: Jetty http producer bridge endpoint should avoid NPE error and detect invalid uri and provide exception with suggestions what to do.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/e7571acc
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/e7571acc
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/e7571acc
Branch: refs/heads/camel-2.18.x
Commit: e7571acc92dd8860f65c26b7c08160ca812804f8
Parents: 061c980
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Nov 29 10:32:31 2016 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Nov 29 16:49:27 2016 +0100
----------------------------------------------------------------------
.../component/jetty/JettyHttpProducer.java | 15 ++++++
.../jetty/rest/RestBridgeEndpointTest.java | 52 ++++++++++++++++++
.../jetty/rest/RestNoBridgeEndpointTest.java | 56 ++++++++++++++++++++
3 files changed, 123 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/e7571acc/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
index e0595bc..7280b0c 100644
--- a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
+++ b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
@@ -20,6 +20,7 @@ import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.Serializable;
import java.net.URI;
+import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@@ -114,6 +115,20 @@ public class JettyHttpProducer extends DefaultAsyncProducer implements AsyncProc
JettyContentExchange httpExchange = getEndpoint().createContentExchange();
httpExchange.init(exchange, getBinding(), client, callback);
+
+ // url must have scheme
+ try {
+ uri = new URI(url);
+ String scheme = uri.getScheme();
+ if (scheme == null) {
+ throw new IllegalArgumentException("Url must include scheme: " + url + ". If you are bridging endpoints set bridgeEndpoint=true."
+ + " If you want to call a specific url, then you may need to remove all CamelHttp* headers in the route before this."
+ + " See more details at: http://camel.apache.org/how-to-remove-the-http-protocol-headers-in-the-camel-message.html");
+ }
+ } catch (URISyntaxException e) {
+ // ignore
+ }
+
httpExchange.setURL(url); // Url has to be set first
httpExchange.setMethod(methodName);
http://git-wip-us.apache.org/repos/asf/camel/blob/e7571acc/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestBridgeEndpointTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestBridgeEndpointTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestBridgeEndpointTest.java
new file mode 100644
index 0000000..dc85698
--- /dev/null
+++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestBridgeEndpointTest.java
@@ -0,0 +1,52 @@
+/**
+ * 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.jetty.rest;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.jetty.BaseJettyTest;
+import org.junit.Test;
+
+public class RestBridgeEndpointTest extends BaseJettyTest {
+
+ @Test
+ public void testJettyBridgeEndpoint() throws Exception {
+ getMockEndpoint("mock:result").expectedMessageCount(1);
+
+ String out = template.requestBody("http://localhost:" + getPort() + "/api/123/", null, String.class);
+ assertEquals("Bye 123", out);
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ // configure to use jetty on localhost with the given port
+ restConfiguration().component("jetty").host("localhost").port(getPort());
+
+ rest("/api/").get("/{id}/").to("jetty:http://localhost:" + getPort2() + "?bridgeEndpoint=true");
+
+ from("jetty:http://localhost:" + getPort2() + "?matchOnUriPrefix=true")
+ .to("mock:result")
+ .transform().simple("Bye ${header.id}");
+ }
+ };
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/e7571acc/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestNoBridgeEndpointTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestNoBridgeEndpointTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestNoBridgeEndpointTest.java
new file mode 100644
index 0000000..fe8967b
--- /dev/null
+++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestNoBridgeEndpointTest.java
@@ -0,0 +1,56 @@
+/**
+ * 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.jetty.rest;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.jetty.BaseJettyTest;
+import org.junit.Test;
+
+public class RestNoBridgeEndpointTest extends BaseJettyTest {
+
+ @Test
+ public void testJettyNoBridgeEndpoint() throws Exception {
+ getMockEndpoint("mock:result").expectedMessageCount(1);
+
+ String out = template.requestBody("http://localhost:" + getPort() + "/api/123/", null, String.class);
+ assertEquals("Bye 123", out);
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ // configure to use jetty on localhost with the given port
+ restConfiguration().component("jetty").host("localhost").port(getPort());
+
+ rest("/api/").get("/{id}/").to("direct:foo");
+
+ from("direct:foo")
+ .removeHeaders("CamelHttp*")
+ .to("jetty:http://localhost:" + getPort2());
+
+ from("jetty:http://localhost:" + getPort2() + "?matchOnUriPrefix=true")
+ .to("mock:result")
+ .transform().simple("Bye ${header.id}");
+ }
+ };
+ }
+
+}