Skip to content

Commit 3c9bb7b

Browse files
committed
Adapted SessionHandler from jetty-9.3.25.v20180904
1 parent 4058b2a commit 3c9bb7b

File tree

2 files changed

+378
-0
lines changed

2 files changed

+378
-0
lines changed
Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.eclipse.jetty.server;
19+
20+
import javax.servlet.SessionCookieConfig;
21+
import javax.servlet.SessionTrackingMode;
22+
import javax.servlet.http.Cookie;
23+
import javax.servlet.http.HttpServletRequest;
24+
import javax.servlet.http.HttpSession;
25+
import java.util.EventListener;
26+
import java.util.Set;
27+
28+
import org.eclipse.jetty.http.HttpCookie;
29+
import org.eclipse.jetty.server.session.SessionHandler;
30+
import org.eclipse.jetty.util.component.LifeCycle;
31+
32+
/**
33+
* Adapted from https://github.com/eclipse/jetty.project/blob/jetty-9.3.25.v20180904/
34+
* jetty-server/src/main/java/org/eclipse/jetty/server/SessionManager.java
35+
*/
36+
public interface SessionManager extends LifeCycle {
37+
/**
38+
* Session cookie name.
39+
* Defaults to <code>JSESSIONID</code>, but can be set with the
40+
* <code>org.eclipse.jetty.servlet.SessionCookie</code> context init parameter.
41+
*/
42+
String __SessionCookieProperty = "org.eclipse.jetty.servlet.SessionCookie";
43+
String __DefaultSessionCookie = "JSESSIONID";
44+
45+
/**
46+
* Session id path parameter name.
47+
* Defaults to <code>jsessionid</code>, but can be set with the
48+
* <code>org.eclipse.jetty.servlet.SessionIdPathParameterName</code> context init parameter.
49+
* If set to null or "none" no URL rewriting will be done.
50+
*/
51+
String __SessionIdPathParameterNameProperty =
52+
"org.eclipse.jetty.servlet.SessionIdPathParameterName";
53+
String __DefaultSessionIdPathParameterName = "jsessionid";
54+
String __CheckRemoteSessionEncoding = "org.eclipse.jetty.servlet.CheckingRemoteSessionIdEncoding";
55+
56+
/**
57+
* Session Domain.
58+
* If this property is set as a ServletContext InitParam, then it is
59+
* used as the domain for session cookies. If it is not set, then
60+
* no domain is specified for the session cookie.
61+
*/
62+
String __SessionDomainProperty = "org.eclipse.jetty.servlet.SessionDomain";
63+
String __DefaultSessionDomain = null;
64+
65+
/**
66+
* Session Path.
67+
* If this property is set as a ServletContext InitParam, then it is
68+
* used as the path for the session cookie. If it is not set, then
69+
* the context path is used as the path for the cookie.
70+
*/
71+
String __SessionPathProperty = "org.eclipse.jetty.servlet.SessionPath";
72+
73+
/**
74+
* Session Max Age.
75+
* If this property is set as a ServletContext InitParam, then it is
76+
* used as the max age for the session cookie. If it is not set, then
77+
* a max age of -1 is used.
78+
*/
79+
String __MaxAgeProperty = "org.eclipse.jetty.servlet.MaxAge";
80+
81+
/**
82+
* Returns the <code>HttpSession</code> with the given session id
83+
*
84+
* @param id the session id
85+
* @return the <code>HttpSession</code> with the corresponding id
86+
* or null if no session with the given id exists
87+
*/
88+
HttpSession getHttpSession(String id);
89+
90+
/**
91+
* Creates a new <code>HttpSession</code>.
92+
*
93+
* @param request the HttpServletRequest containing the requested session id
94+
* @return the new <code>HttpSession</code>
95+
*/
96+
HttpSession newHttpSession(HttpServletRequest request);
97+
98+
/**
99+
* @return true if session cookies should be HTTP-only (Microsoft extension)
100+
* @see HttpCookie#isHttpOnly()
101+
*/
102+
boolean getHttpOnly();
103+
104+
/**
105+
* @return the max period of inactivity, after which the session is invalidated, in seconds.
106+
* @see #setMaxInactiveInterval(int)
107+
*/
108+
int getMaxInactiveInterval();
109+
110+
/**
111+
* Sets the max period of inactivity, after which the session is invalidated, in seconds.
112+
*
113+
* @param seconds the max inactivity period, in seconds.
114+
* @see #getMaxInactiveInterval()
115+
*/
116+
void setMaxInactiveInterval(int seconds);
117+
118+
/**
119+
* Sets the {@link SessionHandler}.
120+
*
121+
* @param handler the <code>SessionHandler</code> object
122+
*/
123+
void setSessionHandler(SessionHandler handler);
124+
125+
/**
126+
* Adds an event listener for session-related events.
127+
*
128+
* @param listener the session event listener to add
129+
* Individual SessionManagers implementations may accept arbitrary listener types,
130+
* but they are expected to at least handle HttpSessionActivationListener,
131+
* HttpSessionAttributeListener,
132+
* HttpSessionBindingListener and HttpSessionListener.
133+
* @see #removeEventListener(EventListener)
134+
*/
135+
void addEventListener(EventListener listener);
136+
137+
/**
138+
* Removes an event listener for for session-related events.
139+
*
140+
* @param listener the session event listener to remove
141+
* @see #addEventListener(EventListener)
142+
*/
143+
void removeEventListener(EventListener listener);
144+
145+
/**
146+
* Removes all event listeners for session-related events.
147+
*
148+
* @see #removeEventListener(EventListener)
149+
*/
150+
void clearEventListeners();
151+
152+
/**
153+
* Gets a Cookie for a session.
154+
*
155+
* @param session the session to which the cookie should refer.
156+
* @param contextPath the context to which the cookie should be linked.
157+
* The client will only send the cookie value when
158+
* requesting resources under this path.
159+
* @param requestIsSecure whether the client is accessing the server over
160+
* a secure protocol (i.e. HTTPS).
161+
* @return if this <code>SessionManager</code> uses cookies, then this method will return a new
162+
* {@link Cookie cookie object} that should be set on the client
163+
* in order to link future HTTP requests
164+
* with the <code>session</code>. If cookies are not in use,
165+
* this method returns <code>null</code>.
166+
*/
167+
HttpCookie getSessionCookie(HttpSession session, String contextPath, boolean requestIsSecure);
168+
169+
/**
170+
* @return the cross context session id manager.
171+
* @see #setSessionIdManager(SessionIdManager)
172+
*/
173+
SessionIdManager getSessionIdManager();
174+
175+
/**
176+
* @return the cross context session id manager.
177+
* @deprecated use {@link #getSessionIdManager()}
178+
*/
179+
@Deprecated
180+
SessionIdManager getMetaManager();
181+
182+
/**
183+
* Sets the cross context session id manager
184+
*
185+
* @param idManager the cross context session id manager.
186+
* @see #getSessionIdManager()
187+
*/
188+
void setSessionIdManager(SessionIdManager idManager);
189+
190+
/**
191+
* @param session the session to test for validity
192+
* @return whether the given session is valid, that is, it has not been invalidated.
193+
*/
194+
boolean isValid(HttpSession session);
195+
196+
/**
197+
* @param session the session object
198+
* @return the unique id of the session within the cluster, extended with an optional node id.
199+
* @see #getClusterId(HttpSession)
200+
*/
201+
String getNodeId(HttpSession session);
202+
203+
/**
204+
* @param session the session object
205+
* @return the unique id of the session within the cluster (without a node id extension)
206+
* @see #getNodeId(HttpSession)
207+
*/
208+
String getClusterId(HttpSession session);
209+
210+
/**
211+
* Called by the {@link SessionHandler} when a session is first accessed by a request.
212+
*
213+
* @param session the session object
214+
* @param secure whether the request is secure or not
215+
* @return the session cookie. If not null,
216+
* this cookie should be set on the response to either migrate
217+
* the session or to refresh a session cookie that may expire.
218+
* @see #complete(HttpSession)
219+
*/
220+
HttpCookie access(HttpSession session, boolean secure);
221+
222+
/**
223+
* Called by the {@link SessionHandler} when a session is last accessed by a request.
224+
*
225+
* @param session the session object
226+
* @see #access(HttpSession, boolean)
227+
*/
228+
void complete(HttpSession session);
229+
230+
/**
231+
* Sets the session id URL path parameter name.
232+
*
233+
* @param parameterName the URL path parameter name
234+
* for session id URL rewriting (null or "none" for no rewriting).
235+
* @see #getSessionIdPathParameterName()
236+
* @see #getSessionIdPathParameterNamePrefix()
237+
*/
238+
void setSessionIdPathParameterName(String parameterName);
239+
240+
/**
241+
* @return the URL path parameter name for session id URL rewriting, by default "jsessionid".
242+
* @see #setSessionIdPathParameterName(String)
243+
*/
244+
String getSessionIdPathParameterName();
245+
246+
/**
247+
* @return a formatted version of {@link #getSessionIdPathParameterName()}, by default
248+
* ";" + sessionIdParameterName + "=", for easier lookup in URL strings.
249+
* @see #getSessionIdPathParameterName()
250+
*/
251+
String getSessionIdPathParameterNamePrefix();
252+
253+
/**
254+
* @return whether the session management is handled via cookies.
255+
*/
256+
boolean isUsingCookies();
257+
258+
/**
259+
* @return whether the session management is handled via URLs.
260+
*/
261+
boolean isUsingURLs();
262+
263+
Set<SessionTrackingMode> getDefaultSessionTrackingModes();
264+
265+
Set<SessionTrackingMode> getEffectiveSessionTrackingModes();
266+
267+
void setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes);
268+
269+
SessionCookieConfig getSessionCookieConfig();
270+
271+
/**
272+
* @return True if absolute URLs are check for remoteness before being session encoded.
273+
*/
274+
boolean isCheckingRemoteSessionIdEncoding();
275+
276+
/**
277+
* @param remote True if absolute URLs are check for remoteness before being session encoded.
278+
*/
279+
void setCheckingRemoteSessionIdEncoding(boolean remote);
280+
281+
/** Change the existing session id.
282+
*
283+
* @param oldClusterId the old cluster id
284+
* @param oldNodeId the old node id
285+
* @param newClusterId the new cluster id
286+
* @param newNodeId the new node id
287+
*/
288+
void renewSessionId(String oldClusterId, String oldNodeId, String newClusterId, String newNodeId);
289+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.eclipse.jetty.server.session;
19+
20+
import javax.servlet.ServletException;
21+
import javax.servlet.http.HttpServletRequest;
22+
import javax.servlet.http.HttpServletResponse;
23+
import java.io.IOException;
24+
25+
import org.eclipse.jetty.server.Request;
26+
import org.eclipse.jetty.server.SessionManager;
27+
import org.eclipse.jetty.server.handler.ScopedHandler;
28+
29+
/**
30+
* Adapted from https://github.com/eclipse/jetty.project/blob/jetty-9.3.25.v20180904/
31+
* jetty-server/src/main/java/org/eclipse/jetty/server/session/SessionHandler.java
32+
*/
33+
public class SessionHandler extends ScopedHandler {
34+
private SessionManager _sessionManager;
35+
36+
public SessionHandler() {
37+
}
38+
39+
/**
40+
* @param manager
41+
* The session manager
42+
*/
43+
public SessionHandler(SessionManager manager) {
44+
setSessionManager(manager);
45+
}
46+
47+
/**
48+
* @return Returns the sessionManager.
49+
*/
50+
public SessionManager getSessionManager() {
51+
return _sessionManager;
52+
}
53+
54+
/**
55+
* @param sessionManager
56+
* The sessionManager to set.
57+
*/
58+
public void setSessionManager(SessionManager sessionManager) {
59+
if (isStarted()) {
60+
throw new IllegalStateException();
61+
}
62+
if (sessionManager != null) {
63+
updateBean(_sessionManager,sessionManager);
64+
_sessionManager=sessionManager;
65+
}
66+
}
67+
68+
/*
69+
* @see org.eclipse.jetty.server.Handler#handle(javax.servlet.http.HttpServletRequest,
70+
* javax.servlet.http.HttpServletResponse, int)
71+
*/
72+
@Override
73+
public void doHandle(String target, Request baseRequest, HttpServletRequest request,
74+
HttpServletResponse response) throws IOException, ServletException {
75+
// start manual inline of nextHandle(target,baseRequest,request,response);
76+
if (_nextScope != null && _nextScope == _handler) {
77+
_nextScope.doHandle(target,baseRequest,request,response);
78+
} else if (_handler != null) {
79+
_handler.handle(target,baseRequest,request,response);
80+
// end manual inline
81+
}
82+
}
83+
84+
public void clearEventListeners() {
85+
if (_sessionManager != null) {
86+
_sessionManager.clearEventListeners();
87+
}
88+
}
89+
}

0 commit comments

Comments
 (0)