<commit_before>package io.wovn.wovnjava;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Tag;

import us.codecraft.xsoup.Xsoup;

class Interceptor {
    private Store store;

    Interceptor(FilterConfig config) {
        store = new Store(config);
    }

    void call(HttpServletRequest request, ServletResponse response, FilterChain chain) {

        if (!store.settings.isValid()) {
            try {
                chain.doFilter(request, response);
            } catch (ServletException e) {
            } catch (IOException e) {
            }
            return;
        }

        Headers h = new Headers(request, store.settings);
        if (store.settings.testMode && !store.settings.testUrl.equals(h.url)) {
            try {
                chain.doFilter(request, response);
            } catch (ServletException e) {
            } catch (IOException e) {
            }
        }

        if (h.getPathLang().equals(store.settings.defaultLang)) {
            try {
                ((HttpServletResponse) response).sendRedirect(
                        h.redirectLocation(store.settings.defaultLang)
                );
            } catch (IOException e) {
            }
            return;
        }

        WovnHttpServletResponse wovnResponse = new WovnHttpServletResponse(
                (HttpServletResponse)response, h
        );

        try {
            chain.doFilter(request, wovnResponse);
        } catch (ServletException e) {
        } catch (IOException e) {
        }

        String body = wovnResponse.toString();
        if (Pattern.compile("html").matcher(response.getContentType()).find()) {
            Values values = store.getValues(h.pageUrl);
            if (!String.valueOf(wovnResponse.status).matches("^1|302")) {
                String lang = h.langCode();
                HashMap<String,String> url = new HashMap<String,String>();
                url.put("protocol", h.protocol);
                url.put("host", h.host);
                url.put("pathname", h.pathName);

                body = this.switchLang(body, values, url, lang, h);
                wovnResponse.setContentLength(body.getBytes().length);
            }
        }

        try {
            PrintWriter out = response.getWriter();
            out.write(body);
            out.close();
        } catch (IOException e) {
        }

        h.out(request, wovnResponse);
    }

    private String addLangCode(String href, String pattern, String lang, Headers headers) {<commit_msg>There is a possibility that response.getContentType() is null.<commit_after>package io.wovn.wovnjava;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Tag;

import us.codecraft.xsoup.Xsoup;

class Interceptor {
    private Store store;

    Interceptor(FilterConfig config) {
        store = new Store(config);
    }

    void call(HttpServletRequest request, ServletResponse response, FilterChain chain) {

        if (!store.settings.isValid()) {
            try {
                chain.doFilter(request, response);
            } catch (ServletException e) {
            } catch (IOException e) {
            }
            return;
        }

        Headers h = new Headers(request, store.settings);
        if (store.settings.testMode && !store.settings.testUrl.equals(h.url)) {
            try {
                chain.doFilter(request, response);
            } catch (ServletException e) {
            } catch (IOException e) {
            }
        }

        if (h.getPathLang().equals(store.settings.defaultLang)) {
            try {
                ((HttpServletResponse) response).sendRedirect(
                        h.redirectLocation(store.settings.defaultLang)
                );
            } catch (IOException e) {
            }
            return;
        }

        WovnHttpServletResponse wovnResponse = new WovnHttpServletResponse(
                (HttpServletResponse)response, h
        );

        try {
            chain.doFilter(request, wovnResponse);
        } catch (ServletException e) {
        } catch (IOException e) {
        }

        String body = wovnResponse.toString();
        // There is a possibility that response.getContentType() is null when the response is an image.
        if (response.getContentType() != null && Pattern.compile("html").matcher(response.getContentType()).find()) {
            Values values = store.getValues(h.pageUrl);
            if (!String.valueOf(wovnResponse.status).matches("^1|302")) {
                String lang = h.langCode();
                HashMap<String,String> url = new HashMap<String,String>();
                url.put("protocol", h.protocol);
                url.put("host", h.host);
                url.put("pathname", h.pathName);

                body = this.switchLang(body, values, url, lang, h);
                wovnResponse.setContentLength(body.getBytes().length);
            }
        }

        try {
            PrintWriter out = response.getWriter();
            out.write(body);
            out.close();
        } catch (IOException e) {
        }

        h.out(request, wovnResponse);
    }

    private String addLangCode(String href, String pattern, String lang, Headers headers) {
{'fraction_non_alphanumeric': 0.10908797937479858, 'fraction_numerical': 0.0012890750886239124, 'mean_word_length': 2.1348484848484848, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '7051646', 'n_tokens_mistral': 1689, 'n_tokens_neox': 1642, 'n_words': 343}