Using XML Transformer in Mule - java

I currently receive a string message from a queue, The string message contains XML. How do I pick each element and convert to a POJO. This is the content of my queue
<MsgContent>
<MsgHeader>
<code>1010</code>
</MsgHeader>
<MsgBody>
<value>fundstransfer</value>
</MsgBody>
</MsgContent>
I want to be able to get the values of the two keys(code and value)
This is my mule configuration xml
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:data- mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper" xmlns:xm="http://www.mulesoft.org/schema/mule/xml" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:jms="http://www.mulesoft.org/schema/mule/jms" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.xsd">
<jms:activemq-connector name="Active_MQ" brokerURL="tcp://localhost:61616" validateConnections="true" doc:name="Active MQ"/>
<mulexml:jaxb-context name="myJaxb" doc:name="myJaxb" packageNames="com.test.jaxb" />
<flow name="JMSMessageFlow1" doc:name="JMSMessageFlow1">
<jms:inbound-endpoint queue="StudioIns" connector-ref="Active_MQ" doc:name="JMS" />
<custom-transformer class="org.mule.module.xml.transformer.XmlToXMLStreamReader" />
<mulexml:jaxb-xml-to-object-transformer jaxbContext-ref="myJaxb" returnClass="com.test.jaxb.MsgContent"/>
<component class="com.test.HelloMessage" doc:name="Java"/>
<file:outbound-endpoint path="D:\Documents\MuleStudio\workspace\jmsmessage\src\main\java\com\test" outputPattern="output.csv" responseTimeout="10000" doc:name="File"/>
</flow>
But still can't retrieve the objects

You can't use the xml namespace for this, it's reserved. People typically use xm or mulexml.
For example:
xmlns:xm="http://www.mulesoft.org/schema/mule/xml"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd"
...
<xm:xml-to-object-transformer />

In my app i have a xml declaring all the transformers i need to use so that i just reference them on my flows. See below. (You dont need to use iso-8859-1). This one considers you intend to use JAXB to bind the XML to POJO.
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mxml="http://www.mulesoft.org/schema/mule/xml"
xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
xmlns:spring="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.2/mule.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/3.2/mule-xml.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/3.1/mule-vm.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<mxml:jaxb-context
name="jaxb-context"
packageNames="my.package.v1:my.package.v2:soap.package" />
<mxml:jaxb-xml-to-object-transformer
name="xml-to-object"
encoding="ISO-8859-1"
jaxbContext-ref="jaxb-context"
ignoreBadInput="true" />
<mxml:jaxb-object-to-xml-transformer
name="object-to-xml"
encoding="ISO-8859-1"
jaxbContext-ref="jaxb-context"
returnClass="java.lang.String"
ignoreBadInput="true" />
<byte-array-to-string-transformer
name="byte-array-to-string"
encoding="ISO-8859-1"
returnClass="java.lang.String" />
<string-to-byte-array-transformer
name="string-to-byte-array"
encoding="ISO-8859-1"
returnClass="byte[]" />
<custom-transformer
name="xml-to-xml-stream-reader"
class="org.mule.module.xml.transformer.XmlToXMLStreamReader" />
<object-to-string-transformer name="object-to-string" />
<flow name="ByteArrayToObjectXml">
<vm:inbound-endpoint
path="myapp/conversor/byte-array-to-object-xml"
exchange-pattern="request-response" />
<transformer ref="byte-array-to-string" />
<transformer ref="xml-to-object" />
</flow>

I have resolved it now. The major issue is that one must have a valid and correct JAXB Binding Class. Thanks all

Related

How to convert SFTP byte payload to java in dataweave

I am trying to convert SFTP byte payload to java using dataweave so I can loop through the input records using an existing flow. How do I do this?
I have set it to java in Dataweave and preview shoulds the correct output but when I run it it is a byte and I transform to e.g. String before the Transform message connector it remains as a string after the transform message connector too.
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:ftp="http://www.mulesoft.org/schema/mule/ftp" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:sftp="http://www.mulesoft.org/schema/mule/sftp" xmlns:batch="http://www.mulesoft.org/schema/mule/batch" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/sftp http://www.mulesoft.org/schema/mule/sftp/current/mule-sftp.xsd
http://www.mulesoft.org/schema/mule/batch http://www.mulesoft.org/schema/mule/batch/current/mule-batch.xsd
http://www.mulesoft.org/schema/mule/ee/ftp http://www.mulesoft.org/schema/mule/ee/ftp/current/mule-ftp-ee.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ftp http://www.mulesoft.org/schema/mule/ftp/current/mule-ftp.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd">
<http:request-config name="HTTP_Request_Configuration" host="${host}" port="${userprocess.port}" doc:name="HTTP Request Configuration"></http:request-config>
<sftp:connector name="SFTP" validateConnections="true" doc:name="SFTP" pollingFrequency="1000000"/>
<flow name="userexperienceFlow" >
<sftp:inbound-endpoint connector-ref="SFTP" host="localhost" port="2222" path="//input" user="${ftp.user}" password="${ftp.password}" responseTimeout="10000" doc:name="SFTP"/>
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
payload]]></dw:set-payload>
</dw:transform-message>
<logger level="INFO" doc:name="Logger"/>
<sftp:outbound-endpoint exchange-pattern="one-way" host="localhost" port="2222" responseTimeout="10000" doc:name="SFTP" connector-ref="SFTP" password="${ftp.password}" path="//output" user="${ftp.user}" outputPattern="#[message.inboundProperties.originalFilename+'.processed']"/>
</flow>
</mule>
CSV file
UserId,UserStatus
ON1234,active
ON1235,active
Thanks
try to log message as #[message.payloadAs(java.lang.String)] instead of #[payload].
Also add <dw:input-payload mimeType="application/csv" /> line before this <dw:set-payload>
Hope this helps.

