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/01/26 10:48:09 UTC
[1/3] camel git commit: Fixed typo
Repository: camel
Updated Branches:
refs/heads/master 8fb7ed5db -> 4e11a54d6
Fixed typo
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/00f6f4a1
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/00f6f4a1
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/00f6f4a1
Branch: refs/heads/master
Commit: 00f6f4a13112176ec850280c33537b87641fb38b
Parents: 8fb7ed5
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Jan 26 10:00:36 2016 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Jan 26 10:00:36 2016 +0100
----------------------------------------------------------------------
.../main/java/org/apache/camel/component/rest/RestEndpoint.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/00f6f4a1/camel-core/src/main/java/org/apache/camel/component/rest/RestEndpoint.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/rest/RestEndpoint.java b/camel-core/src/main/java/org/apache/camel/component/rest/RestEndpoint.java
index bf475b3..d2be1f2 100644
--- a/camel-core/src/main/java/org/apache/camel/component/rest/RestEndpoint.java
+++ b/camel-core/src/main/java/org/apache/camel/component/rest/RestEndpoint.java
@@ -283,7 +283,7 @@ public class RestEndpoint extends DefaultEndpoint {
}
// there may be an optional context path configured to help Camel calculate the correct urls for the REST services
- // this may be needed when using camel-serlvet where we cannot get the actual context-path or port number of the servlet engine
+ // this may be needed when using camel-servlet where we cannot get the actual context-path or port number of the servlet engine
// during init of the servlet
String contextPath = config.getContextPath();
if (contextPath != null) {
[3/3] camel git commit: CAMEL-9312: Rest DSL should support
defaulting values for query strings
Posted by da...@apache.org.
CAMEL-9312: Rest DSL should support defaulting values for query strings
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/4e11a54d
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/4e11a54d
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/4e11a54d
Branch: refs/heads/master
Commit: 4e11a54d626e846db82c7ce9f3cb4d204c3f2428
Parents: 06facc3
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Jan 26 10:47:52 2016 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Jan 26 10:47:52 2016 +0100
----------------------------------------------------------------------
.../jetty/rest/RestJettyDefaultValueTest.java | 77 ++++++++++++++++++++
1 file changed, 77 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/4e11a54d/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyDefaultValueTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyDefaultValueTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyDefaultValueTest.java
new file mode 100644
index 0000000..881bd1d
--- /dev/null
+++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyDefaultValueTest.java
@@ -0,0 +1,77 @@
+/**
+ * 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.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.jetty.BaseJettyTest;
+import org.apache.camel.component.jetty.JettyRestHttpBinding;
+import org.apache.camel.impl.JndiRegistry;
+import org.apache.camel.model.rest.RestParamType;
+import org.apache.camel.util.ObjectHelper;
+import org.junit.Test;
+
+public class RestJettyDefaultValueTest extends BaseJettyTest {
+
+ @Override
+ protected JndiRegistry createRegistry() throws Exception {
+ JndiRegistry jndi = super.createRegistry();
+ jndi.bind("mybinding", new JettyRestHttpBinding());
+ return jndi;
+ }
+
+ @Test
+ public void testDefaultValue() throws Exception {
+ String out = template.requestBody("http://localhost:" + getPort() + "/users/123/basic", null, String.class);
+ assertEquals("123;Donald Duck", out);
+
+ String out2 = template.requestBody("http://localhost:" + getPort() + "/users/123/basic?verbose=true", null, String.class);
+ assertEquals("123;Donald Duck;1113 Quack Street Duckburg", out2);
+ }
+
+ @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()).endpointProperty("httpBindingRef", "#mybinding");
+
+ // use the rest DSL to define the rest services
+ rest("/users/")
+ .get("{id}/basic").param().name("verbose").type(RestParamType.query).defaultValue("false").endParam()
+ .route()
+ .process(new Processor() {
+ public void process(Exchange exchange) throws Exception {
+ String id = exchange.getIn().getHeader("id", String.class);
+
+ Object verbose = exchange.getIn().getHeader("verbose");
+ ObjectHelper.notNull(verbose, "verbose");
+
+ if ("true".equals(verbose)) {
+ exchange.getOut().setBody(id + ";Donald Duck;1113 Quack Street Duckburg");
+ } if ("false".equals(verbose)) {
+ exchange.getOut().setBody(id + ";Donald Duck");
+ }
+ }
+ });
+ }
+ };
+ }
+
+}
[2/3] camel git commit: CAMEL-9312: Rest DSL should support
defaulting values for query strings
Posted by da...@apache.org.
CAMEL-9312: Rest DSL should support defaulting values for query strings
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/06facc33
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/06facc33
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/06facc33
Branch: refs/heads/master
Commit: 06facc33e576e6f126062e5e1a8b2266adb843d2
Parents: 00f6f4a
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Jan 26 10:35:43 2016 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Jan 26 10:36:40 2016 +0100
----------------------------------------------------------------------
.../camel/model/rest/RestBindingDefinition.java | 46 ++++++++++++----
.../apache/camel/model/rest/RestDefinition.java | 7 +++
.../processor/binding/RestBindingProcessor.java | 16 +++++-
.../rest/FromRestDefaultValueTest.java | 57 ++++++++++++++++++++
4 files changed, 114 insertions(+), 12 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/06facc33/camel-core/src/main/java/org/apache/camel/model/rest/RestBindingDefinition.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/RestBindingDefinition.java b/camel-core/src/main/java/org/apache/camel/model/rest/RestBindingDefinition.java
index dec6006..b46aba5 100644
--- a/camel-core/src/main/java/org/apache/camel/model/rest/RestBindingDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/rest/RestBindingDefinition.java
@@ -23,6 +23,7 @@ import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlTransient;
import org.apache.camel.CamelContext;
import org.apache.camel.Processor;
@@ -43,13 +44,17 @@ import org.apache.camel.util.IntrospectionSupport;
@XmlAccessorType(XmlAccessType.FIELD)
public class RestBindingDefinition extends NoOutputDefinition<RestBindingDefinition> {
+ @XmlTransient
+ private Map<String, String> defaultValues;
+
@XmlAttribute
private String consumes;
@XmlAttribute
private String produces;
- @XmlAttribute @Metadata(defaultValue = "auto")
+ @XmlAttribute
+ @Metadata(defaultValue = "auto")
private RestBindingMode bindingMode;
@XmlAttribute
@@ -63,25 +68,25 @@ public class RestBindingDefinition extends NoOutputDefinition<RestBindingDefinit
@XmlAttribute
private Boolean enableCORS;
-
+
@XmlAttribute
private String component;
- public RestBindingDefinition() {
+ public RestBindingDefinition() {
}
@Override
public String toString() {
return "RestBinding";
}
-
+
@Override
public Processor createProcessor(RouteContext routeContext) throws Exception {
CamelContext context = routeContext.getCamelContext();
RestConfiguration config = context.getRestConfiguration(component, true);
-
- // these options can be overriden per rest verb
+
+ // these options can be overridden per rest verb
String mode = config.getBindingMode().name();
if (bindingMode != null) {
mode = bindingMode.name();
@@ -100,7 +105,7 @@ public class RestBindingDefinition extends NoOutputDefinition<RestBindingDefinit
if (mode == null || "off".equals(mode)) {
// binding mode is off, so create a off mode binding processor
- return new RestBindingProcessor(context, null, null, null, null, consumes, produces, mode, skip, cors, corsHeaders);
+ return new RestBindingProcessor(context, null, null, null, null, consumes, produces, mode, skip, cors, corsHeaders, defaultValues);
}
// setup json data format
@@ -195,10 +200,10 @@ public class RestBindingDefinition extends NoOutputDefinition<RestBindingDefinit
setAdditionalConfiguration(config, context, outJaxb, "xml.out.");
}
- return new RestBindingProcessor(context, json, jaxb, outJson, outJaxb, consumes, produces, mode, skip, cors, corsHeaders);
+ return new RestBindingProcessor(context, json, jaxb, outJson, outJaxb, consumes, produces, mode, skip, cors, corsHeaders, defaultValues);
}
- private void setAdditionalConfiguration(RestConfiguration config, CamelContext context,
+ private void setAdditionalConfiguration(RestConfiguration config, CamelContext context,
DataFormat dataFormat, String prefix) throws Exception {
if (config.getDataFormatProperties() != null && !config.getDataFormatProperties().isEmpty()) {
// must use a copy as otherwise the options gets removed during introspection setProperties
@@ -236,7 +241,28 @@ public class RestBindingDefinition extends NoOutputDefinition<RestBindingDefinit
public String getConsumes() {
return consumes;
}
-
+
+ /**
+ * Adds a default value for the query parameter
+ *
+ * @param paramName query parameter name
+ * @param defaultValue the default value
+ */
+ public void addDefaultValue(String paramName, String defaultValue) {
+ if (defaultValues == null) {
+ defaultValues = new HashMap<String, String>();
+ }
+ defaultValues.put(paramName, defaultValue);
+ }
+
+
+ /**
+ * Gets the registered default values for query parameters
+ */
+ public Map<String, String> getDefaultValues() {
+ return defaultValues;
+ }
+
/**
* Sets the component name that this definition will apply to
*/
http://git-wip-us.apache.org/repos/asf/camel/blob/06facc33/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 41f6fe3..f8e5891 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
@@ -642,6 +642,13 @@ public class RestDefinition extends OptionalIdentifiedDefinition<RestDefinition>
} else {
binding.setEnableCORS(getEnableCORS());
}
+ // register all the default values for the query parameters
+ for (RestOperationParamDefinition param : verb.getParams()) {
+ if (RestParamType.query == param.getType() && param.getDefaultValue() != null) {
+ binding.addDefaultValue(param.getName(), param.getDefaultValue());
+ }
+ }
+
route.getOutputs().add(0, binding);
// create the from endpoint uri which is using the rest component
http://git-wip-us.apache.org/repos/asf/camel/blob/06facc33/camel-core/src/main/java/org/apache/camel/processor/binding/RestBindingProcessor.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/processor/binding/RestBindingProcessor.java b/camel-core/src/main/java/org/apache/camel/processor/binding/RestBindingProcessor.java
index f901f31..9c345c7 100644
--- a/camel-core/src/main/java/org/apache/camel/processor/binding/RestBindingProcessor.java
+++ b/camel-core/src/main/java/org/apache/camel/processor/binding/RestBindingProcessor.java
@@ -58,12 +58,14 @@ public class RestBindingProcessor extends ServiceSupport implements AsyncProcess
private final boolean skipBindingOnErrorCode;
private final boolean enableCORS;
private final Map<String, String> corsHeaders;
+ private final Map<String, String> queryDefaultValues;
public RestBindingProcessor(CamelContext camelContext, DataFormat jsonDataFormat, DataFormat xmlDataFormat,
DataFormat outJsonDataFormat, DataFormat outXmlDataFormat,
String consumes, String produces, String bindingMode,
boolean skipBindingOnErrorCode, boolean enableCORS,
- Map<String, String> corsHeaders) {
+ Map<String, String> corsHeaders,
+ Map<String, String> queryDefaultValues) {
this.camelContext = camelContext;
@@ -99,6 +101,7 @@ public class RestBindingProcessor extends ServiceSupport implements AsyncProcess
this.skipBindingOnErrorCode = skipBindingOnErrorCode;
this.enableCORS = enableCORS;
this.corsHeaders = corsHeaders;
+ this.queryDefaultValues = queryDefaultValues;
}
@Override
@@ -160,6 +163,15 @@ public class RestBindingProcessor extends ServiceSupport implements AsyncProcess
}
}
+ // add missing default values which are mapped as headers
+ if (queryDefaultValues != null) {
+ for (Map.Entry<String, String> entry : queryDefaultValues.entrySet()) {
+ if (exchange.getIn().getHeader(entry.getKey()) == null) {
+ exchange.getIn().setHeader(entry.getKey(), entry.getValue());
+ }
+ }
+ }
+
// favor json over xml
if (isJson && jsonUnmarshal != null) {
// add reverse operation
@@ -182,7 +194,7 @@ public class RestBindingProcessor extends ServiceSupport implements AsyncProcess
}
// we could not bind
- if (bindingMode == null || "off".equals(bindingMode) || bindingMode.equals("auto")) {
+ if ("off".equals(bindingMode) || bindingMode.equals("auto")) {
// okay for auto we do not mind if we could not bind
exchange.addOnCompletion(new RestBindingMarshalOnCompletion(exchange.getFromRouteId(), jsonMarshal, xmlMarshal, false, accept));
callback.done(true);
http://git-wip-us.apache.org/repos/asf/camel/blob/06facc33/camel-core/src/test/java/org/apache/camel/component/rest/FromRestDefaultValueTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/rest/FromRestDefaultValueTest.java b/camel-core/src/test/java/org/apache/camel/component/rest/FromRestDefaultValueTest.java
new file mode 100644
index 0000000..9002c7a
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/component/rest/FromRestDefaultValueTest.java
@@ -0,0 +1,57 @@
+/**
+ * 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;
+import org.apache.camel.model.rest.RestParamType;
+
+public class FromRestDefaultValueTest extends ContextTestSupport {
+
+ @Override
+ protected JndiRegistry createRegistry() throws Exception {
+ JndiRegistry jndi = super.createRegistry();
+ jndi.bind("dummy-rest", new DummyRestConsumerFactory());
+ return jndi;
+ }
+
+ public void testDefaultValue() throws Exception {
+ // the rest becomes routes and the input is a seda endpoint created by the DummyRestConsumerFactory
+ getMockEndpoint("mock:bye").expectedHeaderReceived("kind", "customer");
+ getMockEndpoint("mock:bye").expectedHeaderReceived("kind", "admin");
+
+ template.sendBody("seda:get-say-bye", "Hello World");
+ template.sendBodyAndHeader("seda:get-say-bye", "Bye World", "kind", "admin");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ restConfiguration().host("localhost").enableCORS(true);
+
+ rest("/say/bye").consumes("application/json")
+ .get().param().type(RestParamType.query).name("kind").defaultValue("customer").endParam()
+ .to("mock:bye");
+ }
+ };
+ }
+}