use of org.eclipse.jetty.http.HttpCookie in project jetty.project by eclipse.
the class SessionHandler method access.
/* ------------------------------------------------------------ */
/**
* Called by the {@link SessionHandler} when a session is first accessed by a request.
*
* @param session the session object
* @param secure whether the request is secure or not
* @return the session cookie. If not null, this cookie should be set on the response to either migrate
* the session or to refresh a session cookie that may expire.
* @see #complete(HttpSession)
*/
public HttpCookie access(HttpSession session, boolean secure) {
long now = System.currentTimeMillis();
Session s = ((SessionIf) session).getSession();
if (s.access(now)) {
// Do we need to refresh the cookie?
if (isUsingCookies() && (s.isIdChanged() || (getSessionCookieConfig().getMaxAge() > 0 && getRefreshCookieAge() > 0 && ((now - s.getCookieSetTime()) / 1000 > getRefreshCookieAge())))) {
HttpCookie cookie = getSessionCookie(session, _context == null ? "/" : (_context.getContextPath()), secure);
s.cookieSet();
s.setIdChanged(false);
return cookie;
}
}
return null;
}
use of org.eclipse.jetty.http.HttpCookie in project jetty.project by eclipse.
the class SessionHandler method doScope.
/* ------------------------------------------------------------ */
/*
* @see org.eclipse.jetty.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
*/
@Override
public void doScope(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
SessionHandler old_session_manager = null;
HttpSession old_session = null;
HttpSession existingSession = null;
try {
old_session_manager = baseRequest.getSessionHandler();
old_session = baseRequest.getSession(false);
if (old_session_manager != this) {
// new session context
baseRequest.setSessionHandler(this);
baseRequest.setSession(null);
checkRequestedSessionId(baseRequest, request);
}
// access any existing session for this context
existingSession = baseRequest.getSession(false);
if ((existingSession != null) && (old_session_manager != this)) {
HttpCookie cookie = access(existingSession, request.isSecure());
// Handle changed ID or max-age refresh, but only if this is not a redispatched request
if ((cookie != null) && (request.getDispatcherType() == DispatcherType.ASYNC || request.getDispatcherType() == DispatcherType.REQUEST))
baseRequest.getResponse().addCookie(cookie);
}
if (LOG.isDebugEnabled()) {
LOG.debug("sessionHandler=" + this);
LOG.debug("session=" + existingSession);
}
if (_nextScope != null)
_nextScope.doScope(target, baseRequest, request, response);
else if (_outerScope != null)
_outerScope.doHandle(target, baseRequest, request, response);
else
doHandle(target, baseRequest, request, response);
} finally {
//if there is a session that was created during handling this context, then complete it
HttpSession finalSession = baseRequest.getSession(false);
if (LOG.isDebugEnabled())
LOG.debug("FinalSession=" + finalSession + " old_session_manager=" + old_session_manager + " this=" + this);
if ((finalSession != null) && (old_session_manager != this)) {
complete((Session) finalSession, baseRequest);
}
if (old_session_manager != null && old_session_manager != this) {
baseRequest.setSessionHandler(old_session_manager);
baseRequest.setSession(old_session);
}
}
}
use of org.eclipse.jetty.http.HttpCookie in project jetty.project by eclipse.
the class Response method reset.
public void reset(boolean preserveCookies) {
resetForForward();
_status = 200;
_reason = null;
_contentLength = -1;
List<HttpField> cookies = preserveCookies ? _fields.stream().filter(f -> f.getHeader() == HttpHeader.SET_COOKIE).collect(Collectors.toList()) : null;
_fields.clear();
String connection = _channel.getRequest().getHeader(HttpHeader.CONNECTION.asString());
if (connection != null) {
for (String value : StringUtil.csvSplit(null, connection, 0, connection.length())) {
HttpHeaderValue cb = HttpHeaderValue.CACHE.get(value);
if (cb != null) {
switch(cb) {
case CLOSE:
_fields.put(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.toString());
break;
case KEEP_ALIVE:
if (HttpVersion.HTTP_1_0.is(_channel.getRequest().getProtocol()))
_fields.put(HttpHeader.CONNECTION, HttpHeaderValue.KEEP_ALIVE.toString());
break;
case TE:
_fields.put(HttpHeader.CONNECTION, HttpHeaderValue.TE.toString());
break;
default:
}
}
}
}
if (preserveCookies)
cookies.forEach(f -> _fields.add(f));
else {
Request request = getHttpChannel().getRequest();
HttpSession session = request.getSession(false);
if (session != null && session.isNew()) {
SessionHandler sh = request.getSessionHandler();
if (sh != null) {
HttpCookie c = sh.getSessionCookie(session, request.getContextPath(), request.isSecure());
if (c != null)
addCookie(c);
}
}
}
}
use of org.eclipse.jetty.http.HttpCookie in project jetty.project by eclipse.
the class Request method getSession.
/* ------------------------------------------------------------ */
/*
* @see javax.servlet.http.HttpServletRequest#getSession(boolean)
*/
@Override
public HttpSession getSession(boolean create) {
if (_session != null) {
if (_sessionHandler != null && !_sessionHandler.isValid(_session))
_session = null;
else
return _session;
}
if (!create)
return null;
if (getResponse().isCommitted())
throw new IllegalStateException("Response is committed");
if (_sessionHandler == null)
throw new IllegalStateException("No SessionManager");
_session = _sessionHandler.newHttpSession(this);
HttpCookie cookie = _sessionHandler.getSessionCookie(_session, getContextPath(), isSecure());
if (cookie != null)
_channel.getResponse().addCookie(cookie);
return _session;
}
Aggregations