2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.server;
21 import java.util.EventListener;
24 import javax.servlet.SessionCookieConfig;
25 import javax.servlet.SessionTrackingMode;
26 import javax.servlet.http.Cookie;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpSession;
30 import org.eclipse.jetty.http.HttpCookie;
31 import org.eclipse.jetty.server.session.SessionHandler;
32 import org.eclipse.jetty.util.component.LifeCycle;
34 /* --------------------------------------------------------------------- */
37 * The API required to manage sessions for a servlet context.
41 /* ------------------------------------------------------------ */
44 public interface SessionManager extends LifeCycle
46 /* ------------------------------------------------------------ */
48 * Session cookie name.
49 * Defaults to <code>JSESSIONID</code>, but can be set with the
50 * <code>org.eclipse.jetty.servlet.SessionCookie</code> context init parameter.
52 public final static String __SessionCookieProperty = "org.eclipse.jetty.servlet.SessionCookie";
53 public final static String __DefaultSessionCookie = "JSESSIONID";
56 /* ------------------------------------------------------------ */
58 * Session id path parameter name.
59 * Defaults to <code>jsessionid</code>, but can be set with the
60 * <code>org.eclipse.jetty.servlet.SessionIdPathParameterName</code> context init parameter.
61 * If set to null or "none" no URL rewriting will be done.
63 public final static String __SessionIdPathParameterNameProperty = "org.eclipse.jetty.servlet.SessionIdPathParameterName";
64 public final static String __DefaultSessionIdPathParameterName = "jsessionid";
65 public final static String __CheckRemoteSessionEncoding = "org.eclipse.jetty.servlet.CheckingRemoteSessionIdEncoding";
68 /* ------------------------------------------------------------ */
71 * If this property is set as a ServletContext InitParam, then it is
72 * used as the domain for session cookies. If it is not set, then
73 * no domain is specified for the session cookie.
75 public final static String __SessionDomainProperty = "org.eclipse.jetty.servlet.SessionDomain";
76 public final static String __DefaultSessionDomain = null;
79 /* ------------------------------------------------------------ */
82 * If this property is set as a ServletContext InitParam, then it is
83 * used as the path for the session cookie. If it is not set, then
84 * the context path is used as the path for the cookie.
86 public final static String __SessionPathProperty = "org.eclipse.jetty.servlet.SessionPath";
88 /* ------------------------------------------------------------ */
91 * If this property is set as a ServletContext InitParam, then it is
92 * used as the max age for the session cookie. If it is not set, then
93 * a max age of -1 is used.
95 public final static String __MaxAgeProperty = "org.eclipse.jetty.servlet.MaxAge";
97 /* ------------------------------------------------------------ */
99 * Returns the <code>HttpSession</code> with the given session id
101 * @param id the session id
102 * @return the <code>HttpSession</code> with the corresponding id or null if no session with the given id exists
104 public HttpSession getHttpSession(String id);
106 /* ------------------------------------------------------------ */
108 * Creates a new <code>HttpSession</code>.
110 * @param request the HttpServletRequest containing the requested session id
111 * @return the new <code>HttpSession</code>
113 public HttpSession newHttpSession(HttpServletRequest request);
116 /* ------------------------------------------------------------ */
118 * @return true if session cookies should be HTTP-only (Microsoft extension)
119 * @see org.eclipse.jetty.http.HttpCookie#isHttpOnly()
121 public boolean getHttpOnly();
123 /* ------------------------------------------------------------ */
125 * @return the max period of inactivity, after which the session is invalidated, in seconds.
126 * @see #setMaxInactiveInterval(int)
128 public int getMaxInactiveInterval();
130 /* ------------------------------------------------------------ */
132 * Sets the max period of inactivity, after which the session is invalidated, in seconds.
134 * @param seconds the max inactivity period, in seconds.
135 * @see #getMaxInactiveInterval()
137 public void setMaxInactiveInterval(int seconds);
139 /* ------------------------------------------------------------ */
141 * Sets the {@link SessionHandler}.
143 * @param handler the <code>SessionHandler</code> object
145 public void setSessionHandler(SessionHandler handler);
147 /* ------------------------------------------------------------ */
149 * Adds an event listener for session-related events.
151 * @param listener the session event listener to add
152 * Individual SessionManagers implementations may accept arbitrary listener types,
153 * but they are expected to at least handle HttpSessionActivationListener,
154 * HttpSessionAttributeListener, HttpSessionBindingListener and HttpSessionListener.
155 * @see #removeEventListener(EventListener)
157 public void addEventListener(EventListener listener);
159 /* ------------------------------------------------------------ */
161 * Removes an event listener for for session-related events.
163 * @param listener the session event listener to remove
164 * @see #addEventListener(EventListener)
166 public void removeEventListener(EventListener listener);
168 /* ------------------------------------------------------------ */
170 * Removes all event listeners for session-related events.
172 * @see #removeEventListener(EventListener)
174 public void clearEventListeners();
176 /* ------------------------------------------------------------ */
178 * Gets a Cookie for a session.
180 * @param session the session to which the cookie should refer.
181 * @param contextPath the context to which the cookie should be linked.
182 * The client will only send the cookie value when requesting resources under this path.
183 * @param requestIsSecure whether the client is accessing the server over a secure protocol (i.e. HTTPS).
184 * @return if this <code>SessionManager</code> uses cookies, then this method will return a new
185 * {@link Cookie cookie object} that should be set on the client in order to link future HTTP requests
186 * with the <code>session</code>. If cookies are not in use, this method returns <code>null</code>.
188 public HttpCookie getSessionCookie(HttpSession session, String contextPath, boolean requestIsSecure);
190 /* ------------------------------------------------------------ */
192 * @return the cross context session id manager.
193 * @see #setSessionIdManager(SessionIdManager)
195 public SessionIdManager getSessionIdManager();
197 /* ------------------------------------------------------------ */
199 * @return the cross context session id manager.
200 * @deprecated use {@link #getSessionIdManager()}
203 public SessionIdManager getMetaManager();
205 /* ------------------------------------------------------------ */
207 * Sets the cross context session id manager
209 * @param idManager the cross context session id manager.
210 * @see #getSessionIdManager()
212 public void setSessionIdManager(SessionIdManager idManager);
214 /* ------------------------------------------------------------ */
216 * @param session the session to test for validity
217 * @return whether the given session is valid, that is, it has not been invalidated.
219 public boolean isValid(HttpSession session);
221 /* ------------------------------------------------------------ */
223 * @param session the session object
224 * @return the unique id of the session within the cluster, extended with an optional node id.
225 * @see #getClusterId(HttpSession)
227 public String getNodeId(HttpSession session);
229 /* ------------------------------------------------------------ */
231 * @param session the session object
232 * @return the unique id of the session within the cluster (without a node id extension)
233 * @see #getNodeId(HttpSession)
235 public String getClusterId(HttpSession session);
237 /* ------------------------------------------------------------ */
239 * Called by the {@link SessionHandler} when a session is first accessed by a request.
241 * @param session the session object
242 * @param secure whether the request is secure or not
243 * @return the session cookie. If not null, this cookie should be set on the response to either migrate
244 * the session or to refresh a session cookie that may expire.
245 * @see #complete(HttpSession)
247 public HttpCookie access(HttpSession session, boolean secure);
249 /* ------------------------------------------------------------ */
251 * Called by the {@link SessionHandler} when a session is last accessed by a request.
253 * @param session the session object
254 * @see #access(HttpSession, boolean)
256 public void complete(HttpSession session);
259 * Sets the session id URL path parameter name.
261 * @param parameterName the URL path parameter name for session id URL rewriting (null or "none" for no rewriting).
262 * @see #getSessionIdPathParameterName()
263 * @see #getSessionIdPathParameterNamePrefix()
265 public void setSessionIdPathParameterName(String parameterName);
268 * @return the URL path parameter name for session id URL rewriting, by default "jsessionid".
269 * @see #setSessionIdPathParameterName(String)
271 public String getSessionIdPathParameterName();
274 * @return a formatted version of {@link #getSessionIdPathParameterName()}, by default
275 * ";" + sessionIdParameterName + "=", for easier lookup in URL strings.
276 * @see #getSessionIdPathParameterName()
278 public String getSessionIdPathParameterNamePrefix();
281 * @return whether the session management is handled via cookies.
283 public boolean isUsingCookies();
286 * @return whether the session management is handled via URLs.
288 public boolean isUsingURLs();
290 public Set<SessionTrackingMode> getDefaultSessionTrackingModes();
292 public Set<SessionTrackingMode> getEffectiveSessionTrackingModes();
294 public void setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes);
296 public SessionCookieConfig getSessionCookieConfig();
299 * @return True if absolute URLs are check for remoteness before being session encoded.
301 public boolean isCheckingRemoteSessionIdEncoding();
304 * @param remote True if absolute URLs are check for remoteness before being session encoded.
306 public void setCheckingRemoteSessionIdEncoding(boolean remote);
308 /* ------------------------------------------------------------ */
309 /** Change the existing session id.
311 * @param oldClusterId
313 * @param newClusterId
316 public void renewSessionId(String oldClusterId, String oldNodeId, String newClusterId, String newNodeId);