JavDB.apk(点击下载) / SyncSender.java


package com.rollbar.notifier.sender;

import com.rollbar.api.payload.Payload;
import com.rollbar.notifier.sender.json.JsonSerializer;
import com.rollbar.notifier.sender.json.JsonSerializerImpl;
import com.rollbar.notifier.sender.result.Response;
import d5.b;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import tv.danmaku.ijk.media.player.BuildConfig;

public class SyncSender extends AbstractSender {
    public static final String DEFAULT_API_ENDPOINT = "https://api.rollbar.com/api/1/item/";
    public static final String UTF_8 = "UTF-8";
    private final String accessToken;
    private final JsonSerializer jsonSerializer;
    private final Proxy proxy;
    private final URL url;

    public static final class Builder {
        private String accessToken;
        private JsonSerializer jsonSerializer;
        private Proxy proxy;
        private URL url;

        public Builder() {
            this(SyncSender.DEFAULT_API_ENDPOINT);
        }

        private static URL parseUrl(String str) {
            try {
                return new URL(str);
            } catch (MalformedURLException e7) {
                throw new IllegalArgumentException("The url provided is not valid: " + str, e7);
            }
        }

        public Builder accessToken(String str) {
            this.accessToken = str;
            return this;
        }

        public SyncSender build() {
            return new SyncSender(this);
        }

        public Builder jsonSerializer(JsonSerializer jsonSerializer2) {
            this.jsonSerializer = jsonSerializer2;
            return this;
        }

        public Builder proxy(Proxy proxy2) {
            this.proxy = proxy2;
            return this;
        }

        public Builder url(String str) {
            this.url = parseUrl(str);
            return this;
        }

        public Builder(String str) {
            this.url = parseUrl(str);
            this.jsonSerializer = new JsonSerializerImpl();
            this.proxy = null;
        }

        public Builder url(URL url2) {
            this.url = url2;
            return this;
        }
    }

    SyncSender(Builder builder) {
        this.url = builder.url;
        this.jsonSerializer = builder.jsonSerializer;
        this.accessToken = builder.accessToken;
        this.proxy = builder.proxy != null ? builder.proxy : Proxy.NO_PROXY;
    }

    private HttpURLConnection getConnection() throws IOException {
        HttpURLConnection httpURLConnection = (HttpURLConnection) this.url.openConnection(this.proxy);
        String str = this.accessToken;
        if (str != null && !BuildConfig.VERSION_NAME.equals(str)) {
            httpURLConnection.setRequestProperty("x-rollbar-access-token", this.accessToken);
        }
        httpURLConnection.setRequestProperty("Accept-Charset", UTF_8);
        httpURLConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        httpURLConnection.setRequestProperty("Accept", "application/json");
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setRequestMethod("POST");
        return httpURLConnection;
    }

    private static String getResponseContent(HttpURLConnection httpURLConnection) throws IOException {
        InputStream inputStream;
        if (httpURLConnection.getResponseCode() == 200) {
            inputStream = httpURLConnection.getInputStream();
        } else {
            inputStream = httpURLConnection.getErrorStream();
        }
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, UTF_8));
        StringBuilder sb = new StringBuilder();
        while (true) {
            String readLine = bufferedReader.readLine();
            if (readLine != null) {
                if (sb.length() != 0) {
                    sb.append("\n");
                }
                sb.append(readLine);
            } else {
                bufferedReader.close();
                return sb.toString();
            }
        }
    }

    private Response send(String str) throws IOException {
        HttpURLConnection connection = getConnection();
        sendJson(connection, str.getBytes(UTF_8));
        return readResponse(connection);
    }

    private void sendJson(HttpURLConnection httpURLConnection, byte[] bArr) throws IOException {
        OutputStream outputStream = null;
        try {
            outputStream = httpURLConnection.getOutputStream();
            outputStream.write(bArr, 0, bArr.length);
            b.a(outputStream);
        } catch (IOException e7) {
            throw e7;
        } catch (Throwable th) {
            b.a(outputStream);
            throw th;
        }
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable, com.rollbar.notifier.sender.AbstractSender
    public void close() throws IOException {
        getConnection().disconnect();
    }

    @Override // com.rollbar.notifier.sender.AbstractSender
    public Response doSend(Payload payload) throws Exception {
        return send(this.jsonSerializer.toJson(payload));
    }

    /* access modifiers changed from: package-private */
    public Response readResponse(HttpURLConnection httpURLConnection) throws IOException {
        int responseCode = httpURLConnection.getResponseCode();
        return new Response.Builder().status(responseCode).result(this.jsonSerializer.resultFrom(getResponseContent(httpURLConnection))).build();
    }
}