<commit_before>package net.onrc.openvirtex.api.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

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

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.thetransactioncompany.jsonrpc2.JSONRPC2ParseException;
import com.thetransactioncompany.jsonrpc2.JSONRPC2Request;
import com.thetransactioncompany.jsonrpc2.JSONRPC2Response;

public abstract class AbstractService {

	private static Logger log = LogManager.getLogger(AbstractService.class
			.getName());

	public abstract void handle(HttpServletRequest request,
			HttpServletResponse response);

	/**
	 * Parses the json.
	 * 
	 * @param request
	 *            the request
	 * @return the jSON object
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 * @throws JSONRPC2ParseException
	 * @throws JSONException
	 *             the jSON exception
	 */
	protected JSONRPC2Request parseJSONRequest(final HttpServletRequest request)
			throws IOException, JSONRPC2ParseException {
		final BufferedReader reader = request.getReader();
		final StringBuilder sb = new StringBuilder();
		String line = reader.readLine();
		while (line != null) {
			sb.append(line + "\n");
			line = reader.readLine();
		}
		reader.close();
		AbstractService.log.debug("---------JSON RPC request: {}",
				sb.toString());
		return JSONRPC2Request.parse(sb.toString());

	}

	/**
	 * Write json object.
	 * 
	 * @param response
	 *            the response
	 * @param jobj
	 *            the jobj
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 */
	protected void writeJSONObject(final HttpServletResponse response,
			final JSONRPC2Response jresp) throws IOException {
		response.setStatus(HttpServletResponse.SC_OK);
		response.setHeader("Content-Type", "application/json; charset=utf-8");
		response.setContentType("application/json; charset=utf-8");
		final String json = jresp.toJSONString();
		AbstractService.log.debug("---------JSON RPC response: {}", json);

		response.getWriter().println(json);
	}

	protected static String stack2string(final Exception e) {
		PrintWriter pw = null;
		try {
			final StringWriter sw = new StringWriter();
			pw = new PrintWriter(sw);
			e.printStackTrace(pw);
			return "------\r\n" + sw.toString() + "------\r\n";
		} catch (final Exception e2) {
			return "bad stack2string";
		} finally {<commit_msg>adding cross site scripting support ;)<commit_after>package net.onrc.openvirtex.api.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

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

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.thetransactioncompany.jsonrpc2.JSONRPC2ParseException;
import com.thetransactioncompany.jsonrpc2.JSONRPC2Request;
import com.thetransactioncompany.jsonrpc2.JSONRPC2Response;

public abstract class AbstractService {

	private static Logger log = LogManager.getLogger(AbstractService.class
			.getName());

	public abstract void handle(HttpServletRequest request,
			HttpServletResponse response);

	/**
	 * Parses the json.
	 * 
	 * @param request
	 *            the request
	 * @return the jSON object
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 * @throws JSONRPC2ParseException
	 * @throws JSONException
	 *             the jSON exception
	 */
	protected JSONRPC2Request parseJSONRequest(final HttpServletRequest request)
			throws IOException, JSONRPC2ParseException {
		final BufferedReader reader = request.getReader();
		final StringBuilder sb = new StringBuilder();
		String line = reader.readLine();
		while (line != null) {
			sb.append(line + "\n");
			line = reader.readLine();
		}
		reader.close();
		AbstractService.log.debug("---------JSON RPC request: {}",
				sb.toString());
		return JSONRPC2Request.parse(sb.toString());

	}

	/**
	 * Write json object.
	 * 
	 * @param response
	 *            the response
	 * @param jobj
	 *            the jobj
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 */
	protected void writeJSONObject(final HttpServletResponse response,
			final JSONRPC2Response jresp) throws IOException {
		response.setStatus(HttpServletResponse.SC_OK);
		response.setHeader("Content-Type", "application/json; charset=utf-8");
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("application/json; charset=utf-8");
		final String json = jresp.toJSONString();
		AbstractService.log.debug("---------JSON RPC response: {}", json);

		response.getWriter().println(json);
	}

	protected static String stack2string(final Exception e) {
		PrintWriter pw = null;
		try {
			final StringWriter sw = new StringWriter();
			pw = new PrintWriter(sw);
			e.printStackTrace(pw);
			return "------\r\n" + sw.toString() + "------\r\n";
		} catch (final Exception e2) {
			return "bad stack2string";
		} finally {
{'fraction_non_alphanumeric': 0.11713791113675708, 'fraction_numerical': 0.006924408540103866, 'mean_word_length': 3.7445255474452557, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2653460', 'n_tokens_mistral': 1586, 'n_tokens_neox': 1449, 'n_words': 362}