/* 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 javax.websocket.server.HandshakeRequest;
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();
		if(session!=null) {
			session.getUserProperties().remove(Constants.LOCALE);
			if (locale != null) {
				session.getUserProperties().put(Constants.LOCALE, locale);
			}
		}
	}

	HandshakeRequest getHandshakeRequest() {
		return (HandshakeRequest) ThreadLocalContextHolder.get(Constants.HANDSHAKEREQUEST);
	}

	public boolean isUserInRole(String role) {
		HandshakeRequest hr = getHandshakeRequest();
		if(hr!=null) {
			return hr.isUserInRole(role);
		}
		logger.warn("Fail to get userInRole, HandshakeRequest is null in threadLocal");
		return false;
	}

	public Principal getPrincipal() {
		Session session = getSession();
		if (null != session) {
			Principal p = session.getUserPrincipal();
			if (null != p) {
				return p;
			}
		}
		return ANONYMOUS;
	}
	
	private static final Principal ANONYMOUS = new Principal() {
		@Override
		public String getName() {
			return Constants.ANONYMOUS;
		}
	};
}

@@ -293,16 +293,39 @@
 ocale;%0D%0A
+import java.util.Map;%0D%0A
 import j
@@ -836,24 +836,54 @@
 Locale() %7B%0D%0A
+%09%09Locale locale = Locale.US;%0D%0A
 %09%09Session se
@@ -909,26 +909,8 @@
 );%0D%0A
-%09%09Locale locale;%0D%0A
 %09%09if
@@ -919,17 +919,17 @@
 session 
-=
+!
 = null) 
@@ -938,41 +938,75 @@
 %0A%09%09%09
-locale = Locale.US;%0D%0A%09%09%7D else
+if (session.getUserProperties().containsKey(Constants.LOCALE))
  %7B%0D%0A
+%09
 %09%09%09l
@@ -1078,72 +1078,8 @@
 );%0D%0A
-%09%09%09if (null == locale) %7B%0D%0A%09%09%09%09locale = new Locale(%22en%22, %22US%22);%0D%0A
 %09%09%09%09
@@ -1128,18 +1128,8 @@
 es :
- default :
  %7B%7D%22
@@ -1195,32 +1195,42 @@
 OcelotServices :
+ default :
  %7B%7D%22, locale);%0D%0A
@@ -1366,16 +1366,46 @@
 ) %7B%0D%0A%09%09%09
+Map%3CString,Object%3E userProps =
 session.
@@ -1423,16 +1423,31 @@
 erties()
+;%0D%0A%09%09%09userProps
 .remove(
@@ -1500,35 +1500,17 @@
 %09%09%09%09
-session.getUserProperties()
+userProps
 .put

improve algo setLocale