1 2 3 Previous Next 32 Replies Latest reply on Jul 24, 2013 9:34 PM by Willem Jiang

    Marshalling/Unmarshalling XML Messages

    Anirban Gupta Newbie

      Hi,

       

      I was trying to marshal/unmarshal messages using jaxb. For this I created a simple camel-spring project in Fuse IDE. The project structure and the files each folder contain looks as follows:

       

      camel-spring

           data

                message (XML message to be unmarshalled)

           src

                main

                     java

                          com

                               mycompany

                                    Person.java (JAXB annotated java class for the XML message)

                                    jaxb.index (containing only the class name Person)

           resources

                META-INF

                     spring

                          camel-context.xml

           target

                the compiled classes

       

      My camel-context.xml loks like this:

       

      <?xml version="1.0" encoding="UTF-8"?>

      <beans xmlns="http://www.springframework.org/schema/beans"

             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

             xmlns:camel="http://camel.apache.org/schema/spring"

             xsi:schemaLocation="

             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

             http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

       

        <camel:camelContext xmlns="http://camel.apache.org/schema/spring">

          <camel:dataFormats>

                         <camel:jaxb id="test" contextPath="com.mycmpany"/>

          </camel:dataFormats>

          <camel:route>

            <camel:from uri="file:D:/workspace/fuse/camel-spring/src/data?noop=true"/>

            <camel:unmarshal ref="test"></camel:unmarshal>

            <camel:to uri="file:target:messages"></camel:to>

          </camel:route>

        </camel:camelContext>

      </beans>

       

      Here the context path is com.mycompanyand the jaxb.index file is also under it. But when running this, I'm getting the following exception:

       

      Caused by: javax.xml.bind.JAXBException: "com.mycompany" doesnt contain ObjectFactory.class or jaxb.index

                at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:231)

                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

                at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

                at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

                at java.lang.reflect.Method.invoke(Unknown Source)

                at javax.xml.bind.ContextFinder.newInstance(Unknown Source)

       

      Can you kindly suggest the changes I've to make to run this?

       

      Regards,

      Anirban.

       


        • 1. Re: Marshalling/Unmarshalling XML Messages
          Anirban Gupta Newbie

          Also in Spring DSL, I found two namespaces in camel-conteat.xml (

          xmlns:camel="http://camel.apache.org/schema/spring",

          xmlns="http://www.springframework.org/schema/beans"

          )

          The required tags like camelContext,route etc are there in both the namespaces, like I can also write <camel:route> instead of <route>. So is there any difference between the two?

          • 2. Re: Marshalling/Unmarshalling XML Messages
            Willem Jiang Newbie

            How did you run the application?

            Did you try to use ObjectFactory instead of the jaxb.index file?

            BTW, it could be helpful to track the cause of the issue by using the JAXB API to create the jaxb context diretly withe the package name of "com.mycompany".

            • 3. Re: Marshalling/Unmarshalling XML Messages
              Anirban Gupta Newbie

              I didn't use ObjectFactory, I run the application successfully using JAXBContext, but to try out all the methods, I was trying it using jaxb.index. Also can you kindly say me the difference between using <route> and <camel:route> (I mean between the tags from the two namespaces).

               

              Regards,

              Anirban.

              • 4. Re: Marshalling/Unmarshalling XML Messages
                Willem Jiang Newbie

                As the camelContext is specify the namespace, you can use or not use the camel: perfix.

                <camel:route> and  <route> are mean the same thing inside of the <camelContext>.

                • 5. Re: Marshalling/Unmarshalling XML Messages
                  Anirban Gupta Newbie

                  My next target is to enrich a SOAP message using the contents of another SOAP message. Previously I have enriched a XML by unmarshaling the XML using proper JAXB annotated class, performing the enrichment and then marshaling it to XML, but I'm not sure how to do it for SOAP message. Kindly suggest.

                   

                  Regards,

                  Anirban.

                  • 6. Re: Marshalling/Unmarshalling XML Messages
                    Willem Jiang Newbie

                    Please don't send the same question across the mailing list and this forum.

                     

                    It could be more easy to enrich the SOAP message just by using xslt component.

                    http://camel.apache.org/xslt.htmlYou can also try the SOAP data format, it just works as the camel-jaxb component does.

                    • 7. Re: Marshalling/Unmarshalling XML Messages
                      Anirban Gupta Newbie

                      I apollogize for posting the same question in both the places.

                       

                      Regards,

                      Anirban.

                      • 8. Re: Marshalling/Unmarshalling XML Messages
                        Anirban Gupta Newbie

                        The XSLT component only transforms the message if the dataFormat is MESSAGE or PAYLOAD, but we also need to perform validations on the incoming and the outgoing messages and so use POJO dataFormat. I'll go through SOAP dataFormat but on preliminary investigation, I've foung that the SOAP dataFormat only works on the message payload, not on the header.  So we need some way to read the SOAP header is the soap message dataFormat is POJO.

                         

                        Also I need a way to access the SOAP reply message from the producer. I mean my preliminary route was like this (simplified)

                         

                        <route streamCache="true">

                                      <from uri="cxf:bean:helloConsumerEndpoint"/>

                                      <to uri="file:D:/workspace/fuse/ExposeWS/target/messages/test/In"/>

                                      <to uri="cxf:bean:helloProducerEndpoint"/>

                        </route>

                         

                        This stores the input SOAP message in target/messages/In folder. Now I want to store the reply message from the producer in target/messages/Out folder. A reverse route like this didn't help:

                         

                        <route streamCache="true">

                                      <from uri="cxf:bean:helloProducerEndpoint"/>

                                      <to uri="file:D:/workspace/fuse/ExposeWS/target/messages/test/Out"/>

                                      <to uri="cxf:bean:helloConsumerEndpoint"/>

                        </route>

                         

                        Kindly suggest where to look for it.

                         

                        Regards,

                        Anirban

                        • 9. Re: Marshalling/Unmarshalling XML Messages
                          Willem Jiang Newbie

                          If you want to store the response you need to change the route like this

                           

                           

                          <route streamCache="true">

                                        <from uri="cxf:bean:helloProducerEndpoint"/>

                                        <to uri="file:D:/workspace/fuse/ExposeWS/target/messages/test/request"/>

                                        <to uri="cxf:bean:helloConsumerEndpoint"/>

                                        <to uri="file:D:/workspace/fuse/ExposeWS/target/messages/test/response"/>

                          </route>


                          • 10. Re: Marshalling/Unmarshalling XML Messages
                            Anirban Gupta Newbie

                            Hi Willem,

                             

                            I was just storing the request and resp 'cause I want to access the request and response message. My ultimate aim will be to:

                             

                            1. Take the incoming SOAP from consumer

                            2. Perform some transformations on it.

                            3. Call the producer with the transformed SOAP.

                            4. Transform the response SOAP message from the producer.

                            5. Give it to the consumer as a response of its original request.

                             

                            Using the above process as suggested by you, I can access the response SOAP from the producer but not send it to the consumer as a response to its original request. I used this mapping but it didnt work:

                             

                            <route streamCache="true">

                            <from uri="cxf:bean:helloConsumerEndpoint"/>

                            <to uri="file:D:/workspace/fuse/ExposeWS/target/messages/test/In"/>

                            <to uri="cxf:bean:helloProducerEndpoint"/>

                            <to uri="file:D:/workspace/fuse/ExposeWS/target/messages/test/Out"/>

                            <to uri="cxf:bean:helloConsumerEndpoint"/>

                            </route>

                            • 11. Re: Marshalling/Unmarshalling XML Messages
                              Willem Jiang Newbie

                              you don't need to send the message to cxf:bean:helloConsumerEndpoint, you just need to setup the response to the out message, then camel will take the response to the "helloConsumerEndpoint".

                               

                              The route could be

                              <route streamCache="true">

                              <from uri="cxf:bean:helloConsumerEndpoint"/>

                              <to uri="file:D:/workspace/fuse/ExposeWS/target/messages/test/In"/>

                              <!-- you can prepare the request here -->

                              <to uri="cxf:bean:helloProducerEndpoint"/>

                              <to uri="file:D:/workspace/fuse/ExposeWS/target/messages/test/Out"/>

                              <!-- you can prepare the response here -->

                              </route>

                               

                               

                               

                              Willem

                              • 12. Re: Marshalling/Unmarshalling XML Messages
                                Anirban Gupta Newbie

                                Thanks a lot, it worked this time. The problem was if I only stored the resp in the file, it was not returned but I I used some processing node after storing the resp. it is getting returned. Also if available, can you kindly give me some link of some example using SOAP data format as its use was not very clear to me after reading the documentation.

                                 

                                Regards,

                                Anirban.

                                • 13. Re: Marshalling/Unmarshalling XML Messages
                                  Willem Jiang Newbie

                                  If you are using the SOAP data format,  you need to use the camel-jetty or camel-servlet component to receive the request , then use camel-http component to send the request to the back end.

                                  The other part are most like using the camel-cxf component. The camel route could be

                                   

                                  <route>

                                      <from uri="jetty://some url"/>

                                      <!-- unmarshal the request -->

                                     <!-- do some transformation and marshall -->

                                      <to uri="http://theBackEnd"/>

                                      <!-- unmarshal the response -->

                                      <!-- do some transformation and marshall -->

                                  </route>

                                  • 14. Re: Marshalling/Unmarshalling XML Messages
                                    Anirban Gupta Newbie

                                    For navigating SOAP messages inpot as POJO dataFormat, I wsa trying to use jxpath, but it is giving the following error:

                                     

                                    java.lang.IllegalArgumentException: No script engine could be created for: jxpath

                                              at org.apache.camel.builder.script.ScriptBuilder.createScriptEngine(ScriptBuilder.java:287)

                                              at org.apache.camel.builder.script.ScriptBuilder.getEngine(ScriptBuilder.java:197)

                                              at org.apache.camel.builder.script.ScriptBuilder.getScriptContext(ScriptBuilder.java:244)

                                              at org.apache.camel.builder.script.ScriptBuilder.evaluateScript(ScriptBuilder.java:337)

                                              at org.apache.camel.builder.script.ScriptBuilder.matches(ScriptBuilder.java:100)

                                              at org.apache.camel.processor.ChoiceProcessor.process(ChoiceProcessor.java:66)

                                     

                                    I have added the camel-jxpath dependency in maven, then what might go wrong?

                                    1 2 3 Previous Next