<commit_before>package com.atg.openssp.common.core.connector;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.atg.openssp.common.exception.BidProcessingException;

/**
 * 
 * Doing a HTTP Connection with ContentType JSON and transport method POST.
 * 
 * @author <NAME>
 *
 */
public class JsonPostConnector extends DefaultConnector {

	private static final Logger log = LoggerFactory.getLogger(JsonPostConnector.class);

	/**
	 * Does a connection with POST method and sends the data in {@code entity}. Reads the response and returns it as string value.
	 * 
	 * @param entity
	 * @param httpPost
	 * @return String with the body of response if connection was succesful, <code>null</code> otherwise.
	 * @throws BidProcessingException
	 */
	public String connect(final StringEntity entity, HttpPost httpPost) throws BidProcessingException {
		CloseableHttpResponse httpResponse = null;
		HttpEntity respEntity = null;
		try {
			httpPost.setEntity(entity);
			httpResponse = httpClient.execute(httpPost);
			final int statusCode = httpResponse.getStatusLine().getStatusCode();
			if (HttpStatus.SC_OK == statusCode) {
				respEntity = httpResponse.getEntity();
				if (respEntity != null) {
					String content = null;
					if (httpResponse.getFirstHeader("Content-Encoding") != null && httpResponse.getFirstHeader("Content-Encoding").getValue().equalsIgnoreCase("gzip")) {
						content = readContentFromStream(new GZIPInputStream(respEntity.getContent()));
					} else {
						content = readContentFromStream(respEntity.getContent());
					}
					return content;
				} else {
					log.debug("bad result: {}", statusCode);
				}
			}
		} catch (final IOException e) {
			throw new BidProcessingException("IO " + e.getMessage());
		} finally {
			EntityUtils.consumeQuietly(respEntity);
			httpPost = null;
			if (httpResponse != null) {
				try {<commit_msg>Checking for availablity of Header Content-Encoding.<commit_after>package com.atg.openssp.common.core.connector;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.atg.openssp.common.exception.BidProcessingException;

/**
 * 
 * Doing a HTTP Connection with ContentType JSON and transport method POST.
 * 
 * @author <NAME>
 *
 */
public class JsonPostConnector extends DefaultConnector {

	private static final Logger log = LoggerFactory.getLogger(JsonPostConnector.class);

	/**
	 * Does a connection with POST method and sends the data in {@code entity}. Reads the response and returns it as string value.
	 * 
	 * @param entity
	 * @param httpPost
	 * @return String with the body of response if connection was succesful, <code>null</code> otherwise.
	 * @throws BidProcessingException
	 */
	public String connect(final StringEntity entity, HttpPost httpPost) throws BidProcessingException {
		CloseableHttpResponse httpResponse = null;
		HttpEntity respEntity = null;
		try {
			httpPost.setEntity(entity);
			httpResponse = httpClient.execute(httpPost);
			final int statusCode = httpResponse.getStatusLine().getStatusCode();
			if (HttpStatus.SC_OK == statusCode) {
				respEntity = httpResponse.getEntity();
				if (respEntity != null) {
					String content = null;
					if (httpResponse.containsHeader("Content-Encoding") && httpResponse.getFirstHeader("Content-Encoding").getValue().equalsIgnoreCase("gzip")) {
						content = readContentFromStream(new GZIPInputStream(respEntity.getContent()));
					} else {
						content = readContentFromStream(respEntity.getContent());
					}
					return content;
				} else {
					log.debug("bad result: {}", statusCode);
				}
			}
		} catch (final IOException e) {
			throw new BidProcessingException("IO " + e.getMessage());
		} finally {
			EntityUtils.consumeQuietly(respEntity);
			httpPost = null;
			if (httpResponse != null) {
				try {
{'fraction_non_alphanumeric': 0.10639672625457679, 'fraction_numerical': 0.0008615119534783546, 'mean_word_length': 4.48936170212766, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2684633', 'n_tokens_mistral': 1475, 'n_tokens_neox': 1311, 'n_words': 326}