use of javax.xml.bind.Unmarshaller in project camel by apache.
the class CamelContextAddRouteDefinitionsFromXmlTest method parseUri.
protected Object parseUri(String uri) throws JAXBException {
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
URL resource = getClass().getResource(uri);
assertNotNull("Cannot find resource on the classpath: " + uri, resource);
Object value = unmarshaller.unmarshal(resource);
return value;
}
use of javax.xml.bind.Unmarshaller in project camel by apache.
the class RestContextRefDefinitionHelper method cloneRestDefinition.
private static RestDefinition cloneRestDefinition(JAXBContext jaxbContext, RestDefinition def) throws JAXBException {
Marshaller marshal = jaxbContext.createMarshaller();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
marshal.marshal(def, bos);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Object clone = unmarshaller.unmarshal(bis);
if (clone != null && clone instanceof RestDefinition) {
RestDefinition def2 = (RestDefinition) clone;
Iterator<VerbDefinition> verbit1 = def.getVerbs().iterator();
Iterator<VerbDefinition> verbit2 = def2.getVerbs().iterator();
while (verbit1.hasNext() && verbit2.hasNext()) {
VerbDefinition verb1 = verbit1.next();
VerbDefinition verb2 = verbit2.next();
if (verb1.getToOrRoute() instanceof RouteDefinition && verb2.getToOrRoute() instanceof RouteDefinition) {
RouteDefinition route1 = (RouteDefinition) verb1.getToOrRoute();
RouteDefinition route2 = (RouteDefinition) verb2.getToOrRoute();
// need to clone the namespaces also as they are not JAXB marshalled (as they are transient)
Iterator<ExpressionNode> it = ProcessorDefinitionHelper.filterTypeInOutputs(route1.getOutputs(), ExpressionNode.class);
Iterator<ExpressionNode> it2 = ProcessorDefinitionHelper.filterTypeInOutputs(route2.getOutputs(), ExpressionNode.class);
while (it.hasNext() && it2.hasNext()) {
ExpressionNode node = it.next();
ExpressionNode node2 = it2.next();
NamespaceAwareExpression name = null;
NamespaceAwareExpression name2 = null;
if (node.getExpression() instanceof NamespaceAwareExpression) {
name = (NamespaceAwareExpression) node.getExpression();
}
if (node2.getExpression() instanceof NamespaceAwareExpression) {
name2 = (NamespaceAwareExpression) node2.getExpression();
}
if (name != null && name2 != null && name.getNamespaces() != null && !name.getNamespaces().isEmpty()) {
Map<String, String> map = new HashMap<String, String>();
map.putAll(name.getNamespaces());
name2.setNamespaces(map);
}
}
}
}
return def2;
}
return null;
}
use of javax.xml.bind.Unmarshaller in project camel by apache.
the class DefaultCamelContext method loadRestsDefinition.
public synchronized RestsDefinition loadRestsDefinition(InputStream is) throws Exception {
// load routes using JAXB
if (jaxbContext == null) {
// must use classloader from CamelContext to have JAXB working
jaxbContext = getModelJAXBContextFactory().newJAXBContext();
}
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Object result = unmarshaller.unmarshal(is);
if (result == null) {
throw new IOException("Cannot unmarshal to rests using JAXB from input stream: " + is);
}
// can either be routes or a single route
RestsDefinition answer;
if (result instanceof RestDefinition) {
RestDefinition rest = (RestDefinition) result;
answer = new RestsDefinition();
answer.getRests().add(rest);
} else if (result instanceof RestsDefinition) {
answer = (RestsDefinition) result;
} else {
throw new IllegalArgumentException("Unmarshalled object is an unsupported type: " + ObjectHelper.className(result) + " -> " + result);
}
return answer;
}
use of javax.xml.bind.Unmarshaller in project camel by apache.
the class MessageHelperTest method testMessageDump.
public void testMessageDump() throws Exception {
JAXBContext jaxb = JAXBContext.newInstance(MessageDump.class);
Unmarshaller unmarshaller = jaxb.createUnmarshaller();
CamelContext context = new DefaultCamelContext();
context.start();
message = new DefaultExchange(context).getIn();
// xml message body
message.setBody("Hello World");
message.setHeader("foo", 123);
String out = MessageHelper.dumpAsXml(message, true);
MessageDump dump = (MessageDump) unmarshaller.unmarshal(new StringReader(out));
assertNotNull(dump);
assertEquals("java.lang.String", dump.getBody().getType());
assertEquals("Hello World", dump.getBody().getValue());
assertEquals(1, dump.getHeaders().size());
assertEquals("foo", dump.getHeaders().get(0).getKey());
assertEquals("java.lang.Integer", dump.getHeaders().get(0).getType());
assertEquals("123", dump.getHeaders().get(0).getValue());
}
use of javax.xml.bind.Unmarshaller in project camel by apache.
the class JaxbDataFormat method createUnmarshaller.
protected Unmarshaller createUnmarshaller() throws JAXBException, SAXException, FileNotFoundException, MalformedURLException {
Unmarshaller unmarshaller = getContext().createUnmarshaller();
if (schema != null) {
unmarshaller.setSchema(cachedSchema);
unmarshaller.setEventHandler(new ValidationEventHandler() {
public boolean handleEvent(ValidationEvent event) {
// ERROR
return event.getSeverity() == ValidationEvent.WARNING;
}
});
}
return unmarshaller;
}
Aggregations