Coverage Report - org.mule.MuleSessionHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleSessionHandler
30%
13/44
11%
2/18
5
 
 1  
 /*
 2  
  * $Id: MuleSessionHandler.java 11158 2008-03-04 15:41:08Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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;
 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.Iterator;
 25  
 import java.util.StringTokenizer;
 26  
 
 27  
 import org.apache.commons.logging.Log;
 28  
 import org.apache.commons.logging.LogFactory;
 29  
 
 30  
 /**
 31  
  * A default session handler used to store and retrieve session information on an
 32  
  * event. The DefaultMuleSession information is stored as a header on the message (does not
 33  
  * support Tcp, Udp, etc. unless the MuleMessage object is serialised across the
 34  
  * wire). The session is stored in the "MULE_SESSION" property as String key/value
 35  
  * pairs that are Base64 encoded, for example:
 36  
  * ID=dfokokdf-3ek3oke-dkfokd;MySessionProp1=Value1;MySessionProp2=Value2
 37  
  */
 38  944
 public class MuleSessionHandler implements SessionHandler
 39  
 {
 40  
 
 41  
     /**
 42  
      * logger used by this class
 43  
      */
 44  944
     protected transient Log logger = LogFactory.getLog(getClass());
 45  
 
 46  2
     private static Transformer encoder = new Base64Encoder();
 47  2
     private static Transformer decoder = new Base64Decoder();
 48  
 
 49  
     public void retrieveSessionInfoFromMessage(MuleMessage message, MuleSession session) throws MuleException
 50  
     {
 51  0
         String sessionId = (String) message.removeProperty(MuleProperties.MULE_SESSION_ID_PROPERTY);
 52  0
         Object sessionHeader = message.removeProperty(MuleProperties.MULE_SESSION_PROPERTY);
 53  
 
 54  0
         if (sessionId != null)
 55  
         {
 56  0
             throw new IllegalStateException(
 57  
                 "This session handler does not know how to look up session information for session id: "
 58  
                                 + sessionId);
 59  
         }
 60  0
         if (sessionHeader != null)
 61  
         {
 62  0
             String sessionString = null;
 63  
             try
 64  
             {
 65  0
                 sessionString = new String((byte[]) decoder.transform(sessionHeader), message.getEncoding());
 66  
             }
 67  0
             catch (UnsupportedEncodingException e)
 68  
             {
 69  0
                 sessionString = new String((byte[]) decoder.transform(sessionHeader));
 70  0
             }
 71  0
             if (logger.isDebugEnabled())
 72  
             {
 73  0
                 logger.debug("Parsing session header: " + sessionString);
 74  
             }
 75  
             String pair;
 76  
             String name;
 77  
             String value;
 78  0
             for (StringTokenizer stringTokenizer = new StringTokenizer(sessionString, ";"); stringTokenizer.hasMoreTokens();)
 79  
             {
 80  0
                 pair = stringTokenizer.nextToken();
 81  0
                 int i = pair.indexOf("=");
 82  0
                 if (i == -1)
 83  
                 {
 84  0
                     throw new IllegalArgumentException(
 85  
                         CoreMessages.sessionValueIsMalformed(pair).toString());
 86  
                 }
 87  0
                 name = pair.substring(0, i).trim();
 88  0
                 value = pair.substring(i + 1).trim();
 89  0
                 session.setProperty(name, value);
 90  0
                 if (logger.isDebugEnabled())
 91  
                 {
 92  0
                     logger.debug("Added MuleSession variable: " + pair);
 93  
                 }
 94  0
             }
 95  
 
 96  
         }
 97  0
     }
 98  
 
 99  
     public void storeSessionInfoToMessage(MuleSession session, MuleMessage message) throws MuleException
 100  
     {
 101  8
         StringBuffer buf = new StringBuffer();
 102  8
         buf.append(getSessionIDKey()).append("=").append(session.getId());
 103  8
         for (Iterator iterator = session.getPropertyNames(); iterator.hasNext();)
 104  
         {
 105  0
             Object o = iterator.next();
 106  0
             buf.append(";");
 107  0
             buf.append(o).append("=").append(session.getProperty(o));
 108  0
             if (logger.isDebugEnabled())
 109  
             {
 110  0
                 logger.debug("Adding property to session header: " + o + "=" + session.getProperty(o));
 111  
             }
 112  0
         }
 113  8
         String sessionString = buf.toString();
 114  8
         if (logger.isDebugEnabled())
 115  
         {
 116  0
             logger.debug("Adding session header to message: " + sessionString);
 117  
         }
 118  8
         sessionString = (String) encoder.transform(sessionString);
 119  8
         message.setProperty(MuleProperties.MULE_SESSION_PROPERTY, sessionString);
 120  8
     }
 121  
 
 122  
     public String getSessionIDKey()
 123  
     {
 124  8
         return "ID";
 125  
     }
 126  
 }