<commit_before>package org.aravind.oss.jenkins;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.aravind.oss.jenkins.domain.Jenkins;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
import java.util.Optional;

/**
 * Represents one single running instance of Jenkins. You will create one object per running Jenkins instance.
 *
 * @author <NAME>
 * @since 0.5.0
 */
public class JenkinsClient {
    private static final int SO_TIMEOUT_IN_MILLIS = 3000;
    private static final int CONN_TIMEOUT_IN_MILLIS = 500;
    private final URL resourceUrl;
    private Optional<String> userName = Optional.empty();
    private Optional<String> passwordOrApiToken = Optional.empty();
    private ObjectMapper mapper = new ObjectMapper();
    private static final Logger logger = LoggerFactory.getLogger(JenkinsClient.class);

    /**
     * @param url resource url of the jenkins item
     */
    public JenkinsClient(URL url) {
        resourceUrl = url;
    }

    /**
     * @param url      url of the jenkins instance
     * @param uname    username if authentication is enabled in jenkins instance
     * @param password password or API token if authentication is enabled in jenkins instance
     */
    public JenkinsClient(URL url, String uname, String password) throws JenkinsException {
        if (uname.isEmpty()) {
            throw new JenkinsException("Missing Jenkins username for authentication");
        }
        if (password.isEmpty()) {
            throw new JenkinsException("Missing Jenkins password (or API token) for authentication");
        }
        resourceUrl = url;
        userName = Optional.of(uname);
        passwordOrApiToken = Optional.of(password);
    }

    public HttpURLConnection connect() throws JenkinsException {
        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) resourceUrl.openConnection();
            conn.setConnectTimeout(CONN_TIMEOUT_IN_MILLIS);
            conn.setReadTimeout(SO_TIMEOUT_IN_MILLIS);
            conn.connect();

            if (userName.isPresent()) {
                conn.setRequestProperty("Authorization", "Basic " + getAuthenticationString());
            }

            return conn;
        } catch (IOException e) {
            logger.error("Error while connecting to {}", resourceUrl, e);
            throw new JenkinsException("Error while opening a connection to " + resourceUrl, e);
        }
    }

    private String getAuthenticationString() {
        String authString = userName.get() + ":" + passwordOrApiToken.get();
        return Base64.getEncoder().encodeToString(authString.getBytes());
    }

    public Optional<String> get(int bufferSize) throws JenkinsException {
        HttpURLConnection conn = connect();

        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            InputStream is = conn.getInputStream();

            // read the response body
            byte[] buf = new byte[bufferSize];

            while (is.read(buf) > 0) {
                bos.write(buf);
            }

            // close the input stream so that the connection can be reused
            is.close();

            return Optional.of(bos.toString());
        } catch (IOException e) {
            logger.warn("IGNORING this exception. Just a WARNING to debug this issue. Error while HTTP GET to {}", resourceUrl, e);
            //Need to read even the error stream so that we can take advantage of socket reuse in Keep-Alive
            //http://docs.oracle.com/javase/7/docs/technotes/guides/net/http-keepalive.html
            try {
                int respCode = ((HttpURLConnection) conn).getResponseCode();
                logger.warn("HTTP response code {}", respCode);
                InputStream es = ((HttpURLConnection) conn).getErrorStream();

                if (es != null) {
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();

                    // read the error response body so that the connection can be reused
                    byte[] buf = new byte[bufferSize];
                    while (es.read(buf) > 0) {
                        bos.write(buf);
                    }
                    // close the error stream so that the connection can be reused
                    es.close();
                }
            } catch (IOException ex) {
                // ignore the exception
            }
        }
        return Optional.empty();
    }

    public Optional<Jenkins> getJenkins() throws JenkinsException {
        Optional<String> resp = get(512);

        if (resp.isPresent()) {
            try {
                Jenkins j = mapper.readValue(resp.get(), Jenkins.class);
                return Optional.of(j);
            } catch (IOException e) {
                logger.error("Error while parsing the JSON {}", resp.get(), e);
                return Optional.empty();
            }
        }
        return Optional.empty();
    }<commit_msg>Fix issue with reading the HTTP response<commit_after>package org.aravind.oss.jenkins;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.IOUtils;
