Repository: camel Updated Branches: refs/heads/camel-2.14.x 4d4c39c3e -> fe1d2808f refs/heads/master fcf63a214 -> 2f4010e03
CAMEL-7959: rest-dsl add support for onException,intercept etc Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/bc848661 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/bc848661 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/bc848661 Branch: refs/heads/master Commit: bc848661f53c2591de4173c7b744b91ec8d55b1f Parents: fcf63a2 Author: Claus Ibsen <davscl...@apache.org> Authored: Sun Nov 30 09:21:43 2014 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Sun Nov 30 09:21:43 2014 +0100 ---------------------------------------------------------------------- .../org/apache/camel/builder/RouteBuilder.java | 12 +++- .../apache/camel/impl/DefaultCamelContext.java | 7 --- .../apache/camel/model/rest/RestDefinition.java | 11 +++- .../rest/FromRestGetInterceptTest.java | 58 ++++++++++++++++++++ .../rest/FromRestGetOnExceptionTest.java | 55 +++++++++++++++++++ .../rest/FromRestGetRouteOnExceptionTest.java | 36 ++++++++++++ 6 files changed, 169 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/bc848661/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java b/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java index 08c8abb..3186b41 100644 --- a/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java +++ b/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java @@ -16,6 +16,8 @@ */ package org.apache.camel.builder; +import java.util.ArrayList; +import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; import org.apache.camel.CamelContext; @@ -302,7 +304,7 @@ public abstract class RouteBuilder extends BuilderSupport implements RoutesBuild public void addRoutesToCamelContext(CamelContext context) throws Exception { // must configure routes before rests - configureRoutes((ModelCamelContext)context); + configureRoutes((ModelCamelContext) context); configureRests((ModelCamelContext) context); // but populate rests before routes, as we want to turn rests into routes @@ -409,6 +411,14 @@ public abstract class RouteBuilder extends BuilderSupport implements RoutesBuild camelContext.setRestConfiguration(config); } camelContext.addRestDefinitions(getRestCollection().getRests()); + + // convert rests into routes so we reuse routes for runtime + for (RestDefinition rest : getRestCollection().getRests()) { + List<RouteDefinition> routes = rest.asRouteDefinition(getContext()); + for (RouteDefinition route : routes) { + getRouteCollection().route(route); + } + } } public RestsDefinition getRestCollection() { http://git-wip-us.apache.org/repos/asf/camel/blob/bc848661/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java index bd4488c..2e40e80 100644 --- a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java +++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java @@ -1676,13 +1676,6 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon } this.restDefinitions.addAll(restDefinitions); - - // convert rests into routes so we reuse routes for runtime - List<RouteDefinition> routes = new ArrayList<RouteDefinition>(); - for (RestDefinition rest : restDefinitions) { - routes.addAll(rest.asRouteDefinition(this)); - } - addRouteDefinitions(routes); } public RestConfiguration getRestConfiguration() { http://git-wip-us.apache.org/repos/asf/camel/blob/bc848661/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java b/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java index 008846c..83a78a5 100644 --- a/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java @@ -16,6 +16,7 @@ */ package org.apache.camel.model.rest; +import java.net.URISyntaxException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -30,6 +31,7 @@ import org.apache.camel.CamelContext; import org.apache.camel.model.OptionalIdentifiedDefinition; import org.apache.camel.model.RouteDefinition; import org.apache.camel.model.ToDefinition; +import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.URISupport; /** @@ -346,7 +348,7 @@ public class RestDefinition extends OptionalIdentifiedDefinition<RestDefinition> * Camel routing engine can add and run. This allows us to define REST services using this * REST DSL and turn those into regular Camel routes. */ - public List<RouteDefinition> asRouteDefinition(CamelContext camelContext) throws Exception { + public List<RouteDefinition> asRouteDefinition(CamelContext camelContext) { List<RouteDefinition> answer = new ArrayList<RouteDefinition>(); for (VerbDefinition verb : getVerbs()) { @@ -408,7 +410,12 @@ public class RestDefinition extends OptionalIdentifiedDefinition<RestDefinition> options.put("outType", outType); } if (!options.isEmpty()) { - String query = URISupport.createQueryString(options); + String query; + try { + query = URISupport.createQueryString(options); + } catch (URISyntaxException e) { + throw ObjectHelper.wrapRuntimeCamelException(e); + } from = from + "?" + query; } http://git-wip-us.apache.org/repos/asf/camel/blob/bc848661/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetInterceptTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetInterceptTest.java b/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetInterceptTest.java new file mode 100644 index 0000000..e0e6f77 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetInterceptTest.java @@ -0,0 +1,58 @@ +/** + * 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; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.JndiRegistry; + +public class FromRestGetInterceptTest extends ContextTestSupport { + + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry jndi = super.createRegistry(); + jndi.bind("dummy-rest", new DummyRestConsumerFactory()); + return jndi; + } + + public void testFromRestModel() throws Exception { + getMockEndpoint("mock:hello").expectedMessageCount(1); + getMockEndpoint("mock:bar").expectedMessageCount(1); + getMockEndpoint("mock:intercept").expectedMessageCount(4); + + String out = template.requestBody("seda:get-say-hello", "I was here", String.class); + assertEquals("Bye World", out); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + intercept().to("mock:intercept"); + + rest("/say/hello") + .get().route() + .to("mock:hello") + .to("mock:bar") + .transform().constant("Bye World"); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/bc848661/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetOnExceptionTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetOnExceptionTest.java b/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetOnExceptionTest.java new file mode 100644 index 0000000..365d826 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetOnExceptionTest.java @@ -0,0 +1,55 @@ +/** + * 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; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.JndiRegistry; + +public class FromRestGetOnExceptionTest extends ContextTestSupport { + + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry jndi = super.createRegistry(); + jndi.bind("dummy-rest", new DummyRestConsumerFactory()); + return jndi; + } + + public void testFromRestModel() throws Exception { + getMockEndpoint("mock:hello").expectedMessageCount(1); + + String out = template.requestBody("seda:get-say-hello", "I was here", String.class); + assertEquals("Handled the error", out); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + onException(IllegalArgumentException.class).handled(true).transform().constant("Handled the error"); + + rest("/say/hello") + .get().route() + .to("mock:hello") + .throwException(new IllegalArgumentException("Forced")); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/bc848661/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetRouteOnExceptionTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetRouteOnExceptionTest.java b/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetRouteOnExceptionTest.java new file mode 100644 index 0000000..a98945d --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetRouteOnExceptionTest.java @@ -0,0 +1,36 @@ +/** + * 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; + +import org.apache.camel.builder.RouteBuilder; + +public class FromRestGetRouteOnExceptionTest extends FromRestGetOnExceptionTest { + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + rest("/say/hello") + .get().route() + .onException(IllegalArgumentException.class).handled(true).transform().constant("Handled the error").end() + .to("mock:hello") + .throwException(new IllegalArgumentException("Forced")); + } + }; + } +}