WSO2 ESB Getting a cookie from transport header with multiple cookies

荒凉一梦 提交于 2020-01-06 04:15:08

问题


I'm calling an authentication service with credentials and it responses back cookies with authentication info. I need that info for accessing a Web Service.

The problem is that the authentication service returns a response with multiple cookies but I can only access to the first cookie from response it's the second cookie (WSL-external=VhTee1...) that I need for accessing the Web Service.

The response I'm getting from the authentication server:

HTTP/1.1 200 OK
Content-Language: en-US
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Set-Cookie: JSESSIONID=000036_xxxxxxx_xxxxxx_xxxxxx:xxxxxxxxx; Path=/
Set-Cookie: WSL-external=VhTee1YVaxsBANcABHVzZXJpZD11d2lzaABpcGFkZHI9Ny43LjcuNwBhY2lncm91cD1SSVAAZGVwdD02NzIwAG9yZ2NvZGU9PwBlbXBjb2RlPUEAbXJyb2xlPU4Ab3JnPVBBRwBjb21wYW55PT8AZGl2YWJicj1HRwBzaXRlY29kZT03MDAzAGNpdHk9Y2l0eQBzdGF0ZT0/AGNvdW50cnk9R0cAc3ViamVjdGlkPXV3aXNoQGdnLmNvbQAAAENOPXdzbC1leHRlcm5hbABqdXN0aW5jYXNlaWZ0aGlzY29pbnRhaW5zc29tZXNlbnRpdml0ZWRhdGFpbW5vdGdpdmluZ2l0dG95b3U=; Path=/; Domain=.xxx.xxx
Content-Type: text/html; charset=UTF-8; charset=UTF-8
Pragma: no-cache
Cache-Control: no-cache
Date: Wed, 07 Oct 2015 08:58:36 GMT
Transfer-Encoding: chunked
Connection: Keep-Alive

The ESB sequence:

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="TestLogProxy"
       transports="https,http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <property name="uri.var.userid" value="userid"/>
         <property name="uri.var.password" value="password"/>
         <send>
            <endpoint>
               <http method="GET"
                     uri-template="https://www.company.biz/auth.cgi?userid={uri.var.userid}&amp;password={uri.var.password}"/>
            </endpoint>
         </send>
      </inSequence>
      <outSequence>
         <property name="setCookieHeader"
                   expression="$trp:Set-Cookie"
                   scope="default"
                   type="STRING"/>
         <log level="custom">
            <property name="setCookieHeader value" expression="$ctx:setCookieHeader"/>
         </log>
         <send/>
      </outSequence>
   </target>
   <description/>
</proxy>

The log message I'm getting:

setCookieHeader value = JSESSIONID=000036_xxxxxxx_xxxxxx_xxxxxx:xxxxxxxxx; Path=/

I have also tried to make my own Class mediator:

package org.wso2.mediator;

import java.util.Map;

import org.apache.synapse.MessageContext; 
import org.apache.synapse.core.axis2.Axis2MessageContext;
import org.apache.synapse.mediators.AbstractMediator;

public class CookieMediator extends AbstractMediator { 

    public boolean mediate(MessageContext synCtx) { 
        try {
            System.out.println("CookieMediator doing stuff...");

            // Extracting transport headers
            org.apache.axis2.context.MessageContext msgContext = ((Axis2MessageContext) synCtx).getAxis2MessageContext();

            Map headersMap = (Map) msgContext.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);

            // Printing the cookie
            System.out.println("Cookie: " +  headersMap.get("Set-Cookie"));

          } catch (Exception e) {
            System.out.println("Exception: " + e);
            handleException("Exception", e, synCtx);
          }

        return true;
    }
}

And calling it from the sequence like this:

<class name="org.wso2.mediator.CookieMediator"/>

But it also returns only the first cookie:

Cookie: JSESSIONID=000036_xxxxxxx_xxxxxx_xxxxxx:xxxxxxxxx; Path=/

I have already read these posts but it doesn't help with the 2nd cookie problem I'm having:

In WSO2 ESB, how to store cookies and use them later for authentication?

WSO2 ESB - How to get and set cookies in WSDL calls in

Thank you.

UPDATE:

My solution below:

package org.wso2.mediator;

import java.util.Map;
import org.apache.synapse.MessageContext; 
import org.apache.synapse.core.axis2.Axis2MessageContext;
import org.apache.synapse.mediators.AbstractMediator;

public class CookieMediator extends AbstractMediator { 

    public boolean mediate(MessageContext synCtx) { 
        try {
            System.out.println("CookieMediator extracting cookie...");

            // Extracting cookie from excess headers
            org.apache.axis2.context.MessageContext msgContext = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
            Map excessHeaders = (Map) msgContext.getProperty("EXCESS_TRANSPORT_HEADERS");

            if (excessHeaders != null) {
                String cookie = excessHeaders.get("Set-Cookie").toString().split(";")[0];

                if (cookie.startsWith("[WSL-external")) {
                    System.out.println("Cookie: " + cookie.substring(1));
                }
            }
          } catch (Exception e) {
            System.out.println("Exception: " + e);
            handleException("Exception", e, synCtx);
          }

        return true;
    }
}

回答1:


You may need to get this from the EXCESS_TRANSPORT_HEADERS map.

Map excessHeaders = (Map) synCtx.getProperty(NhttpConstants.EXCESS_TRANSPORT_HEADERS);

more specifically this is a MultiValueMap that contains multi-valued headers.



来源:https://stackoverflow.com/questions/32990465/wso2-esb-getting-a-cookie-from-transport-header-with-multiple-cookies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!