View Javadoc

1   /*
2    * $Id: SerializeOnlySessionHandler.java 22686 2011-08-16 19:39:20Z pablo.lagreca $
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.MuleContext;
14  import org.mule.api.MuleException;
15  import org.mule.api.MuleMessage;
16  import org.mule.api.MuleSession;
17  import org.mule.api.config.MuleProperties;
18  import org.mule.api.transport.SessionHandler;
19  import org.mule.util.SerializationUtils;
20  
21  import java.io.Serializable;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  /**
27   * A session handler used to store and retrieve session information on an
28   * event. The MuleSession information is stored as a header on the message (does not
29   * support Tcp, Udp, etc. unless the MuleMessage object is serialised across the
30   * wire). The session is stored in the "MULE_SESSION" property as an array of bytes (byte[])
31   */
32  public class SerializeOnlySessionHandler implements SessionHandler
33  {
34      protected transient Log logger = LogFactory.getLog(getClass());
35  
36      public MuleSession retrieveSessionInfoFromMessage(MuleMessage message) throws MuleException
37      {
38          MuleSession session = null;
39          byte[] serializedSession = message.getInboundProperty(MuleProperties.MULE_SESSION_PROPERTY);
40  
41          if (serializedSession != null)
42          {
43              session = (MuleSession) SerializationUtils.deserialize(serializedSession, message.getMuleContext());
44          }
45          return session;
46      }
47  
48      /**
49       * @deprecated Use retrieveSessionInfoFromMessage(MuleMessage message) instead
50       */
51      public void retrieveSessionInfoFromMessage(MuleMessage message, MuleSession session) throws MuleException
52      {
53          session = retrieveSessionInfoFromMessage(message);
54      }
55  
56      public void storeSessionInfoToMessage(MuleSession session, MuleMessage message) throws MuleException
57      {
58          byte[] serializedSession = SerializationUtils.serialize(removeNonSerializableProperties(session,message.getMuleContext()));
59          
60          if (logger.isDebugEnabled())
61          {
62              logger.debug("Adding serialized Session header to message: " + serializedSession);
63          }
64          message.setOutboundProperty(MuleProperties.MULE_SESSION_PROPERTY, serializedSession);
65      }
66      
67      protected MuleSession removeNonSerializableProperties(final MuleSession session, final MuleContext muleContext)
68      {
69          MuleSession copy = new DefaultMuleSession(session, muleContext);
70          for (String key : copy.getPropertyNamesAsSet())
71          {
72              final Object value = copy.getProperty(key);
73              if (!(value instanceof Serializable))
74              {
75                  logger.warn(String.format("Property %s is not serializable, it will not be preserved " +
76                                            "as part of the MuleSession", key));
77                  copy.removeProperty(key);
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  }