<commit_before>package uk.ac.ebi.pride.web.util.frontier;

import org.springframework.core.io.Resource;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * @author  <EMAIL>
 * Date: 19/11/12
 *
 * UPDATE: jadianes - added 'exceptions'
 */
public class CustomizedFrontierTemplateFilter extends FrontierTemplateFilter {

    private List<String> exceptions = new LinkedList();

    private Resource jsonConfigAuthenticated;

    public void setJsonConfigAuthenticated(Resource jsonConfigAuthenticated) {
        this.jsonConfigAuthenticated = jsonConfigAuthenticated;
    }

    //will overwrite it to do not inject header/footer to all pages
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        if (request instanceof HttpServletRequest) {
            // backup original JSON config
            Resource originalJsonConfig = this.getJsonConfig();

            String url = ((HttpServletRequest)request).getRequestURL().toString();

            // choose filter based on authentication state
            if (this.jsonConfigAuthenticated != null) {
                Authentication auth = SecurityContextHolder.getContext().getAuthentication();
                if (auth != null && !(auth instanceof AnonymousAuthenticationToken)) {
                    // userDetails = auth.getPrincipal()
                    //only do injection if does not contain exceptions
                    this.setJsonConfig(this.jsonConfigAuthenticated);
                }
            }

            //only do injection if does not contain exceptions
            if (matchesExceptions(url))
                chain.doFilter(request, response);
            else
                super.doFilter(request, response, chain);

            // restore JSON config
            this.setJsonConfig(originalJsonConfig);
        }
    }

    public List getExceptions() {
        return exceptions;
    }

    public void setExceptions(List exceptions) {
        this.exceptions = exceptions;
    }

    private boolean matchesExceptions(String url) {

        boolean matches = false;

        Iterator<String> it = exceptions.iterator();

        while (!matches && it.hasNext()) {
            String exception = it.next();
            // ToDo: perhaps allow for wild cards and whole directories to be recognised as exceptions
            matches = url.contains(exception);
        }
<commit_msg>fixed a bug frontier template filter<commit_after>package uk.ac.ebi.pride.web.util.frontier;

import org.springframework.core.io.Resource;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * @author <EMAIL>
 *         Date: 19/11/12
 *         <p/>
 *         UPDATE: jadianes - added 'exceptions'
 */
public class CustomizedFrontierTemplateFilter extends FrontierTemplateFilter {

    private List<String> exceptions = new LinkedList<String>();

    private Resource jsonConfigAuthenticated;

    public void setJsonConfigAuthenticated(Resource jsonConfigAuthenticated) {
        this.jsonConfigAuthenticated = jsonConfigAuthenticated;
    }

    //will overwrite it to do not inject header/footer to all pages
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        if (request instanceof HttpServletRequest) {
            // backup original JSON config
            Resource originalJsonConfig = this.getJsonConfig();

            try {
                String url = ((HttpServletRequest) request).getRequestURL().toString();

                // choose filter based on authentication state
                if (this.jsonConfigAuthenticated != null) {
                    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
                    if (auth != null && !(auth instanceof AnonymousAuthenticationToken)) {
                        // userDetails = auth.getPrincipal()
                        //only do injection if does not contain exceptions
                        this.setJsonConfig(this.jsonConfigAuthenticated);
                    }
                }

                //only do injection if does not contain exceptions
                if (matchesExceptions(url)) {
                    chain.doFilter(request, response);
                } else {
                    super.doFilter(request, response, chain);
                }
            } finally {
                // restore JSON config
                this.setJsonConfig(originalJsonConfig);
            }
        }
    }

    public List getExceptions() {
        return exceptions;
    }

    public void setExceptions(List<String> exceptions) {
        this.exceptions = exceptions;
    }

    private boolean matchesExceptions(String url) {

        boolean matches = false;

        Iterator<String> it = exceptions.iterator();

        while (!matches && it.hasNext()) {
            String exception = it.next();
            // ToDo: perhaps allow for wild cards and whole directories to be recognised as exceptions
            matches = url.contains(exception);
        }

{'fraction_non_alphanumeric': 0.08103647466578644, 'fraction_numerical': 0.001980524839082357, 'mean_word_length': 2.57944477259303, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, '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': '6545161', 'n_tokens_mistral': 1452, 'n_tokens_neox': 1375, 'n_words': 377}