<commit_before>package org.collectionspace.chain.controller;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

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

import org.apache.commons.io.input.TeeInputStream;
import org.collectionspace.chain.csp.config.ConfigRoot;
import org.collectionspace.chain.csp.persistence.services.connection.ConnectionException;
import org.collectionspace.chain.csp.schema.Instance;
import org.collectionspace.chain.csp.schema.Record;
import org.collectionspace.chain.csp.schema.Spec;
import org.collectionspace.chain.csp.webui.external.UIMapping;
import org.collectionspace.chain.csp.webui.main.WebUI;
import org.collectionspace.csp.api.ui.UI;

public class TenantUIServlet extends TenantServlet {
	private static final Logger log = LoggerFactory
			.getLogger(TenantUIServlet.class);

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected void serviceUIWTenant(String tenant, HttpServletRequest servlet_request, HttpServletResponse servlet_response) throws BadRequestException,ConnectionException,DocumentException, IOException{

		String pathinfo = servlet_request.getPathInfo();
		String[] pathbits = pathinfo.substring(1).split("/");
		String urlredirect = "/collectionspace/ui/"+tenant+"/index.html";
		
		if (pathinfo.equals("/html") || pathinfo.equals("/html/")) {
			servlet_response.sendRedirect(urlredirect);
		}
		else if (pathinfo.equals("/"+tenant) || pathinfo.equals("/"+tenant+"/")) {
			servlet_response.sendRedirect(urlredirect);
		}

		ServletContext sc = null;
		sc = getServletContext().getContext("/cspace-ui");
		if(sc==null){
			servlet_response.sendError(HttpServletResponse.SC_BAD_REQUEST,"missing servlet context cspace-ui");
		}

		if(pathbits[0].equals("css") || pathbits[0].equals("js") || pathbits[0].equals("lib") || pathbits[0].equals("bundle") || pathbits[0].equals("images") || pathbits[0].equals("config")){
			String tenantposs = getTenantByCookie(servlet_request);
			if (serverFixedExternalContent(servlet_request, servlet_response,
					sc, pathinfo, tenantposs)) {
				return;
			}
		}

		if(!tenantInit.containsKey(tenant) || !tenantInit.get(tenant))
			setup(tenant);

		// should we redirect this url or just do the normal stuff

		// Setup our request object
		UI web = tenantCSPM.get(tenant).getUI("web");<commit_msg>nojira - improve redirect<commit_after>package org.collectionspace.chain.controller;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

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

import org.apache.commons.io.input.TeeInputStream;
import org.collectionspace.chain.csp.config.ConfigRoot;
import org.collectionspace.chain.csp.persistence.services.connection.ConnectionException;
import org.collectionspace.chain.csp.schema.Instance;
import org.collectionspace.chain.csp.schema.Record;
import org.collectionspace.chain.csp.schema.Spec;
import org.collectionspace.chain.csp.webui.external.UIMapping;
import org.collectionspace.chain.csp.webui.main.WebUI;
import org.collectionspace.csp.api.ui.UI;

public class TenantUIServlet extends TenantServlet {
	private static final Logger log = LoggerFactory
			.getLogger(TenantUIServlet.class);

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected void serviceUIWTenant(String tenant, HttpServletRequest servlet_request, HttpServletResponse servlet_response) throws BadRequestException,ConnectionException,DocumentException, IOException{

		String pathinfo = servlet_request.getPathInfo();
		String[] pathbits = pathinfo.substring(1).split("/");
		String urlredirect = "/collectionspace/ui/"+tenant+"/index.html";
		
		if (pathinfo.equals("/html") || pathinfo.equals("/html/")) {
			servlet_response.sendRedirect(urlredirect);
		}
		else if (pathinfo.equals("/"+tenant) || pathinfo.equals("/"+tenant+"/html/")) {
			servlet_response.sendRedirect(urlredirect);
		}
		else if (pathinfo.equals("/"+tenant+"/html") || pathinfo.equals("/"+tenant+"/html/")) {
			servlet_response.sendRedirect(urlredirect);
		}

		ServletContext sc = null;
		sc = getServletContext().getContext("/cspace-ui");
		if(sc==null){
			servlet_response.sendError(HttpServletResponse.SC_BAD_REQUEST,"missing servlet context cspace-ui");
		}

		if(pathbits[0].equals("css") || pathbits[0].equals("js") || pathbits[0].equals("lib") || pathbits[0].equals("bundle") || pathbits[0].equals("images") || pathbits[0].equals("config")){
			String tenantposs = getTenantByCookie(servlet_request);
			if (serverFixedExternalContent(servlet_request, servlet_response,
					sc, pathinfo, tenantposs)) {
				return;
			}
		}

		if(!tenantInit.containsKey(tenant) || !tenantInit.get(tenant))
			setup(tenant);

		// should we redirect this url or just do the normal stuff

		// Setup our request object
		UI web = tenantCSPM.get(tenant).getUI("web");
{'fraction_non_alphanumeric': 0.12930746068919372, 'fraction_numerical': 0.005352960856473737, 'mean_word_length': 5.802047781569966, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '2565577', 'n_tokens_mistral': 1931, 'n_tokens_neox': 1870, 'n_words': 318}