I'm using Mule EE 3.5.2.
I'm posting an HTTP request (PostMan) with a cookie header, to an incoming HTTP end point in Mule. How can I read this Cookie?
In practice this cookie will be coming through an NGinX proxy; i need it to pass to another application.
First make sure your connector have enableCookies="true".
Then you'll find a inboundProperty called cookies of type Lorg.apache.commons.httpclient.Cookie.
To access them just #[message.inboundProperties['cookies'].
Here's how to save the cookies from a rest response in a session variable without a custom java class.
<set-session-variable variableName="incomingCookies" value="#[org.mule.transport.http.CookieHelper.parseCookiesAsAClient(message.inboundProperties['set-cookie'],null)]" doc:name="Set incomingCookies as Session Variable"/>
<set-variable variableName="cookie-name" value="#[org.mule.transport.http.CookieHelper.getCookieValueFromCookies(incomingCookieMap,'cookie-name')]" doc:name=“Set cookie-name as Flow Variable”/>
You can use a similar approach for extracting cookies from a rest request using the parseCookiesAsAServer method from the CookieHelper class.
More info on the CookieHelper class is here https://www.mulesoft.org/docs/site/3.8.0/apidocs/org/mule/transport/http/CookieHelper.html
That doesn't work anymore with the new http:listener component.
Setting that property will give :
org.xml.sax.SAXParseException: cvc-complex-type.3.2.2: Attribute 'enableCookies' is not allowed to appear in element 'http:listener'.
So how to do this with the new http:listener component...
A problem I had was that I needed to access the cookies that came in and Mule only provides the cookies in an unformatted string.
So this is an option a friend of mine developed, which I enhanced a bit to get easy access to the cookies in a flow:
package transformers;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.mule.api.MuleMessage;
import org.mule.api.transformer.TransformerException;
import org.mule.api.transport.PropertyScope;
import org.mule.transformer.AbstractMessageTransformer;
import org.mule.transport.http.CookieHelper;
import org.apache.commons.httpclient.Cookie;
public class CookieGrabber extends AbstractMessageTransformer {
public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException {
Object _CookieHeader = message.getInboundProperty("Cookie");
List<Cookie> _CookieList = null;
Map<String,String> _CookieMap = new HashMap<String,String>();
try {
//Grab the cookies from the header and put them into a List
_CookieList = (List<Cookie>) Arrays.asList(CookieHelper.parseCookiesAsAServer(_CookieHeader.toString(),
new URI("" + message.getInboundProperty("host"))));
//And put them in a convenient List which can be accessed from the flow
message.setProperty("incomingCookieList", _CookieList, PropertyScope.SESSION);
//Let's also put them in a nice Map, since incoming cookies will
//usually only contain a name and a value, so let's get easy access to them by their name.
for (Cookie _Cookie : _CookieList){
_CookieMap.put(_Cookie.getName(), _Cookie.getValue());
}
message.setProperty("incomingCookieMap", _CookieMap, PropertyScope.SESSION);
} catch (URISyntaxException e) {
e.printStackTrace();
}
return message;
}
}
Then there is this flow example which shows how to use this code snippet.
It contains a listener that sets some cookies, forwards it to a "proxy", which will read the cookies, but also forward the request to another endpoint, making it a transparent proxy, but which does read the cookies in the process.
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:json="http://www.mulesoft.org/schema/mule/json"
xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
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/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
<custom-transformer class="transformers.CookieGrabber"
name="MyCookieTranformer"
doc:name="Java"/>
<http:listener-config name="HTTP_Configuration_CookieHandlerExample"
host="0.0.0.0"
port="8080"
doc:name="HTTP Listener Configuration"/>
<http:request-config name="HTTP_Request_Configuration"
host="localhost"
port="8080"
doc:name="HTTP Request Configuration"/>
<flow name="CookieSetterFlow">
<http:listener config-ref="HTTP_Configuration_CookieHandlerExample"
path="/setCookies/*"
doc:name="setCookies"
doc:description="Call this module by entering http://localhost:8080/setCookie"/>
<message-properties-transformer doc:name="Set the cookies"
doc:description="Set some random cookies in the header">
<add-message-property key="Cookie"
value="token=abcde; name=dennis"/>
</message-properties-transformer>
<http:request config-ref="HTTP_Request_Configuration"
path="/proxyCookie"
method="GET"
doc:name="call proxyCookies"
doc:description="Invoke the cookieReceiver with the cookies we've just set. Note the failure status code validator with a non-existing http code. It's a nasty bug, but it works like this...">
<http:failure-status-code-validator values="00000"/>
</http:request>
</flow>
<flow name="CookieProxyFlow">
<http:listener config-ref="HTTP_Configuration_CookieHandlerExample"
path="/proxyCookie"
doc:name="proxyCookies"
doc:description="This connector will proxy the cookieReceiver"/>
<transformer ref="MyCookieTranformer"
doc:name="GrabCookies"
doc:description="Use our custom transformers.CookieGrabber class to put the cookies in a nice java.util.List in a session variable."/>
<logger message="CookieProxy: Value of cookie "token": "#[sessionVars.incomingCookieMap.get('token')]"."
level="INFO"
doc:name="Have a cookie!"
doc:description="Let get a cookie value, simply by referring the name of it as the key from our map"/>
<flow-ref name="copy-and-clean-headers"
doc:name="copy-and-clean-headers"
doc:description="Cope the headers and clean the Mule stuff from the headers to forward it clean to the receiver."/>
<set-property propertyName="host"
value="localhost"
doc:name="Set Host"
doc:description="Now not really necessary, but you'll probably want to set the hostname to the actual service endpoint."/>
<http:request config-ref="HTTP_Request_Configuration"
path="/receiveCookie"
method="GET"
doc:name="forward to receiveCookies"
doc:description="Invoke the cookieReceiver.">
<http:failure-status-code-validator values="00000"/>
</http:request>
<flow-ref name="copy-and-clean-headers"
doc:name="copy-and-clean-headers"
doc:description="Again copy the headers and clean the Mule http stuff."/>
</flow>
<sub-flow name="copy-and-clean-headers" >
<copy-properties propertyName="*"
doc:name="Copy All HTTP Headers"/>
<remove-property propertyName="Content-Length"
doc:name="Remove Content Length"/>
<remove-property propertyName="MULE_*"
doc:name="Remove MULE Properties"/>
<remove-property propertyName="X_MULE*"
doc:name="Remove X_MULE Properties"/>
<remove-property propertyName="http.*"
doc:name="Remove http Properties"/>
</sub-flow>
<flow name="CookieReceiverFlow">
<http:listener config-ref="HTTP_Configuration_CookieHandlerExample"
path="/receiveCookie"
doc:name="receiveCookies"
doc:description="This connector receives the cookies we've just set"/>
<transformer ref="MyCookieTranformer"
doc:name="GrabCookies"
doc:description="Use our custom transformers.CookieGrabber class to put the cookies in a nice java.util.List in a session variable."/>
<logger message="CookieReceiver: Value of cookie "token": "#[sessionVars.incomingCookieMap.get('token')]". Yep, still there :)"
level="INFO"
doc:name="Have a cookie!"
doc:description="Let get a cookie value, simply by referring the name of it as the key from our map"/>
<set-payload value="#[sessionVars.incomingCookieList.toArray(String)]"
doc:name="Put CookieList to payload"
doc:description="Put the session vairable List that contains the cookies in the payload"/>
<json:object-to-json-transformer returnClass="java.lang.String"
doc:name="Object to JSON"
doc:description="Convert our payload to a JSON object"/>
</flow>
</mule>
You can test it by running it and opening this page: http://localhost:8080/setCookies
Hope this helps.
You can fetch the cookies with:
#[headers:INBOUND:cookie] or #[message.inboundProperties['cookie']]
Related
I am trying to use a UDP socket to send a simple datagram to a local service. I've tested the request and response using a nodejs client, but I cannot get the same response when using Mulesoft.
This is the nodejs code that works:
const dgram = require('node:dgram');
const process = require('node:process');
const buffer = require('node:buffer');
var soc = dgram.createSocket('udp4');
soc.on('error', (err) => {
console.error(`socket error:\n${err.stack}`);
});
soc.on('message', (msg) => {
console.log(`message received: ${msg}`);
});
soc.connect(1434, () => {
console.log(`socket connected`);
const queryInstanceCode = 3;
const buff = buffer.Buffer.alloc(1);
buff[0] = queryInstanceCode;
soc.send(buff);
});
Executing this node app will query the local service and return expected data.
My mule app uses the socket connector with this configuration:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns:sockets="http://www.mulesoft.org/schema/mule/sockets"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="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/sockets http://www.mulesoft.org/schema/mule/sockets/current/mule-sockets.xsd
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd">
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="aaeb8ad9-c25d-4076-90b4-5fbf0584fefa" >
<http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>
<sockets:request-config name="SQLServiceBrowserSocket" doc:name="Sockets Request config" doc:id="ccebf069-3ea5-403b-8f38-14a1b181c93f" >
<sockets:udp-requester-connection host="127.0.0.1" port="1434" clientTimeout="5000"/>
</sockets:request-config>
<flow name="named.instance.finderFlow" doc:id="f0259fdc-c7ce-43ba-a124-07df0bee9ec4" >
<http:listener doc:name="Listener" doc:id="3ece9740-15b6-4681-b5e3-20ff66fa46c7" config-ref="HTTP_Listener_config" path="/mssqlhost/{host}/instance/{instance}/port" allowedMethods="GET" responseStreamingMode="NEVER">
</http:listener>
<logger level="INFO" doc:name="Log Request" doc:id="3bd2076d-23f6-4388-a3d8-0da2816d00dc" message="#attributes"/>
<sockets:send-and-receive doc:name="Query SQL Server Browser" doc:id="14f15af2-ee9a-41f9-9890-a021fd066d35" config-ref="SQLServiceBrowserSocket" target="instancePort" outputMimeType="application/octet-stream" outputEncoding="US-ASCII">
<non-repeatable-stream />
<sockets:content><![CDATA[3]]></sockets:content>
</sockets:send-and-receive>
<logger level="INFO" doc:name="Logger" doc:id="6ca780c0-21c7-409c-9c64-17cb0a0fc543" message="#payload"/>
</flow>
</mule>
When running the Mule app and performing a simple CURL on the url, the flow will timeout after 5 seconds, with no additional information than "UDP connection timed out".
I've tried adding debug logging for sockets, but I get no additional information. How do I get additional information on why the UDP Sockets connector is not properly interacting with the service?
The nodejs application works and proves that the service is available and functioning. The Mule application times out after 5 seconds without any further information. I have tried increasing level logging by adding this logger:
<AsyncLogger name="org.mule.extension.socket" level="DEBUG"/>
To the log4j2.xml of the package, but I get no additional information.
I expect the Sockets connector to send the UDP packate and receive the same response that the nodejs application receives. Instead the Socket connector times out.
The issue was encoding. Supplying the string "3" to the content of the socket made it encoded in ASCII. Instead, use dataweave:
%dw 2.0
output application/octet-stream
---
3
I need to send an acknowledge response for some messages went an acknowledge needed flag is set for that purpose.
Using a Java component I build a Payload string array with this structure:
Payload[0]=message entry.
Payload[1]="true"/"false" // This is the acknowledge needed flag.
Payload[2]="OK"/"ON"/"OFF"// Response to the transport (went flag="true")
This is my XML code
<tcp:connector name="TCP" validateConnections="false" sendBufferSize="0" receiveBufferSize="0" receiveBacklog="0" keepAlive="true" clientSoTimeout="10000" serverSoTimeout="10000" socketSoLinger="0" doc:name="TCP">
<tcp:direct-protocol rethrowExceptionOnRead="true" payloadOnly="true"/>
</tcp:connector>
<flow name="verifyFlow">
<tcp:inbound-endpoint exchange-pattern="request-response" host="localhost" port="9446" connector-ref="TCP" responseTimeout="10000" doc:name="TCP" transformer-refs="Byte_Array_to_String"/>
<component class="verify.as" doc:name="Java Component"/>
<choice doc:name="Choice">
<when expression="#[message.payload[1] == 'false']">
<set-payload value="""" doc:name="No response"/>
</when>
<otherwise>
<expression-transformer mimeType="text/plain" expression="#[message.payload[2]]" doc:name="Expression"/>
</otherwise>
</choice>
</flow>
The problem I'm facing is that went ever the flag is "false" there shouldn't be any response send to the transport (no 'null' value, no empty string, no "", Etc.) ¿Is there any way to change the exchange pattern of the TCP (or UDP) connector from request-response to one way inside the flow?
My mule application is receiving a HTTP POST request with the Content-Type as multipart/form-data. I tried to use a custom transformer but do not know how to access the paramters as the object is of class ContentLengthInputStream.
How do I access the form parameters?
I want to convert the parameters into a map. How do I do this?
you can use #[message.inboundAttachments[payload] in a flow, for example, to send a http outbound endpoint:
<message-properties-transformer doc:name="Message">
<add-message-property key="Content-type" value="multipart/form-data/>
</message-properties-transformer>
<http:outbound-endpoint exchange-pattern="request-response" method="POST" doc:name="HTTP" address="http://localhost:9090" mimeType="multipart/form-data"/>
To receive can be used:
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8090" connector-ref="HTTP_HTTPS" doc:name="HTTP" contentType="multipart/form-data"/>
<logger message="#[message.inboundAttachments[payload]]" level="ERROR" doc:name="Logger"/>
You can use a transformer(AbstractMessageTransformer) and get the payload with:
DataHandler inboundAttachment = message.getInboundAttachment("AttachmentName");
In Mule I need to manipulate via java an xlsx file sent via http post.
How can I get the file posted via java?
I thought it was reachable via Mule message but
eventContext.getMessage().getOutboundAttachmentNames()
and neither
eventContext.getMessage().getInboundAttachmentNames()
give results.
Any ideas?
to make http post test I use curl in this way:
curl --form upload=#filename --form press=OK http://localhost:8088/HttpController
The flow is simply something like this:
<flow name="xlsx_to_xls_converterFlow1" doc:name="xlsx_to_xls_converterFlow1">
<http:inbound-endpoint exchange-pattern="request-response" doc:name="HTTP" address="http://localhost:8088/HttpController"/>
<logger level="INFO" doc:name="Logger"/>
<component class="Convert_XLSXtoXLS" doc:name="Java"/>
</flow>
Thank you
UPDATED
To let the flagged solution work occurs to override extractPayloadFromHttpRequest of HttpMultipartMuleMessageFactory to choose the proper input file name.
In fact with the current HttpMultipartMuleMessageFactory implementation the file is uploaded only if input file name = "payload"
You need to configure your HTTP connector to handle multipart requests to receive them in attachments. Add the following inside its XML configuration:
<service-overrides messageFactory="org.mule.transport.http.HttpMultipartMuleMessageFactory"/>
( if you think this is cumbersome, please upvote https://www.mulesoft.org/jira/browse/MULE-6862 )
Putting a Java component behind an http:inbound-endpoint results in an InputStream as argument to the method in the component.
You have to work with the input stream or just put an echo in between:
<flow name="FileUpload" doc:name="FileUpload">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="9090" doc:name="HTTP"/>
<echo-component doc:name="Echo"/>
<component class="de.codecentric.basics.FileUploadComponent" doc:name="Java"/>
</flow>
The component has one method:
package de.codecentric.basics;
public class FileUploadComponent {
public String process(String message) {
System.out.println("message: " + message);
return "OK";
}
}
You still have to parse the multipart form data in this case.
Or try to use the REST component, see: http://www.javaroots.com/2013/05/createfileuploadmulejerseyrest.html
I have defined a Mule HTTP Inbound Endpoint as :
<flow name="jfeed_fill_data">
<http:inbound-endpoint address="http://localhost:1212/jcore/insert/feed/">
</http:inbound-endpoint>
<component class="main.java.com.joshlabs.jcore.Feed"/>
</flow>
Now this Service Works Fine.
But When i type a Deformed URL , something like "http://localhost:1212/jcore/insert/feedasdasdAwes/", I get the following Message from MULE :
Cannot bind to address "http://localhost:1212/jcore/insert/feedasdasdAwes/"
No component registered on that endpoint
My Question is : How can i Change the above default Message to Something of my own.?
Note : Actually i wanted to return a JSON String as an Error message. Something like :
{
Exception: "Invalid URL"
}
And if possible, then "Can MULE throw HTTP 404 : Not Found Error in above case"..??
You just need to make your endpoint accept all sub-paths and then handle wrong ones with message routing:
<flow name="jfeed_fill_data">
<http:inbound-endpoint address="http://localhost:1212" />
<choice>
<when evaluator="header" expression="INBOUND:http.request.path=/jcore/insert/feed/">
<component class="main.java.com.joshlabs.jcore.Feed"/>
</when>
<otherwise>
<message-properties-transformer>
<add-message-property key="http.status" value="404"/>
</message-properties-transformer>
<expression-transformer>
<return-argument evaluator="string" expression="{Exception: "Invalid URL"}"/>
</expression-transformer>
</otherwise>
</choice>
</flow>