<commit_before>package cc.topicexplorer.web;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

import java.nio.charset.Charset;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;

import java.util.ArrayList;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

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

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.apache.log4j.Logger;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import cc.commandmanager.core.Context;
import cc.topicexplorer.database.Database;
import cc.topicexplorer.utils.PropertiesUtil;


/**
 * Servlet implementation class TestServlet
 */
public class JsonServlet extends HttpServlet {

	private static final Logger logger = Logger.getLogger(JsonServlet.class);
	
	private static final long serialVersionUID = 1L;
	
	private File tempPath = new File(getServletContext().getRealPath("/") + "WEB-INF" + File.separator +"temp");
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
	           throws ServletException, java.io.IOException {
		resp.setCharacterEncoding("UTF8");

		PrintWriter writer = resp.getWriter();
		
		if (req.getParameter("Command").equals("generateCSV")) {
			logger.info("bla");
			if (req.getParameterMap().containsKey("wordList")) {
				logger.info("bli");
				String wordListJSON = req.getParameter("wordList");
				JSONArray wordList = (JSONArray) JSONSerializer.toJSON(wordListJSON);
				FileOutputStream wordtypesOut = new FileOutputStream(new File(tempPath + "/wordtype.local.properties"));
				if(wordList.size() > 0) {
					JSONObject wordtype = (JSONObject) JSONSerializer.toJSON(wordList.get(0));
					wordtypesOut.write(new String("wordtypes=" + wordtype.getString("id")).getBytes()); 
					for(int i = 1 ; i < wordList.size(); i++) {
						wordtype = (JSONObject) JSONSerializer.toJSON(wordList.get(i));
						wordtypesOut.write(new String("," + wordtype.getInt("id")).getBytes()); 
					}
				}
				wordtypesOut.close();
				
				FileOutputStream jsonOut = new FileOutputStream(new File(tempPath + "/wordlist.json"));
				
				jsonOut.write(wordListJSON.getBytes(Charset.forName("UTF-8")));
				
				jsonOut.close();
				logger.info("word list: " + wordListJSON);
				
				writer.write("1");
			} else {
				logger.error("no word types specified");
				writer.write("0");
			}
		}
	}

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String command = request.getParameter("Command");

		response.setCharacterEncoding("UTF8");

		PrintWriter writer = response.getWriter();

		Context context = new Context(WebChainManagement.getContext());
		context.bind("SERVLET_WRITER", writer);
		
		Database database = (Database) context.get("database");
		
		try {
			if (command.equals("init")) {
				
				if(!tempPath.exists()) {
					tempPath.mkdir();
				}
				// save db properties
				Properties dbProps = PropertiesUtil.loadMandatoryProperties("database", "");
				
				FileOutputStream out = new FileOutputStream(new File(tempPath + "/database.local.properties"));
				dbProps.store(out, null);
				out.close();
				
				<commit_msg>#212 bugfix: setting the path needs to be in doget or dopost environment<commit_after>package cc.topicexplorer.web;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

import java.nio.charset.Charset;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;

import java.util.ArrayList;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

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

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.apache.log4j.Logger;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import cc.commandmanager.core.Context;
import cc.topicexplorer.database.Database;
import cc.topicexplorer.utils.PropertiesUtil;


/**
 * Servlet implementation class TestServlet
 */
public class JsonServlet extends HttpServlet {

	private static final Logger logger = Logger.getLogger(JsonServlet.class);
	
	private static final long serialVersionUID = 1L;
	
	private File tempPath;
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
	           throws ServletException, java.io.IOException {
		tempPath = new File(getServletContext().getRealPath("/") + "WEB-INF" + File.separator +"temp");
		
		resp.setCharacterEncoding("UTF8");

		PrintWriter writer = resp.getWriter();
		
		if (req.getParameter("Command").equals("generateCSV")) {
			logger.info("bla");
			if (req.getParameterMap().containsKey("wordList")) {
				logger.info("bli");
				String wordListJSON = req.getParameter("wordList");
				JSONArray wordList = (JSONArray) JSONSerializer.toJSON(wordListJSON);
				FileOutputStream wordtypesOut = new FileOutputStream(new File(tempPath + "/wordtype.local.properties"));
				if(wordList.size() > 0) {
					JSONObject wordtype = (JSONObject) JSONSerializer.toJSON(wordList.get(0));
					wordtypesOut.write(new String("wordtypes=" + wordtype.getString("id")).getBytes()); 
					for(int i = 1 ; i < wordList.size(); i++) {
						wordtype = (JSONObject) JSONSerializer.toJSON(wordList.get(i));
						wordtypesOut.write(new String("," + wordtype.getInt("id")).getBytes()); 
					}
				}
				wordtypesOut.close();
				
				FileOutputStream jsonOut = new FileOutputStream(new File(tempPath + "/wordlist.json"));
				
				jsonOut.write(wordListJSON.getBytes(Charset.forName("UTF-8")));
				
				jsonOut.close();
				logger.info("word list: " + wordListJSON);
				
				writer.write("1");
			} else {
				logger.error("no word types specified");
				writer.write("0");
			}
		}
	}

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		tempPath = new File(getServletContext().getRealPath("/") + "WEB-INF" + File.separator +"temp");
		
		String command = request.getParameter("Command");

		response.setCharacterEncoding("UTF8");

		PrintWriter writer = response.getWriter();

		Context context = new Context(WebChainManagement.getContext());
		context.bind("SERVLET_WRITER", writer);
		
		Database database = (Database) context.get("database");
		
		try {
			if (command.equals("init")) {
				
				if(!tempPath.exists()) {
					tempPath.mkdir();
				}
				// save db properties
				Properties dbProps = PropertiesUtil.loadMandatoryProperties("database", "");
				
				FileOutputStream out = new FileOutputStream(new File(tempPath + "/database.local.properties"));
				dbProps.store(out, null);
				out.close();
				
				
{'fraction_non_alphanumeric': 0.1266205160478288, 'fraction_numerical': 0.0033983637507866583, 'mean_word_length': 4.639460610361959, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, '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': '1090646', 'n_tokens_mistral': 2665, 'n_tokens_neox': 2307, 'n_words': 459}