Importing Spring bean definition file using relative path

I am writing spring configurations for my project in JAVA. I am new to spring and not to able to figure out the errors related to spring.
I have the following file structure:
ProjectName
--> src
--> META-INF
--> jobs
-->edx
-->request-details.xml
-->clients.xml
-->daos.xml
-->environment.xml
--> request-details-edx-upload.xml
In META-INF/request-details-edx-upload.xml I import the following:
<import resource="jobs/environment.xml" />
<import resource="classpath:META-INF/jobs/edx/request-details.xml" />
<import resource="jobs/clients.xml" />
<import resource="jobs/daos.xml" />
But I get the following error:
Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:META-INF/jobs/edx/request-details.xml]
Offending resource: class path resource [META-INF/request-details-edx-upload.xml]; nested exception is org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Cannot locate BeanDefinitionDecorator for attribute [dataSetName]
Offending resource: class path resource [META-INF/jobs/edx/request-details.xml]
request-details.xml is something 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:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
">
<!-- RequestDetails EDX data upload dao -->
<bean id="requestDetailsDataUploadDao" class="com.amazon.edx.dao.DataUploadDaoEdxClientImpl"
context:providerName="scot"
context:subjectName="${edxRequestDetailsSubject}"
context:dataSetName="by-01-day"
context:keyName="${regionValue}"
context:edxClient-ref="edxClient"
/>
<util:list id="requestDetailsColumnMetaData" value-type="com.amazon.edx.transformer.ColumnMetaData">
<ref bean="Column"/>
<ref bean="Column1"/>
</util:list>
<bean id="Column" class="com.amazon.edx.transformer.ColumnMetaData"
context:attributeName="Id"
context:dataType="VARCHAR2"
context:columnDisplayName="IMS"
/>
<bean id="Column1" class="com.amazon.edx.transformer.ColumnMetaData"
context:attributeName="id1"
context:dataType="VARCHAR2"
context:columnDisplayName="IMS"
/>
<util:constant id="tabDelimiter"
static-field="com.amazon.edx.transformer.Delimiters.TAB_DELIMITER" />
<!-- RequestDetails data backup -->
<bean id="requestDetailsDataTransformer" class="com.amazon.edx.transformer.DataTransformerImpl"
context:dataFlattener-ref="requestDetailsDataFlattener"
context:columnMetadata-ref="requestDetailsColumnMetaData"
context:delimiter="{tabDelimiter}"
/>
</beans>
Is there anything obvious I'm doing wrong with my import of the relative path?
Thanks.
Seems it is wrong schema definition. Try to remove namspace xmlns:context="http://www.springframework.org/schema/context" from <beans> tag. Instead add xmlns:p="http://www.springframework.org/schema/p" namespace. Also you should replace all context: suffix with p: suffix. Proper xml using shortcut with the p-namespace should looks like following:
<?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:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- RequestDetails EDX data upload dao -->
<bean id="requestDetailsDataUploadDao" class="com.amazon.edx.dao.DataUploadDaoEdxClientImpl"
p:providerName="scot"
p:subjectName="${edxRequestDetailsSubject}"
p:dataSetName="by-01-day"
p:keyName="${regionValue}"
p:edxClient-ref="edxClient"
/>
....
</beans>

What does "The prefix "vm" for element "vm:endpoint" is not bound." means and how to solve it?

