View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.session;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.MuleException;
11  import org.mule.api.MuleMessage;
12  import org.mule.api.MuleSession;
13  import org.mule.api.config.MuleProperties;
14  import org.mule.api.transport.SessionHandler;
15  import org.mule.util.SerializationUtils;
16  
17  import java.io.Serializable;
18  import java.util.Iterator;
19  import java.util.Map.Entry;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  /**
25   * A session handler used to store and retrieve session information on an
26   * event. The MuleSession information is stored as a header on the message (does not
27   * support Tcp, Udp, etc. unless the MuleMessage object is serialised across the
28   * wire). The session is stored in the "MULE_SESSION" property as an array of bytes (byte[])
29   */
30  public class SerializeOnlySessionHandler implements SessionHandler
31  {
32      protected transient Log logger = LogFactory.getLog(getClass());
33  
34      public MuleSession retrieveSessionInfoFromMessage(MuleMessage message) throws MuleException
35      {
36          MuleSession session = null;
37          byte[] serializedSession = message.getInboundProperty(MuleProperties.MULE_SESSION_PROPERTY);
38  
39          if (serializedSession != null)
40          {
41              session = (MuleSession) SerializationUtils.deserialize(serializedSession, message.getMuleContext());
42          }
43          return session;
44      }
45  
46      /**
47       * @deprecated Use retrieveSessionInfoFromMessage(MuleMessage message) instead
48       */
49      public void retrieveSessionInfoFromMessage(MuleMessage message, MuleSession session) throws MuleException
50      {
51          session = retrieveSessionInfoFromMessage(message);
52      }
53  
54      public void storeSessionInfoToMessage(MuleSession session, MuleMessage message) throws MuleException
55      {
56          byte[] serializedSession = SerializationUtils.serialize(removeNonSerializableProperties(session,message.getMuleContext()));
57          
58          if (logger.isDebugEnabled())
59          {
60              logger.debug("Adding serialized Session header to message: " + serializedSession);
61          }
62          message.setOutboundProperty(MuleProperties.MULE_SESSION_PROPERTY, serializedSession);
63      }
64      
65      protected MuleSession removeNonSerializableProperties(final MuleSession session,
66                                                            final MuleContext muleContext)
67      {
68          DefaultMuleSession copy = new DefaultMuleSession(session, muleContext);
69          Iterator<Entry<String, Object>> propertyIterator = copy.getProperties().entrySet().iterator();
70          while (propertyIterator.hasNext())
71          {
72              final Entry<String, Object> entry = propertyIterator.next();
73              if (!(entry.getValue() instanceof Serializable))
74              {
75                  logger.warn(String.format("Property %s is not serializable, it will not be preserved "
76                                            + "as part of the MuleSession", entry.getKey()));
77                  propertyIterator.remove();
78              }
79          }
80          return copy;
81      }
82      
83      /**
84       * @deprecated This method is no longer needed and will be removed in the next major release
85       */
86      public String getSessionIDKey()
87      {
88          return "ID";
89      }
90  }