1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
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 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
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 | |
|
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 | |
} |