View Javadoc

1   /*
2    * $Id: HttpSessionHandler.java 10489 2008-01-23 17:53:38Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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  
11  package org.mule.transport.http;
12  
13  import org.mule.api.MuleException;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.MuleSession;
16  import org.mule.api.transport.SessionHandler;
17  
18  import java.util.ArrayList;
19  import java.util.Iterator;
20  import java.util.List;
21  
22  import org.apache.commons.httpclient.Cookie;
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  /**
27   * Will read and write Http Cookie information to and from the Mule MuleSession
28   */
29  public class HttpSessionHandler implements SessionHandler
30  {
31  
32      /**
33       * logger used by this class
34       */
35      protected transient Log logger = LogFactory.getLog(getClass());
36  
37      public void retrieveSessionInfoFromMessage(MuleMessage message, MuleSession session) throws MuleException
38      {
39          Cookie[] cookies = (Cookie[])message.getProperty(HttpConnector.HTTP_COOKIES_PROPERTY);
40          if (cookies != null)
41          {
42              for (int i = 0; i < cookies.length; i++)
43              {
44                  Cookie cookie = cookies[i];
45                  session.setProperty(cookie.getName(), cookie.getValue());
46                  if (logger.isDebugEnabled())
47                  {
48                      logger.debug("Added cookie to session: " + cookie.toString());
49                  }
50              }
51          }
52      }
53  
54      public void storeSessionInfoToMessage(MuleSession session, MuleMessage message) throws MuleException
55      {
56          Object name;
57          Object value;
58          List cookies = new ArrayList();
59          for (Iterator iterator = session.getPropertyNames(); iterator.hasNext();)
60          {
61              name = iterator.next();
62              value = session.getProperty(name);
63              // TODO handle domain, path, secure (https) and expiry
64              cookies.add(new Cookie(null, name.toString(), value.toString()));
65          }
66          if (cookies.size() > 0)
67          {
68              message.setProperty(HttpConnector.HTTP_COOKIES_PROPERTY,
69                  cookies.toArray(new Cookie[cookies.size()]));
70          }
71      }
72  
73      public String getSessionIDKey()
74      {
75          return "ID";
76      }
77  }