import org.aravind.oss.jenkins.domain.Jenkins;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Base64;
import java.util.Optional;

/**
 * Represents one single running instance of Jenkins. You will create one object per running Jenkins instance.
 *
 * @author <NAME>
 * @since 0.5.0
 */
public class JenkinsClient {
    private static final int SO_TIMEOUT_IN_MILLIS = 3000;
    private static final int CONN_TIMEOUT_IN_MILLIS = 500;
    private final URL resourceUrl;
    private Optional<String> userName = Optional.empty();
    private Optional<String> passwordOrApiToken = Optional.empty();
    private ObjectMapper mapper = new ObjectMapper();
    private static final Logger logger = LoggerFactory.getLogger(JenkinsClient.class);

    /**
     * @param url resource url of the jenkins item
     */
    public JenkinsClient(URL url) {
        resourceUrl = url;
    }

    /**
     * @param url      url of the jenkins instance
     * @param uname    username if authentication is enabled in jenkins instance
     * @param password password or API token if authentication is enabled in jenkins instance
     */
    public JenkinsClient(URL url, String uname, String password) throws JenkinsException {
        if (uname.isEmpty()) {
            throw new JenkinsException("Missing Jenkins username for authentication");
        }
        if (password.isEmpty()) {
            throw new JenkinsException("Missing Jenkins password (or API token) for authentication");
        }
        resourceUrl = url;
        userName = Optional.of(uname);
        passwordOrApiToken = Optional.of(password);
    }

    public HttpURLConnection connect() throws JenkinsException {
        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) resourceUrl.openConnection();
            conn.setConnectTimeout(CONN_TIMEOUT_IN_MILLIS);
            conn.setReadTimeout(SO_TIMEOUT_IN_MILLIS);
            conn.connect();

            if (userName.isPresent()) {
                conn.setRequestProperty("Authorization", "Basic " + getAuthenticationString());
            }

            return conn;
        } catch (IOException e) {
            logger.error("Error while connecting to {}", resourceUrl, e);
            throw new JenkinsException("Error while opening a connection to " + resourceUrl, e);
        }
    }

    private String getAuthenticationString() {
        String authString = userName.get() + ":" + passwordOrApiToken.get();
        return Base64.getEncoder().encodeToString(authString.getBytes());
    }

    public Optional<String> get(int bufferSize) throws JenkinsException {
        HttpURLConnection conn = connect();

        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            InputStream is = conn.getInputStream();
            String resp = IOUtils.toString(is, Charset.forName("UTF-8"));

            // close the input stream so that the connection can be reused
            is.close();

            return Optional.of(resp);
        } catch (IOException e) {
            logger.warn("IGNORING this exception. Just a WARNING to debug this issue. Error while HTTP GET to {}", resourceUrl, e);
            //Need to read even the error stream so that we can take advantage of socket reuse in Keep-Alive
            //http://docs.oracle.com/javase/7/docs/technotes/guides/net/http-keepalive.html
            try {
                int respCode = ((HttpURLConnection) conn).getResponseCode();
                logger.warn("HTTP response code {}", respCode);
                InputStream es = ((HttpURLConnection) conn).getErrorStream();

                if (es != null) {
                    // read the error response body so that the connection can be reused
                    String resp = IOUtils.toString(es, Charset.forName("UTF-8"));

                    // close the error stream so that the connection can be reused
                    es.close();
                }
            } catch (IOException ex) {
                // ignore the exception
            }
        }
        return Optional.empty();
    }

    public Optional<Jenkins> getJenkins() throws JenkinsException {
        Optional<String> resp = get(512);

        if (resp.isPresent()) {
            try {
                Jenkins j = mapper.readValue(resp.get(), Jenkins.class);
                return Optional.of(j);
            } catch (IOException e) {
                logger.error("Error while parsing the JSON {}", resp.get(), e);
                return Optional.empty();
            }
        }
        return Optional.empty();
    }
{'fraction_non_alphanumeric': 0.09369710245942448, 'fraction_numerical': 0.004381161007667032, 'mean_word_length': 2.234782608695652, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 17, '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': '253294', 'n_tokens_mistral': 2613, 'n_tokens_neox': 2484, 'n_words': 758}