<commit_before>/**
 * Axelor Business Solutions
 *
 * Copyright (C) 2005-2016 Axelor (<http://axelor.com>).
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.axelor.web.tags;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import com.axelor.app.AppSettings;

public abstract class AbstractTag extends SimpleTagSupport {

	private String src;

	private boolean production = AppSettings.get().isProduction();

	public String getSrc() {
		return src;
	}

	public void setSrc(String src) {
		this.src = src;
	}

	private boolean exists(String path) {
		try {
			return getResource(path) != null;
		} catch (MalformedURLException e) {
			return false;
		}
	}

	protected URL getResource(String path) throws MalformedURLException {
		final PageContext ctx = (PageContext) getJspContext();
		return ctx.getServletContext().getResource(path);
	}

	protected List<String> getScripts() throws IOException {
		return Arrays.asList(src);
	}

	protected abstract void doTag(String src) throws IOException;

	private boolean gzipSupported() {
		final PageContext context = (PageContext) getJspContext();
		final HttpServletRequest req = (HttpServletRequest) context.getRequest();
		final String encodings = req.getHeader("Accept-Encoding");
		return encodings != null && encodings.toLowerCase().contains("gzip");
	}

	@Override
	public void doTag() throws JspException, IOException {

		if (production) {
			final String gzipped = src.replaceFirst("\\.(js|css)$", ".gzip.$1");
			if (exists(gzipped) && gzipSupported()) {
				doTag(gzipped);
				return;
			}
			final String minified = src.replaceFirst("\\.(js|css)$", ".min.$1");
			if (exists(minified)) {
				doTag(minified);
				return;<commit_msg>Fix jetty issue<commit_after>/**
 * Axelor Business Solutions
 *
 * Copyright (C) 2005-2016 Axelor (<http://axelor.com>).
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.axelor.web.tags;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import com.axelor.app.AppSettings;

public abstract class AbstractTag extends SimpleTagSupport {

	private String src;

	private boolean production = AppSettings.get().isProduction();

	public String getSrc() {
		return src;
	}

	public void setSrc(String src) {
		this.src = src;
	}

	private boolean exists(String path) {
		try {
			return getResource(path) != null;
		} catch (MalformedURLException e) {
			return false;
		}
	}

	protected URL getResource(String path) throws MalformedURLException {
		final String resource = path.startsWith("/") ? path : "/" + path;
		final PageContext ctx = (PageContext) getJspContext();
		return ctx.getServletContext().getResource(resource);
	}

	protected List<String> getScripts() throws IOException {
		return Arrays.asList(src);
	}

	protected abstract void doTag(String src) throws IOException;

	private boolean gzipSupported() {
		final PageContext context = (PageContext) getJspContext();
		final HttpServletRequest req = (HttpServletRequest) context.getRequest();
		final String encodings = req.getHeader("Accept-Encoding");
		return encodings != null && encodings.toLowerCase().contains("gzip");
	}

	@Override
	public void doTag() throws JspException, IOException {

		if (production) {
			final String gzipped = src.replaceFirst("\\.(js|css)$", ".gzip.$1");
			if (exists(gzipped) && gzipSupported()) {
				doTag(gzipped);
				return;
			}
			final String minified = src.replaceFirst("\\.(js|css)$", ".min.$1");
			if (exists(minified)) {
				doTag(minified);
				return;
{'fraction_non_alphanumeric': 0.1092469761997659, 'fraction_numerical': 0.004291845493562232, 'mean_word_length': 4.3573667711598745, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6200974', 'n_tokens_mistral': 1595, 'n_tokens_neox': 1475, 'n_words': 477}