|
2 | 2 |
|
3 | 3 | import com.mendix.core.Core; |
4 | 4 | import com.mendix.m2ee.api.IMxRuntimeResponse; |
| 5 | +import com.mendix.core.conf.RuntimeVersion; |
5 | 6 | import com.mendix.systemwideinterfaces.core.ISession; |
| 7 | +import java.lang.reflect.Method; |
| 8 | +import java.lang.reflect.InvocationTargetException; |
6 | 9 |
|
7 | 10 | public class SessionHandler { |
8 | 11 |
|
9 | 12 | private static final String XAS_ID = "XASID"; |
10 | 13 |
|
11 | 14 | public static ISession GetFreshGuestSession(IMxRuntimeResponse response) throws Exception { |
12 | | - |
13 | 15 | ISession session = Core.initializeGuestSession(); |
14 | 16 | setCookies(response, session); |
15 | 17 |
|
16 | 18 | return session; |
17 | 19 | } |
18 | 20 |
|
19 | | - private static void setCookies(IMxRuntimeResponse response, ISession session) { |
20 | | - response.addCookie(Core.getConfiguration().getSessionIdCookieName(), session.getId().toString(), "/", "", -1, true); |
21 | | - response.addCookie(XAS_ID, "0."+Core.getXASId(),"/", "", -1, true); |
| 21 | + private static void setCookies(IMxRuntimeResponse response, ISession session) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { |
| 22 | + String[] mxVersion = RuntimeVersion.getInstance().toString().split("\\."); |
| 23 | + if (Integer.parseInt(mxVersion[0]) >=9 && Integer.parseInt(mxVersion[1]) >= 20) { |
| 24 | + // use reflection to call the addCookie method with the 7th parameter for 'isHostOnly', which was added in 9.20 |
| 25 | + @SuppressWarnings("rawtypes") |
| 26 | + Class[] methodSignature = {String.class, String.class, String.class, String.class, int.class, boolean.class, boolean.class}; |
| 27 | + Method addCookie = response.getClass().getMethod("addCookie", methodSignature); |
| 28 | + addCookie.invoke(response, Core.getConfiguration().getSessionIdCookieName(), session.getId().toString(), "/", "", -1, true, true); |
| 29 | + } else { |
| 30 | + response.addCookie(Core.getConfiguration().getSessionIdCookieName(), session.getId().toString(), "/", "", -1, true); |
| 31 | + } |
22 | 32 | } |
23 | | - |
| 33 | + |
24 | 34 | } |
0 commit comments