KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > impl > MuleSessionHandler


1 /*
2  * $Id: MuleSessionHandler.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.impl;
12
13 import java.io.UnsupportedEncodingException JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.StringTokenizer JavaDoc;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19 import org.mule.config.MuleProperties;
20 import org.mule.config.i18n.Message;
21 import org.mule.config.i18n.Messages;
22 import org.mule.transformers.codec.Base64Decoder;
23 import org.mule.transformers.codec.Base64Encoder;
24 import org.mule.umo.UMOException;
25 import org.mule.umo.UMOMessage;
26 import org.mule.umo.UMOSession;
27 import org.mule.umo.provider.UMOSessionHandler;
28 import org.mule.umo.transformer.UMOTransformer;
29
30 /**
31  * A default session handler used to store and retrieve session information on an
32  * event. The MuleSession information is stored as a header on the message (does not
33  * support Tcp, Udp, etc. unless the UMOMessage object is serialised across the
34  * wire). The session is stored in the "MULE_SESSION" property as String key/value
35  * pairs that are Base64 encoded, for example:
36  * ID=dfokokdf-3ek3oke-dkfokd;MySessionProp1=Value1;MySessionProp2=Value2
37  *
38  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
39  * @version $Revision: 3798 $
40  */

41 public class MuleSessionHandler implements UMOSessionHandler
42 {
43
44     /**
45      * logger used by this class
46      */

47     protected transient Log logger = LogFactory.getLog(getClass());
48
49     private static UMOTransformer encoder = new Base64Encoder();
50     private static UMOTransformer decoder = new Base64Decoder();
51
52     public void retrieveSessionInfoFromMessage(UMOMessage message, UMOSession session) throws UMOException
53     {
54         String JavaDoc sessionId = (String JavaDoc)message.removeProperty(MuleProperties.MULE_SESSION_ID_PROPERTY);
55         Object JavaDoc sessionHeader = message.removeProperty(MuleProperties.MULE_SESSION_PROPERTY);
56
57         if (sessionId != null)
58         {
59             // TODO Mule 2.0 grab session from the context
60
throw new IllegalStateException JavaDoc(
61                 "This session handler does not know how to look up session information for session id: "
62                                 + sessionId);
63         }
64         if (sessionHeader != null)
65         {
66             String JavaDoc sessionString = null;
67             try
68             {
69                 sessionString = new String JavaDoc((byte[])decoder.transform(sessionHeader), message.getEncoding());
70             }
71             catch (UnsupportedEncodingException JavaDoc e)
72             {
73                 e.printStackTrace();
74             }
75             if (logger.isDebugEnabled())
76             {
77                 logger.debug("Parsing session header: " + sessionString);
78             }
79             String JavaDoc pair;
80             String JavaDoc name;
81             String JavaDoc value;
82             for (StringTokenizer JavaDoc stringTokenizer = new StringTokenizer JavaDoc(sessionString, ";"); stringTokenizer.hasMoreTokens();)
83             {
84                 pair = stringTokenizer.nextToken();
85                 int i = pair.indexOf("=");
86                 if (i == -1)
87                 {
88                     throw new IllegalArgumentException JavaDoc(new Message(Messages.SESSION_VALUE_X_IS_MALFORMED,
89                         pair).toString());
90                 }
91                 name = pair.substring(0, i).trim();
92                 value = pair.substring(i + 1).trim();
93                 session.setProperty(name, value);
94                 if (logger.isDebugEnabled())
95                 {
96                     logger.debug("Added Session variable: " + pair);
97                 }
98             }
99
100         }
101     }
102
103     public void storeSessionInfoToMessage(UMOSession session, UMOMessage message) throws UMOException
104     {
105         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
106         buf.append(getSessionIDKey()).append("=").append(session.getId());
107         for (Iterator JavaDoc iterator = session.getPropertyNames(); iterator.hasNext();)
108         {
109             Object JavaDoc o = iterator.next();
110             buf.append(";");
111             buf.append(o).append("=").append(session.getProperty(o));
112             if (logger.isDebugEnabled())
113             {
114                 logger.debug("Adding property to session header: " + o + "=" + session.getProperty(o));
115             }
116         }
117         String JavaDoc sessionString = buf.toString();
118         if (logger.isDebugEnabled())
119         {
120             logger.debug("Adding session header to message: " + sessionString);
121         }
122         sessionString = (String JavaDoc)encoder.transform(sessionString);
123         message.setProperty(MuleProperties.MULE_SESSION_PROPERTY, sessionString);
124     }
125
126     public String JavaDoc getSessionIDKey()
127     {
128         return "ID";
129     }
130 }
131
Popular Tags