Search in sources :

Example 6 with HttpCookie

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;
}
Also used : HttpCookie(org.eclipse.jetty.http.HttpCookie) HttpSession(javax.servlet.http.HttpSession)

Example 7 with HttpCookie

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);
        }
    }
}
Also used : HttpSession(javax.servlet.http.HttpSession) HttpCookie(org.eclipse.jetty.http.HttpCookie)

Example 8 with HttpCookie

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);
            }
        }
    }
}
Also used : HttpScheme(org.eclipse.jetty.http.HttpScheme) HttpVersion(org.eclipse.jetty.http.HttpVersion) CookieCompliance(org.eclipse.jetty.http.CookieCompliance) StringUtil(org.eclipse.jetty.util.StringUtil) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpURI(org.eclipse.jetty.http.HttpURI) ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServletOutputStream(javax.servlet.ServletOutputStream) Locale(java.util.Locale) MetaData(org.eclipse.jetty.http.MetaData) IllegalSelectorException(java.nio.channels.IllegalSelectorException) HttpStatus(org.eclipse.jetty.http.HttpStatus) Cookie(javax.servlet.http.Cookie) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) HttpFields(org.eclipse.jetty.http.HttpFields) EnumSet(java.util.EnumSet) PrintWriter(java.io.PrintWriter) HttpSession(javax.servlet.http.HttpSession) HttpContent(org.eclipse.jetty.http.HttpContent) HttpHeaderValue(org.eclipse.jetty.http.HttpHeaderValue) QuotedStringTokenizer(org.eclipse.jetty.util.QuotedStringTokenizer) Collection(java.util.Collection) RequestDispatcher(javax.servlet.RequestDispatcher) HttpCookie(org.eclipse.jetty.http.HttpCookie) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) Collectors(java.util.stream.Collectors) SessionHandler(org.eclipse.jetty.server.session.SessionHandler) List(java.util.List) Syntax(org.eclipse.jetty.http.Syntax) HttpField(org.eclipse.jetty.http.HttpField) PreEncodedHttpField(org.eclipse.jetty.http.PreEncodedHttpField) Log(org.eclipse.jetty.util.log.Log) HttpGenerator(org.eclipse.jetty.http.HttpGenerator) URIUtil(org.eclipse.jetty.util.URIUtil) Logger(org.eclipse.jetty.util.log.Logger) DateGenerator(org.eclipse.jetty.http.DateGenerator) Collections(java.util.Collections) MimeTypes(org.eclipse.jetty.http.MimeTypes) SessionHandler(org.eclipse.jetty.server.session.SessionHandler) HttpField(org.eclipse.jetty.http.HttpField) PreEncodedHttpField(org.eclipse.jetty.http.PreEncodedHttpField) HttpSession(javax.servlet.http.HttpSession) HttpCookie(org.eclipse.jetty.http.HttpCookie) HttpHeaderValue(org.eclipse.jetty.http.HttpHeaderValue)

Example 9 with HttpCookie

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;
}
Also used : HttpCookie(org.eclipse.jetty.http.HttpCookie)

Aggregations

HttpCookie (org.eclipse.jetty.http.HttpCookie)9 HttpSession (javax.servlet.http.HttpSession)3 Response (org.eclipse.jetty.server.Response)3 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 IllegalSelectorException (java.nio.channels.IllegalSelectorException)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 EnumSet (java.util.EnumSet)1 List (java.util.List)1 Locale (java.util.Locale)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Collectors (java.util.stream.Collectors)1 RequestDispatcher (javax.servlet.RequestDispatcher)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 SessionCookieConfig (javax.servlet.SessionCookieConfig)1 Cookie (javax.servlet.http.Cookie)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 CookieCompliance (org.eclipse.jetty.http.CookieCompliance)1 DateGenerator (org.eclipse.jetty.http.DateGenerator)1