/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.ocelotds.context;

import java.security.Principal;
import java.util.Locale;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.websocket.Session;
import org.ocelotds.Constants;
import org.ocelotds.annotations.OcelotLogger;
import org.slf4j.Logger;

/**
 *
 * @author hhfrancois
 */
public class OcelotContext {

	@Inject
	@OcelotLogger
	private Logger logger;

	@Produces
	Session getSession() {
		return (Session) ThreadLocalContextHolder.get(Constants.SESSION);
	}

	public Locale getLocale() {
		Session session = getSession();
		Locale locale;
		if (session == null) {
			locale = Locale.US;
		} else {
			locale = (Locale) session.getUserProperties().get(Constants.LOCALE);
			if (null == locale) {
				locale = new Locale("en", "US");
				logger.debug("Get locale from OcelotServices : default : {}", locale);
			} else {
				logger.debug("Get locale from OcelotServices : {}", locale);
			}
		}
		return locale;
	}

	public void setLocale(Locale locale) {
		Session session = getSession();
		session.getUserProperties().put(Constants.LOCALE, locale);
	}

	public String getUsername() {
		Session session = getSession();
		Principal p = session.getUserPrincipal();
		if (null != p) {
			return p.getName();
		}
		return Constants.ANONYMOUS;
	}
}

@@ -1421,16 +1421,42 @@
 ion();%0D%0A
+%09%09if(null != session) %7B%0D%0A%09
 %09%09Princi
@@ -1490,24 +1490,25 @@
 cipal();%0D%0A%09%09
+%09
 if (null != 
@@ -1513,16 +1513,17 @@
 = p) %7B%0D%0A
+%09
 %09%09%09retur
@@ -1538,16 +1538,22 @@
 ame();%0D%0A
+%09%09%09%7D%0D%0A
 %09%09%7D%0D%0A%09%09r

fix NPE