Coverage Report - org.mule.session.SerializeOnlySessionHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
SerializeOnlySessionHandler
0%
0/24
0%
0/8
0
 
 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  0
 public class SerializeOnlySessionHandler implements SessionHandler
 31  
 {
 32  0
     protected transient Log logger = LogFactory.getLog(getClass());
 33  
 
 34  
     public MuleSession retrieveSessionInfoFromMessage(MuleMessage message) throws MuleException
 35  
     {
 36  0
         MuleSession session = null;
 37  0
         byte[] serializedSession = message.getInboundProperty(MuleProperties.MULE_SESSION_PROPERTY);
 38  
 
 39  0
         if (serializedSession != null)
 40  
         {
 41  0
             session = (MuleSession) SerializationUtils.deserialize(serializedSession, message.getMuleContext());
 42  
         }
 43  0
         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  0
         session = retrieveSessionInfoFromMessage(message);
 52  0
     }
 53  
 
 54  
     public void storeSessionInfoToMessage(MuleSession session, MuleMessage message) throws MuleException
 55  
     {
 56  0
         byte[] serializedSession = SerializationUtils.serialize(removeNonSerializableProperties(session,message.getMuleContext()));
 57  
         
 58  0
         if (logger.isDebugEnabled())
 59  
         {
 60  0
             logger.debug("Adding serialized Session header to message: " + serializedSession);
 61  
         }
 62  0
         message.setOutboundProperty(MuleProperties.MULE_SESSION_PROPERTY, serializedSession);
 63  0
     }
 64  
     
 65  
     protected MuleSession removeNonSerializableProperties(final MuleSession session,
 66  
                                                           final MuleContext muleContext)
 67  
     {
 68  0
         DefaultMuleSession copy = new DefaultMuleSession(session, muleContext);
 69  0
         Iterator<Entry<String, Object>> propertyIterator = copy.getProperties().entrySet().iterator();
 70  0
         while (propertyIterator.hasNext())
 71  
         {
 72  0
             final Entry<String, Object> entry = propertyIterator.next();
 73  0
             if (!(entry.getValue() instanceof Serializable))
 74  
             {
 75  0
                 logger.warn(String.format("Property %s is not serializable, it will not be preserved "
 76  
                                           + "as part of the MuleSession", entry.getKey()));
 77  0
                 propertyIterator.remove();
 78  
             }
 79  0
         }
 80  0
         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  0
         return "ID";
 89  
     }
 90  
 }