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

import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;
import javax.ws.rs.core.HttpHeaders;
import org.ocelotds.Constants;
import org.ocelotds.core.CDIBeanResolver;
import org.ocelotds.security.SecurityContext;
import org.ocelotds.security.SecurityServices;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This class extract info in request, and expose them to userProperties ServerEnpoint
 *
 * @author hhfrancois
 */
public class OcelotRequestConfigurator extends ServerEndpointConfig.Configurator {

	private final Logger logger = LoggerFactory.getLogger(OcelotRequestConfigurator.class);
	
//	private CDIBeanResolver beanResolver = new CDIBeanResolver();

	/**
	 * Set user information from open websocket
	 * 
	 * @param sec
	 * @param request
	 * @param response 
	 */
	@Override
	public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
		Map<String, List<String>> headers = request.getHeaders();
		Locale locale = new Locale("en", "US");
		List<String> accepts = headers.get(HttpHeaders.ACCEPT_LANGUAGE);
		logger.debug("Get accept-language from client headers : {}", accepts);
		if (null != accepts) {
			Iterator<String> iterator = accepts.iterator();
			while (iterator.hasNext()) {
				String accept = iterator.next();
				Pattern pattern = Pattern.compile(".*(\\w\\w)-(\\w\\w).*");
				Matcher matcher = pattern.matcher(accept);
				if (matcher.matches() && matcher.groupCount() == 2) {
					locale = new Locale(matcher.group(1), matcher.group(2));
					break;
				}
			}
		}
		// init from request only on openHandler
		sec.getUserProperties().put(Constants.SECURITY_CONTEXT, getSecurityContext());
		sec.getUserProperties().put(Constants.LOCALE, locale);
		sec.getUserProperties().put(Constants.PRINCIPAL, request.getUserPrincipal());
		super.modifyHandshake(sec, request, response);
	}

	/**
	 * Get subject from container implementation
	 * 
	 * @return 
	 */
	SecurityContext getSecurityContext() {
		CDIBeanResolver beanResolver = new CDIBeanResolver();
		SecurityServices subjectServices = beanResolver.getBean(SecurityServices.class); // getSubjectServices();
		// get SecurityContext from container implementation
		return (null != subjectServices) ? subjectServices.getSecurityContext(): null;
	}
}

@@ -240,16 +240,43 @@
 ion;%0D%0A%0D%0A
+import java.util.HashMap;%0D%0A
 import j
@@ -434,16 +434,56 @@
 ttern;%0D%0A
+import javax.servlet.http.HttpSession;%0D%0A
 import j
@@ -1196,76 +1196,8 @@
 %0A%09%0D%0A
-//%09private CDIBeanResolver beanResolver = new CDIBeanResolver();%0D%0A%0D%0A
 %09/**
@@ -1243,25 +1243,24 @@
 bsocket%0D%0A%09 *
- 
 %0D%0A%09 * @param
@@ -1304,17 +1304,16 @@
 response
- 
 %0D%0A%09 */%0D%0A
@@ -2103,48 +2103,549 @@
 %0D%0A%09%09
-// init from request only on openHandler
+Object session = request.getHttpSession();%0D%0A%09%09Object attribute;%0D%0A%09%09if (session != null && HttpSession.class.isInstance(session)) %7B%0D%0A%09%09%09HttpSession httpSession = (HttpSession) session;%0D%0A%09%09%09attribute = httpSession.getAttribute(Constants.SESSION_BEANS);%0D%0A%09%09%09if(attribute==null) %7B%0D%0A%09%09%09%09attribute = new HashMap();%0D%0A%09%09%09%09httpSession.setAttribute(Constants.SESSION_BEANS, attribute);%0D%0A%09%09%09%7D%0D%0A%09%09%7D else %7B%0D%0A%09%09%09attribute = new HashMap();%0D%0A%09%09%7D%0D%0A%09%09// init from request only on openHandler%0D%0A%09%09sec.getUserProperties().put(Constants.SESSION_BEANS, attribute);
 %0D%0A%09%09
@@ -2975,17 +2975,16 @@
 ion%0D%0A%09 *
- 
 %0D%0A%09 * @r
@@ -2988,17 +2988,16 @@
  @return
- 
 %0D%0A%09 */%0D%0A
@@ -3332,16 +3332,17 @@
 ontext()
+ 
 : null;%0D

use HttpSession for store beansessions