<commit_before>package org.spacehq.mc.auth.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.spacehq.mc.auth.exception.authentication.AuthenticationException;
import org.spacehq.mc.auth.exception.authentication.AuthenticationUnavailableException;
import org.spacehq.mc.auth.exception.authentication.InvalidCredentialsException;
import org.spacehq.mc.auth.exception.authentication.UserMigratedException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.util.UUID;

/**
 * Utilities for making HTTP requests.
 */
public class RequestUtil {
    private static final Gson GSON;

    static {
        GSON = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDSerializer()).create();
    }

    private RequestUtil() {
    }

    /**
     * Makes an HTTP request.
     *
     * @param proxy Proxy to use when making the request.
     * @param url   URL to make the request to.
     * @param input Input to provide in the request.
     * @param clazz Class to provide the response as.
     * @return The response of the request.
     * @throws AuthenticationException If an authentication error occurs.
     */
    public static <T> T makeRequest(Proxy proxy, String url, Object input, Class<T> clazz) throws AuthenticationException {
        T result = null;
        try {
            String jsonString = input == null ? performGetRequest(proxy, url) : performPostRequest(proxy, url, GSON.toJson(input), "application/json");
            result = GSON.fromJson(jsonString, clazz);
        } catch(Exception e) {
            throw new AuthenticationUnavailableException("Could not make request to auth server.", e);
        }

        if(result instanceof Response) {
            Response response = (Response) result;
            if(response.getError() != null && !response.getError().equals("")) {
                if(response.getCause() != null && response.getCause().equals("UserMigratedException")) {
                    throw new UserMigratedException(response.getErrorMessage());
                } else if(response.getError().equals("ForbiddenOperationException")) {
                    throw new InvalidCredentialsException(response.getErrorMessage());
                } else {
                    throw new AuthenticationException(response.getErrorMessage());
                }
            }
        }

        return result;
    }

    private static HttpURLConnection createUrlConnection(Proxy proxy, String url) throws IOException {
        if(proxy == null) {
            throw new IllegalArgumentException("Proxy cannot be null.");
        }

        if(url == null) {
            throw new IllegalArgumentException("URL cannot be null.");
        }

        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(proxy);
        connection.setConnectTimeout(15000);
        connection.setReadTimeout(15000);
        connection.setUseCaches(false);
        return connection;
    }

    private static String performGetRequest(Proxy proxy, String url) throws IOException {
        if(proxy == null) {
            throw new IllegalArgumentException("Proxy cannot be null.");
        }

        if(url == null) {
            throw new IllegalArgumentException("URL cannot be null.");
        }

        HttpURLConnection connection = createUrlConnection(proxy, url);
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(connection.getResponseCode() == 200 ? connection.getInputStream() : connection.getErrorStream()));
            StringBuilder result = new StringBuilder();
            String line = null;
            while((line = reader.readLine()) != null) {
                result.append(line).append("\n");
            }

            return result.toString();
        } finally {
            if(reader != null) {
                try {
                    reader.close();
                } catch(IOException e) {
                }
            }
        }
    }

    private static String performPostRequest(Proxy proxy, String url, String post, String type) throws IOException {
        if(proxy == null) {
            throw new IllegalArgumentException("Proxy cannot be null.");
        }

        if(url == null) {
            throw new IllegalArgumentException("URL cannot be null.");
        }

        if(post == null) {
            throw new IllegalArgumentException("Post cannot be null.");
        }

        if(type == null) {
            throw new IllegalArgumentException("Type cannot be null.");
        }

        HttpURLConnection connection = createUrlConnection(proxy, url);
        byte[] bytes = post.getBytes("UTF-8");
        connection.setRequestProperty("Content-Type", type + "; charset=utf-8");
        connection.setRequestProperty("Content-Length", String.valueOf(bytes.length));
        connection.setDoOutput(true);
        OutputStream out = null;
        try {
            out = connection.getOutputStream();
            out.write(bytes);
        } finally {
            if(out != null) {
                try {
                    out.close();
                } catch(IOException e) {
                }
            }
        }

        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(connection.getResponseCode() == 200 ? connection.getInputStream() : connection.getErrorStream()));
            StringBuilder result = new StringBuilder();
            String line = null;
            while((line = reader.readLine()) != null) {
                result.append(line).append("\n");
            }

            return result.toString();
        } finally {
            if(reader != null) {
                try {<commit_msg>Call setDoInput(true) on HTTP connections.<commit_after>package org.spacehq.mc.auth.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.spacehq.mc.auth.exception.authentication.AuthenticationException;
import org.spacehq.mc.auth.exception.authentication.AuthenticationUnavailableException;
import org.spacehq.mc.auth.exception.authentication.InvalidCredentialsException;
import org.spacehq.mc.auth.exception.authentication.UserMigratedException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.util.UUID;

/**
 * Utilities for making HTTP requests.
 */
public class RequestUtil {
    private static final Gson GSON;

    static {
        GSON = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDSerializer()).create();
    }

    private RequestUtil() {
    }

    /**
     * Makes an HTTP request.
     *
     * @param proxy Proxy to use when making the request.
     * @param url   URL to make the request to.
     * @param input Input to provide in the request.
     * @param clazz Class to provide the response as.
     * @return The response of the request.
     * @throws AuthenticationException If an authentication error occurs.
     */
    public static <T> T makeRequest(Proxy proxy, String url, Object input, Class<T> clazz) throws AuthenticationException {
        T result = null;
        try {
            String jsonString = input == null ? performGetRequest(proxy, url) : performPostRequest(proxy, url, GSON.toJson(input), "application/json");
            result = GSON.fromJson(jsonString, clazz);
        } catch(Exception e) {
            throw new AuthenticationUnavailableException("Could not make request to auth server.", e);
        }

        if(result instanceof Response) {
            Response response = (Response) result;
            if(response.getError() != null && !response.getError().equals("")) {
                if(response.getCause() != null && response.getCause().equals("UserMigratedException")) {
                    throw new UserMigratedException(response.getErrorMessage());
                } else if(response.getError().equals("ForbiddenOperationException")) {
                    throw new InvalidCredentialsException(response.getErrorMessage());
                } else {
                    throw new AuthenticationException(response.getErrorMessage());
                }
            }
        }

        return result;
    }

    private static HttpURLConnection createUrlConnection(Proxy proxy, String url) throws IOException {
        if(proxy == null) {
            throw new IllegalArgumentException("Proxy cannot be null.");
        }

        if(url == null) {
            throw new IllegalArgumentException("URL cannot be null.");
        }

        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(proxy);
        connection.setConnectTimeout(15000);
        connection.setReadTimeout(15000);
        connection.setUseCaches(false);
        return connection;
    }

    private static String performGetRequest(Proxy proxy, String url) throws IOException {
        if(proxy == null) {
            throw new IllegalArgumentException("Proxy cannot be null.");
        }

        if(url == null) {
            throw new IllegalArgumentException("URL cannot be null.");
        }

        HttpURLConnection connection = createUrlConnection(proxy, url);
        connection.setDoInput(true);
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(connection.getResponseCode() == 200 ? connection.getInputStream() : connection.getErrorStream()));
            StringBuilder result = new StringBuilder();
            String line = null;
            while((line = reader.readLine()) != null) {
                result.append(line).append("\n");
            }

            return result.toString();
        } finally {
            if(reader != null) {
                try {
                    reader.close();
                } catch(IOException e) {
                }
            }
        }
    }

    private static String performPostRequest(Proxy proxy, String url, String post, String type) throws IOException {
        if(proxy == null) {
            throw new IllegalArgumentException("Proxy cannot be null.");
        }

        if(url == null) {
            throw new IllegalArgumentException("URL cannot be null.");
        }

        if(post == null) {
            throw new IllegalArgumentException("Post cannot be null.");
        }

        if(type == null) {
            throw new IllegalArgumentException("Type cannot be null.");
        }

        HttpURLConnection connection = createUrlConnection(proxy, url);
        byte[] bytes = post.getBytes("UTF-8");
        connection.setRequestProperty("Content-Type", type + "; charset=utf-8");
        connection.setRequestProperty("Content-Length", String.valueOf(bytes.length));
        connection.setDoInput(true);
        connection.setDoOutput(true);
        OutputStream out = null;
        try {
            out = connection.getOutputStream();
            out.write(bytes);
        } finally {
            if(out != null) {
                try {
                    out.close();
                } catch(IOException e) {
                }
            }
        }

        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(connection.getResponseCode() == 200 ? connection.getInputStream() : connection.getErrorStream()));
            StringBuilder result = new StringBuilder();
            String line = null;
            while((line = reader.readLine()) != null) {
                result.append(line).append("\n");
            }

            return result.toString();
        } finally {
            if(reader != null) {
                try {
{'fraction_non_alphanumeric': 0.10254472159234064, 'fraction_numerical': 0.0030234315948601664, 'mean_word_length': 2.2571115973741795, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '133364', 'n_tokens_mistral': 2938, 'n_tokens_neox': 2847, 'n_words': 754}