<commit_before>package org.bimserver.plugins.web;

/******************************************************************************
 * Copyright (C) 2009-2019  BIMserver.org
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * 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 {@literal<http://www.gnu.org/licenses/>}.
 *****************************************************************************/

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.bimserver.models.store.ObjectDefinition;
import org.bimserver.models.store.ParameterDefinition;
import org.bimserver.models.store.PrimitiveDefinition;
import org.bimserver.models.store.PrimitiveEnum;
import org.bimserver.models.store.StoreFactory;
import org.bimserver.models.store.StringType;
import org.bimserver.plugins.PluginConfiguration;
import org.bimserver.plugins.PluginContext;
import org.bimserver.plugins.PluginSourceType;
import org.bimserver.shared.exceptions.PluginException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.base.Charsets;

public abstract class AbstractWebModulePlugin implements WebModulePlugin {
	private static final Logger LOGGER = LoggerFactory.getLogger(AbstractWebModulePlugin.class);
	private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
	private PluginContext pluginContext;
	private static final DateFormat HTTP_EXPIRES = expiresDateFormat();
	public static final String FAR_FUTURE_EXPIRE_DATE = HTTP_EXPIRES.format(makeExpiresDate());

	public static DateFormat expiresDateFormat() {
		DateFormat httpDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
		httpDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
		return httpDateFormat;
	}
	
	@Override
	public void init(PluginContext pluginContext, PluginConfiguration systemSettings) throws PluginException {
		this.pluginContext = pluginContext;
	}
	
	private static Date makeExpiresDate() {
		GregorianCalendar gregorianCalendar = new GregorianCalendar();
		gregorianCalendar.add(Calendar.DAY_OF_YEAR, 120);
		return gregorianCalendar.getTime();
	}
	
