1 10 11 package org.mule.impl; 12 13 import java.io.UnsupportedEncodingException ; 14 import java.util.Iterator ; 15 import java.util.StringTokenizer ; 16 17 import org.apache.commons.logging.Log; 18 import org.apache.commons.logging.LogFactory; 19 import org.mule.config.MuleProperties; 20 import org.mule.config.i18n.Message; 21 import org.mule.config.i18n.Messages; 22 import org.mule.transformers.codec.Base64Decoder; 23 import org.mule.transformers.codec.Base64Encoder; 24 import org.mule.umo.UMOException; 25 import org.mule.umo.UMOMessage; 26 import org.mule.umo.UMOSession; 27 import org.mule.umo.provider.UMOSessionHandler; 28 import org.mule.umo.transformer.UMOTransformer; 29 30 41 public class MuleSessionHandler implements UMOSessionHandler 42 { 43 44 47 protected transient Log logger = LogFactory.getLog(getClass()); 48 49 private static UMOTransformer encoder = new Base64Encoder(); 50 private static UMOTransformer decoder = new Base64Decoder(); 51 52 public void retrieveSessionInfoFromMessage(UMOMessage message, UMOSession session) throws UMOException 53 { 54 String sessionId = (String )message.removeProperty(MuleProperties.MULE_SESSION_ID_PROPERTY); 55 Object sessionHeader = message.removeProperty(MuleProperties.MULE_SESSION_PROPERTY); 56 57 if (sessionId != null) 58 { 59 throw new IllegalStateException ( 61 "This session handler does not know how to look up session information for session id: " 62 + sessionId); 63 } 64 if (sessionHeader != null) 65 { 66 String sessionString = null; 67 try 68 { 69 sessionString = new String ((byte[])decoder.transform(sessionHeader), message.getEncoding()); 70 } 71 catch (UnsupportedEncodingException e) 72 { 73 e.printStackTrace(); 74 } 75 if (logger.isDebugEnabled()) 76 { 77 logger.debug("Parsing session header: " + sessionString); 78 } 79 String pair; 80 String name; 81 String value; 82 for (StringTokenizer stringTokenizer = new StringTokenizer (sessionString, ";"); stringTokenizer.hasMoreTokens();) 83 { 84 pair = stringTokenizer.nextToken(); 85 int i = pair.indexOf("="); 86 if (i == -1) 87 { 88 throw new IllegalArgumentException (new Message(Messages.SESSION_VALUE_X_IS_MALFORMED, 89 pair).toString()); 90 } 91 name = pair.substring(0, i).trim(); 92 value = pair.substring(i + 1).trim(); 93 session.setProperty(name, value); 94 if (logger.isDebugEnabled()) 95 { 96 logger.debug("Added Session variable: " + pair); 97 } 98 } 99 100 } 101 } 102 103 public void storeSessionInfoToMessage(UMOSession session, UMOMessage message) throws UMOException 104 { 105 StringBuffer buf = new StringBuffer (); 106 buf.append(getSessionIDKey()).append("=").append(session.getId()); 107 for (Iterator iterator = session.getPropertyNames(); iterator.hasNext();) 108 { 109 Object o = iterator.next(); 110 buf.append(";"); 111 buf.append(o).append("=").append(session.getProperty(o)); 112 if (logger.isDebugEnabled()) 113 { 114 logger.debug("Adding property to session header: " + o + "=" + session.getProperty(o)); 115 } 116 } 117 String sessionString = buf.toString(); 118 if (logger.isDebugEnabled()) 119 { 120 logger.debug("Adding session header to message: " + sessionString); 121 } 122 sessionString = (String )encoder.transform(sessionString); 123 message.setProperty(MuleProperties.MULE_SESSION_PROPERTY, sessionString); 124 } 125 126 public String getSessionIDKey() 127 { 128 return "ID"; 129 } 130 } 131 | Popular Tags |