package com.eactive.eai.adapter.socket2.protocol.http.servlet;

import com.eactive.eai.adapter.socket2.config.SocketLoggerManager;
import com.eactive.eai.adapter.socket2.protocol.http.HttpServlet;
import com.eactive.eai.adapter.socket2.protocol.http.codec.HttpRequest;
import com.eactive.eai.adapter.socket2.protocol.http.codec.HttpResponse;
import com.eactive.eai.common.util.Logger;

public class ThreadServlet extends HttpServlet {
    static Logger logger = SocketLoggerManager.getInstance().getLogger("FileLogger{NET}_HTTP");

	protected void doService(HttpRequest request, HttpResponse response) throws Exception {
		String filter = request.getContext();
    	filter = filter.substring(filter.indexOf('/')+1);
    	
    	if ("thread".equals(filter)) {
    		filter = "";
    	}
		
    	if (!"".equals(request.getParameter("kill"))) {
    		logger.fatal("HttpAdapter | kill thread - " + request.getParameter("kill") + " - " + request.getSession());
    		ThreadDumpUtil.kill(request.getParameter("kill"));
    	}
		
		response.setContentType("text/html");
		response.setResponseCode(HttpResponse.HTTP_STATUS_SUCCESS);
		response.appendBody("<pre>" + ThreadDumpUtil.dump(filter) + "</pre>");
	}
}
