<commit_before>package com.github.onsdigital.babbage.util;

import com.github.onsdigital.babbage.locale.LocaleConfig;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by bren on 10/08/15.
 */
public class RequestUtil {

    /**
     * Saves Authentication token and collection id to thread context if available when a request is made to babbage,
     * this ensures all data requests from content service can be authorized both  when data is requested for any purpose on Babbage ( rendering page, sending data back etc. )
     */
    public static void saveRequestContext(HttpServletRequest request) {
        ThreadContext.addData("cookies", getAllCookies(request));
        ThreadContext.addData("parameters", request.getParameterMap());
        Locale locale = resolveLocale(request);
        ThreadContext.addData("labels", LocaleConfig.getLabels(locale));
        ThreadContext.addData("lang", locale.getLanguage());
    }

    public static void clearContext() {
        ThreadContext.clear();
    }

    public static String getCookieValue(HttpServletRequest request, String name) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null) {
            return null;
        }
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(name)) {
                return cookie.getValue();
            }
        }
        return null;
    }

    public static Map<String, String> getAllCookies(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        Map<String, String> cookiesMap = new HashMap<>();
        if (cookies == null) {
            return cookiesMap;
        }
        for (Cookie cookie : cookies) {
            cookiesMap.put(cookie.getName(), cookie.getValue());
        }
        return cookiesMap;
    }

    /**
     *
     * Resolves locale using sub domain name ( e.g. cy.ons.gov.uk )
     *
     * @param request
     * @return language code
     */
    private static Locale resolveLocale(HttpServletRequest request) {
        Enumeration<Locale> locales = request.getLocales();
        String serverName = request.getServerName();
        Collection<Locale> supportedLanguages = LocaleConfig.getSupportedLanguages();
        for (Locale supportedLanguage : supportedLanguages) {
            if (serverName.startsWith(supportedLanguage.getLanguage() + ".")) {
                return supportedLanguage;
            }
        }
        return LocaleConfig.getDefaultLocale();
    }


    /**
     * Extracts GET parameters from query string
     * <p>
     * This method matches parameters to query string, if parameters is in query string it is return in the list of parameters.
     * <p>
     * Note that a post parameters with the same name might also be included. There should not be parameters with same names in both get and post parameters if not wanted to be extracted
     */
    public static Map<String, String[]> getQueryParameters(HttpServletRequest request) {
        Map<String, String[]> queryParameters = new HashMap<>();
        String queryString = request.getQueryString();

        if (StringUtils.isEmpty(queryString)) {
            return queryParameters;
        }

        String[] parameters = queryString.split("&");

        for (String parameter : parameters) {
            String[] keyValuePair = parameter.split("=");
            String[] values = queryParameters.get(keyValuePair[0]);
            values = ArrayUtils.add(values, keyValuePair.length == 1 ? "" : keyValuePair[1]); //length is one if no value is available.
            queryParameters.put(keyValuePair[0], values);
        }
        return queryParameters;
    }

}
<commit_msg>Added language decision based on cookie prior to subdomain<commit_after>package com.github.onsdigital.babbage.util;

import com.github.onsdigital.babbage.locale.LocaleConfig;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by bren on 10/08/15.
 */
public class RequestUtil {

    /**
     * Saves Authentication token and collection id to thread context if available when a request is made to babbage,
     * this ensures all data requests from content service can be authorized both  when data is requested for any purpose on Babbage ( rendering page, sending data back etc. )
     */
    public static void saveRequestContext(HttpServletRequest request) {
        ThreadContext.addData("cookies", getAllCookies(request));
        ThreadContext.addData("parameters", request.getParameterMap());
        Locale locale = resolveLocale(request);
        ThreadContext.addData("labels", LocaleConfig.getLabels(locale));
        ThreadContext.addData("lang", locale.getLanguage());
    }

    public static void clearContext() {
        ThreadContext.clear();
    }

    public static String getCookieValue(HttpServletRequest request, String name) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null) {
            return null;
        }
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(name)) {
                return cookie.getValue();
            }
        }
        return null;
    }

    public static Map<String, String> getAllCookies(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        Map<String, String> cookiesMap = new HashMap<>();
        if (cookies == null) {
            return cookiesMap;
        }
        for (Cookie cookie : cookies) {
            cookiesMap.put(cookie.getName(), cookie.getValue());
        }
        return cookiesMap;
    }

    /**
     *
     * Resolves locale using cookie, if cookie not available falls back to sub domain name ( e.g. cy.ons.gov.uk )
     *
     * @param request
     * @return language code
     */
    private static Locale resolveLocale(HttpServletRequest request) {

        //Decide language from cookie first, if not there check subdomain
        String languageSegment = getCookieValue(request, "lang");
        if (StringUtils.isEmpty(languageSegment)) {
            languageSegment = request.getServerName();
        } else {
            languageSegment += ".";
        }

        Collection<Locale> supportedLanguages = LocaleConfig.getSupportedLanguages();
        for (Locale supportedLanguage : supportedLanguages) {
            if (StringUtils.startsWithIgnoreCase(languageSegment,supportedLanguage.getLanguage() + ".")) {
                return supportedLanguage;
            }
        }
        return LocaleConfig.getDefaultLocale();
    }


    /**
     * Extracts GET parameters from query string
     * <p>
     * This method matches parameters to query string, if parameters is in query string it is return in the list of parameters.
     * <p>
     * Note that a post parameters with the same name might also be included. There should not be parameters with same names in both get and post parameters if not wanted to be extracted
     */
    public static Map<String, String[]> getQueryParameters(HttpServletRequest request) {
        Map<String, String[]> queryParameters = new HashMap<>();
        String queryString = request.getQueryString();

        if (StringUtils.isEmpty(queryString)) {
            return queryParameters;
        }

        String[] parameters = queryString.split("&");

        for (String parameter : parameters) {
            String[] keyValuePair = parameter.split("=");
            String[] values = queryParameters.get(keyValuePair[0]);
            values = ArrayUtils.add(values, keyValuePair.length == 1 ? "" : keyValuePair[1]); //length is one if no value is available.
            queryParameters.put(keyValuePair[0], values);
        }
        return queryParameters;
    }

}

{'fraction_non_alphanumeric': 0.0968267904176596, 'fraction_numerical': 0.0030101592875956353, 'mean_word_length': 2.6780442804428044, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, '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': '138407', 'n_tokens_mistral': 2011, 'n_tokens_neox': 1973, 'n_words': 604}