<commit_before>package org.bimserver.servlets;

/******************************************************************************
 * Copyright (C) 2009-2017  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.IOException;
import java.io.InputStream;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.bimserver.BimServer;
import org.bimserver.plugins.web.WebModulePlugin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RootServlet extends HttpServlet {

	private static final long serialVersionUID = -6631574771887074019L;

	private static final Logger LOGGER = LoggerFactory.getLogger(RootServlet.class);
	private WebServiceServlet11 soap11Servlet;
	private WebServiceServlet12 soap12Servlet;
	private SyndicationServlet syndicationServlet;
	private JsonApiServlet jsonApiServlet;
	private UploadServlet uploadServlet;
	private DownloadServlet downloadServlet;
	private OAuthAuthorizationServlet oAuthAuthorizationServlet;
	private OAuthRegistrationServlet oAuthRegistrationServlet;
	private OAuthAccessTokenServlet oAuthAccesssTokenServlet;
	private ServiceRunnerServlet serviceRunner;
	private BulkUploadServlet bulkUploadServlet;

	private BimServer bimServer;

	@Override
	public void init() throws ServletException {
		super.init();
		ServletContext servletContext = getServletContext();
		bimServer = (BimServer) servletContext.getAttribute("bimserver");
		if (bimServer == null) {
			throw new ServletException("No bimserver context attribute");
		}
		jsonApiServlet = new JsonApiServlet(bimServer, servletContext);
		syndicationServlet = new SyndicationServlet(bimServer, servletContext);
		uploadServlet = new UploadServlet(bimServer, servletContext);
		bulkUploadServlet = new BulkUploadServlet(bimServer, servletContext);
		downloadServlet = new DownloadServlet(bimServer, servletContext);
		soap11Servlet = new WebServiceServlet11(bimServer, servletContext);
		soap11Servlet.init(getServletConfig());
		soap12Servlet = new WebServiceServlet12(bimServer, servletContext);
		soap12Servlet.init(getServletConfig());
		oAuthAuthorizationServlet = new OAuthAuthorizationServlet(bimServer, servletContext);
		oAuthAccesssTokenServlet = new OAuthAccessTokenServlet(bimServer, servletContext);
		oAuthRegistrationServlet = new OAuthRegistrationServlet(bimServer, servletContext);
		serviceRunner = new ServiceRunnerServlet(bimServer, servletContext);
	}

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String requestOrigin = request.getHeader("Origin");
			if (requestOrigin != null && !bimServer.getServerSettingsCache().isHostAllowed(requestOrigin)) {
				response.setStatus(403);
				return;
			}
			if (requestOrigin != null) {
				response.setHeader("Access-Control-Allow-Origin", requestOrigin);
			} else {
				response.setHeader("Access-Control-Allow-Origin", "*");
			}
			response.setHeader("Access-Control-Allow-Headers", "Content-Type, Input-Type, Token");

			if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
				return;
			}

			String requestUri = request.getRequestURI();
			String servletContextPath = getServletContext().getContextPath();
			if (requestUri.startsWith(servletContextPath)) {
				requestUri = requestUri.substring(servletContextPath.length());
			}
			if (requestUri == null) {
				LOGGER.error("RequestURI is null");
			} else {
				LOGGER.debug(requestUri);
//				LOGGER.info(requestUri);
			}
			setContentType(response, requestUri);
			if (request.getRequestURI().endsWith(".getbimserveraddress")) {
				response.setContentType("application/json; charset=utf-8");
				String siteAddress = bimServer.getServerSettingsCache().getServerSettings().getSiteAddress();<commit_msg>Added expose headers<commit_after>package org.bimserver.servlets;

/******************************************************************************
 * Copyright (C) 2009-2017  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.IOException;
import java.io.InputStream;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.bimserver.BimServer;
import org.bimserver.plugins.web.WebModulePlugin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RootServlet extends HttpServlet {

	private static final long serialVersionUID = -6631574771887074019L;

	private static final Logger LOGGER = LoggerFactory.getLogger(RootServlet.class);
	private WebServiceServlet11 soap11Servlet;
	private WebServiceServlet12 soap12Servlet;
	private SyndicationServlet syndicationServlet;
	private JsonApiServlet jsonApiServlet;
	private UploadServlet uploadServlet;
	private DownloadServlet downloadServlet;
	private OAuthAuthorizationServlet oAuthAuthorizationServlet;
	private OAuthRegistrationServlet oAuthRegistrationServlet;
	private OAuthAccessTokenServlet oAuthAccesssTokenServlet;
	private ServiceRunnerServlet serviceRunner;
	private BulkUploadServlet bulkUploadServlet;

	private BimServer bimServer;

	@Override
	public void init() throws ServletException {
		super.init();
		ServletContext servletContext = getServletContext();
		bimServer = (BimServer) servletContext.getAttribute("bimserver");
		if (bimServer == null) {
			throw new ServletException("No bimserver context attribute");
		}
		jsonApiServlet = new JsonApiServlet(bimServer, servletContext);
		syndicationServlet = new SyndicationServlet(bimServer, servletContext);
		uploadServlet = new UploadServlet(bimServer, servletContext);
		bulkUploadServlet = new BulkUploadServlet(bimServer, servletContext);
		downloadServlet = new DownloadServlet(bimServer, servletContext);
		soap11Servlet = new WebServiceServlet11(bimServer, servletContext);
		soap11Servlet.init(getServletConfig());
		soap12Servlet = new WebServiceServlet12(bimServer, servletContext);
		soap12Servlet.init(getServletConfig());
		oAuthAuthorizationServlet = new OAuthAuthorizationServlet(bimServer, servletContext);
		oAuthAccesssTokenServlet = new OAuthAccessTokenServlet(bimServer, servletContext);
		oAuthRegistrationServlet = new OAuthRegistrationServlet(bimServer, servletContext);
		serviceRunner = new ServiceRunnerServlet(bimServer, servletContext);
	}

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String requestOrigin = request.getHeader("Origin");
			if (requestOrigin != null && !bimServer.getServerSettingsCache().isHostAllowed(requestOrigin)) {
				response.setStatus(403);
				return;
			}
			if (requestOrigin != null) {
				response.setHeader("Access-Control-Allow-Origin", requestOrigin);
			} else {
				response.setHeader("Access-Control-Allow-Origin", "*");
			}
			response.setHeader("Access-Control-Allow-Headers", "Content-Type, Input-Type, Token");
			response.setHeader("Access-Control-Expose-Headers", "Content-Length, Data-Title, Output-Type");
			
			if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
				return;
			}

			String requestUri = request.getRequestURI();
			String servletContextPath = getServletContext().getContextPath();
			if (requestUri.startsWith(servletContextPath)) {
				requestUri = requestUri.substring(servletContextPath.length());
			}
			if (requestUri == null) {
				LOGGER.error("RequestURI is null");
			} else {
				LOGGER.debug(requestUri);
//				LOGGER.info(requestUri);
			}
			setContentType(response, requestUri);
			if (request.getRequestURI().endsWith(".getbimserveraddress")) {
				response.setContentType("application/json; charset=utf-8");
				String siteAddress = bimServer.getServerSettingsCache().getServerSettings().getSiteAddress();
{'fraction_non_alphanumeric': 0.11858332454938336, 'fraction_numerical': 0.011383999156740803, 'mean_word_length': 6.0385756676557865, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2763273', 'n_tokens_mistral': 2751, 'n_tokens_neox': 2496, 'n_words': 635}