Repository: camel Updated Branches: refs/heads/master 01dead92e -> 7c23b3103
CAMEL-8991: rest-dsl to support the toD Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/574e699f Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/574e699f Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/574e699f Branch: refs/heads/master Commit: 574e699f0f95056bdd6e5bf6421b8bf5e2f290dd Parents: 01dead9 Author: Claus Ibsen <davscl...@apache.org> Authored: Tue Jul 21 10:24:49 2015 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Tue Jul 21 10:24:49 2015 +0200 ---------------------------------------------------------------------- .../apache/camel/model/rest/RestDefinition.java | 29 ++++++++++++- .../apache/camel/model/rest/VerbDefinition.java | 28 +++++++++++-- .../apache/camel/model/XmlRestParseToDTest.java | 43 ++++++++++++++++++++ .../org/apache/camel/model/simpleRestToD.xml | 27 ++++++++++++ 4 files changed, 122 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/574e699f/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 6804ae9..667c289 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 @@ -29,8 +29,11 @@ import javax.xml.bind.annotation.XmlRootElement; import org.apache.camel.CamelContext; import org.apache.camel.model.OptionalIdentifiedDefinition; +import org.apache.camel.model.ProcessorDefinition; import org.apache.camel.model.RouteDefinition; +import org.apache.camel.model.SendDefinition; import org.apache.camel.model.ToDefinition; +import org.apache.camel.model.ToDynamicDefinition; import org.apache.camel.spi.Metadata; import org.apache.camel.util.FileUtil; import org.apache.camel.util.ObjectHelper; @@ -387,7 +390,7 @@ public class RestDefinition extends OptionalIdentifiedDefinition<RestDefinition> } /** - * Routes directly to the given endpoint. + * Routes directly to the given static endpoint. * <p/> * If you need additional routing capabilities, then use {@link #route()} instead. * @@ -407,6 +410,27 @@ public class RestDefinition extends OptionalIdentifiedDefinition<RestDefinition> return this; } + /** + * Routes directly to the given dynamic endpoint. + * <p/> + * If you need additional routing capabilities, then use {@link #route()} instead. + * + * @param uri the uri of the endpoint + * @return this builder + */ + public RestDefinition toD(String uri) { + // add to last verb + if (getVerbs().isEmpty()) { + throw new IllegalArgumentException("Must add verb first, such as get/post/delete"); + } + + ToDynamicDefinition to = new ToDynamicDefinition(uri); + + VerbDefinition verb = getVerbs().get(getVerbs().size() - 1); + verb.setToD(to); + return this; + } + public RouteDefinition route() { // add to last verb if (getVerbs().isEmpty()) { @@ -462,7 +486,8 @@ public class RestDefinition extends OptionalIdentifiedDefinition<RestDefinition> // it was a singular to, so add a new route and add the singular // to as output to this route route = new RouteDefinition(); - route.getOutputs().add(verb.getTo()); + ProcessorDefinition def = verb.getTo() != null ? verb.getTo() : verb.getToD(); + route.getOutputs().add(def); } // add the binding http://git-wip-us.apache.org/repos/asf/camel/blob/574e699f/camel-core/src/main/java/org/apache/camel/model/rest/VerbDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/VerbDefinition.java b/camel-core/src/main/java/org/apache/camel/model/rest/VerbDefinition.java index 9c1fef8..90a32ce 100644 --- a/camel-core/src/main/java/org/apache/camel/model/rest/VerbDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/rest/VerbDefinition.java @@ -32,6 +32,7 @@ import javax.xml.bind.annotation.XmlTransient; import org.apache.camel.model.OptionalIdentifiedDefinition; import org.apache.camel.model.RouteDefinition; import org.apache.camel.model.ToDefinition; +import org.apache.camel.model.ToDynamicDefinition; import org.apache.camel.spi.Metadata; /** @@ -76,11 +77,13 @@ public class VerbDefinition extends OptionalIdentifiedDefinition<VerbDefinition> @XmlAttribute private String outType; - // used by XML DSL to either select a <to> or <route> + // used by XML DSL to either select a <to>, <toD>, or <route> // so we need to use the common type OptionalIdentifiedDefinition + // must select one of them, and hence why they are all set to required = true, but the XSD is set to only allow one of the element @XmlElements({ - @XmlElement(required = false, name = "to", type = ToDefinition.class), - @XmlElement(required = false, name = "route", type = RouteDefinition.class)} + @XmlElement(required = true, name = "to", type = ToDefinition.class), + @XmlElement(required = true, name = "toD", type = ToDynamicDefinition.class), + @XmlElement(required = true, name = "route", type = RouteDefinition.class)} ) private OptionalIdentifiedDefinition<?> toOrRoute; @@ -88,6 +91,8 @@ public class VerbDefinition extends OptionalIdentifiedDefinition<VerbDefinition> @XmlTransient private ToDefinition to; @XmlTransient + private ToDynamicDefinition toD; + @XmlTransient private RouteDefinition route; @XmlTransient private RestDefinition rest; @@ -267,8 +272,25 @@ public class VerbDefinition extends OptionalIdentifiedDefinition<VerbDefinition> } } + public ToDynamicDefinition getToD() { + if (toD != null) { + return toD; + } else if (toOrRoute instanceof ToDynamicDefinition) { + return (ToDynamicDefinition) toOrRoute; + } else { + return null; + } + } + public void setTo(ToDefinition to) { this.to = to; + this.toD = null; + this.toOrRoute = to; + } + + public void setToD(ToDynamicDefinition to) { + this.to = null; + this.toD = to; this.toOrRoute = to; } http://git-wip-us.apache.org/repos/asf/camel/blob/574e699f/camel-core/src/test/java/org/apache/camel/model/XmlRestParseToDTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/model/XmlRestParseToDTest.java b/camel-core/src/test/java/org/apache/camel/model/XmlRestParseToDTest.java new file mode 100644 index 0000000..277e0ef --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/model/XmlRestParseToDTest.java @@ -0,0 +1,43 @@ +/** + * 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.model; + +import javax.xml.bind.JAXBException; + +import org.apache.camel.model.rest.GetVerbDefinition; +import org.apache.camel.model.rest.RestContainer; +import org.apache.camel.model.rest.RestDefinition; + +public class XmlRestParseToDTest extends XmlTestSupport { + + public void testParseSimpleRestXml() throws Exception { + RestDefinition rest = assertOneRest("simpleRestToD.xml"); + assertEquals("/users", rest.getPath()); + + assertEquals(1, rest.getVerbs().size()); + GetVerbDefinition get = (GetVerbDefinition) rest.getVerbs().get(0); + assertEquals("/view/{id}", get.getUri()); + assertEquals("bean:getUser?id=${header.id}", get.getToD().getUri()); + } + + protected RestDefinition assertOneRest(String uri) throws JAXBException { + RestContainer context = assertParseRestAsJaxb(uri); + RestDefinition rest = assertOneElement(context.getRests()); + return rest; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/574e699f/camel-core/src/test/resources/org/apache/camel/model/simpleRestToD.xml ---------------------------------------------------------------------- diff --git a/camel-core/src/test/resources/org/apache/camel/model/simpleRestToD.xml b/camel-core/src/test/resources/org/apache/camel/model/simpleRestToD.xml new file mode 100644 index 0000000..dc55aa3 --- /dev/null +++ b/camel-core/src/test/resources/org/apache/camel/model/simpleRestToD.xml @@ -0,0 +1,27 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<rests xmlns="http://camel.apache.org/schema/spring" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> + + <rest path="/users"> + <get uri="/view/{id}"> + <toD uri="bean:getUser?id=${header.id}"/> + </get> + </rest> + +</rests>