Parsing Cookies in Integration Broker Request Handler
By Chris Malek | Mon, May 5, 2014
I have been doing a tremendous amount of integration broker work lately.
In one project, I had to retrieve a cookie submitted by the requester
and do some processing with it. The integration broker cookie API
&MSG.IBInfo.IBConnectorInfo.Cookies) returns the cookies with all the
cookies and key value pairs as one large string. As far as I can tell
from the current state of the documentation, you have to write your own
cookie parser on the string returned.
Cookies are submitted in the HTTP headers like this:
Cookie: COOKIE_NAME1=COOKIE_1_VALUE; COOKIE_NAME2=COOKIE_2_VALUE; COOKIE_NAME3=COOKIE_3_VALUE
I threw together a method called
getCookieFromRequest that is meant to
live inside your subscription handler code. Here is the method I wrote
that splits the cookie by
; and then again by
You ask it for a cookie key name and it will return back the value if it exists.
method getCookieFromRequest /+ &MSG as Message, +/ /+ &cookieName as String +/ /+ Returns String +/ Local string &requestCookies; try &requestCookies = &MSG.IBInfo.IBConnectorInfo.Cookies; Local array of string &aInboundCookiesKeyValues; &aInboundCookiesKeyValues = CreateArrayRept("", 0); &aInboundCookiesKeyValues = Split(&requestCookies, ";"); Local integer &i; Local array of string &aKeyValue; Local integer &cookieValLocationStart; For &i = 1 To &aInboundCookiesKeyValues.Len &aKeyValue = Split(&aInboundCookiesKeyValues [&i], "="); If LTrim(RTrim(&aKeyValue )) = &cookieName Then Return &aKeyValue ; End-If; End-For; Return ""; catch Exception &E Return "";; end-try; end-method;
Here is a sample of how it would sit in an subscription handler. In this
example, we are going after the
class INBOUND_TESTER implements PS_PT:Integration:IRequestHandler method onRequest(&MSG As Message) Returns Message; method getCookieFromRequest(&MSG As Message, &cookieName As string) Returns string; end-class; method onRequest /+ &MSG as Message +/ /+ Returns Message +/ /+ Extends/implements PS_PT:Integration:IRequestHandler.OnRequest +/ /* Setup response xml body */ Local Message &response; &response = CreateMessage(Operation.CHG_TEST, %IntBroker_Response); Local XmlDoc &xmlout; Local XmlNode &childNode; &xmlout = CreateXmlDoc(""); Local string &cValue; &cValue = %This.getCookieFromRequest(&MSG, "PS_TOKEN"); if all(&cValue) then /* Do something with cookie */ end-if; Return &response; end-method; method getCookieFromRequest ....snip.....
Let me know if you find a better way or think you have improvements on this.