View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.http.functional;
8   
9   import org.mule.api.MuleEventContext;
10  import org.mule.api.MuleMessage;
11  import org.mule.api.lifecycle.Callable;
12  import org.mule.transport.http.CookieHelper;
13  
14  import org.apache.commons.httpclient.Cookie;
15  import org.slf4j.Logger;
16  import org.slf4j.LoggerFactory;
17  
18  public class HttpMultipleCookiesInEndpointTestComponent implements Callable
19  {
20  
21      protected Logger logger = LoggerFactory.getLogger(this.getClass());
22  
23      public Object onCall(MuleEventContext muleEventContext) throws Exception
24      {
25          String response = "NO COOKIE FOUND!";
26  
27          MuleMessage message = muleEventContext.getMessage();
28          Object cookiesProperty = message.getInboundProperty("cookies");
29  
30          logger.info("****************** Got cookies property: " + cookiesProperty.getClass().getName());
31  
32          Cookie[] cookiesArray = CookieHelper.asArrayOfCookies(cookiesProperty);
33  
34          boolean cookie1Found = false;
35          boolean cookie2Found = false;
36          if (cookiesArray != null && cookiesArray.length > 0)
37          {
38              for (int i = 0; i < cookiesArray.length; i++)
39              {
40                  Cookie cookie = cookiesArray[i];
41  
42                  logger.info("****************** (" + i + ") Got Cookie: " + cookie);
43  
44                  if ("CookieNumber1".equals(cookie.getName())
45                      && "ValueForCookieNumber1".equals(cookie.getValue()))
46                  {
47                      cookie1Found = true;
48                  }
49                  else if ("CookieNumber2".equals(cookie.getName())
50                           && "ValueForCookieNumber2".equals(cookie.getValue()))
51                  {
52                      cookie2Found = true;
53                  }
54              }
55          }
56          if (cookie1Found && cookie2Found)
57          {
58              response = "Both Cookies Found!";
59          }
60          else if (cookie1Found)
61          {
62              response = "Only cookie1 was found";
63          }
64          else if (cookie2Found)
65          {
66              response = "Only cookie2 was found";
67          }
68  
69          return response;
70      }
71  }