I am trying to integrate mule with drools, I have mainly followed the tutorial here but I am getting this error :
"The prefix "vm" for element "vm:endpoint" is not bound."
can anyone help me solve this problem?
following is my .drl file and mule flow files.
<?xml version="1.0" encoding="UTF-8"?>
<mule
xmlns:bpm="http://www.mulesoft.org/schema/mule/bpm"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.6.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/bpm http://www.mulesoft.org/schema/mule/bpm/3.6/mule-bpm.xsd">
<http:listener-config name="HTTP_Listener_Configuration"
host="localhost" port="8084" doc:name="HTTP Listener Configuration" />
<spring:beans>
<spring:bean id="dto" class="com.hamgam.hit.esb.XMLDTO" scope="singleton" />
<spring:bean id="dto-convertor" class="com.hamgam.hit.esb.XMLToDTO" scope="singleton" />
<spring:bean id="rule-convertor" class="com.hamgam.hit.esb.RuleXMLToDRL" scope="singleton" />
</spring:beans>
<bpm:drools />
<flow name="basic_tutorialFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/{name}" doc:name="HTTP" />
<vm:endpoint name = "emc" path="drool.msg" />
<set-variable variableName="name" value="#[message.inboundProperties['http.uri.params']['name']]" />
<set-variable variableName="msg" value="#[payload]"/>
<logger level="DEBUG" doc:name="Logger" message="Current path is flowVars['name']" />
<expression-component>
app.registry['dto'] = app.registry['dto-convertor'].convert(flowVars['name']);
</expression-component>
<bpm:rules rulesDefinition="rule.drl" cepMode="true" initialFacts-ref="dto" entryPoint="XMLDTO stream" />
<choice doc:name="Choice">
<when expression="#[flowVars.emc == 'KeyValueMatched']">
<set-payload doc:name="Set Payload" value="#['KeyValueMatched! You are with us on 8084. ' + flowVars['name'] + '. Today is ' + server.dateTime.format('dd/MM/yy') + '.' ]" />
</when>
<otherwise>
<set-payload doc:name="Set Payload" value="#['Nothing! You are with us on 8084. ' + flowVars['name'] + '. Today is ' + server.dateTime.format('dd/MM/yy') + '.' ]" />
</otherwise>
</choice>
</flow>
</mule>
and the drl file :
global org.mule.module.bpm.MessageService mule;
import com.me.mit.esb.*;
rule "La Vita Ebela"
dialect "mvel"
when
$x:XMLDTO( inputXML == "inputXML" )
then
mule.generateMessage("emc", "KeyValueMatched", null, MessageExchangePattern.ONE_WAY);
end
You have to add XML namespache and scema reference to your xml file:
XML namespace:
xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
XML Schema Location:
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/3.6/mule-vm.xsd
So it looks like so:
<mule
xmlns:bpm="http://www.mulesoft.org/schema/mule/bpm"
xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.6.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/bpm http://www.mulesoft.org/schema/mule/bpm/3.6/mule-bpm.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/3.6/mule-vm.xsd
">
Replase vm:endpoint by vm:inbound-endpoint. As Jens said, add vm schema to your mule-config. And don't forget to include mule-transports-vm-3.6.0.jar to your test classpath.

Embedding Restlets in Mule can't work in Mule 3.1.2

I configure a flow in mule-config.xml based on the documentation(click here). Here is the content in mule-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:tcp="http://www.mulesoft.org/schema/mule/tcp"
xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf"
xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:restlet="http://www.mulesource.org/schema/mule/restlet"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.1/mule.xsd
http://www.mulesoft.org/schema/mule/tcp http://www.mulesoft.org/schema/mule/tcp/3.1/mule-tcp.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/3.1/mule-vm.xsd
http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/3.1/mule-cxf.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/3.1/mule-http.xsd
http://www.mulesoft.org/schema/mule/restlet http://www.mulesoft.org/schema/mule/restlet/3.1/mule-restlet.xsd">
<description>
This configuration uses an HTTP endpoint to receive requests.
</description>
<flow name="fuxk">
<inbound-endpoint address="http://localhost:9002">
</inbound-endpoint>
<restlet:component restlet-ref="helloWorld"/>
</flow>
<spring:bean id="helloWorld" class="com.ggd543.mulerestletdemo.HelloWorldApplication"/>
</mule>
This configuration uses an HTTP endpoint to receive requests.But when I start up the mule instance , I got the following error:
It seams that the documentation is based on mule 2.x . How do I change the configuration in mule-config.xml for Mule 3.1.2 ?
Replace mulesource.org with mulesoft.org in all your Restlet namespace definitions.

Spring and JMS Integration Configuration file error

I keep having this error when I'm trying to integrate Spring JMS into my current project. It's driving me up the wall and I'm not entirely sure how to fix it as I'm new to Spring.
The prefix "jms" for element "jms:listener-container" is not bound.
The code in question is this,
<jms:listener-container container-type="default" connection-factory="connectionFactory" acknowledge="auto">
<jms:listener destination="TESTQUEUE" ref="simpleMessageListener" method="onMessage" />
</jms:listener-container>
I'm pretty sure it has to do with the jms: namespace but I don't know how to fix it as before my program was complaining about the p: namespace so I had to change it to property name="some value" reference="someReference"
Make sure you have the jms namespace in your context file:
<?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:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd">
<!-- <bean/> definitions here -->
</beans>
See http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/jms.html for details.

Categories

Resources