1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
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 | |
|
30 | |
|
31 | 0 | public class HttpSessionHandler implements SessionHandler |
32 | |
{ |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | 0 | protected transient Log logger = LogFactory.getLog(getClass()); |
38 | |
|
39 | |
public MuleSession retrieveSessionInfoFromMessage(MuleMessage message) throws MuleException |
40 | |
{ |
41 | 0 | final Object cookiesObject = message.getOutboundProperty(HttpConnector.HTTP_COOKIES_PROPERTY); |
42 | 0 | final String cookieName = MuleProperties.MULE_SESSION_PROPERTY; |
43 | 0 | final String cookieValue = CookieHelper.getCookieValueFromCookies(cookiesObject, cookieName); |
44 | |
|
45 | 0 | MuleSession session = null; |
46 | |
|
47 | 0 | if (cookieValue != null) |
48 | |
{ |
49 | 0 | byte[] serializedSession = Base64.decode(cookieValue); |
50 | |
|
51 | 0 | if (serializedSession != null) |
52 | |
{ |
53 | 0 | session = (MuleSession) SerializationUtils.deserialize(serializedSession); |
54 | |
} |
55 | |
} |
56 | 0 | return session; |
57 | |
} |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
@Deprecated |
64 | |
public void retrieveSessionInfoFromMessage(MuleMessage message, MuleSession session) throws MuleException |
65 | |
{ |
66 | 0 | session = retrieveSessionInfoFromMessage(message); |
67 | 0 | } |
68 | |
|
69 | |
public void storeSessionInfoToMessage(MuleSession session, MuleMessage message) throws MuleException |
70 | |
{ |
71 | 0 | byte[] serializedSession = SerializationUtils.serialize(session); |
72 | |
String serializedEncodedSession; |
73 | |
try |
74 | |
{ |
75 | 0 | serializedEncodedSession = Base64.encodeBytes(serializedSession, Base64.DONT_BREAK_LINES); |
76 | |
} |
77 | 0 | catch (IOException e) |
78 | |
{ |
79 | 0 | throw new SessionException(MessageFactory.createStaticMessage("Unable to serialize MuleSession"), e); |
80 | 0 | } |
81 | |
|
82 | 0 | if (logger.isDebugEnabled()) |
83 | |
{ |
84 | 0 | logger.debug("Adding serialized and base64-encoded Session header to message: " + serializedEncodedSession); |
85 | |
} |
86 | |
|
87 | 0 | final Object preExistentCookies = message.getOutboundProperty(HttpConnector.HTTP_COOKIES_PROPERTY); |
88 | 0 | final String cookieName = MuleProperties.MULE_SESSION_PROPERTY; |
89 | 0 | final String cookieValue = serializedEncodedSession; |
90 | |
|
91 | 0 | Object mergedCookies = CookieHelper.putAndMergeCookie(preExistentCookies, cookieName, cookieValue); |
92 | |
|
93 | 0 | message.setOutboundProperty(HttpConnector.HTTP_COOKIES_PROPERTY, mergedCookies); |
94 | 0 | } |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
@Deprecated |
102 | |
public String getSessionIDKey() |
103 | |
{ |
104 | 0 | return "ID"; |
105 | |
} |
106 | |
} |