<commit_before>package net.sereneproject.collector.service.impl;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.URI;

import net.sereneproject.collector.dto.AnalyzerRequestDto;
import net.sereneproject.collector.dto.AnalyzerResponseDto;
import net.sereneproject.collector.service.AnalyzerPluginCommunicationService;

import org.apache.commons.codec.CharEncoding;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.io.CharStreams;
import com.google.common.io.Closeables;

public class HttpAnalyzerPluginCommunication implements
        AnalyzerPluginCommunicationService {

    private final HttpClient client;

    @Autowired
    public HttpAnalyzerPluginCommunication(HttpClient client) {
        this.client = client;
    }

    @Override
    public AnalyzerResponseDto send(URI uri, AnalyzerRequestDto request)
            throws ClientProtocolException, IOException {
        HttpPut putRequest = new HttpPut(uri);
        putRequest.setHeader(new BasicHeader("Accept", "application/json"));

        try {
            putRequest.setEntity(new StringEntity(request.toJson(),
                    CharEncoding.UTF_8));
        } catch (UnsupportedEncodingException e) {
            throw new IllegalArgumentException(
                    "UTF-8 should always be supported", e);
        }

        HttpResponse httpResponse = getClient().execute(putRequest);

        // TODO: check HTTP status
        if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            // TODO: throw some kind of exception
        }

        if (httpResponse.getEntity() == null) {
            // TODO: throw some kind of exception
        }

        Reader responseStream = new InputStreamReader(httpResponse.getEntity()
                .getContent());
        try {
            String jsonResponse = CharStreams.toString(responseStream);
            return AnalyzerResponseDto
                    .fromJsonToAnalyzerResponseDto(jsonResponse);
        } catch (IOException ioe) {
            throw ioe;
        } catch (RuntimeException re) {
            putRequest.abort();
            throw re;
        } finally {
            Closeables.closeQuietly(responseStream);
        }
    }

    private HttpClient getClient() {
        return this.client;<commit_msg>use an explicit char encoding to decode HTTP Response<commit_after>package net.sereneproject.collector.service.impl;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.URI;

import net.sereneproject.collector.dto.AnalyzerRequestDto;
import net.sereneproject.collector.dto.AnalyzerResponseDto;
import net.sereneproject.collector.service.AnalyzerPluginCommunicationService;

import org.apache.commons.codec.CharEncoding;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.io.CharStreams;
import com.google.common.io.Closeables;

public class HttpAnalyzerPluginCommunication implements
        AnalyzerPluginCommunicationService {

    private final HttpClient client;

    @Autowired
    public HttpAnalyzerPluginCommunication(HttpClient client) {
        this.client = client;
    }

    @Override
    public AnalyzerResponseDto send(URI uri, AnalyzerRequestDto request)
            throws ClientProtocolException, IOException {
        HttpPut putRequest = new HttpPut(uri);
        putRequest.setHeader(new BasicHeader("Accept", "application/json"));

        try {
            putRequest.setEntity(new StringEntity(request.toJson(),
                    CharEncoding.UTF_8));
        } catch (UnsupportedEncodingException e) {
            throw new IllegalArgumentException(
                    "UTF-8 should always be supported", e);
        }

        HttpResponse httpResponse = getClient().execute(putRequest);

        // TODO: check HTTP status
        if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            // TODO: throw some kind of exception
        }

        if (httpResponse.getEntity() == null) {
            // TODO: throw some kind of exception
        }

        Reader responseStream = new InputStreamReader(httpResponse.getEntity()
                .getContent(), CharEncoding.UTF_8);
        try {
            String jsonResponse = CharStreams.toString(responseStream);
            return AnalyzerResponseDto
                    .fromJsonToAnalyzerResponseDto(jsonResponse);
        } catch (IOException ioe) {
            throw ioe;
        } catch (RuntimeException re) {
            putRequest.abort();
            throw re;
        } finally {
            Closeables.closeQuietly(responseStream);
        }
    }

    private HttpClient getClient() {
        return this.client;
{'fraction_non_alphanumeric': 0.08579773640014604, 'fraction_numerical': 0.0009127418765972983, 'mean_word_length': 2.978939724037763, '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': '7254614', 'n_tokens_mistral': 1446, 'n_tokens_neox': 1428, 'n_words': 294}