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 2017/02/28 13:54:18 UTC
camel git commit: CAMEL-5716: Resource based endpoints can now call a
method on a bean to get the resource so you can have more flexibility (ie
load it from database or whatever).
Repository: camel
Updated Branches:
refs/heads/master 901ba699f -> 523fccb5b
CAMEL-5716: Resource based endpoints can now call a method on a bean to get the resource so you can have more flexibility (ie load it from database or whatever).
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/523fccb5
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/523fccb5
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/523fccb5
Branch: refs/heads/master
Commit: 523fccb5bfb8b8eecaa775a1d3ffba236bf93774
Parents: 901ba69
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Feb 28 14:24:23 2017 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Feb 28 14:24:23 2017 +0100
----------------------------------------------------------------------
.../camel/component/ResourceEndpoint.java | 17 ++++-
.../org/apache/camel/util/ResourceHelper.java | 32 +++++++-
.../validator/ValidatorBeanCallTest.java | 79 ++++++++++++++++++++
3 files changed, 125 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/523fccb5/camel-core/src/main/java/org/apache/camel/component/ResourceEndpoint.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/ResourceEndpoint.java b/camel-core/src/main/java/org/apache/camel/component/ResourceEndpoint.java
index f99441a..5e973fb 100644
--- a/camel-core/src/main/java/org/apache/camel/component/ResourceEndpoint.java
+++ b/camel-core/src/main/java/org/apache/camel/component/ResourceEndpoint.java
@@ -43,7 +43,14 @@ import org.slf4j.LoggerFactory;
public abstract class ResourceEndpoint extends ProcessorEndpoint implements ManagedResourceEndpointMBean {
protected final Logger log = LoggerFactory.getLogger(getClass());
private volatile byte[] buffer;
- @UriPath(description = "Path to the resource, or a reference to lookup a bean in the Registry to use as the resource") @Metadata(required = "true")
+
+ @UriPath(description = "Path to the resource."
+ + " You can prefix with: classpath, file, http, ref, or bean."
+ + " classpath, file and http loads the resource using these protocols (classpath is default)."
+ + " ref will lookup the resource in the registry."
+ + " bean will call a method on a bean to be used as the resource."
+ + " For bean you can specify the method name after dot, eg bean:myBean.myMethod.")
+ @Metadata(required = "true")
private String resourceUri;
@UriParam(defaultValue = "false", description = "Sets whether to use resource content cache or not")
private boolean contentCache;
@@ -141,7 +148,13 @@ public abstract class ResourceEndpoint extends ProcessorEndpoint implements Mana
}
/**
- * Path to the resource, or a reference to lookup a bean in the Registry to use as the resource
+ * Path to the resource.
+ * <p/>
+ * You can prefix with: classpath, file, http, ref, or bean.
+ * classpath, file and http loads the resource using these protocols (classpath is default).
+ * ref will lookup the resource in the registry.
+ * bean will call a method on a bean to be used as the resource.
+ * For bean you can specify the method name after dot, eg bean:myBean.myMethod
*
* @param resourceUri the resource path
*/
http://git-wip-us.apache.org/repos/asf/camel/blob/523fccb5/camel-core/src/main/java/org/apache/camel/util/ResourceHelper.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/ResourceHelper.java b/camel-core/src/main/java/org/apache/camel/util/ResourceHelper.java
index cea7c38..316c179 100644
--- a/camel-core/src/main/java/org/apache/camel/util/ResourceHelper.java
+++ b/camel-core/src/main/java/org/apache/camel/util/ResourceHelper.java
@@ -32,7 +32,9 @@ import java.net.URLDecoder;
import java.util.Map;
import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.impl.DefaultExchange;
import org.apache.camel.spi.ClassResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -123,8 +125,9 @@ public final class ResourceHelper {
* <ul>
* <il>file:nameOfFile - to refer to the file system</il>
* <il>classpath:nameOfFile - to refer to the classpath (default)</il>
- * <il>http:uri - to load the resoufce using HTTP</il>
+ * <il>http:uri - to load the resource using HTTP</il>
* <il>ref:nameOfBean - to lookup the resource in the {@link org.apache.camel.spi.Registry}</il>
+ * <il>bean:nameOfBean.methodName - to lookup a bean in the {@link org.apache.camel.spi.Registry} and call the method</il>
* </ul>
* If no prefix has been given, then the resource is loaded from the classpath
* <p/>
@@ -140,7 +143,34 @@ public final class ResourceHelper {
String ref = uri.substring(4);
String value = CamelContextHelper.mandatoryLookup(camelContext, ref, String.class);
return new ByteArrayInputStream(value.getBytes());
+ } else if (uri.startsWith("bean:")) {
+ String bean = uri.substring(5);
+ if (bean.contains(".")) {
+ String method = StringHelper.after(bean, ".");
+ bean = StringHelper.before(bean, ".") + "?method=" + method;
+ }
+ Exchange dummy = new DefaultExchange(camelContext);
+ Object out = camelContext.resolveLanguage("bean").createExpression(bean).evaluate(dummy, Object.class);
+ if (dummy.getException() != null) {
+ IOException io = new IOException("Cannot find resource: " + uri + " from calling the bean");
+ io.initCause(dummy.getException());
+ throw io;
+ }
+ if (out != null) {
+ InputStream is = camelContext.getTypeConverter().tryConvertTo(InputStream.class, dummy, out);
+ if (is == null) {
+ String text = camelContext.getTypeConverter().tryConvertTo(String.class, dummy, out);
+ if (text != null) {
+ return new ByteArrayInputStream(text.getBytes());
+ }
+ } else {
+ return is;
+ }
+ } else {
+ throw new IOException("Cannot find resource: " + uri + " from calling the bean");
+ }
}
+
InputStream is = resolveResourceAsInputStream(camelContext.getClassResolver(), uri);
if (is == null) {
String resolvedName = resolveUriPath(uri);
http://git-wip-us.apache.org/repos/asf/camel/blob/523fccb5/camel-core/src/test/java/org/apache/camel/component/validator/ValidatorBeanCallTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/validator/ValidatorBeanCallTest.java b/camel-core/src/test/java/org/apache/camel/component/validator/ValidatorBeanCallTest.java
new file mode 100644
index 0000000..b1953d7
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/component/validator/ValidatorBeanCallTest.java
@@ -0,0 +1,79 @@
+/**
+ * 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.validator;
+
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.JndiRegistry;
+
+public class ValidatorBeanCallTest extends ContextTestSupport {
+
+ protected MockEndpoint validEndpoint;
+ protected MockEndpoint invalidEndpoint;
+
+ public void testCallBean() throws Exception {
+ validEndpoint.expectedMessageCount(1);
+ invalidEndpoint.expectedMessageCount(0);
+
+ template.sendBody(
+ "direct:rootPath",
+ "<report xmlns='http://foo.com/report' xmlns:rb='http://foo.com/report-base'><author><rb:name>Knuth</rb:name></author><content><rb:chapter><rb:subject></rb:subject>"
+ + "<rb:abstract></rb:abstract><rb:body></rb:body></rb:chapter></content></report>");
+
+ MockEndpoint.assertIsSatisfied(validEndpoint, invalidEndpoint);
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ validEndpoint = resolveMandatoryEndpoint("mock:valid", MockEndpoint.class);
+ invalidEndpoint = resolveMandatoryEndpoint("mock:invalid", MockEndpoint.class);
+ }
+
+ @Override
+ protected JndiRegistry createRegistry() throws Exception {
+ JndiRegistry jndi = super.createRegistry();
+ jndi.bind("myBean", new MyValidatorBean());
+ return jndi;
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:rootPath")
+ .to("validator:bean:myBean.loadFile")
+ .to("mock:valid");
+ }
+ };
+ }
+
+ public static class MyValidatorBean {
+
+ public InputStream loadFile() throws FileNotFoundException {
+ return new FileInputStream("src/test/resources/report.xsd");
+ }
+
+ }
+}