View Javadoc

1   /*
2    * $Id: LegacySessionHandler.java 19191 2010-08-25 21:05:23Z tcarlson $
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  
11  package org.mule.session;
12  
13  import org.mule.api.MuleException;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.MuleSession;
16  import org.mule.api.config.MuleProperties;
17  import org.mule.api.transformer.Transformer;
18  import org.mule.api.transport.SessionHandler;
19  import org.mule.config.i18n.CoreMessages;
20  import org.mule.transformer.codec.Base64Decoder;
21  import org.mule.transformer.codec.Base64Encoder;
22  
23  import java.io.UnsupportedEncodingException;
24  import java.util.StringTokenizer;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  /**
30   * A session handler used to store and retrieve session information on an
31   * event. The MuleSession information is stored as a header on the message (does not
32   * support Tcp, Udp, etc. unless the MuleMessage object is serialised across the
33   * wire). The session is stored in the "MULE_SESSION" property as String key/value
34   * pairs that are Base64 encoded, for example:
35   * ID=dfokokdf-3ek3oke-dkfokd;MySessionProp1=Value1;MySessionProp2=Value2
36   * 
37   * @deprecated Since all properties are converted to Strings, this session handler has the issue EE-1705/MULE-4567.  Use {@link SerializeAndEncodeSessionHandler} or {@link SerializeOnlySessionHandler} instead.
38   */
39  public class LegacySessionHandler implements SessionHandler
40  {
41      protected transient Log logger = LogFactory.getLog(getClass());
42  
43      private static Transformer encoder = new Base64Encoder();
44      private static Transformer decoder = new Base64Decoder();
45  
46      public MuleSession retrieveSessionInfoFromMessage(MuleMessage message) throws MuleException
47      {
48           MuleSession session = new DefaultMuleSession(message.getMuleContext());
49  
50           String sessionId = message.getInboundProperty(MuleProperties.MULE_SESSION_ID_PROPERTY);
51           Object sessionHeader = message.getInboundProperty(MuleProperties.MULE_SESSION_PROPERTY);
52  
53           if (sessionId != null)
54           {
55               throw new IllegalStateException(
56                   "This session handler does not know how to look up session information for session id: "
57                                   + sessionId);
58           }
59           if (sessionHeader != null)
60           {
61               String sessionString;
62               try
63               {
64                   sessionString = new String((byte[]) decoder.transform(sessionHeader), message.getEncoding());
65               }
66               catch (UnsupportedEncodingException e)
67               {
68                   sessionString = new String((byte[]) decoder.transform(sessionHeader));
69               }
70               if (logger.isDebugEnabled())
71               {
72                   logger.debug("Parsing session header: " + sessionString);
73               }
74               String pair;
75               String name;
76               String value;
77               for (StringTokenizer stringTokenizer = new StringTokenizer(sessionString, ";"); stringTokenizer.hasMoreTokens();)
78               {
79                   pair = stringTokenizer.nextToken();
80                   int i = pair.indexOf("=");
81                   if (i == -1)
82                   {
83                       throw new IllegalArgumentException(
84                           CoreMessages.sessionValueIsMalformed(pair).toString());
85                   }
86                   name = pair.substring(0, i).trim();
87                   value = pair.substring(i + 1).trim();
88                   session.setProperty(name, value);
89                   if (logger.isDebugEnabled())
90                   {
91                       logger.debug("Added MuleSession variable: " + pair);
92                   }
93               }
94           }
95           return session;
96     }
97  
98      /**
99       * @deprecated Use retrieveSessionInfoFromMessage(MuleMessage message) instead
100      */
101     public void retrieveSessionInfoFromMessage(MuleMessage message, MuleSession session) throws MuleException
102     {
103         session = retrieveSessionInfoFromMessage(message);
104     }
105 
106     public void storeSessionInfoToMessage(MuleSession session, MuleMessage message) throws MuleException
107     {
108         StringBuffer buf = new StringBuffer();
109         buf.append(getSessionIDKey()).append("=").append(session.getId());
110         for (String key : session.getPropertyNamesAsSet())
111         {
112             buf.append(";");
113             buf.append(key).append("=").append(session.getProperty(key));
114             if (logger.isDebugEnabled())
115             {
116                 logger.debug(String.format("Adding property to session header: %s=%s", key, session.getProperty(key)));
117             }
118         }
119         String sessionString = buf.toString();
120         if (logger.isDebugEnabled())
121         {
122             logger.debug("Adding session header to message: " + sessionString);
123         }
124         sessionString = (String) encoder.transform(sessionString);
125         message.setOutboundProperty(MuleProperties.MULE_SESSION_PROPERTY, sessionString);
126     }
127     
128     /**
129      * @deprecated This method is no longer needed and will be removed in the next major release
130      */
131     public String getSessionIDKey()
132     {
133         return "ID";
134     }
135 }