<commit_before>package org.maptalks.servletrest.servlets;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

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

/**
 * 可以作为所有servlet的基类，提供了write方法、显示对象转换类和显示对象的获取方法<br/>
 * 若urlpattern中存在{return}变量定义了返回类型（json或xml）,
 * 则子servlet能够通过getConvertor和getReturnBuilder获取到相应的返回类型转换对象<br/>
 * 若urlpattern中没有定义{return}变量，则默认为json模式 /u/{id}/ /u/10000/
 *
 * @author fuzhen
 *
 */
public class BaseServlet extends HttpServlet {

	private boolean allowGzip = false;

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// try {
		final String encoding = req.getHeader("Accept-Encoding");
		if (encoding != null && encoding.indexOf("gzip") != -1) {
			allowGzip = true;
		}
		super.service(req, resp);
		// } catch (final Exception ex) {
		// getOutputBuilder().resolveException(ex);
		// write(outputBuilder.toResult(), resp);
		// }
	}

	/**
	 * 从httpservletrequest中取得post请求的data
	 *
	 * @param req
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 */
	public Map<String, String> getPostParameters(final HttpServletRequest req)
			throws IOException {
		Map<String, String> postData = new HashMap<String, String>();
        String contentType = req.getHeader("Content-Encoding");
        boolean gzipReader = false;
        if (contentType != null && contentType.indexOf("gzip") > -1) {
            gzipReader = true;
        }
		StringBuilder rawPostDataBuilder = new StringBuilder();
        BufferedReader br;
        if (gzipReader) {
            br = new BufferedReader(new InputStreamReader(new GZIPInputStream(
                    req.getInputStream()) , "UTF-8"));
        } else {
            br = new BufferedReader(new InputStreamReader(
                    req.getInputStream(), "UTF-8"));
        }

		String encoding = "UTF-8";
		int temp;
		while ((temp = br.read()) != -1) {
			rawPostDataBuilder.append((char)temp);
		}
		br.close();
		String rawPostData = rawPostDataBuilder.toString();
		//		if (rawPostDataBuilder.length()==0)
		//			return postData;
		String[] params = rawPostData.split("&");
		for (int i = 0; i < params.length; i++) {
			String parameter = params[i];
			String[] paraValue = parseParameter(parameter, encoding);
			if (paraValue == null)
				continue;
			postData.put(paraValue[0], paraValue[1]);
		}
		//解析url中的参数
		String queryString = req.getQueryString();
		if (queryString != null) {
			String[] queryParams = queryString.split("&");
			for (int i = 0; i < queryParams.length; i++) {
				String parameter = queryParams[i];
				String[] paraValue = parseParameter(parameter, encoding);
				if (paraValue == null)
					continue;
				postData.put(paraValue[0], paraValue[1]);
			}
		}
		return postData;
	}

	private String[] parseParameter(String parameter, String encoding)
			throws UnsupportedEncodingException {
		final String[] dataArr = parameter.split("=", 2);
		if (dataArr == null || dataArr.length != 2)
			return null;
		dataArr[1] = URLDecoder.decode(dataArr[1], encoding);
		return dataArr;
	}

	/**
	 * 向输出流中写入toWrite
	 *
	 * @param toWrite
	 * @param resp
	 * @throws IOException
	 */
	protected void writeResp(String toWrite, HttpServletResponse resp)
			throws IOException {
		final Writer writer = getWriter(resp);
		writer.write(toWrite);
		writer.flush();
		writer.close();
	}

	protected Writer getWriter(final HttpServletResponse resp)
			throws IOException {
		//		if (writer != null)
		//			return writer;
		if (allowGzip) {
			resp.setHeader("Content-Encoding", "gzip");
			Writer writer = new OutputStreamWriter(new GZIPOutputStream(
					resp.getOutputStream()), "UTF-8");
			return writer;
		} else {
			return resp.getWriter();<commit_msg>make BaseServlet thread safe by setting allowGzip in HttpRequest<commit_after>package org.maptalks.servletrest.servlets;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

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

/**
 * 可以作为所有servlet的基类，提供了write方法、显示对象转换类和显示对象的获取方法<br/>
 * 若urlpattern中存在{return}变量定义了返回类型（json或xml）,
 * 则子servlet能够通过getConvertor和getReturnBuilder获取到相应的返回类型转换对象<br/>
 * 若urlpattern中没有定义{return}变量，则默认为json模式 /u/{id}/ /u/10000/
 *
 * @author fuzhen
 *
 */
public class BaseServlet extends HttpServlet {

	private static String ATTRIBUTE_GZIP = "MAPTALKS-ALLOW-GZIP";

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		final String encoding = req.getHeader("Accept-Encoding");
		if (encoding != null && encoding.indexOf("gzip") != -1) {
			req.setAttribute(ATTRIBUTE_GZIP, true);
		}
		super.service(req, resp);
	}

	/**
	 * 从httpservletrequest中取得post请求的data
	 *
	 * @param req
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 */
	public Map<String, String> getPostParameters(final HttpServletRequest req)
			throws IOException {
		Map<String, String> postData = new HashMap<String, String>();
        String contentType = req.getHeader("Content-Encoding");
        boolean gzipReader = false;
        if (contentType != null && contentType.indexOf("gzip") > -1) {
            gzipReader = true;
        }
		StringBuilder rawPostDataBuilder = new StringBuilder();
        BufferedReader br;
        if (gzipReader) {
            br = new BufferedReader(new InputStreamReader(new GZIPInputStream(
                    req.getInputStream()) , "UTF-8"));
        } else {
            br = new BufferedReader(new InputStreamReader(
                    req.getInputStream(), "UTF-8"));
        }

		String encoding = "UTF-8";
		int temp;
		while ((temp = br.read()) != -1) {
			rawPostDataBuilder.append((char)temp);
		}
		br.close();
		String rawPostData = rawPostDataBuilder.toString();
		String[] params = rawPostData.split("&");
		for (int i = 0; i < params.length; i++) {
			String parameter = params[i];
			String[] paraValue = parseParameter(parameter, encoding);
			if (paraValue == null)
				continue;
			postData.put(paraValue[0], paraValue[1]);
		}
		//解析url中的参数
		String queryString = req.getQueryString();
		if (queryString != null) {
			String[] queryParams = queryString.split("&");
			for (int i = 0; i < queryParams.length; i++) {
				String parameter = queryParams[i];
				String[] paraValue = parseParameter(parameter, encoding);
				if (paraValue == null)
					continue;
				postData.put(paraValue[0], paraValue[1]);
			}
		}
		return postData;
	}

	private String[] parseParameter(String parameter, String encoding)
			throws UnsupportedEncodingException {
		final String[] dataArr = parameter.split("=", 2);
		if (dataArr == null || dataArr.length != 2)
			return null;
		dataArr[1] = URLDecoder.decode(dataArr[1], encoding);
		return dataArr;
	}

	/**
	 * 向输出流中写入toWrite
	 *
	 * @param toWrite
	 * @param resp
	 * @throws IOException
	 */
	protected void writeResp(String toWrite, HttpServletResponse resp)
			throws IOException {
		final Writer writer = getWriter(resp);
		writer.write(toWrite);
		writer.flush();
		writer.close();
	}

	protected Writer getWriter(final HttpServletResponse resp)
			throws IOException {
		if (req.getAttribute(ATTRIBUTE_GZIP) != null) {
			resp.setHeader("Content-Encoding", "gzip");
			Writer writer = new OutputStreamWriter(new GZIPOutputStream(
					resp.getOutputStream()), "UTF-8");
			return writer;
		} else {
			return resp.getWriter();
{'fraction_non_alphanumeric': 0.12471354480762273, 'fraction_numerical': 0.005427572066095766, 'mean_word_length': 3.8920353982300884, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '2766733', 'n_tokens_mistral': 2852, 'n_tokens_neox': 2654, 'n_words': 566}