View Javadoc

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