KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > http > HttpSessionHandler


1 /*
2  * $Id: HttpSessionHandler.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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.apache.commons.httpclient.Cookie;
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16 import org.mule.umo.UMOException;
17 import org.mule.umo.UMOMessage;
18 import org.mule.umo.UMOSession;
19 import org.mule.umo.provider.UMOSessionHandler;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 /**
26  * Will read and write Http Cookie information to and from the Mule Session
27  *
28  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
29  * @version $Revision: 3798 $
30  */

31 public class HttpSessionHandler implements UMOSessionHandler
32 {
33
34     /**
35      * logger used by this class
36      */

37     protected transient Log logger = LogFactory.getLog(getClass());
38
39     public void retrieveSessionInfoFromMessage(UMOMessage message, UMOSession session) throws UMOException
40     {
41         Cookie[] cookies = (Cookie[])message.getProperty(HttpConnector.HTTP_COOKIES_PROPERTY);
42         if (cookies != null)
43         {
44             for (int i = 0; i < cookies.length; i++)
45             {
46                 Cookie cookie = cookies[i];
47                 session.setProperty(cookie.getName(), cookie.getValue());
48                 if (logger.isDebugEnabled())
49                 {
50                     logger.debug("Added cookie to session: " + cookie.toString());
51                 }
52             }
53         }
54     }
55
56     public void storeSessionInfoToMessage(UMOSession session, UMOMessage message) throws UMOException
57     {
58         Object JavaDoc name;
59         Object JavaDoc value;
60         List JavaDoc cookies = new ArrayList JavaDoc();
61         for (Iterator JavaDoc iterator = session.getPropertyNames(); iterator.hasNext();)
62         {
63             name = iterator.next();
64             value = session.getProperty(name);
65             // TODO handle domain, path, secure (https) and expiry
66
cookies.add(new Cookie(null, name.toString(), value.toString()));
67         }
68         if (cookies.size() > 0)
69         {
70             message.setProperty(HttpConnector.HTTP_COOKIES_PROPERTY,
71                 cookies.toArray(new Cookie[cookies.size()]));
72         }
73     }
74
75     public String JavaDoc getSessionIDKey()
76     {
77         return "ID";
78     }
79 }
80
Popular Tags