how to pass jaxb object to camelconfig xml file - jaxb

I am trying to use jaxb in camel routing.I have tried to load router from xml using jaxb and got it done. Inside a method I have created jaxb object and unmarshaller the router in xml(loaded router from xml).
my code:
JAXBContext jaxb = JAXBContext.newInstance(Constants.JAXB_CONTEXT_PACKAGES);
Unmarshaller unmarshaller = jaxb.createUnmarshaller();
Resource rs = new ClassPathResource("cameltest.xml");
Object value = unmarshaller.unmarshal(rs.getInputStream());
RoutesDefinition routes = (RoutesDefinition) value;
context.addRouteDefinitions(routes.getRoutes());
Requirement:
Instead of doing it in java file. I need when a method in a service is called, the jaxb object inside the method should trigger off the xml which contains the flow (router concept). help me........

Using JAXB in a Camel route is quite easy, as is shown in JAXB. Unless the processing is very complex it can be done in Spring and requires a minimal amount of code.
What it looks like you are trying to do from the code is extract the routes using JAXB from the Camel xml file, perhaps you are wanting to create dynamic router - I would suggest looking at the EIP Patterns that Camel implements and picking something like the Content Based Router or Dynamic Router.

Related

No MessageBodyWriter for Single

I'm trying to use resteasy-rxjava2 to provide an XML document using jaxb, within a vertx application (using a non-vertx legacy library we have). But I get:
Could not find MessageBodyWriter for response object of type:
org.jboss.resteasy.rxjava2.propagation.ContextPropagatorOnSingleAssemblyAction$ContextPropagatorSingle of media type:
application/xml;charset=UTF-8
From what I can tell, this comes down to the difference between a MessageBodyWriter and the AsyncResponseProvider that is in the resteasy-rxjava2 dependency for a Single (SingleProvider).
I have the following resteasy service definition
#GET
#Path(FdsnwsPaths.QUERY)
#Produces(MediaType.APPLICATION_XML)
#Stream
// CHECKSTYLE:OFF too many parameters
public Response getQuery(...)
How do I get resteasy to properly serve the data asynchrously, using the SingleProvider or otherwise.
The #Get method must return the Single explicitly or it doesn't work. (Can't use Response or Object). In my case, the Single contains the jaxb xml root element.
#GET
#Path(FdsnwsPaths.QUERY)
#Produces(MediaType.APPLICATION_XML)
#Stream
public Single<FDSNStationXML> getQuery(...)
Then, to make things more complicated, in order to handle specific exception types and map them to specific response status codes, I have to create a custom ExceptionMapper which creates the Response object I used to be able to create directly in the method. (in resteasy-vertx, I found no documentation on how to do this, but in my case, I am creating my own VertxRestEasyDeployment object so I can register the class of the ExceptionMapper(s) like this:
VertxResteasyDeployment deployment = new VertxResteasyDeployment();
deployment.getActualProviderClasses().addAll(providerClasses);
For reference, this is all being done with:
RestEasy 5.0.3.Final (including resteasy-rxjava2)
RxJava 2.2.20
Vertx 3.9.5

Mule Jaxb to object transforms to wrong object

I have an integration with some flows and JAXBcontext declared like this
<mulexml:jaxb-context name="JaxbContext" packageNames="se.razorlib.SystemAProduct:se.razorlib.SystemAPurchase:se.razorlib.SystemAOrder"/>
In one of my flows I transform XML to Purchase and in another XML to Order, which are very similar objects.
However, in the Order flow JAXB transforms my XML to PurchaseObject instead of OrderObject, which of course throws me an error:
The object transformed is of type: "SimpleDataType{type=se.razorlib.SystemAPurchase.Header, mimeType='*/*', encoding='null'}", but the expected return type is "SimpleDataType{type=se.razorlib.SystemAOrder.Header, mimeType='*/*', encoding='null'}"
This is my configuration, how can I force a tranfsformation to the "right" object?
<file:inbound-endpoint path="C:\temp\OrderfileIn" responseTimeout="10000" doc:name="FileIn"/>
<mulexml:jaxb-xml-to-object-transformer jaxbContext-ref="JAXB_Context" doc:name="XML to JAXB Object" returnClass="se.razorlib.SystemAOrder.Header"/>
<custom-transformer returnClass="se.razorlib.SystemBOrder.SalesOrder" encoding="UTF-8" class="se.didriksson.Transformer.Map2SystemBOrder" doc:name="Map2B"/>
To pick the correct bean class you have to place the
#XmlRootElement(name = "Order")
Instead of order you may have to put the root element of your xml.
Another point
I also face an issue with mule 3.7 which is it doesn't read package.info file in the package

How to keep IDREF link when unmarshalling an object with JAXB for cloning?

I defined objects with XSD model and I used Jaxb to create corresponding classes, to be able to load XML files on instance of classes and to generate XML files from instance of classes.
My elements defined on the XSD model are complex with several hierarchical level, using list, ID and IDREF.
One command of my program is to duplicate elements.
I can't use the clone method cause classes of element are generated by JAXB.
So i have tried to do deep copy with BeanUtils.cloneBean, next with SerializationUtils.clone from Apache but both methods don't work correctly with all my objects because of their complexity.
I found a solution that work, using JAXB to create a clone unmarshalling my element :
public ObjectXML duplicate(ObjectXML objectXML) throws JAXBException {
JAXBContext sourceJaxbContext = JAXBContext.newInstance(objectXML.getClass());
JAXBContext targetJaxbContext = JAXBContext.newInstance(objectXML.getClass());
Unmarshaller unmarshaller = targetJaxbContext.createUnmarshaller();
ObjectXML objectCopy = (ObjectXML) unmarshaller.unmarshal(new JAXBSource(sourceJaxbContext, objectXML));
return objectCopy;
}
ObjectXML is my parent class from all elements generated by JAXB. All elements inherit directly or indirectly from this class.
This method work fine except with IDREF attributes that link to another object : JAXB don't know the element that have the corresponding ID when I unmarshall an individual object, so it assign null to the attribute.
Have someone a solution to keep IDREF on the copy when linked object are not supplied to the unmarshal of JAXB?
Thank you for advance.
Few hints.
Try the copyable plugin from JAXB2-Basics. This plugin generates deep reflection-free copying methods which handle JAXB structures very well, and you can also specify your own copying strategy. Disclaimer: I'm the author.
Implement and register your own IDResolver, see this blog post. This would allow you to plug your own ID resolution strategy.

JAXB marshalling in Apache Camel

I am new to Apache camel and need to perform a task where I need to marshal an object to XML file. I am using the below code but it is not working. Here, foo.pojo is package where JAXB annotated classes are present
JaxbDataFormat jaxbDataFormat = new JaxbDataFormat("foo.pojo");
from("direct:start").marshal(jaxbDataFormat).to("file:C:/Users/Anand.Jain/Desktop/hello/abc.xml").end();
Option 1: Configure the context path
JaxbDataFormat jaxbDataFormat = new JaxbDataFormat("foo.pojo");
OptionFactory or jaxb.index file must be defined in the given package as explained here.
Option 2: Configure the class(es) to be bound
JAXBContext jaxbContext = JAXBContext.newInstance(MyAnnotatedClass.class);
JaxbDataFormat jaxbDataFormat = new JaxbDataFormat(jaxbContext);
I prefere Option 2.
Option 1 is recently impossible as JaxbDataFormat(String) constructor is not available as you can see in official javadoc
Documentation seems to be outdated about this point.
EDIT: BE CAREFUL there's TWO JaxbDataFormat
I have understood: there's TWO jaxbDataFormat in camel ecosystem
one is in camel-core package org.apache.camel.model.dataformat
another in camel-jaxb package org.apache.camel.converter.jaxb

Use JAXB to marshall bean in the service layer

I use Jersey with JAXB to convert my response to XML. I have several methods that consume XML from the Request Body. I can specify the bean as a parameter to the method being called and JAXB will be used to automatically transform the request body data to that bean, or fail if it's not compatible.
This works great for the majority of all my cases, and I have not had the need to write a MessageBodyReader or MessageBodyWriter to handle the transformation to and from. I have this one instance where I need the request body to be mapped to the bean, but I also need the original XML that was in the body of the request. I need to store that in the DB.
I've tried getting to the request body through the HttpContext, and can't figure out how - I just see the header values and URI values. I tried through the HTTPServletContext as well ending in the same result.
Is there a way to capture the request body as it is before it's transformed or after? Will I need to create a MessageBodyReader to handle this case? It seemed like those were used to map the contents to a bean, which I already have functioning, so I didn't think that was the solution. But, perhaps in the MessageBodyReader I can put the request body into some variable and pass it along while still transforming the data to the beans. That still seems kind of more work than is needed. I would think I could get to the body through the request somehow.
Update:
I tried getting the body from ContainerRequest object using the getEntity method.
String xmlString = request.getEntity(String.class);
And, that worked great. The request body is captured in that property and I can add it to the request properties to access later in the resource, etc. The problem is, and I think it's because the entity is a stream, it clears out the body. So by the time the jaxb mapping to the beans happen, there is nothing to transform and I'm given a bad request response.
That didn't work but I wanted to throw it out there as a possibility if someone could get it working better.
Here is what I ended up doing, and it would be nice to not have to do it this way. I'm basically marshalling what was unmarshalled, in my service class.
final StringWriter st = new StringWriter();
try
{
final JAXBContext jaxbContext = JAXBContext.newInstance(full.classpath.and.classname.of.root.bean);
final Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(unmarshalled object, st);
} catch (final JAXBException je)
{
// TODO Auto-generated catch block
je.printStackTrace();
}
final String xml = st.toString();
So you can see - I basically take the object, the root bean that the body was unmarshalled to, and marshal it, giving the location of the annotated root bean to use in marhsalling.
It seems a bit redonkulous to have to marshal what was already unmarshalled, and if that is the response, maybe marshalled again on the way out. I would like a solution where I could grab a copy of the body before the unmarshalling happens. But in the meantime, this will have to work.
I ran into the same scenario and ended with similar conclusions, except I do it the other way around by getting it as a String and then marshal manually to avoid unnecessary JAXB work, e.g.:
String xmlString = request.getEntity(String.class);
Reader sReader = new StringReader(xmlString);
return (T)jaxbCtx.createUnmarshaller().unmarshal(sReader);

Resources