<commit_before>package io.yawp.servlet;

import io.yawp.driver.api.DriverFactory;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletResponse;

public class CrossDomainManager {

    private static final String ENABLE_CROSS_DOMAIN_PARAM = "enableCrossDomain";

    private static final String CROSS_DOMAIN_ORIGINS_PARAM = "crossDomainOrigins";

    private static final String CROSS_DOMAIN_METHODS_PARAM = "crossDomainMethods";

    private static final String CROSS_DOMAIN_HEADERS_PARAM = "crossDomainHeaders";

    public static final String DEFAULT_ORIGINS = "*";

    public static final String DEFAULT_METHODS = "GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD";

    public static final String DEFAULT_HEADERS = "Origin, X-Requested-With, Content-Type, Accept, Authorization";

    private boolean enableCrossDomain;

    private String origins;

    private String methods;

    private String headers;

    public void init(ServletConfig config) {
        this.enableCrossDomain = isCrossDomainEnabled(config);

        if (enableCrossDomain) {
            setOrigins(getOrigins(config));
            setMethods(getMethods(config));
            setHeaders(getHeaders(config));
        }
    }

    public void setResponseHeaders(HttpServletResponse response) {
        if (enableCrossDomain) {
            response.setHeader("Access-Control-Allow-Origin", origins);
            response.setHeader("Access-Control-Allow-Headers", headers);
            response.setHeader("Access-Control-Allow-Methods", methods);
        }
    }

    private String getOrigins(ServletConfig config) {
        return defaultTo(config.getInitParameter(CROSS_DOMAIN_ORIGINS_PARAM), DEFAULT_ORIGINS);
    }

    private String getMethods(ServletConfig config) {
        return defaultTo(config.getInitParameter(CROSS_DOMAIN_METHODS_PARAM), DEFAULT_METHODS);
    }

    private String getHeaders(ServletConfig config) {
        return defaultTo(config.getInitParameter(CROSS_DOMAIN_HEADERS_PARAM), DEFAULT_HEADERS);
    }

    private void setOrigins(String origins) {
        this.origins = origins;
    }

    private void setHeaders(String headers) {
        this.headers = headers;
    }

    private void setMethods(String methods) {
        this.methods = methods;
    }

    private String defaultTo(String customValue, String defaultValue) {
        return StringUtils.isEmpty(customValue) ? defaultValue : customValue;
    }

    private boolean isCrossDomainEnabled(ServletConfig config) {
        if (config.getInitParameter(ENABLE_CROSS_DOMAIN_PARAM) != null) {
            return Boolean.valueOf(config.getInitParameter(ENABLE_CROSS_DOMAIN_PARAM));
        } else {
            return !DriverFactory.getDriver().environment().isProduction();
        }
    }
}<commit_msg>CORS management merge<commit_after>package io.yawp.servlet;

import io.yawp.driver.api.DriverFactory;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletResponse;

public class CrossDomainManager {

    private static final String ENABLE_CROSS_DOMAIN_PARAM = "enableCrossDomain";

    private static final String CROSS_DOMAIN_ORIGIN_PARAM = "crossDomainOrigins";

    private static final String CROSS_DOMAIN_METHODS_PARAM = "crossDomainMethods";

    private static final String CROSS_DOMAIN_HEADERS_PARAM = "crossDomainHeaders";

    public static final String DEFAULT_ORIGIN = "*";

    public static final String DEFAULT_METHODS = "GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD";

    public static final String DEFAULT_HEADERS = "Origin, X-Requested-With, Content-Type, Accept, Authorization";

    private boolean enableCrossDomain;

    private String origin;

    private String methods;

    private String headers;

    public void init(ServletConfig config) {
        this.enableCrossDomain = isCrossDomainEnabled(config);

        if (enableCrossDomain) {
            if (hasAnyValueSet(config)) {
                setOrigin(getOrigin(config));
                setMethods(getMethods(config));
                setHeaders(getHeaders(config));
            } else {
                setOrigin(DEFAULT_ORIGIN);
                setMethods(DEFAULT_METHODS);
                setHeaders(DEFAULT_HEADERS);
            }
        }
    }

    public void setResponseHeaders(HttpServletResponse response) {
        if (enableCrossDomain) {
            if (!StringUtils.isEmpty(origin)) {
                response.setHeader("Access-Control-Allow-Origin", origin);
            }

            if (!StringUtils.isEmpty(methods)) {
                response.setHeader("Access-Control-Allow-Methods", methods);
            }

            if (!StringUtils.isEmpty(headers)) {
                response.setHeader("Access-Control-Allow-Headers", headers);
            }
        }
    }

    private String getOrigin(ServletConfig config) {
        return config.getInitParameter(CROSS_DOMAIN_ORIGIN_PARAM);
    }

    private String getMethods(ServletConfig config) {
        return config.getInitParameter(CROSS_DOMAIN_METHODS_PARAM);
    }

    private String getHeaders(ServletConfig config) {
        return config.getInitParameter(CROSS_DOMAIN_HEADERS_PARAM);
    }

    private void setOrigin(String origin) {
        this.origin = origin;
    }

    private void setMethods(String methods) {
        this.methods = methods;
    }

    private void setHeaders(String headers) {
        this.headers = headers;
    }

    private boolean isCrossDomainEnabled(ServletConfig config) {
        if (config.getInitParameter(ENABLE_CROSS_DOMAIN_PARAM) != null) {
            return Boolean.valueOf(config.getInitParameter(ENABLE_CROSS_DOMAIN_PARAM));
        } else {
            return !DriverFactory.getDriver().environment().isProduction();
        }
    }

    public boolean hasAnyValueSet(ServletConfig config) {
        return (config.getInitParameter(CROSS_DOMAIN_ORIGIN_PARAM) != null
                || config.getInitParameter(CROSS_DOMAIN_METHODS_PARAM) != null
                || config.getInitParameter(CROSS_DOMAIN_HEADERS_PARAM) != null);
    }
}
{'fraction_non_alphanumeric': 0.08410606800065072, 'fraction_numerical': 0.00032536196518626975, 'mean_word_length': 3.1794697484704284, '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': '677749', 'n_tokens_mistral': 1699, 'n_tokens_neox': 1617, 'n_words': 332}