<commit_before>/*
 * version 1.0 - MUSES prototype software
 * Copyright MUSES project (European Commission FP7) - 2013 
 * 
 */
package eu.musesproject.server.connectionmanager;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Queue;

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

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
	
	/**
	 * Class ComMainServlet
	 * 
	 * @author <NAME>
	 * @version Jan 27, 2014
	 */

public class ComMainServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(ComMainServlet.class.getName());
	private Helper helper;
	private SessionHandler sessionHandler;
	private ConnectionManager connectionManager;
	private String dataAttachedInCurrentReuqest;
	private String dataToSendBackInResponse="";
	private static final String CONNECTION_TYPE = "connection-type";
	private static final String DATA = "data";
	private static final int INTERVAL_TO_WAIT = 5;
	private static final long SLEEP_INTERVAL = 1000;
	private static final String MUSES_TAG = "MUSES_TAG";
	private static final String MUSES_TAG_LEVEL_2 = "MUSES_TAG_LEVEL_2";
	private static String connectionType = "connect";
	
	/**
	 * 
	 * @param sessionHandler
	 * @param helper
	 * @param communicationManager
	 */
	
	public ComMainServlet(SessionHandler sessionHandler, Helper helper, ConnectionManager communicationManager){
		this.sessionHandler=sessionHandler;
		this.helper=helper;
		this.connectionManager=communicationManager;
	}
	

	public ComMainServlet() {
	}
	
	/**
	 * Initialize servlet
	 * 
	 * @throws Servlet exception
	 */
	@Override
	public void init() throws ServletException {
		super.init();
		logger.log(Level.INFO, MUSES_TAG + " init");
		helper = new Helper();
		connectionManager = ConnectionManager.getInstance();
		sessionHandler = SessionHandler.getInstance(getServletContext());
	}
	
	/**
	 * Handle POST http/https requests
	 * @param HttpServletRequest request 
	 * @param com.swedenconnectivity.comserverHttpServletResponse response
	 * @throws ServletException, IOException
	 */
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// create cookie if not in the request
		helper.setCookie(request);
		Cookie cookie = helper.getCookie();
		String currentJSessionID = cookie.getValue();
		
		// Retrieve data in the request
		if (request.getMethod().equalsIgnoreCase("POST")) {
			// Retrieve connection-type from request header
			connectionType = request.getHeader(CONNECTION_TYPE);
			dataAttachedInCurrentReuqest = helper.getRequestData(request);
		}else  {
			// Retrieve connection-type from request parameter
			connectionType = DATA;
			dataAttachedInCurrentReuqest = request.getParameter(DATA);
		} 

		// if "connect" request
		if (connectionType!=null && connectionType.equalsIgnoreCase(RequestType.CONNECT)) {
			logger.log(Level.INFO, MUSES_TAG + " Request type:"+connectionType+" with *ID*: "+currentJSessionID+ " with **dataInRequest**: "+dataAttachedInCurrentReuqest);
		}
		
		// if "send-data" request
		if (connectionType!=null && connectionType.equalsIgnoreCase(RequestType.DATA)) {
			// Callback the FL to receive data from the client and get the response data back into string
			dataToSendBackInResponse="";
			if (dataAttachedInCurrentReuqest != null){
				dataToSendBackInResponse = ConnectionManager.toReceive(currentJSessionID, dataAttachedInCurrentReuqest); // FIXME needs to be tested properly
				if (dataToSendBackInResponse == null) {
					dataToSendBackInResponse = "";
				}
			}
			if (dataToSendBackInResponse.equals("")) {
				dataToSendBackInResponse = waitForDataIfAvailable(INTERVAL_TO_WAIT, currentJSessionID);
			}
			
			response.setHeader("Content-Type", "text/plain");
			PrintWriter writer = response.getWriter();
			writer.write(dataToSendBackInResponse);
			
			//response.addHeader(DATA,dataToSendBackInResponse); // Now data is added in the body instead
			logger.log(Level.INFO, MUSES_TAG + " Data avaialble Request type:"+connectionType+" with *ID*: "+currentJSessionID+ " with **dataInResponse**: "+dataToSendBackInResponse);
		}
				
		// if "poll" request<commit_msg>content type set to JSON by response method call, few other attributes also set when sending response back.<commit_after>/*
 * version 1.0 - MUSES prototype software
 * Copyright MUSES project (European Commission FP7) - 2013 
 * 
 */
