View Javadoc

1   /*
2    * $Id: HttpSessionHandler.java 19494 2010-09-09 15:58:39Z epere4 $
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.transport.http;
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.api.transport.SessionHandler;
19  import org.mule.config.i18n.MessageFactory;
20  import org.mule.util.Base64;
21  
22  import java.io.IOException;
23  
24  import org.apache.commons.lang.SerializationUtils;
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  /**
29   * Will read and write Http Cookie information to and from the Mule MuleSession
30   */
31  public class HttpSessionHandler implements SessionHandler
32  {
33  
34      /**
35       * logger used by this class
36       */
37      protected transient Log logger = LogFactory.getLog(getClass());
38  
39      public MuleSession retrieveSessionInfoFromMessage(MuleMessage message) throws MuleException
40      {
41          final Object cookiesObject = message.getOutboundProperty(HttpConnector.HTTP_COOKIES_PROPERTY);
42          final String cookieName = MuleProperties.MULE_SESSION_PROPERTY;
43          final String cookieValue = CookieHelper.getCookieValueFromCookies(cookiesObject, cookieName);
44  
45          MuleSession session = null;
46  
47          if (cookieValue != null)
48          {
49              byte[] serializedSession = Base64.decode(cookieValue);
50              
51              if (serializedSession != null)
52              {
53                  session = (MuleSession) SerializationUtils.deserialize(serializedSession);
54              }
55          }
56          return session;
57      }
58  
59  
60      /**
61       * @deprecated Use retrieveSessionInfoFromMessage(MuleMessage message) instead
62       */
63      @Deprecated
64      public void retrieveSessionInfoFromMessage(MuleMessage message, MuleSession session) throws MuleException
65      {
66          session = retrieveSessionInfoFromMessage(message);
67      }
68  
69      public void storeSessionInfoToMessage(MuleSession session, MuleMessage message) throws MuleException
70      {
71          byte[] serializedSession = SerializationUtils.serialize(session);
72          String serializedEncodedSession;
73          try
74          {
75              serializedEncodedSession = Base64.encodeBytes(serializedSession, Base64.DONT_BREAK_LINES);
76          }
77          catch (IOException e)
78          {
79              throw new SessionException(MessageFactory.createStaticMessage("Unable to serialize MuleSession"), e);
80          }
81          
82          if (logger.isDebugEnabled())
83          {
84              logger.debug("Adding serialized and base64-encoded Session header to message: " + serializedEncodedSession);
85          }
86  
87          final Object preExistentCookies = message.getOutboundProperty(HttpConnector.HTTP_COOKIES_PROPERTY);
88          final String cookieName = MuleProperties.MULE_SESSION_PROPERTY;
89          final String cookieValue = serializedEncodedSession;
90  
91          Object mergedCookies = CookieHelper.putAndMergeCookie(preExistentCookies, cookieName, cookieValue);
92  
93          message.setOutboundProperty(HttpConnector.HTTP_COOKIES_PROPERTY, mergedCookies);
94      }
95  
96  
97  
98      /**
99       * @deprecated This method is no longer needed and will be removed in the next major release
100      */
101     @Deprecated
102     public String getSessionIDKey()
103     {
104         return "ID";
105     }
106 }