	@Override
	public boolean service(String requestUri, HttpServletResponse response) {
		try {
			if (requestUri.startsWith(getDefaultContextPath())) {
				requestUri = requestUri.substring(getDefaultContextPath().length());
			}
			while (requestUri.startsWith("/")) {
				requestUri = requestUri.substring(1);
			}
			if (requestUri.equals("")) {
				requestUri = "index.html";
			}
			if (requestUri.endsWith("plugin.version")) {
				ObjectNode version = OBJECT_MAPPER.createObjectNode();
				version.put("groupId", pluginContext.getPluginBundle().getPluginBundleVersion().getGroupId());
				version.put("artifactId", pluginContext.getPluginBundle().getPluginBundleVersion().getArtifactId());
				version.put("version", pluginContext.getPluginBundle().getPluginBundleVersion().getVersion());
				version.put("description", pluginContext.getPluginBundle().getPluginBundleVersion().getDescription());
				version.put("icon", pluginContext.getPluginBundle().getPluginBundleVersion().getIcon());
				version.put("name", pluginContext.getPluginBundle().getPluginBundleVersion().getName());
				version.put("organization", pluginContext.getPluginBundle().getPluginBundleVersion().getOrganization());
				
				if (getPluginContext().getPluginType() == PluginSourceType.INTERNAL) {
					// Probably the default plugin
					return false;
				} else if (getPluginContext().getPluginType() == PluginSourceType.ECLIPSE_PROJECT) {
					// We don't want to cache in Eclipse, because we change files without changing the plugin version everytime
					version.put("nonce", System.nanoTime());
					response.setContentType("application/json");
					response.getOutputStream().write(version.toString().getBytes(Charsets.UTF_8));
					return true;
				} else if (getPluginContext().getPluginType() == PluginSourceType.JAR_FILE) {
					version.put("nonce", pluginContext.getPluginBundle().getPluginBundleVersion().getVersion());
					response.setContentType("application/json");
					response.getOutputStream().write(version.toString().getBytes(Charsets.UTF_8));
					return true;
				}
			}
			if (!requestUri.equals("index.html")) {
				response.setHeader("Expires", FAR_FUTURE_EXPIRE_DATE);
			}<commit_msg>Disabled far future expire date because of ES6 modules not loaded with ?_v=[version] argument<commit_after>package org.bimserver.plugins.web;

/******************************************************************************
 * Copyright (C) 2009-2019  BIMserver.org
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * 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 {@literal<http://www.gnu.org/licenses/>}.
 *****************************************************************************/

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.bimserver.models.store.ObjectDefinition;
import org.bimserver.models.store.ParameterDefinition;
import org.bimserver.models.store.PrimitiveDefinition;
import org.bimserver.models.store.PrimitiveEnum;
import org.bimserver.models.store.StoreFactory;
import org.bimserver.models.store.StringType;
import org.bimserver.plugins.PluginConfiguration;
import org.bimserver.plugins.PluginContext;
import org.bimserver.plugins.PluginSourceType;
import org.bimserver.shared.exceptions.PluginException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.base.Charsets;

public abstract class AbstractWebModulePlugin implements WebModulePlugin {
	private static final Logger LOGGER = LoggerFactory.getLogger(AbstractWebModulePlugin.class);
	private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
	private PluginContext pluginContext;
	private static final DateFormat HTTP_EXPIRES = expiresDateFormat();
	public static final String FAR_FUTURE_EXPIRE_DATE = HTTP_EXPIRES.format(makeExpiresDate());

	public static DateFormat expiresDateFormat() {
		DateFormat httpDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
		httpDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
		return httpDateFormat;
	}
	
	@Override
	public void init(PluginContext pluginContext, PluginConfiguration systemSettings) throws PluginException {
		this.pluginContext = pluginContext;
	}
	
	private static Date makeExpiresDate() {
		GregorianCalendar gregorianCalendar = new GregorianCalendar();
		gregorianCalendar.add(Calendar.DAY_OF_YEAR, 120);
		return gregorianCalendar.getTime();
	}
	
	@Override
	public boolean service(String requestUri, HttpServletResponse response) {
		try {
			if (requestUri.startsWith(getDefaultContextPath())) {
				requestUri = requestUri.substring(getDefaultContextPath().length());
			}
			while (requestUri.startsWith("/")) {
				requestUri = requestUri.substring(1);
			}
			if (requestUri.equals("")) {
				requestUri = "index.html";
			}
			if (requestUri.endsWith("plugin.version")) {
				ObjectNode version = OBJECT_MAPPER.createObjectNode();
				version.put("groupId", pluginContext.getPluginBundle().getPluginBundleVersion().getGroupId());
				version.put("artifactId", pluginContext.getPluginBundle().getPluginBundleVersion().getArtifactId());
				version.put("version", pluginContext.getPluginBundle().getPluginBundleVersion().getVersion());
				version.put("description", pluginContext.getPluginBundle().getPluginBundleVersion().getDescription());
				version.put("icon", pluginContext.getPluginBundle().getPluginBundleVersion().getIcon());
				version.put("name", pluginContext.getPluginBundle().getPluginBundleVersion().getName());
				version.put("organization", pluginContext.getPluginBundle().getPluginBundleVersion().getOrganization());
				
				if (getPluginContext().getPluginType() == PluginSourceType.INTERNAL) {
					// Probably the default plugin
					return false;
				} else if (getPluginContext().getPluginType() == PluginSourceType.ECLIPSE_PROJECT) {
					// We don't want to cache in Eclipse, because we change files without changing the plugin version everytime
					version.put("nonce", System.nanoTime());
					response.setContentType("application/json");
					response.getOutputStream().write(version.toString().getBytes(Charsets.UTF_8));
					return true;
				} else if (getPluginContext().getPluginType() == PluginSourceType.JAR_FILE) {
					version.put("nonce", pluginContext.getPluginBundle().getPluginBundleVersion().getVersion());
					response.setContentType("application/json");
					response.getOutputStream().write(version.toString().getBytes(Charsets.UTF_8));
					return true;
				}
			}
			if (!requestUri.equals("index.html")) {
				// Disabled for now since ES6 modules can't be loaded with a ?_v=[version] addition, causing all sorts of problems
//				response.setHeader("Expires", FAR_FUTURE_EXPIRE_DATE);
			}
{'fraction_non_alphanumeric': 0.13270492560761482, 'fraction_numerical': 0.0033268644302744664, 'mean_word_length': 5.667898952556993, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, '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': '15509', 'n_tokens_mistral': 3244, 'n_tokens_neox': 2886, 'n_words': 689}