Coverage Report - org.mule.session.SerializeAndEncodeSessionHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
SerializeAndEncodeSessionHandler
0%
0/17
0%
0/6
0
 
 1  
 /*
 2  
  * $Id: SerializeAndEncodeSessionHandler.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.model.SessionException;
 18  
 import org.mule.config.i18n.MessageFactory;
 19  
 import org.mule.util.Base64;
 20  
 
 21  
 import java.io.IOException;
 22  
 
 23  
 import org.apache.commons.lang.SerializationUtils;
 24  
 
 25  
 /**
 26  
  * A session handler used to store and retrieve session information on an
 27  
  * event. The DefaultMuleSession information is stored as a header on the message (does not
 28  
  * support Tcp, Udp, etc. unless the MuleMessage object is serialised across the
 29  
  * wire). The session is stored in the "MULE_SESSION" property as Base64 encoded
 30  
  * byte array.
 31  
  */
 32  0
 public class SerializeAndEncodeSessionHandler extends SerializeOnlySessionHandler
 33  
 {
 34  
     @Override
 35  
     public MuleSession retrieveSessionInfoFromMessage(MuleMessage message) throws MuleException
 36  
     {
 37  0
         MuleSession session = null;
 38  0
         String serializedEncodedSession = message.getInboundProperty(MuleProperties.MULE_SESSION_PROPERTY);
 39  
         
 40  0
         if (serializedEncodedSession != null)
 41  
         {
 42  0
             byte[] serializedSession = Base64.decode(serializedEncodedSession);            
 43  0
             if (serializedSession != null)
 44  
             {
 45  
                 // TODO may need to use a classloader-aware org.mule.util.SerializationUtils.deserialize()
 46  0
                 session = (MuleSession) SerializationUtils.deserialize(serializedSession);
 47  
             }
 48  
         }
 49  0
         return session;
 50  
     }
 51  
 
 52  
     @Override
 53  
     public void storeSessionInfoToMessage(MuleSession session, MuleMessage message) throws MuleException
 54  
     {        
 55  0
         byte[] serializedSession = SerializationUtils.serialize(removeNonSerializableProperties(session));
 56  
         String serializedEncodedSession;
 57  
         try
 58  
         {
 59  0
             serializedEncodedSession = Base64.encodeBytes(serializedSession, Base64.DONT_BREAK_LINES);
 60  
         }
 61  0
         catch (IOException e)
 62  
         {
 63  0
             throw new SessionException(MessageFactory.createStaticMessage("Unable to serialize MuleSession"), e);
 64  0
         }
 65  
         
 66  0
         if (logger.isDebugEnabled())
 67  
         {
 68  0
             logger.debug("Adding serialized and base64-encoded Session header to message: " + serializedEncodedSession);
 69  
         }
 70  0
         message.setOutboundProperty(MuleProperties.MULE_SESSION_PROPERTY, serializedEncodedSession);
 71  0
     }
 72  
 }