org.graylog2.security.MongoDbSessionDAO Maven / Gradle / Ivy
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* .
*/
package org.graylog2.security;
import com.github.rholder.retry.RetryException;
import com.github.rholder.retry.Retryer;
import com.github.rholder.retry.RetryerBuilder;
import com.github.rholder.retry.StopStrategies;
import com.github.rholder.retry.WaitStrategies;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import com.mongodb.DuplicateKeyException;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.SimpleSession;
import org.apache.shiro.session.mgt.eis.CachingSessionDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
public class MongoDbSessionDAO extends CachingSessionDAO {
private static final Logger LOG = LoggerFactory.getLogger(MongoDbSessionDAO.class);
private final MongoDBSessionService mongoDBSessionService;
@Inject
public MongoDbSessionDAO(MongoDBSessionService mongoDBSessionService, EventBus eventBus) {
this.mongoDBSessionService = mongoDBSessionService;
eventBus.register(this);
}
@SuppressWarnings("unused")
@Subscribe
public void sessionDeleted(SessionDeletedEvent event) {
final Session cachedSession = getCachedSession(event.sessionId());
if (cachedSession != null) {
LOG.debug("Removing deleted session from cache.");
uncache(cachedSession);
}
}
@Override
protected Serializable doCreate(Session session) {
final Serializable id = generateSessionId(session);
assignSessionId(session, id);
Map fields = Maps.newHashMap();
fields.put("session_id", id);
fields.put("host", session.getHost());
fields.put("start_timestamp", session.getStartTimestamp());
fields.put("last_access_time", session.getLastAccessTime());
fields.put("timeout", session.getTimeout());
Map
© 2015 - 2024 Weber Informatics LLC | Privacy Policy