View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.api.MuleSession;
11  import org.mule.api.config.MuleProperties;
12  import org.mule.api.security.Authentication;
13  import org.mule.api.security.Credentials;
14  import org.mule.api.security.SecurityContext;
15  import org.mule.api.transport.SessionHandler;
16  import org.mule.security.DefaultMuleAuthentication;
17  import org.mule.security.DefaultSecurityContextFactory;
18  import org.mule.security.MuleCredentials;
19  import org.mule.session.DefaultMuleSession;
20  import org.mule.session.LegacySessionHandler;
21  import org.mule.session.SerializeAndEncodeSessionHandler;
22  import org.mule.tck.junit4.AbstractMuleContextTestCase;
23  
24  import java.util.ArrayList;
25  import java.util.Date;
26  import java.util.List;
27  
28  import org.apache.commons.lang.SerializationException;
29  import org.junit.Test;
30  
31  import static org.junit.Assert.assertEquals;
32  import static org.junit.Assert.assertNull;
33  import static org.junit.Assert.assertTrue;
34  import static org.junit.Assert.fail;
35  
36  public class MuleSessionHandlerTestCase extends AbstractMuleContextTestCase
37  {
38      /** @see EE-1705/MULE-4567 */
39      @Test
40      public void testSessionProperties() throws Exception 
41      {
42          DefaultMuleMessage message = new DefaultMuleMessage("Test Message", muleContext);
43          SessionHandler handler = new SerializeAndEncodeSessionHandler();
44          MuleSession session = new DefaultMuleSession(muleContext);
45          
46          String string = "bar";
47          session.setProperty("fooString", string);
48  
49          Date date = new Date(0);
50          session.setProperty("fooDate", date);
51          
52          List<String> list = createList();
53          session.setProperty("fooList", list);
54          
55          handler.storeSessionInfoToMessage(session, message);
56          // store save session to outbound, move it to the inbound
57          // for retrieve to deserialize
58          Object s = message.removeProperty(MuleProperties.MULE_SESSION_PROPERTY);
59          message.setInboundProperty(MuleProperties.MULE_SESSION_PROPERTY, s);
60          session = handler.retrieveSessionInfoFromMessage(message);
61          
62          Object obj = session.getProperty("fooString");
63          assertTrue(obj instanceof String);
64          assertEquals(string, obj);
65          
66          obj = session.getProperty("fooDate");
67          assertTrue("Object should be a Date but is " + obj.getClass().getName(), obj instanceof Date);
68          assertEquals(date, obj);
69  
70          obj = session.getProperty("fooList");
71          assertTrue("Object should be a List but is " + obj.getClass().getName(), obj instanceof List);
72          assertEquals(list, obj);
73      }
74  
75      /** @see EE-1774 */
76      @Test
77      public void testNonSerializableSessionProperties() throws Exception 
78      {
79          DefaultMuleMessage message = new DefaultMuleMessage("Test Message", muleContext);
80          MuleSession session = new DefaultMuleSession(muleContext);
81          SessionHandler handler = new SerializeAndEncodeSessionHandler();
82          
83          NotSerializableClass clazz = new NotSerializableClass();
84          session.setProperty("foo", clazz);
85          handler.storeSessionInfoToMessage(session, message);
86          // store save session to outbound, move it to the inbound
87          // for retrieve to deserialize
88          Object s = message.removeProperty(MuleProperties.MULE_SESSION_PROPERTY);
89          message.setInboundProperty(MuleProperties.MULE_SESSION_PROPERTY, s);
90          session = handler.retrieveSessionInfoFromMessage(message);
91          // Property was removed because it could not be serialized
92          assertNull(session.getProperty("foo"));
93      }    
94  
95      /** @see EE-1820 */
96      @Test
97      public void testBackwardsCompatibility() throws Exception 
98      {
99          MuleMessage message = new DefaultMuleMessage("Test Message", muleContext);
100         SessionHandler legacyHandler = new LegacySessionHandler();
101         MuleSession session = new DefaultMuleSession(muleContext);
102         
103         String string = "bar";
104         session.setProperty("fooString", string);
105 
106         Date date = new Date(0);
107         session.setProperty("fooDate", date);
108         
109         List<String> list = createList();
110         session.setProperty("fooList", list);
111         
112         legacyHandler.storeSessionInfoToMessage(session, message);
113         try
114         {
115             // Try to deserialize legacy format with new session handler
116             session = new SerializeAndEncodeSessionHandler().retrieveSessionInfoFromMessage(message);
117         }
118         catch (SerializationException e)
119         {
120             // expected
121         }
122         session = legacyHandler.retrieveSessionInfoFromMessage(message);
123     }    
124     
125     /** @see EE-1820 */
126     @Test
127     public void testSessionPropertiesLegacyFormat() throws Exception 
128     {
129         DefaultMuleMessage message = new DefaultMuleMessage("Test Message", muleContext);
130         SessionHandler handler = new LegacySessionHandler();
131         MuleSession session = new DefaultMuleSession(muleContext);
132         
133         String string = "bar";
134         session.setProperty("fooString", string);
135 
136         Date date = new Date(0);
137         session.setProperty("fooDate", date);
138         
139         List<String> list = createList();
140         session.setProperty("fooList", list);
141 
142         handler.storeSessionInfoToMessage(session, message);
143         // store save session to outbound, move it to the inbound
144         // for retrieve to deserialize
145         Object s = message.removeProperty(MuleProperties.MULE_SESSION_PROPERTY);
146         message.setInboundProperty(MuleProperties.MULE_SESSION_PROPERTY, s);
147         session = handler.retrieveSessionInfoFromMessage(message);
148         
149         Object obj = session.getProperty("fooString");
150         assertTrue(obj instanceof String);
151         assertEquals(string, obj);
152         
153         obj = session.getProperty("fooDate");
154         // MULE-4567 / EE-1705 
155         assertTrue(obj instanceof String);
156 
157         obj = session.getProperty("fooList");
158         // MULE-4567 / EE-1705 
159         assertTrue(obj instanceof String);
160     }    
161 
162     /** @see MULE-4720 */
163     @Test
164     public void testSecurityContext() throws Exception 
165     {
166         DefaultMuleMessage message = new DefaultMuleMessage("Test Message", muleContext);
167         SessionHandler handler = new SerializeAndEncodeSessionHandler();
168         MuleSession session = new DefaultMuleSession(muleContext);
169         
170         Credentials credentials = new MuleCredentials("joe", "secret".toCharArray());
171         SecurityContext sc = new DefaultSecurityContextFactory().create(new DefaultMuleAuthentication(credentials));
172         session.setSecurityContext(sc);
173 
174         handler.storeSessionInfoToMessage(session, message);
175         // store save session to outbound, move it to the inbound
176         // for retrieve to deserialize
177         Object s = message.removeProperty(MuleProperties.MULE_SESSION_PROPERTY);
178         message.setInboundProperty(MuleProperties.MULE_SESSION_PROPERTY, s);
179         session = handler.retrieveSessionInfoFromMessage(message);
180 
181         sc = session.getSecurityContext();
182         assertEquals("joe", sc.getAuthentication().getPrincipal());
183     }    
184     
185     /** @see EE-1774 */
186     @Test
187     public void testNotSerializableSecurityContext() throws Exception 
188     {
189         MuleMessage message = new DefaultMuleMessage("Test Message", muleContext);
190         SessionHandler handler = new SerializeAndEncodeSessionHandler();
191         MuleSession session = new DefaultMuleSession(muleContext);
192         
193         session.setSecurityContext(new NotSerializableSecurityContext());
194         
195         try
196         {
197             handler.storeSessionInfoToMessage(session, message);
198             fail("Should throw a SerializationException");
199         }
200         catch (SerializationException e)
201         {
202             // expected
203         }
204     }    
205 
206     private List<String> createList()
207     {
208         List<String> list = new ArrayList<String>();
209         list.add("bar1");
210         list.add("bar2");
211         return list;
212     }    
213 
214     private class NotSerializableClass
215     {
216         public NotSerializableClass()
217         {
218             super();
219         }
220     }
221     
222     private class NotSerializableSecurityContext implements SecurityContext
223     {
224         public NotSerializableSecurityContext()
225         {
226             super();
227         }
228         
229         public void setAuthentication(Authentication authentication)
230         { 
231             // nothing to do
232         }
233 
234         public Authentication getAuthentication() 
235         { 
236             return null;
237         }
238     }
239     
240 }