private T sendRequest()

in aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/internal/UnsignedXrayClient.java [118:161]


    private <T> T sendRequest(URL endpoint, Object request, Class<T> responseClass) {
        final HttpURLConnection connection;
        try {
            connection = (HttpURLConnection) endpoint.openConnection();
        } catch (IOException e) {
            throw new XrayClientException("Could not connect to endpoint " + endpoint, e);
        }

        connection.setConnectTimeout(TIME_OUT_MILLIS);
        connection.setReadTimeout(TIME_OUT_MILLIS);

        try {
            connection.setRequestMethod("POST");
        } catch (ProtocolException e) {
            throw new IllegalStateException("Invalid protocol, can't happen.");
        }

        connection.addRequestProperty("Content-Type", "application/json");
        connection.setDoOutput(true);

        try (OutputStream outputStream = connection.getOutputStream()) {
            OBJECT_MAPPER.writeValue(outputStream, request);
        } catch (IOException | IllegalArgumentException e) {
            throw new XrayClientException("Could not serialize and send request.", e);
        }

        final int responseCode;
        try {
            responseCode = connection.getResponseCode();
        } catch (IOException e) {
            throw new XrayClientException("Could not read response code.", e);
        }

        if (responseCode != 200) {
            throw new XrayClientException("Error response from X-Ray: " +
                                          readResponseString(connection));
        }

        try {
            return OBJECT_MAPPER.readValue(connection.getInputStream(), responseClass);
        } catch (IOException e) {
            throw new XrayClientException("Error reading response.", e);
        }
    }