All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cc.protea.foundation.utility.SessionUtil Maven / Gradle / Ivy

package cc.protea.foundation.utility;

import java.util.Set;

import org.apache.commons.lang3.RandomStringUtils;

import cc.protea.foundation.model.ProteaException;

public class SessionUtil {

	public static Integer getUserId(final String token) {
		switch (ProfoundConfiguration.storage.sessions) {
		case DATABASE:
			return SessionUtilDatabase.getUserId(token);
		case REDIS:
			return SessionUtilRedis.getUserId(token);
		}
		throw new ProteaException("Unexpected session storage configuration");
	}

	public static String create(final Integer userId) {
		if (userId == null) {
			return null;
		}
		final String token = RandomStringUtils.randomAlphanumeric(30);
		switch (ProfoundConfiguration.storage.sessions) {
		case DATABASE:
			SessionUtilDatabase.create(userId, token);
			break;
		case REDIS:
			SessionUtilRedis.create(userId, token);
			break;
		}
		return token;
	}

	public static void remove(final String token) {
		if (token == null) {
			return;
		}
		switch (ProfoundConfiguration.storage.sessions) {
		case DATABASE:
			SessionUtilDatabase.remove(token);
			break;
		case REDIS:
			SessionUtilRedis.remove(token);
			break;
		}
	}

	public static void removeAllForUser(final Integer userId) {
		if (userId == null) {
			return;
		}
		switch (ProfoundConfiguration.storage.sessions) {
		case DATABASE:
			SessionUtilDatabase.removeAllForUser(userId);
			break;
		case REDIS:
			SessionUtilRedis.removeAllForUser(userId);
			break;
		}
	}

	public static Set getAllForUser(final Integer userId) {
		if (userId == null) {
			return null;
		}
		switch (ProfoundConfiguration.storage.sessions) {
		case DATABASE:
			return SessionUtilDatabase.getAllForUser(userId);
		case REDIS:
			return SessionUtilRedis.getAllForUser(userId);
		}
		throw new ProteaException("Unexpected session storage configuration");
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy