View Javadoc

1   /*
2    * $Id: HttpSessionHandler.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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.providers.http;
12  
13  import org.mule.umo.UMOException;
14  import org.mule.umo.UMOMessage;
15  import org.mule.umo.UMOSession;
16  import org.mule.umo.provider.UMOSessionHandler;
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 Session
28   * 
29   * @author <a href="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
30   * @version $Revision: 7976 $
31   */
32  public class HttpSessionHandler implements UMOSessionHandler
33  {
34  
35      /**
36       * logger used by this class
37       */
38      protected transient Log logger = LogFactory.getLog(getClass());
39  
40      public void retrieveSessionInfoFromMessage(UMOMessage message, UMOSession session) throws UMOException
41      {
42          Cookie[] cookies = (Cookie[])message.getProperty(HttpConnector.HTTP_COOKIES_PROPERTY);
43          if (cookies != null)
44          {
45              for (int i = 0; i < cookies.length; i++)
46              {
47                  Cookie cookie = cookies[i];
48                  session.setProperty(cookie.getName(), cookie.getValue());
49                  if (logger.isDebugEnabled())
50                  {
51                      logger.debug("Added cookie to session: " + cookie.toString());
52                  }
53              }
54          }
55      }
56  
57      public void storeSessionInfoToMessage(UMOSession session, UMOMessage message) throws UMOException
58      {
59          Object name;
60          Object value;
61          List cookies = new ArrayList();
62          for (Iterator iterator = session.getPropertyNames(); iterator.hasNext();)
63          {
64              name = iterator.next();
65              value = session.getProperty(name);
66              // TODO handle domain, path, secure (https) and expiry
67              cookies.add(new Cookie(null, name.toString(), value.toString()));
68          }
69          if (cookies.size() > 0)
70          {
71              message.setProperty(HttpConnector.HTTP_COOKIES_PROPERTY,
72                  cookies.toArray(new Cookie[cookies.size()]));
73          }
74      }
75  
76      public String getSessionIDKey()
77      {
78          return "ID";
79      }
80  }