package eu.musesproject.server.connectionmanager;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Queue;

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

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
	
	/**
	 * Class ComMainServlet
	 * 
	 * @author <NAME>
	 * @version Jan 27, 2014
	 */

public class ComMainServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(ComMainServlet.class.getName());
	private Helper helper;
	private SessionHandler sessionHandler;
	private ConnectionManager connectionManager;
	private String dataAttachedInCurrentReuqest;
	private String dataToSendBackInResponse="";
	private static final String CONNECTION_TYPE = "connection-type";
	private static final String DATA = "data";
	private static final int INTERVAL_TO_WAIT = 5;
	private static final long SLEEP_INTERVAL = 1000;
	private static final String MUSES_TAG = "MUSES_TAG";
	private static final String MUSES_TAG_LEVEL_2 = "MUSES_TAG_LEVEL_2";
	private static String connectionType = "connect";
	
	/**
	 * 
	 * @param sessionHandler
	 * @param helper
	 * @param communicationManager
	 */
	
	public ComMainServlet(SessionHandler sessionHandler, Helper helper, ConnectionManager communicationManager){
		this.sessionHandler=sessionHandler;
		this.helper=helper;
		this.connectionManager=communicationManager;
	}
	

	public ComMainServlet() {
	}
	
	/**
	 * Initialize servlet
	 * 
	 * @throws Servlet exception
	 */
	@Override
	public void init() throws ServletException {
		super.init();
		logger.log(Level.INFO, MUSES_TAG + " init");
		helper = new Helper();
		connectionManager = ConnectionManager.getInstance();
		sessionHandler = SessionHandler.getInstance(getServletContext());
	}
	
	/**
	 * Handle POST http/https requests
	 * @param HttpServletRequest request 
	 * @param com.swedenconnectivity.comserverHttpServletResponse response
	 * @throws ServletException, IOException
	 */
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// create cookie if not in the request
		helper.setCookie(request);
		Cookie cookie = helper.getCookie();
		String currentJSessionID = cookie.getValue();
		
		// Retrieve data in the request
		if (request.getMethod().equalsIgnoreCase("POST")) {
			// Retrieve connection-type from request header
			connectionType = request.getHeader(CONNECTION_TYPE);
			dataAttachedInCurrentReuqest = helper.getRequestData(request);
		}else  {
			// Retrieve connection-type from request parameter
			connectionType = DATA;
			dataAttachedInCurrentReuqest = request.getParameter(DATA);
		} 

		// if "connect" request
		if (connectionType!=null && connectionType.equalsIgnoreCase(RequestType.CONNECT)) {
			logger.log(Level.INFO, MUSES_TAG + " Request type:"+connectionType+" with *ID*: "+currentJSessionID+ " with **dataInRequest**: "+dataAttachedInCurrentReuqest);
		}
		
		// if "send-data" request
		if (connectionType!=null && connectionType.equalsIgnoreCase(RequestType.DATA)) {
			// Callback the FL to receive data from the client and get the response data back into string
			dataToSendBackInResponse="";
			if (dataAttachedInCurrentReuqest != null){
				dataToSendBackInResponse = ConnectionManager.toReceive(currentJSessionID, dataAttachedInCurrentReuqest); // FIXME needs to be tested properly
				if (dataToSendBackInResponse == null) {
					dataToSendBackInResponse = "";
				}
			}
			if (dataToSendBackInResponse.equals("")) {
				dataToSendBackInResponse = waitForDataIfAvailable(INTERVAL_TO_WAIT, currentJSessionID);
			}
			
			response.setContentType("application/json");
			response.setHeader("Cache-Control", "nocache");
	        response.setCharacterEncoding("utf-8");
			
	        PrintWriter writer = response.getWriter();
			writer.write(dataToSendBackInResponse);
			
			//response.addHeader(DATA,dataToSendBackInResponse); // Now data is added in the body instead
			logger.log(Level.INFO, MUSES_TAG + " Data avaialble Request type:"+connectionType+" with *ID*: "+currentJSessionID+ " with **dataInResponse**: "+dataToSendBackInResponse);
		}
				
		// if "poll" request
{'fraction_non_alphanumeric': 0.0969449652586302, 'fraction_numerical': 0.005183632954670784, 'mean_word_length': 4.842783505154639, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4503791', 'n_tokens_mistral': 2816, 'n_tokens_neox': 2493, 'n_words': 640}