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

com.genexus.webpanels.WebSession Maven / Gradle / Ivy

Go to download

Core classes for the runtime used by Java and Android apps generated with GeneXus

There is a newer version: 4.7.3
Show newest version
package com.genexus.webpanels;


import java.util.*;

import com.genexus.servlet.http.IHttpServletRequest;
import com.genexus.servlet.http.IHttpSession;

import com.genexus.CommonUtil;
import com.genexus.internet.HttpContext;
import com.genexus.util.Encryption;
import com.genexus.util.SubmitThreadPool;

public class WebSession {
	private IHttpServletRequest request;
	private Hashtable sessionValues;
	private boolean useLocalSession;
	private static Set internalKeys = new HashSet(Arrays.asList(Encryption.AJAX_ENCRYPTION_KEY, HttpContext.GX_NAV_HELPER, HttpContext.GXTheme, HttpContext.GXLanguage));

	public WebSession(IHttpServletRequest request) {
		this.request = request;
		sessionValues = null;
		updateSessionInvalidated();
	}

	private IHttpSession getSession() {
		return getSession(true);
	}

	private IHttpSession getSession(boolean createIfNotExists) {
		if (useLocalSession || request == null)
			return null;
		return request.getSession(createIfNotExists);
	}

	private void updateSessionInvalidated() {
		useLocalSession = useLocalSession || Thread.currentThread().getName().startsWith(SubmitThreadPool.SUBMIT_THREAD) || this.request == null;
	}

	private void putHashValue(String key, Object value) {
		if (sessionValues == null) {
			sessionValues = new Hashtable<>();
		}
		sessionValues.put(key, value);
	}

	private Object getHashValue(String key) {
		if (sessionValues != null) {
			return sessionValues.get(key);
		}
		return null;
	}

	private void removeHashValue(String key) {
		if (sessionValues != null) {
			sessionValues.remove(key);
		}
	}

	private void clearHashValues() {
		if (sessionValues != null) {
			sessionValues.clear();
			sessionValues = null;
		}
	}

	public void invalidate() {
		useLocalSession = true;
	}

	public String getId() {
		if (!useLocalSession) {
			return getSession().getId();
		}
		return "";
	}

	public void setValue(String key, String value) {
		setAttribute(key, value);
	}

	public String getValue(String key) {
		return getAttribute(key);
	}

	public void remove(String key) {
		removeAttribute(key);
	}

	public void setObjectAttribute(String key, Object value) {
		updateSessionInvalidated();
		key = normalizeKey(key);
		IHttpSession session = getSession(true);
		if (useLocalSession || session == null) {
			putHashValue(key, value);
			return;
		}
		session.setAttribute(key, value);
	}

	public void setAttribute(String key, String value) {
		setObjectAttribute(key, value);
	}

	public String getAttribute(String key) {
		Object out = getObjectAttribute(key);
		if (out == null)
			return "";
		else
			return (String) out;
	}

	public Object getObjectAttribute(String key) {
		updateSessionInvalidated();
		key = normalizeKey(key);
		Object out = null;
		if (useLocalSession) {
			return getHashValue(key);
		}
		IHttpSession session = getSession(false);
		if (session != null) {
			out = session.getAttribute(key);
		}
		return out;
	}

	public void removeAttribute(String key) {
		updateSessionInvalidated();
		key = normalizeKey(key);
		if (useLocalSession) {
			removeHashValue(key);
			return;
		}
		IHttpSession session = getSession(false);
		if (session != null) {
			session.removeAttribute(key);
		}
	}

	public void destroy() {
		updateSessionInvalidated();
		if (!useLocalSession) {
			IHttpSession session = getSession(false);
			if (session != null) {
				session.invalidate();
			}
		} else {
			clear();
		}
	}

	public void renew() {
		updateSessionInvalidated();
		if (!useLocalSession) {
			IHttpSession session = getSession(false);
			if (session != null) {
				Map internalValues = backupInternalKeys(session);
				session.invalidate();
				restoreInternalKeys(internalValues);
			}
		} else {
			clear();
		}
	}
	private Map backupInternalKeys(IHttpSession session) {
		Map internalValues = new HashMap<>();
		Enumeration e = session.getAttributeNames();
		while (e.hasMoreElements()) {
			String key = (String) e.nextElement();
			if (internalKeys.contains(key)) {
				Object value = getObjectAttribute(key);
				if (value != null)
					internalValues.put(key, value);
			}
		}
		return internalValues;
	}
	private void restoreInternalKeys(Map internalValues) {
		Iterator e = internalValues.keySet().iterator();
		while (e.hasNext()) {
			String key = (String) e.next();
			setObjectAttribute(key, internalValues.get(key));
		}
	}

	public void clear() {
		updateSessionInvalidated();
		if (!useLocalSession) {
			IHttpSession session = getSession(false);
			if (session != null) {
				Vector toRemove = new Vector<>();
				Enumeration e = session.getAttributeNames();
				while (e.hasMoreElements()) {
					String key = (String) e.nextElement();
					if (!internalKeys.contains(key)) {
						toRemove.add(key);
					}
				}
				e = toRemove.elements();
				while (e.hasMoreElements()) {
					remove((String) e.nextElement());
				}
				toRemove.clear();
			}
		} else {
			clearHashValues();
		}
	}

	private String normalizeKey(String key) {
		return CommonUtil.rtrim(CommonUtil.upper(key));
	}

	public static boolean isSessionExpired(IHttpServletRequest request) {
		return request.getRequestedSessionId() != null && !request.isRequestedSessionIdValid();            //
	}
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy