title: WSO2 ESB Getting a cookie from transport header with multiple cookies
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>The response I'm getting from the authentication server:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>The ESB sequence:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;proxy xmlns="http://ws.apache.org/ns/synapse"
         name="TestLogProxy"
         transports="https,http"
         statistics="disable"
         trace="disable"
         startOnLoad="true"&gt;
     &lt;target&gt;
        &lt;inSequence&gt;
           &lt;property name="uri.var.userid" value="userid"/&gt;
           &lt;property name="uri.var.password" value="password"/&gt;
           &lt;send&gt;
              &lt;endpoint&gt;
                 &lt;http method="GET"
                       uri-template="https://www.company.biz/auth.cgi?userid={uri.var.userid}&amp;amp;password={uri.var.password}"/&gt;
              &lt;/endpoint&gt;
           &lt;/send&gt;
        &lt;/inSequence&gt;
        &lt;outSequence&gt;
           &lt;property name="setCookieHeader"
                     expression="$trp:Set-Cookie"
                     scope="default"
                     type="STRING"/&gt;
           &lt;log level="custom"&gt;
              &lt;property name="setCookieHeader value" expression="$ctx:setCookieHeader"/&gt;
           &lt;/log&gt;
           &lt;send/&gt;
        &lt;/outSequence&gt;
     &lt;/target&gt;
     &lt;description/&gt;
  &lt;/proxy&gt;
  </code></pre>
  
  <p>The log message I'm getting:</p>
  
  <pre><code>setCookieHeader value = JSESSIONID=000036_xxxxxxx_xxxxxx_xxxxxx:xxxxxxxxx; Path=/
  </code></pre>
  
  <p>I have also tried to make my own Class mediator:</p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p>And calling it from the sequence like this:</p>
  
  <pre><code>&lt;class name="org.wso2.mediator.CookieMediator"/&gt;
  </code></pre>
  
  <p>But it also returns only the first cookie:</p>
  
  <pre><code>Cookie: JSESSIONID=000036_xxxxxxx_xxxxxx_xxxxxx:xxxxxxxxx; Path=/
  </code></pre>
  
  <p>I have already read these posts but it doesn't help with the 2nd cookie problem I'm having:</p>
  
  <p><a href="https://stackoverflow.com/questions/27788670/in-wso2-esb-how-to-store-cookies-and-use-them-later-for-authentication">In WSO2 ESB, how to store cookies and use them later for authentication?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/26657671/wso2-esb-how-to-get-and-set-cookies-in-wsdl-calls-in">WSO2 ESB - How to get and set cookies in WSDL calls in</a></p>
  
  <p>Thank you.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>My solution below:</p>
  
  <pre><code>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;
      }
  }
  </code></pre>