<commit_before>/**
 * Copyright (C) 2015 Agro-Know, Deutsches Forschungszentrum für Künstliche Intelligenz, iMinds,
 * Institut für Angewandte Informatik e. V. an der Universität Leipzig,
 * Istit<NAME>, Tilde, Vistatec, WRIPL (http://freme-project.eu)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package eu.freme.bservices.filters.internationalizationfilter;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import eu.freme.bservices.internationalization.api.InternationalizationAPI;
import eu.freme.common.conversion.SerializationFormatMapper;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.ReaderInputStream;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import eu.freme.common.exception.BadRequestException;
import eu.freme.common.exception.InternalServerErrorException;
import eu.freme.common.exception.ExceptionHandlerService;
import eu.freme.bservices.internationalization.okapi.nif.converter.ConversionException;
import org.springframework.web.filter.GenericFilterBean;

/**
 * Filter that converts HTML and XLIFF input to NIF and changes the informat
 * parameter. It also performs roundtripping, e.g. to convert HTML to NIF and
 * then back to HTML.
 *
 * @author <NAME> - <EMAIL>
 */

@Component
public class InternationalizationFilter extends GenericFilterBean {

	/**
	 * Disable e-Internationalization filter for a set of endpoints with the
	 * configuration option freme.einternationalization.endpoint-blacklist. You
	 * can configure a comma separated list of regular expressions. For example
	 * the regular expression like /toolbox/.* will match the endpoint
	 * http://localhost:8080/toolbox/test
	 */
	@Value("#{'${freme.einternationalization.endpoint-blacklist:/toolbox/convert/.*}'.split(',')}")
	Set<String> endpointBlacklist;

	Set<Pattern> endpointBlacklistRegex;

	@Autowired
	ExceptionHandlerService exceptionHandlerService;

	@Autowired
	SerializationFormatMapper serializationFormatMapper;

	/*
	 * EInternationalization accepts these formats for roundtripping
	 */
	private HashSet<String> roundtrippingFormats;

	private Logger logger = Logger.getLogger(InternationalizationFilter.class);

	@Autowired
	InternationalizationAPI internationalizationApi;

	public InternationalizationFilter() {
		roundtrippingFormats = new HashSet<>();
		roundtrippingFormats.add(InternationalizationAPI.MIME_TYPE_HTML
				.toLowerCase());
		roundtrippingFormats.add(InternationalizationAPI.MIME_TYPE_XLIFF_1_2
				.toLowerCase());
	}

	@PostConstruct
	public void doInit() {
		endpointBlacklistRegex = new HashSet<Pattern>();
		for (String str : endpointBlacklist) {<commit_msg>[internalisation-filter] added xml to roundtripping formats https://github.com/freme-project/basic-services/issues/49<commit_after>/**
 * Copyright (C) 2015 Agro-Know, Deutsches Forschungszentrum für Künstliche Intelligenz, iMinds,
 * Institut für Angewandte Informatik e. V. an der Universität Leipzig,
 * Istituto Superiore <NAME>, Tilde, Vistatec, WRIPL (http://freme-project.eu)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package eu.freme.bservices.filters.internationalizationfilter;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import eu.freme.bservices.internationalization.api.InternationalizationAPI;
import eu.freme.common.conversion.SerializationFormatMapper;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.ReaderInputStream;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import eu.freme.common.exception.BadRequestException;
import eu.freme.common.exception.InternalServerErrorException;
import eu.freme.common.exception.ExceptionHandlerService;
import eu.freme.bservices.internationalization.okapi.nif.converter.ConversionException;
import org.springframework.web.filter.GenericFilterBean;

/**
 * Filter that converts HTML and XLIFF input to NIF and changes the informat
 * parameter. It also performs roundtripping, e.g. to convert HTML to NIF and
 * then back to HTML.
 *
 * @author <NAME> - <EMAIL>
 */

@Component
public class InternationalizationFilter extends GenericFilterBean {

	/**
	 * Disable e-Internationalization filter for a set of endpoints with the
	 * configuration option freme.einternationalization.endpoint-blacklist. You
	 * can configure a comma separated list of regular expressions. For example
	 * the regular expression like /toolbox/.* will match the endpoint
	 * http://localhost:8080/toolbox/test
	 */
	@Value("#{'${freme.einternationalization.endpoint-blacklist:/toolbox/convert/.*}'.split(',')}")
	Set<String> endpointBlacklist;

	Set<Pattern> endpointBlacklistRegex;

	@Autowired
	ExceptionHandlerService exceptionHandlerService;

	@Autowired
	SerializationFormatMapper serializationFormatMapper;

	/*
	 * EInternationalization accepts these formats for roundtripping
	 */
	private HashSet<String> roundtrippingFormats;

	private Logger logger = Logger.getLogger(InternationalizationFilter.class);

	@Autowired
	InternationalizationAPI internationalizationApi;

	public InternationalizationFilter() {
		roundtrippingFormats = new HashSet<>();
		roundtrippingFormats.add(InternationalizationAPI.MIME_TYPE_HTML
				.toLowerCase());
		roundtrippingFormats.add(InternationalizationAPI.MIME_TYPE_XLIFF_1_2
				.toLowerCase());
		roundtrippingFormats.add(InternationalizationAPI.MIME_TYPE_XML
				.toLowerCase());
	}

	@PostConstruct
	public void doInit() {
		endpointBlacklistRegex = new HashSet<Pattern>();
		for (String str : endpointBlacklist) {
{'fraction_non_alphanumeric': 0.08942379645796956, 'fraction_numerical': 0.003991020204539786, 'mean_word_length': 5.594572368421052, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, 'xml': 1}, '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': '3147492', 'n_tokens_mistral': 2259, 'n_tokens_neox': 2188, 'n_words': 590}