I'm currently writing a rather simple mule-flow that uses basic authentication
<custom-transformer class="com.org.HttpRequestToMyRequest" name="HttpRequestToMyRequest" doc:name="Java"/>
<flow name="Input_Flow" doc:name="Input_Flow">
<http:inbound-endpoint exchange-pattern="request-response" host="http://12.23.34.45" port="1234" path="/foo">
<mule-ss:http-security-filter realm="mule-realm"/>
</http:inbound-endpoint>
<flow-ref name="Sorting_Flow" doc:name="Sorting flow"/>
</flow>
<sub-flow name="Sorting_Flow" doc:name="Sorting_Flow">
<transformer ref="HttpRequestToMyRequest" doc:name="Transform Http Request to POJO with interesting methods"/>
<enricher target="#[variable:authorized]">
<http:outbound-endpoint exchange-pattern="request-response" host="12.34.45.56" port="1234" path="foo/bar" method="GET" disableTransportTransformer="true"
responseTransformer-refs="ObjectToString" doc:name="Authorization Service"/>
</enricher>
<choice doc:name="Choice">
<when expression="header:INVOCATION:authorized=true">
<processor-chain>
<component class="com.org.MyClass"/>
<transformer ref="MyObjToString"/>
<http:response-builder status="200" contentType="text/xml" doc:name="HTTP Response Builder"/>
</processor-chain>
</when>
<when SOMETHING ELSE>
DO SOMETHING ELSE
</when>
</choice>
</sub-flow>
If I remove the http-security-filter then this flow works as expected and the payload in the MuleMessage is of the type MyRequest returned by the HttpRequestToMyRequest transformer. But once i add the security-filter the enricher changes my payload from the expected type to a byte[]. If i convert the byte[] to a String then it turns out to be the instancename of MyRequest (com.org.MyRequest#15ae9009).
Any help or ideas would be appreciated.
Edit
Just to be clear. Here are three flow xml fragments where two behave well and the last one has the strange behaviour i don't understand.
This flow, where I've commented out the security-filter on the inbound endpoint, works as expected and the ClassNameLogger statements prints out, in order, String, MyRequest, MyRequest.
<custom-transformer class="com.org.HttpRequestToMyRequest" name="HttpRequestToMyRequest" doc:name="Java"/>
<custom-transformer class="com.org.ClassNameLogger" name="ClassNameLogger" doc:name="Java"/>
<flow name="Input_Flow" doc:name="Input_Flow">
<http:inbound-endpoint exchange-pattern="request-response" host="http://12.23.34.45" port="1234" path="/foo">
<!-- <mule-ss:http-security-filter realm="mule-realm"/> -->
</http:inbound-endpoint>
<flow-ref name="Sorting_Flow" doc:name="Sorting flow"/>
</flow>
<sub-flow name="Sorting_Flow" doc:name="Sorting_Flow">
<transformer ref="ClassNameLogger" doc:name="Logs The Class Name of MuleMessage payload"/>
<transformer ref="HttpRequestToMyRequest" doc:name="Transform Http Request to POJO with interesting methods"/>
<transformer ref="ClassNameLogger" doc:name="Logs The Class Name of MuleMessage payload"/>
<enricher target="#[variable:authorized]">
<http:outbound-endpoint exchange-pattern="request-response" host="12.34.45.56" port="1234" path="foo/bar" method="GET" disableTransportTransformer="true"
responseTransformer-refs="ObjectToString" doc:name="Authorization Service"/>
</enricher>
<transformer ref="ClassNameLogger" doc:name="Logs The Class Name of MuleMessage payload"/>
<choice doc:name="Choice">
<when expression="header:INVOCATION:authorized=true">
<processor-chain>
<component class="com.org.MyClass"/>
<transformer ref="MyObjToString"/>
<http:response-builder status="200" contentType="text/xml" doc:name="HTTP Response Builder"/>
</processor-chain>
</when>
<when SOMETHING ELSE>
DO SOMETHING ELSE
</when>
</choice>
</sub-flow>
This flow, where I've commented out the enricher, works as expected, aside from the fact that it doesn't enter the choise since the variable set by the enricher is not defined. The ClassNameLogger statements prints out, in order, String, MyRequest, MyRequest.
<custom-transformer class="com.org.HttpRequestToMyRequest" name="HttpRequestToMyRequest" doc:name="Java"/>
<custom-transformer class="com.org.ClassNameLogger" name="ClassNameLogger" doc:name="Java"/>
<flow name="Input_Flow" doc:name="Input_Flow">
<http:inbound-endpoint exchange-pattern="request-response" host="http://12.23.34.45" port="1234" path="/foo">
<mule-ss:http-security-filter realm="mule-realm"/>
</http:inbound-endpoint>
<flow-ref name="Sorting_Flow" doc:name="Sorting flow"/>
</flow>
<sub-flow name="Sorting_Flow" doc:name="Sorting_Flow">
<transformer ref="ClassNameLogger" doc:name="Logs The Class Name of MuleMessage payload"/>
<transformer ref="HttpRequestToMyRequest" doc:name="Transform Http Request to POJO with interesting methods"/>
<transformer ref="ClassNameLogger" doc:name="Logs The Class Name of MuleMessage payload"/>
<!-- <enricher target="#[variable:authorized]">
<http:outbound-endpoint exchange-pattern="request-response" host="12.34.45.56" port="1234" path="foo/bar" method="GET" disableTransportTransformer="true"
responseTransformer-refs="ObjectToString" doc:name="Authorization Service"/>
</enricher> -->
<transformer ref="ClassNameLogger" doc:name="Logs The Class Name of MuleMessage payload"/>
<choice doc:name="Choice">
<when expression="header:INVOCATION:authorized=true">
<processor-chain>
<component class="com.org.MyClass"/>
<transformer ref="MyObjToString"/>
<http:response-builder status="200" contentType="text/xml" doc:name="HTTP Response Builder"/>
</processor-chain>
</when>
<when SOMETHING ELSE>
DO SOMETHING ELSE
</when>
</choice>
</sub-flow>
This flow, where I have both the enricher and the security-filter in place, doesn't work as expected and the ClassNameLogger statements prints out, in order, String, MyRequest, byte[].
<custom-transformer class="com.org.HttpRequestToMyRequest" name="HttpRequestToMyRequest" doc:name="Java"/>
<custom-transformer class="com.org.ClassNameLogger" name="ClassNameLogger" doc:name="Java"/>
<flow name="Input_Flow" doc:name="Input_Flow">
<http:inbound-endpoint exchange-pattern="request-response" host="http://12.23.34.45" port="1234" path="/foo">
<mule-ss:http-security-filter realm="mule-realm"/>
</http:inbound-endpoint>
<flow-ref name="Sorting_Flow" doc:name="Sorting flow"/>
</flow>
<sub-flow name="Sorting_Flow" doc:name="Sorting_Flow">
<transformer ref="ClassNameLogger" doc:name="Logs The Class Name of MuleMessage payload"/>
<transformer ref="HttpRequestToMyRequest" doc:name="Transform Http Request to POJO with interesting methods"/>
<transformer ref="ClassNameLogger" doc:name="Logs The Class Name of MuleMessage payload"/>
<enricher target="#[variable:authorized]">
<http:outbound-endpoint exchange-pattern="request-response" host="12.34.45.56" port="1234" path="foo/bar" method="GET" disableTransportTransformer="true"
responseTransformer-refs="ObjectToString" doc:name="Authorization Service"/>
</enricher>
<transformer ref="ClassNameLogger" doc:name="Logs The Class Name of MuleMessage payload"/>
<choice doc:name="Choice">
<when expression="header:INVOCATION:authorized=true">
<processor-chain>
<component class="com.org.MyClass"/>
<transformer ref="MyObjToString"/>
<http:response-builder status="200" contentType="text/xml" doc:name="HTTP Response Builder"/>
</processor-chain>
</when>
<when SOMETHING ELSE>
DO SOMETHING ELSE
</when>
</choice>
</sub-flow>
Edit 2
The code in the HttpRequestToMyRequest transformer
public class HttpRequestToMyRequest extends AbstractMessageTransformer{
#Override
public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException {
if("/foo".equals(message.getInboundProperty("http.request.path")){
return new MyFooRequest();
}
if("/bar".equals(message.getInboundParameter("http.request.path")){
return new MyBarRequest();
}
else return new MyEmptyRequest();
}
}
Edit 3
Apparently this was something Mule 3.3 specific. Once I upgraded to 3.4 everything started working as expected.
With Mule 3.4, I notice no difference of payload type with or without the http-security-filter element.
It is either a java.lang.String if the request is an HTTP GET or java.io.InputStream if it is a POST or PUT.
Since you are on 3.3, you are possibly hitting a bug that is gone in 3.4, since I don't have any issue. If that is the case, finding a workaround for the issue on 3.3 would be hairy: upgrading to 3.4 is the path forward.
Related
I have this mule flow , where its polling the source folder to read the text file which I am adding as a attachment and sending through REST call , the same attachment I am trying to read in the different flow but inbound attachment is coming as null , please have a look into the code and help me on this.
<flow name="createAttachment" doc:description="Reading file and sending as attachment.">
<file:inbound-endpoint path="src/test/resources/in/attachment/" responseTimeout="10000" doc:name="File"/>
<file:file-to-byte-array-transformer doc:name="File to Byte Array"/>
<!-- <set-attachment attachmentName="#[originalFilename]" value="#[payload]" contentType="multipart/form-data" doc:name="Attachment"/> -->
<set-attachment attachmentName="#[originalFilename]" value="#[payload]" contentType="multipart/form-data" doc:name="Attachment" />
<http:request config-ref="HTTP_Request_Configuration" path="attachment/excel" method="POST" doc:name="HTTP"/>
</flow>
<flow name="readAttachment">
<http:listener config-ref="HTTP_Listener_Configuration" path="attachment/excel" allowedMethods="POST" parseRequest="false" />
<set-payload value="#[message.inboundAttachments['myattachment.txt']]" doc:name="Retrieve Attachments"/>
<set-payload value="#[payload.getInputStream() ]" doc:name="Get Inputstream from Payload"/>
<file:outbound-endpoint path="src/test/resources/out/attachment" responseTimeout="10000" doc:name="File" outputPattern="#[server.dateTime.toString()].pdf"/>
</flow>
I used the following:
<flow name="readAttachment">
<http:listener config-ref="HTTP_Listener_Configuration"
path="/" allowedMethods="POST" parseRequest="false" doc:name="HTTP" />
<byte-array-to-string-transformer doc:name="Byte Array to String"/>
<logger message="#[payload]" level="INFO" doc:name="Logger" /><file:outbound-endpoint path="src/test/resources" connector-ref="File" responseTimeout="10000" doc:name="File"/>
</flow>
When the attachment was received it was automatically parsed to be the payload, so it was just a case of turning the byte array to string.
I hope this helps
I have a scenario where in I need to call the service until the http status of the webservice call is 200.
The subflow(SearchSubFlow) works fine when it is not in Until Successful but when it is inside Until Sucessful it falls over with the below error at Byte Array to String in SearchSubFlow.
org.mule.api.transformer.TransformerMessagingException: null (java.lang.NullPointerException). Message payload is of type: WeaveMessageProcessor$WeaveOutputHandler.
<flow name="CheckFlow">
<http:listener config-ref="HTTP_Internal_Listener" path="${CONNECTION_URI}" doc:name="HTTP"/>
<cxf:proxy-service payload="envelope" doc:name="CXF" namespace="http://abc/RetrieveService" service="RetrieveService" soapVersion="1.2" wsdlLocation="${WSDL_RETRIEVE}"/>
<mulexml:dom-to-xml-transformer doc:name="DOM to XML"/>
<set-variable variableName="Ref" value="#[xpath3('//*:CheckRequest/*:Ref')]" doc:name="Get External Ref Number"/>
<until-successful objectStore-ref="objectStore" maxRetries="${RESEND_COUNTER}" millisBetweenRetries="${RESEND_INTERVAL}" doc:name="Retry to check policy status" >
<flow-ref name="SearchSubFlow" doc:name="SearchSubFlow"/>
</until-successful>
</flow>
<sub-flow name="SearchSubFlow">
<dw:transform-message doc:name="PolicySearchWS Request">
<dw:set-payload><![CDATA[%dw 1.0
%output application/xml
%namespace soapenv http://schemas.xmlsoap.org/soap/envelope/
---
{
soapenv#Envelope: {
soapenv#Header: {
InsureJHeader:{
role: "${HEADER_ROLE}"
}
},
soapenv#Body: {
arguments:{
externalQuoteReference: flowVars.Ref
}
}
}
}]]></dw:set-payload>
</dw:transform-message>
<byte-array-to-string-transformer doc:name="Byte Array to String"/>
<message-properties-transformer scope="invocation" doc:name="Set Service Endpoint">
<add-message-property key="serviceEndPoint" value="${APP_SYSTEM}/${SELECT_SERVICE_BASE_URL}/${.CREATE.ADDRESS}"/>
</message-properties-transformer>
<message-properties-transformer doc:name="Set SOAP Action">
<add-message-property key="SOAPAction" value="${CREATE.SOAPACTION}"/>
</message-properties-transformer>
<cxf:proxy-client payload="envelope" doc:name="Proxy"/>
<http:request config-ref="HTTP_Select_Request" path="#[serviceEndPoint]" method="POST" doc:name="Call Search WS">
<http:success-status-code-validator values="200,500"/>
</http:request>
<mulexml:dom-to-xml-transformer doc:name="DOM to XML"/>
<message-properties-transformer scope="invocation" doc:name="Message Properties">
<add-message-property key="PolicySearchErrorMessage" value="#[xpath3('//*:Fault/*:ProcessingFault/*:message')]"/>
<add-message-property key="httpStatus" value="#[message.inboundProperties.'http.status']"/>
</message-properties-transformer>
</sub-flow>
Highly appreciate your help!
Not referencing to the objectstore as below resolved the issue
<until-successful maxRetries=5 doc:name="Retry to check status" >
<flow-ref name="SearchSubFlow" doc:name="SearchSubFlow"/>
</until-successful>
use <mulexml:dom-to-xml-transformer doc:name="DOM to XML"/> instead of <byte-array-to-string-transformer doc:name="Byte Array to String"/>
Hope this helps.
mule code
<flow name="getDetails">
<http:inbound-endpoint doc:description="This endpoint receives an HTTP message." doc:name="HTTP" exchange-pattern="request-response" host="localhost" port="8081" path="getDetails"/>
<json:xml-to-json-transformer doc:name="XML to JSON" mimeType="text/json" ignoreBadInput="true"/>
<logger message="xml to json output #[message.payload]" level="INFO" doc:name="Logger"/>
<logger message=" custom header username ... #[message.outboundProperties.get('http.headers.username')]" level="INFO" />
<logger message=" custom header username ... #[message.inboundProperties.get('http.headers.username')]" level="INFO" />
<http:outbound-endpoint exchange-pattern="request-response" address="http://localhost:8081/callReservation" method="POST" contentType="application/json" doc:name="HTTP"/>
</flow>
Here i give input as employee xml in the body
and in header i give username=user1 password=pwd1 in postman rest client
<?xml version="1.0" encoding="UTF-8"?>
<employee>
<name>abc</name>
<address>add1</address>
<phone>1212</phone>
</employee>
I got like error like below
com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '{' (code 123) in prolog; expected '<'
at [row,col {unknown-source}]: [1,1] (javax.xml.transform.TransformerException) (org.mule.api.transformer.TransformerException). Message payload is of type: String
using header and xmlto json conversion is mandatory for me.
can anyone help me out
Following code works fine for me. HTH.
<flow name="getDetails" doc:name="getDetails">
<http:inbound-endpoint doc:description="This endpoint receives an HTTP message."
doc:name="HTTP" exchange-pattern="request-response" host="localhost"
port="1000" path="getDetails" />
<json:xml-to-json-transformer doc:name="XML to JSON"
mimeType="text/json" ignoreBadInput="true" />
<logger message="xml to json output #[message.payload]" level="INFO"
doc:name="Logger" />
<logger
message=" custom header username ... #[message.inboundProperties.'http.headers'.username]"
level="INFO" doc:name="Logger"/>
<http:outbound-endpoint exchange-pattern="request-response"
address="http://localhost:1001/callReservation" method="POST"
contentType="application/json" doc:name="HTTP" />
</flow>
<flow name="testFlow1" doc:name="testFlow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="1001" path="callReservation" doc:name="HTTP"/>
<json:json-to-xml-transformer doc:name="JSON to XML"/>
<logger message="Data received : #[payload]" level="INFO" doc:name="Logger"/>
</flow>
I am getting the value using following :-
<logger message=" custom header username ... #[message.inboundProperties['username']]"
level="INFO" doc:name="Logger"/>
You need to pass the XML in the body of postman
UPDATE
I am getting the value as below :-
Use the data mapper after the http:outBound to convert to json response.
<flow name="listobjects">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="listobjects" contentType="text/plain" doc:name="HTTP"/>
<s3:list-objects config-ref="Amazon_S3" bucketName="demo" doc:name="Amazon S3" maxKeys="5" />
<!-- <payload-type-filter expectedType="java.util.List" doc:name="Payload"/> -->
<foreach collection="#[payload]" doc:name="For Each">
<!-- <foreach doc:name="For Each file"> -->
<logger message=" inside foreach...... #[payload.getKey()] ...." level="INFO" doc:name="Logger" />
<s3:get-object-content config-ref="Amazon_S3" bucketName="demo" key="#[payload.getKey()]" doc:name="Amazon S3"/>
<object-to-byte-array-transformer/>
<file:outbound-endpoint path="C:\output" responseTimeout="10000" doc:name="File" outputPattern="#[payload.getKey()] "></file:outbound-endpoint>
</foreach>
</flow>
I have bucket name called demo.
In that bucket I have 3 pdf files. I want to download all files and put it in c:\output folder.
I hit my url like http://localhost:8081/listobjects.
But I got the error:
Could not find a transformer to transform "CollectionDataType{type=org.mule.module.s3.simpleapi.SimpleAmazonS3AmazonDevKitImpl$S3ObjectSummaryIterable, itemType=com.amazonaws.services.s3.model.S3ObjectSummary, mimeType='/'}" to "SimpleDataType{type=org.mule.api.transport.OutputHandler, mimeType='/'}". (org.mule.api.transformer.TransformerException) (org.mule.api.transformer.TransformerException). Message payload is of type: SimpleAmazonS3AmazonDevKitImpl$S3ObjectSummaryIterable
The error occurs because after the foreach processor the payload is an instance of an S3 class, and you haven't specified any Content-Type to return. So Mule tries to transform the S3 instance to the default SimpleDataType and fails.
One way to solve it is simply to add something like
<set-property propertyName="Content-Type" value="application/json" doc:name="Content-Type" />
<set-payload value="{'result': 'ok'}"/>
at the end to make it explicit.
Also note that in your flow after running:
<object-to-byte-array-transformer/>
the S3 payload is gone, so #[payload.getKey()] will fail in the next processor:
<file:outbound-endpoint path="C:\output" responseTimeout="10000" doc:name="File" outputPattern="#[payload.getKey()] "></file:outbound-endpoint>
I've run this without problems:
<flow name="listobjects">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8083" path="listobjects" contentType="text/plain" doc:name="HTTP"/>
<s3:list-objects config-ref="Amazon_S3" bucketName="mule_test" doc:name="Amazon S3" maxKeys="5" />
<foreach collection="#[payload]" doc:name="For Each">
<logger message=" inside foreach...... #[payload.getKey()] ...." level="INFO" doc:name="Logger" />
<set-variable variableName="fileKey" value="#[payload.getKey()]" doc:name="Variable" />
<s3:get-object-content config-ref="Amazon_S3" bucketName="#[payload.getBucketName()]" key="#[payload.getKey()]" doc:name="Amazon S3"/>
<object-to-byte-array-transformer/>
<file:outbound-endpoint path="/tmp" responseTimeout="10000" doc:name="File" outputPattern="#[flowVars.fileKey] "></file:outbound-endpoint>
</foreach>
<set-property propertyName="Content-Type" value="application/json" doc:name="Content-Type" />
<set-payload value="{'result': 'ok'}"/>
</flow>
I have a back end service that I need to throttle access to. I'm trying to use the approach described here: http://blogs.mulesoft.org/synchronous-and-asynchronous-throttling-2/
I started with a simple pass through flow that receives a SOAP request and forwards it. When I hit this using the SOAPUI utility, I get the expected response in a second or two.
<http:connector name="httpConnector" doc:name="HTTP\HTTPS">
<receiver-threading-profile maxThreadsActive="1" maxBufferSize="100" />
</http:connector>
<jms:activemq-connector name="amqConnector" brokerURL="tcp://localhost:61616" specification="1.1" doc:name="AMQ" />
<flow name="Flow1" processingStrategy="synchronous" doc:name="Flow1">
<http:inbound-endpoint exchange-pattern="request-response"
host="localhost" port="8088" path="test" doc:name="HTTP"
mimeType="text/xml" encoding="UTF-8" connector-ref="httpConnector"/>
<http:outbound-endpoint
address="http://dnbdirect-api.dnb.com/DnBAPI-11"
exchange-pattern="request-response" doc:name="HTTP" mimeType="text/xml"/>
</flow>
If I then move the outbound call to a separate flow and add in the request-reply block, the behavior changes. I get no response back (nor do I get the "After queue" message from the logger) and SOAPUI eventually times out.
<http:connector name="httpConnector" doc:name="HTTP\HTTPS">
<receiver-threading-profile maxThreadsActive="1" maxBufferSize="100" />
</http:connector>
<jms:activemq-connector name="amqConnector" brokerURL="tcp://localhost:61616" specification="1.1" doc:name="AMQ" />
<flow name="Flow1" processingStrategy="synchronous" doc:name="Flow1">
<http:inbound-endpoint exchange-pattern="request-response"
host="localhost" port="8088" path="test" doc:name="HTTP"
mimeType="text/xml" encoding="UTF-8" connector-ref="httpConnector"/>
<message-properties-transformer doc:name="Message Properties">
<add-message-property key="AMQ_SCHEDULED_DELAY" value="5000"/>
</message-properties-transformer>
<logger message="Before queue" level="INFO"/>
<request-reply>
<jms:outbound-endpoint queue="request" connector-ref="amqConnector"></jms:outbound-endpoint>
<jms:inbound-endpoint queue="response" connector-ref="amqConnector"></jms:inbound-endpoint>
</request-reply>
<logger message="After queue" level="INFO"/>
</flow>
<flow name="flow2" doc:name="Flow2">
<jms:inbound-endpoint queue="request" connector-ref="amqConnector" doc:name="JMS"/>
<http:outbound-endpoint
address="http://dnbdirect-api.dnb.com/DnBAPI-11"
exchange-pattern="request-response" doc:name="HTTP" mimeType="text/xml" />
</flow>
The throttling behavior works as I see the delays if I pull out the call to the back end service. But I can't get it to work with the service call there.
What am I missing?
I found that the message's payload will be "ArrayList" after "request-reply".
so i add a java component to split it, then the result will be corrected.
#Override
public Object onCall(MuleEventContext eventContext) throws Exception {
MuleMessage message = eventContext.getMessage();
//int groupSize = message.getCorrelationGroupSize();
//System.out.println("############# correlationGroupSize: " + groupSize);
Object payload = message.getPayload();
if (payload != null && payload instanceof ArrayList) {
//message.setPayload(((ArrayList)payload).get(0));
return ((ArrayList)payload).get(0);
}
return message.getPayload();
}
the completed flow is:
<message-properties-transformer doc:name="Message Properties">
<add-message-property key="AMQ_SCHEDULED_DELAY" value="10000"/>
</message-properties-transformer>
<request-reply storePrefix="mainFlow">
<jms:outbound-endpoint queue="request" connector-ref="amqConnector" doc:name="JMS"></jms:outbound-endpoint>
<jms:inbound-endpoint queue="response" connector-ref="amqConnector" doc:name="JMS"></jms:inbound-endpoint>
</request-reply>
<component class="com.neusoft.fx.JmsMessageTransformer" doc:name="Java"/>
<message-properties-transformer doc:name="Set Content Type">
<delete-message-property key="Content-type" />
<add-message-property key="Content-Type" value="text/xml"/>
</message-properties-transformer>
<logger message="----- LOGGER ----- after #[groovy:message.toString()]" level="INFO" doc:name="Logger" />
</flow>
Try adding the following before the HTTP outbound endpoint in flow2:
<copy-properties propertyName="MULE_*"/>