1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.server.session;
20
21 import org.eclipse.jetty.server.NetworkConnector;
22 import org.eclipse.jetty.server.Server;
23 import org.eclipse.jetty.server.SessionIdManager;
24 import org.eclipse.jetty.server.SessionManager;
25 import org.eclipse.jetty.server.handler.ContextHandlerCollection;
26 import org.eclipse.jetty.servlet.ServletContextHandler;
27 import org.eclipse.jetty.webapp.WebAppContext;
28
29
30
31
32
33
34
35 public abstract class AbstractTestServer
36 {
37 public static int DEFAULT_MAX_INACTIVE = 30;
38 public static int DEFAULT_SCAVENGE = 10;
39
40 protected final Server _server;
41 protected final int _maxInactivePeriod;
42 protected final int _scavengePeriod;
43 protected final ContextHandlerCollection _contexts;
44 protected SessionIdManager _sessionIdManager;
45
46
47
48 public static String extractSessionId (String sessionCookie)
49 {
50 if (sessionCookie == null)
51 return null;
52 sessionCookie = sessionCookie.trim();
53 int i = sessionCookie.indexOf(';');
54 if (i >= 0)
55 sessionCookie = sessionCookie.substring(0,i);
56 if (sessionCookie.startsWith("JSESSIONID"))
57 sessionCookie = sessionCookie.substring("JSESSIONID=".length());
58 i = sessionCookie.indexOf('.');
59 if (i >=0)
60 sessionCookie = sessionCookie.substring(0,i);
61 return sessionCookie;
62 }
63
64
65
66 public AbstractTestServer(int port)
67 {
68 this(port, DEFAULT_MAX_INACTIVE, DEFAULT_SCAVENGE);
69 }
70
71 public AbstractTestServer(int port, int maxInactivePeriod, int scavengePeriod)
72 {
73 this (port, maxInactivePeriod, scavengePeriod, null);
74 }
75
76 public AbstractTestServer(int port, int maxInactivePeriod, int scavengePeriod, Object sessionIdMgrConfig)
77 {
78 _server = new Server(port);
79 _maxInactivePeriod = maxInactivePeriod;
80 _scavengePeriod = scavengePeriod;
81 _contexts = new ContextHandlerCollection();
82 _sessionIdManager = newSessionIdManager(sessionIdMgrConfig);
83 _server.setSessionIdManager(_sessionIdManager);
84 }
85
86
87
88 public abstract SessionIdManager newSessionIdManager(Object config);
89 public abstract SessionManager newSessionManager();
90 public abstract SessionHandler newSessionHandler(SessionManager sessionManager);
91
92
93 public void start() throws Exception
94 {
95
96 _server.setHandler(_contexts);
97 _server.start();
98 }
99
100 public int getPort()
101 {
102 return ((NetworkConnector)getServer().getConnectors()[0]).getLocalPort();
103 }
104
105 public ServletContextHandler addContext(String contextPath)
106 {
107 ServletContextHandler context = new ServletContextHandler(_contexts, contextPath);
108
109 SessionManager sessionManager = newSessionManager();
110 sessionManager.setSessionIdManager(_sessionIdManager);
111 sessionManager.setMaxInactiveInterval(_maxInactivePeriod);
112
113 SessionHandler sessionHandler = newSessionHandler(sessionManager);
114 sessionManager.setSessionHandler(sessionHandler);
115 context.setSessionHandler(sessionHandler);
116
117 return context;
118 }
119
120 public void stop() throws Exception
121 {
122 _server.stop();
123 }
124
125 public void join() throws Exception
126 {
127 _server.join();
128 }
129
130 public WebAppContext addWebAppContext(String warPath, String contextPath)
131 {
132 WebAppContext context = new WebAppContext(_contexts, warPath, contextPath);
133
134 SessionManager sessionManager = newSessionManager();
135 sessionManager.setSessionIdManager(_sessionIdManager);
136 sessionManager.setMaxInactiveInterval(_maxInactivePeriod);
137
138 SessionHandler sessionHandler = newSessionHandler(sessionManager);
139 sessionManager.setSessionHandler(sessionHandler);
140 context.setSessionHandler(sessionHandler);
141
142 return context;
143 }
144
145 public Server getServer()
146 {
147 return _server;
148 }
149 }