<commit_before>package org.innovateuk.ifs.filter;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.security.crypto.encrypt.TextEncryptor;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.OrRequestMatcher;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.GenericFilterBean;
import org.springframework.web.util.WebUtils;

import javax.annotation.PostConstruct;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static java.util.Arrays.asList;

/**
 * Since the default Spring flash messages use sessions and we cannot use session because of the stateless design,
 * we need to use cookies. This class reads the cookies, and if there is a cookie then a new request variable is set to
 * true. The request variable's name is equals to the value of the cookie. After adding the value to the request variables,
 * the cookie is removed (set to "").
 *
 * ATM this is only used for setting a flashMessage on redirection.
 */
@Service
@Configurable
public class CookieFlashMessageFilter extends GenericFilterBean {
    public static final String COOKIE_NAME = "flashMessage";
    private static final Integer COOKIE_LIFETIME = 60;

    private static final OrRequestMatcher IGNORED_REQUESTS = new OrRequestMatcher(asList(
        new AntPathRequestMatcher("/css/**"),
        new AntPathRequestMatcher("/js/**"),
        new AntPathRequestMatcher("/images/**"),
        new AntPathRequestMatcher("/monitoring/**"),
        new AntPathRequestMatcher("/error"),
        new AntPathRequestMatcher("/"),<commit_msg>INFUND-9200 INFUND-9200 Parameterize the monitoring endpoint URI for the CookieFlahMessageFilter<commit_after>package org.innovateuk.ifs.filter;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.security.crypto.encrypt.TextEncryptor;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.OrRequestMatcher;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.GenericFilterBean;
import org.springframework.web.util.WebUtils;

import javax.annotation.PostConstruct;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static java.util.Arrays.asList;

/**
 * Since the default Spring flash messages use sessions and we cannot use session because of the stateless design,
 * we need to use cookies. This class reads the cookies, and if there is a cookie then a new request variable is set to
 * true. The request variable's name is equals to the value of the cookie. After adding the value to the request variables,
 * the cookie is removed (set to "").
 *
 * ATM this is only used for setting a flashMessage on redirection.
 */
@Service
@Configurable
public class CookieFlashMessageFilter extends GenericFilterBean {

    public static final String COOKIE_NAME = "flashMessage";
    private static final Integer COOKIE_LIFETIME = 60;

    private static String monitoringEndpoint;

    @Value("${management.contextPath}")
    public static void setMonitoringEndpoint(String monitoringEndpoint) {
        CookieFlashMessageFilter.monitoringEndpoint = monitoringEndpoint;
    }

    private static OrRequestMatcher IGNORED_REQUESTS = new OrRequestMatcher(asList(
        new AntPathRequestMatcher("/css/**"),
        new AntPathRequestMatcher("/js/**"),
        new AntPathRequestMatcher("/images/**"),
        new AntPathRequestMatcher(monitoringEndpoint+"/**"),
        new AntPathRequestMatcher("/error"),
        new AntPathRequestMatcher("/"),
{'fraction_non_alphanumeric': 0.08444148936170212, 'fraction_numerical': 0.0026595744680851063, 'mean_word_length': 5.233425414364641, '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': '22990', 'n_tokens_mistral': 1149, 'n_tokens_neox': 1131, 'n_words': 343}