<commit_before>package org.sagebionetworks.web.server.servlet;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.sagebionetworks.repo.model.UserProfile;
import org.sagebionetworks.repo.model.UserSessionData;
import org.sagebionetworks.schema.adapter.org.json.EntityFactory;
import org.sagebionetworks.web.client.cookie.CookieKeys;
import org.sagebionetworks.web.shared.WebConstants;
import org.springframework.http.HttpHeaders;

/**
 * Helper to get the UserData from the thread local cookie.
 * 
 * @author jmhill
 *
 */
public class UserDataProvider {
	
	static private Logger logger = LogManager.getLogger(UserDataProvider.class);
	
	/**
	 * The key used to put the session token in a header.
	 */
	
	public static final String SESSION_TOKEN_KEY = "sessionToken";
	/**
	 * Get the user data from the Cookies of the ThreadLocalRequest.
	 * Will return null if the cookie does not exist. 
	 * @return
	 */
	public static String getThreadLocalUserToken(HttpServletRequest threadLocalRequest) {
		if (threadLocalRequest == null)	return null;
		Cookie[] cookies = threadLocalRequest.getCookies();
		if (cookies != null) {
			// Find the cookie
			for (Cookie cookie : cookies) {
				if (CookieKeys.USER_LOGIN_TOKEN.equals(cookie.getName())) {
					String value = cookie.getValue();
					if (value == null)
						return null;
					try {
						return URLDecoder.decode(value, "UTF-8");
					} catch (UnsupportedEncodingException e) {
						throw new IllegalArgumentException(e);
					}

				}
			}
		}
		logger.info("Cannot find user login data in the cookies using cookie.name="	+ CookieKeys.USER_LOGIN_TOKEN);
		return null;
	}
	
	/**
	 * Add the user data to the header if it exists.
	 * @param threadLocalRequest
	 * @param headers
	 */<commit_msg>extra logging if login token is missing from cookies<commit_after>package org.sagebionetworks.web.server.servlet;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.sagebionetworks.web.client.cookie.CookieKeys;
import org.springframework.http.HttpHeaders;

/**
 * Helper to get the UserData from the thread local cookie.
 * 
 * @author jmhill
 *
 */
public class UserDataProvider {
	
	static private Logger logger = LogManager.getLogger(UserDataProvider.class);
	
	/**
	 * The key used to put the session token in a header.
	 */
	
	public static final String SESSION_TOKEN_KEY = "sessionToken";
	/**
	 * Get the user data from the Cookies of the ThreadLocalRequest.
	 * Will return null if the cookie does not exist. 
	 * @return
	 */
	public static String getThreadLocalUserToken(HttpServletRequest threadLocalRequest) {
		if (threadLocalRequest == null)	return null;
		Cookie[] cookies = threadLocalRequest.getCookies();
		if (cookies != null) {
			// Find the cookie
			for (Cookie cookie : cookies) {
				if (CookieKeys.USER_LOGIN_TOKEN.equals(cookie.getName())) {
					String value = cookie.getValue();
					if (value == null)
						return null;
					try {
						return URLDecoder.decode(value, "UTF-8");
					} catch (UnsupportedEncodingException e) {
						throw new IllegalArgumentException(e);
					}

				}
			}
		}
		logger.info("Cannot find user login data in the cookies using cookie.name="	+ CookieKeys.USER_LOGIN_TOKEN, new Exception("Missing " + CookieKeys.USER_LOGIN_TOKEN));
		return null;
	}
	
	/**
	 * Add the user data to the header if it exists.
	 * @param threadLocalRequest
	 * @param headers
	 */
{'fraction_non_alphanumeric': 0.09840848806366048, 'fraction_numerical': 0.0015915119363395225, 'mean_word_length': 3.929411764705882, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6722681', 'n_tokens_mistral': 1241, 'n_tokens_neox': 1102, 'n_words': 309}