Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions core/camel-api/src/main/java/org/apache/camel/spi/Resource.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
*/
package org.apache.camel.spi;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
* Describe a resource, such as a file or class path resource.
Expand Down Expand Up @@ -62,4 +64,28 @@ public InputStream getInputStream() throws IOException {
}
};
}

/**
* Create a resource from bytes.
*/
static Resource fromBytes(String location, byte[] content) {
return new Resource() {
@Override
public String getLocation() {
return location;
}

@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(content);
}
};
}

/**
* Create a resource from a string.
*/
static Resource fromString(String location, String content) {
return fromBytes(location, content.getBytes(StandardCharsets.UTF_8));
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,18 @@ protected void expectTag(String name) throws XmlPullParserException, IOException
}
}

protected boolean hasTag(String name) throws XmlPullParserException, IOException {
if (parser.nextTag() != XmlPullParser.START_TAG) {
throw new XmlPullParserException("Expected starting tag");
}

if (!Objects.equals(name, parser.getName()) || !Objects.equals(namespace, parser.getNamespace())) {
return false;
}

return true;
}

@SuppressWarnings("unchecked")
protected void handleOtherAttribute(Object definition, String name, String ns, String val) throws XmlPullParserException {
// Ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
import org.apache.camel.CamelContextAware;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.RouteTemplatesDefinition;
import org.apache.camel.model.RoutesDefinition;
import org.apache.camel.model.rest.RestsDefinition;
import org.apache.camel.spi.Resource;
import org.apache.camel.spi.RoutesBuilderLoader;
import org.apache.camel.spi.annotations.JdkService;
Expand Down Expand Up @@ -57,22 +54,19 @@ public RoutesBuilder loadRoutesBuilder(Resource resource) throws Exception {
@Override
public void configure() throws Exception {
try (InputStream is = resource.getInputStream()) {
RouteTemplatesDefinition templates = new ModelParser(is, NAMESPACE).parseRouteTemplatesDefinition();
if (templates != null) {
setRouteTemplateCollection(templates);
}
new ModelParser(is, NAMESPACE)
.parseRouteTemplatesDefinition()
.ifPresent(this::setRouteTemplateCollection);
}
try (InputStream is = resource.getInputStream()) {
RestsDefinition rests = new ModelParser(is, NAMESPACE).parseRestsDefinition();
if (rests != null) {
setRestCollection(rests);
}
new ModelParser(is, NAMESPACE)
.parseRestsDefinition()
.ifPresent(this::setRestCollection);
}
try (InputStream is = resource.getInputStream()) {
RoutesDefinition routes = new ModelParser(is, NAMESPACE).parseRoutesDefinition();
if (routes != null) {
setRouteCollection(routes);
}
new ModelParser(is, NAMESPACE)
.parseRoutesDefinition()
.ifPresent(this::setRouteCollection);
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ public void testFiles() throws Exception {
boolean isRest = REST_XMLS.contains(path.getFileName().toString());
boolean isTemplate = TEMPLATE_XMLS.contains(path.getFileName().toString());
if (isRest) {
RestsDefinition rests = parser.parseRestsDefinition();
RestsDefinition rests = parser.parseRestsDefinition().orElse(null);
assertNotNull(rests);
} else if (isTemplate) {
RouteTemplatesDefinition templates = parser.parseRouteTemplatesDefinition();
RouteTemplatesDefinition templates = parser.parseRouteTemplatesDefinition().orElse(null);
assertNotNull(templates);
} else {
RoutesDefinition routes = parser.parseRoutesDefinition();
RoutesDefinition routes = parser.parseRoutesDefinition().orElse(null);
assertNotNull(routes);
}
}
Expand All @@ -67,9 +67,11 @@ public void testFiles() throws Exception {
public void testSimpleString() throws Exception {
RoutesDefinition routes = new ModelParser(
new StringReader(
"<routes>" + " <route id='foo'>" + " <from uri='my:bar'/>" + " <to uri='mock:res'/>"
"<routes>"
+ " <route id='foo'>" + " <from uri='my:bar'/>" + " <to uri='mock:res'/>"
+ " </route>"
+ "</routes>")).parseRoutesDefinition();
+ "</routes>")).parseRoutesDefinition().orElse(null);

assertNotNull(routes);
}

Expand All @@ -86,7 +88,7 @@ public void namespaces() throws Exception {
+ " </setBody>\n"
+ " </route>\n"
+ "</routes>";
final RoutesDefinition routes = new ModelParser(new StringReader(routesXml)).parseRoutesDefinition();
final RoutesDefinition routes = new ModelParser(new StringReader(routesXml)).parseRoutesDefinition().orElse(null);
final RouteDefinition route0 = routes.getRoutes().get(0);
final SetBodyDefinition setBody = (SetBodyDefinition) route0.getOutputs().get(0);
final XPathExpression xPath = (XPathExpression) setBody.getExpression();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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.xml.in;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.spi.Resource;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;

public class XmlRoutesBuilderLoaderTest {
@Test
public void canLoadRoutes() throws Exception {
String content = ""
+ "<routes xmlns=\"http://camel.apache.org/schema/spring\">"
+ " <route id=\"xpath-route\">"
+ " <from uri=\"direct:test\"/>"
+ " <setBody>"
+ " <xpath resultType=\"java.lang.String\">"
+ " /foo:orders/order[1]/country/text()"
+ " </xpath>"
+ " </setBody>"
+ " </route>"
+ "</routes>";

Resource resource = Resource.fromString("in-memory.xml", content);
RouteBuilder builder = (RouteBuilder) new XmlRoutesBuilderLoader().loadRoutesBuilder(resource);
builder.setContext(new DefaultCamelContext());
builder.configure();

assertFalse(builder.getRouteCollection().getRoutes().isEmpty());
}

@Test
public void canLoadRests() throws Exception {
String content = ""
+ "<rests xmlns=\"http://camel.apache.org/schema/spring\">"
+ " <rest id=\"bar\" path=\"/say/hello\">"
+ " <get uri=\"/bar\">"
+ " <to uri=\"mock:bar\"/>"
+ " </get>"
+ " </rest>"
+ "</rests>";

Resource resource = Resource.fromString("in-memory.xml", content);
RouteBuilder builder = (RouteBuilder) new XmlRoutesBuilderLoader().loadRoutesBuilder(resource);
builder.setContext(new DefaultCamelContext());
builder.configure();

assertFalse(builder.getRestCollection().getRests().isEmpty());
}

@Test
public void canLoadTemplates() throws Exception {
String content = ""
+ "<routeTemplates xmlns=\"http://camel.apache.org/schema/spring\">"
+ " <routeTemplate id=\"myTemplate\">"
+ " <templateParameter name=\"foo\"/>"
+ " <templateParameter name=\"bar\"/>"
+ " <route>"
+ " <from uri=\"direct:{{foo}}\"/>"
+ " <to uri=\"mock:{{bar}}\"/>"
+ " </route>"
+ " </routeTemplate>"
+ "</routeTemplates>";

Resource resource = Resource.fromString("in-memory.xml", content);
RouteBuilder builder = (RouteBuilder) new XmlRoutesBuilderLoader().loadRoutesBuilder(resource);
builder.setContext(new DefaultCamelContext());
builder.configure();

assertFalse(builder.getRouteTemplateCollection().getRouteTemplates().isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
Expand Down Expand Up @@ -475,8 +476,12 @@ private JavaClass generateParser(List<Class<?>> model, ClassLoader classLoader)
});
if (clazz == routesDefinitionClass || clazz == routeTemplatesDefinitionClass || clazz == restsDefinitionClass) {
String element = clazz.getAnnotation(XmlRootElement.class).name();
parser.addMethod().setPublic().setReturnType(clazz).setName("parse" + name).addThrows(IOException.class).addThrows(XML_PULL_PARSER_EXCEPTION)
.setBody("expectTag(\"" + element + "\");\nreturn doParse" + name + "();");
parser.addMethod().setPublic()
.setReturnType(new GenericType(Optional.class, new GenericType(clazz)))
.setName("parse" + name)
.addThrows(IOException.class)
.addThrows(XML_PULL_PARSER_EXCEPTION)
.setBodyF("return hasTag(\"%s\") ? Optional.of(doParse%s()) : Optional.empty();", element, name);
}
if (hasDerived) {
if (!attributeMembers.isEmpty()) {
Expand Down