/* 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.objects;

import java.security.Principal;
import javax.websocket.server.HandshakeRequest;
import org.ocelotds.Constants;
import org.ocelotds.security.UserContext;

/**
 *
 * @author hhfrancois
 */
public class WsUserContext implements UserContext {

	private final Principal ANONYMOUS = new Principal() {
		@Override
		public String getName() {
			return Constants.ANONYMOUS;
		}
	};

	Principal principal;
	HandshakeRequest handshakeRequest;

	public WsUserContext(HandshakeRequest handshakeRequest) {
		this.handshakeRequest = handshakeRequest;
		if (null != handshakeRequest) {
			this.principal = handshakeRequest.getUserPrincipal();
		} else {
			this.principal = ANONYMOUS;
		}
	}

	@Override
	public Principal getPrincipal() {
		return principal;
	}

	@Override
	public boolean isUserInRole(String role) {
		if (null != handshakeRequest) {
			return handshakeRequest.isUserInRole(role);
		}
		return false;
	}

}

@@ -329,25 +329,36 @@
 celotds.
-Constants
+security.UserContext
 ;%0Aimport
@@ -371,36 +371,34 @@
 celotds.
-security.UserContext
+web.PrincipalTools
 ;%0A%0A/**%0A 
@@ -482,143 +482,8 @@
  %7B%0A%0A
-%09private final Principal ANONYMOUS = new Principal() %7B%0A%09%09@Override%0A%09%09public String getName() %7B%0A%09%09%09return Constants.ANONYMOUS;%0A%09%09%7D%0A%09%7D;%0A%0A
 %09Pri
@@ -498,16 +498,23 @@
 rincipal
+ = null
 ;%0A%09Hands
@@ -542,16 +542,48 @@
 Request;
+%0A%09PrincipalTools principalTools;
 %0A%0A%09publi
@@ -685,84 +685,88 @@
 ;%0A%09%09
-if (null != handshakeRequest) %7B%0A%09%09%09this.principal = handshakeRequest.getUser
+this.principalTools = new PrincipalTools();%0A%09%7D%0A%0A%09@Override%0A%09public Principal get
 Prin
@@ -776,88 +776,62 @@
 al()
-;%0A%09%09%7D else %7B%0A%09%09%09this.principal = ANONYMOUS;%0A%09%09%7D%0A%09%7D%0A%0A%09@Override%0A%09public P
+ %7B%0A%09%09if (null == principal) %7B%0A%09%09%09principal = p
 rincipal
  get
@@ -818,33 +818,38 @@
 ipal = principal
- 
+Tools.
 getPrincipal() %7B
@@ -845,19 +845,38 @@
 incipal(
-) %7B
+handshakeRequest);%0A%09%09%7D
 %0A%09%09retur
@@ -1051,11 +1051,10 @@
 lse;%0A%09%7D%0A
-%0A
 %7D%0A

use principalTools