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

xworker.rap.RapSession Maven / Gradle / Ivy

package xworker.rap;

import org.eclipse.rap.rwt.RWT;
import org.eclipse.rap.rwt.widgets.WidgetUtil;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import xworker.lang.security.SecuritySession;
import xworker.web.WebSession;
import xworker.web.WebSessionManager;
import xworker.web.javax.JavaxHttpSession;
import xworker.web.javax.JavaxServletContext;
import xworker.web.servlet.ServletContext;
import xworker.web.util.HttpUtils;
import xworker.web.javax.JavaxHttpServletRequest;

import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class RapSession extends SecuritySession {
	Shell rootShell;
	Composite rootComposite;
	Locale locale = null;
	Map attributes = new HashMap<>();

	public RapSession() {
	}
	
	@Override
	public Object getAttribute(Object name) {
		WebSession webSession = getWebSession();
		if(webSession != null) {
			return webSession.getAttribute(name);
		}else {
			return attributes.get(name);
		}
	}

	@Override
	public void setAttribute(Object name, Object value) {
		WebSession webSession = getWebSession();
		if(webSession != null) {
			webSession.setAttribute(name, value);
		}
		
		attributes.put(name, value);
	}

	@Override
	public Object removeAttribute(Object key) {
		WebSession webSession = getWebSession();
		if(webSession != null) {
			return webSession.removeAttribute(key);
		}else {
			return attributes.remove(key);
		}
	}

	public static WebSession getWebSession() {
		HttpSession httpSession = RWT.getUISession().getHttpSession();
		if(httpSession != null) {
			WebSession session = (WebSession) httpSession.getAttribute(WebSessionManager.KEY);
			if(session == null) {
				JavaxServletContext context = new JavaxServletContext(RWT.getRequest().getServletContext());
				session = new WebSession(new JavaxHttpSession(httpSession, context), HttpUtils.getRealRemoteAddr(new JavaxHttpServletRequest(RWT.getRequest(), context)));
				httpSession.setAttribute(WebSessionManager.KEY, session);
			}
			
			return session;
		}else {
			return null;
		}
	}
	
	@Override
	public Locale getLocale() {
		WebSession webSession = getWebSession();
		if(webSession != null) {
			return webSession.getLocale();
		}
		
		if(locale == null) {
			try {
				locale =  RWT.getLocale();
			}catch(Exception e) {
				locale = Locale.getDefault();
			}
		}
		
		return locale;
	}

	@Override
	public void setLocale(Locale locale) {
		WebSession webSession = getWebSession();
		if(webSession != null) {
			webSession.setLocale(locale);
		}
		
		this.locale = locale;
		try {
			RWT.setLocale(locale);
		}catch(Exception ignored) {
		}
	}

	@Override
	public Serializable getId() {
		WebSession webSession = getWebSession();
		if(webSession != null) {
			return webSession.getId();
		}else {
			return 0;
		}
	}

	@Override
	public String getHost() {
		WebSession webSession = getWebSession();
		if(webSession != null) {
			return webSession.getHost();
		}else {
			return null;
		}
	}

	@Override
	public void touch() {
		WebSession webSession = getWebSession();
		if(webSession != null) {
			webSession.touch();
		}else {
			touch();
		}
	}

	@Override
	public void stop() {
		WebSession webSession = getWebSession();
		if(webSession != null) {
			webSession.stop();
		}else {
			stop();
		}
	}

	@Override
	public Collection getAttributeKeys() {
		WebSession webSession = getWebSession();
		if(webSession != null) {
			return webSession.getAttributeKeys();
		}else {
			return attributes.keySet();
		}
	}

	@Override
	public  S getSource() {
		WebSession webSession = getWebSession();
		if(webSession != null) {
			return webSession.getSource();
		}else {
			return null;
		}
	}

	public Shell getRootShell() {
		return rootShell;
	}
	
	/**
	 * 是一个FillLayout的Composite,在XWorkerEntryPoint中设置。
	 * @return
	 */
	public Composite getRootComposite() {
		return rootComposite;
	}

	public void setRootComposite(Composite rootComposite) {
		this.rootComposite = rootComposite;
	}

	public void setRootShell(Shell rootShell) {
		this.rootShell = rootShell;
	}
	
	
}