Search in sources :

Example 1 with WebSessionEntity

use of org.apache.ignite.internal.websession.WebSessionEntity in project ignite by apache.

the class WebSessionFilter method updateAttributesV2.

/**
     * @param sesId Session ID.
     * @param ses Web session.
     */
public void updateAttributesV2(final String sesId, final WebSessionV2 ses) throws IOException {
    assert sesId != null;
    assert ses != null;
    final Map<String, byte[]> updatesMap = ses.binaryUpdatesMap();
    if (log.isDebugEnabled())
        log.debug("Session binary attributes updated [id=" + sesId + ", updates=" + updatesMap.keySet() + ']');
    try {
        for (int i = 0; i < retries; i++) {
            try {
                final IgniteCache<String, WebSessionEntity> cache0 = cacheWithExpiryPolicy(ses.getMaxInactiveInterval(), binaryCache);
                cache0.invoke(sesId, new WebSessionAttributeProcessor(updatesMap.isEmpty() ? null : updatesMap, ses.getLastAccessedTime(), ses.getMaxInactiveInterval(), ses.isMaxInactiveIntervalChanged()));
                break;
            } catch (CacheException | IgniteException | IllegalStateException e) {
                handleAttributeUpdateException(sesId, i, e);
            }
        }
    } catch (Exception e) {
        U.error(log, "Failed to update session V2 attributes [id=" + sesId + ']', e);
    }
}
Also used : CacheException(javax.cache.CacheException) IgniteException(org.apache.ignite.IgniteException) WebSessionAttributeProcessor(org.apache.ignite.internal.websession.WebSessionAttributeProcessor) ServletException(javax.servlet.ServletException) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) CacheException(javax.cache.CacheException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteException(org.apache.ignite.IgniteException) IOException(java.io.IOException) WebSessionEntity(org.apache.ignite.internal.websession.WebSessionEntity)

Example 2 with WebSessionEntity

use of org.apache.ignite.internal.websession.WebSessionEntity in project ignite by apache.

the class WebSessionFilter method doFilterV2.

/**
     * @param httpReq Request.
     * @param res Response.
     * @param chain Filter chain.
     * @return Session ID.
     * @throws IOException In case of I/O error.
     * @throws ServletException In case oif servlet error.
     * @throws CacheException In case of other error.
     */
private String doFilterV2(HttpServletRequest httpReq, ServletResponse res, FilterChain chain) throws IOException, ServletException, CacheException {
    WebSessionV2 cached = null;
    String sesId = httpReq.getRequestedSessionId();
    if (sesId != null) {
        sesId = transformSessionId(sesId);
        // Load from cache.
        for (int i = 0; i < retries; i++) {
            try {
                final WebSessionEntity entity = binaryCache.get(sesId);
                if (entity != null)
                    cached = new WebSessionV2(sesId, httpReq.getSession(false), false, ctx, entity, marshaller);
                break;
            } catch (CacheException | IgniteException | IllegalStateException e) {
                handleLoadSessionException(sesId, i, e);
            }
        }
        if (cached != null) {
            if (log.isDebugEnabled())
                log.debug("Using cached session for ID: " + sesId);
        } else // If not found - invalidate session and create new one.
        // Invalidate, because session might be removed from cache
        // according to expiry policy.
        {
            if (log.isDebugEnabled())
                log.debug("Cached session was invalidated and doesn't exist: " + sesId);
            final HttpSession ses = httpReq.getSession(false);
            if (ses != null) {
                try {
                    ses.invalidate();
                } catch (IllegalStateException ignore) {
                // Session was already invalidated.
                }
            }
            cached = createSessionV2(httpReq);
        }
    } else
        // No session was requested by the client, create new one and put in the request.
        cached = createSessionV2(httpReq);
    assert cached != null;
    sesId = cached.getId();
    httpReq = new RequestWrapperV2(httpReq, cached);
    chain.doFilter(httpReq, res);
    WebSessionV2 cachedNew = (WebSessionV2) httpReq.getSession(false);
    if (cachedNew != null && cachedNew.isValid())
        updateAttributesV2(cachedNew.getId(), cachedNew);
    return sesId;
}
Also used : CacheException(javax.cache.CacheException) IgniteException(org.apache.ignite.IgniteException) HttpSession(javax.servlet.http.HttpSession) WebSessionEntity(org.apache.ignite.internal.websession.WebSessionEntity)

Example 3 with WebSessionEntity

use of org.apache.ignite.internal.websession.WebSessionEntity in project ignite by apache.

the class WebSessionFilter method createSessionV2.

/**
     * Creates a new web session with the specified id.
     *
     * @param ses Base session.
     * @param sesId Session id.
     * @return New session.
     */
private WebSessionV2 createSessionV2(final HttpSession ses, final String sesId) throws IOException {
    assert ses != null;
    assert sesId != null;
    WebSessionV2 cached = new WebSessionV2(sesId, ses, true, ctx, null, marshaller);
    final WebSessionEntity marshaledEntity = cached.marshalAttributes();
    for (int i = 0; i < retries; i++) {
        try {
            final IgniteCache<String, WebSessionEntity> cache0 = cacheWithExpiryPolicy(cached.getMaxInactiveInterval(), binaryCache);
            final WebSessionEntity old = cache0.getAndPutIfAbsent(sesId, marshaledEntity);
            if (old != null)
                cached = new WebSessionV2(sesId, ses, false, ctx, old, marshaller);
            else
                cached = new WebSessionV2(sesId, ses, false, ctx, marshaledEntity, marshaller);
            break;
        } catch (CacheException | IgniteException | IllegalStateException e) {
            handleCreateSessionException(sesId, i, e);
        }
    }
    return cached;
}
Also used : CacheException(javax.cache.CacheException) IgniteException(org.apache.ignite.IgniteException) WebSessionEntity(org.apache.ignite.internal.websession.WebSessionEntity)

Aggregations

CacheException (javax.cache.CacheException)3 IgniteException (org.apache.ignite.IgniteException)3 WebSessionEntity (org.apache.ignite.internal.websession.WebSessionEntity)3 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1 HttpSession (javax.servlet.http.HttpSession)1 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)1 ClusterTopologyException (org.apache.ignite.cluster.ClusterTopologyException)1 WebSessionAttributeProcessor (org.apache.ignite.internal.websession.WebSessionAttributeProcessor)1