导航菜单

页面标题

页面副标题

Meine NEW v2.0.3616 - UrlConnectionHttpClient.java 源代码

正在查看: Meine NEW v2.0.3616 应用的 UrlConnectionHttpClient.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.microsoft.identity.common.internal.net;

import android.support.v4.media.a;
import com.microsoft.identity.common.internal.net.HttpClient;
import com.microsoft.identity.common.internal.telemetry.Telemetry;
import com.microsoft.identity.common.internal.telemetry.events.HttpEndEvent;
import com.microsoft.identity.common.internal.telemetry.events.HttpStartEvent;
import com.microsoft.identity.common.internal.util.StringUtil;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicReference;

public class UrlConnectionHttpClient implements HttpClient {
    private static transient AtomicReference<UrlConnectionHttpClient> defaultReference = new AtomicReference<>(null);
    private final int connectTimeoutMs;
    private final Supplier<Integer> connectTimeoutMsSupplier;
    private final int readTimeoutMs;
    private final Supplier<Integer> readTimeoutMsSupplier;
    private final RetryPolicy<HttpResponse> retryPolicy;
    private final int streamBufferSize;

    public interface Supplier<T> {
        T get();
    }

    public static class UrlConnectionHttpClientBuilder {
        private boolean connectTimeoutMs$set;
        private int connectTimeoutMs$value;
        private boolean connectTimeoutMsSupplier$set;
        private Supplier<Integer> connectTimeoutMsSupplier$value;
        private boolean readTimeoutMs$set;
        private int readTimeoutMs$value;
        private boolean readTimeoutMsSupplier$set;
        private Supplier<Integer> readTimeoutMsSupplier$value;
        private boolean retryPolicy$set;
        private RetryPolicy<HttpResponse> retryPolicy$value;
        private boolean streamBufferSize$set;
        private int streamBufferSize$value;

        public UrlConnectionHttpClient build() {
            RetryPolicy<HttpResponse> retryPolicy = this.retryPolicy$value;
            if (!this.retryPolicy$set) {
                retryPolicy = UrlConnectionHttpClient.$default$retryPolicy();
            }
            RetryPolicy<HttpResponse> retryPolicy2 = retryPolicy;
            int i = this.connectTimeoutMs$value;
            if (!this.connectTimeoutMs$set) {
                i = UrlConnectionHttpClient.$default$connectTimeoutMs();
            }
            int i2 = i;
            int i3 = this.readTimeoutMs$value;
            if (!this.readTimeoutMs$set) {
                i3 = UrlConnectionHttpClient.$default$readTimeoutMs();
            }
            int i4 = i3;
            Supplier<Integer> supplier = this.connectTimeoutMsSupplier$value;
            if (!this.connectTimeoutMsSupplier$set) {
                supplier = UrlConnectionHttpClient.$default$connectTimeoutMsSupplier();
            }
            Supplier<Integer> supplier2 = supplier;
            Supplier<Integer> supplier3 = this.readTimeoutMsSupplier$value;
            if (!this.readTimeoutMsSupplier$set) {
                supplier3 = UrlConnectionHttpClient.$default$readTimeoutMsSupplier();
            }
            Supplier<Integer> supplier4 = supplier3;
            int i7 = this.streamBufferSize$value;
            if (!this.streamBufferSize$set) {
                i7 = UrlConnectionHttpClient.$default$streamBufferSize();
            }
            return new UrlConnectionHttpClient(retryPolicy2, i2, i4, supplier2, supplier4, i7);
        }

        public UrlConnectionHttpClientBuilder connectTimeoutMs(int i) {
            this.connectTimeoutMs$value = i;
            this.connectTimeoutMs$set = true;
            return this;
        }

        public UrlConnectionHttpClientBuilder connectTimeoutMsSupplier(Supplier<Integer> supplier) {
            this.connectTimeoutMsSupplier$value = supplier;
            this.connectTimeoutMsSupplier$set = true;
            return this;
        }

        public UrlConnectionHttpClientBuilder readTimeoutMs(int i) {
            this.readTimeoutMs$value = i;
            this.readTimeoutMs$set = true;
            return this;
        }

        public UrlConnectionHttpClientBuilder readTimeoutMsSupplier(Supplier<Integer> supplier) {
            this.readTimeoutMsSupplier$value = supplier;
            this.readTimeoutMsSupplier$set = true;
            return this;
        }

        public UrlConnectionHttpClientBuilder retryPolicy(RetryPolicy<HttpResponse> retryPolicy) {
            this.retryPolicy$value = retryPolicy;
            this.retryPolicy$set = true;
            return this;
        }

        public UrlConnectionHttpClientBuilder streamBufferSize(int i) {
            this.streamBufferSize$value = i;
            this.streamBufferSize$set = true;
            return this;
        }

        public String toString() {
            StringBuilder sb = new StringBuilder("UrlConnectionHttpClient.UrlConnectionHttpClientBuilder(retryPolicy$value=");
            sb.append(this.retryPolicy$value);
            sb.append(", connectTimeoutMs$value=");
            sb.append(this.connectTimeoutMs$value);
            sb.append(", readTimeoutMs$value=");
            sb.append(this.readTimeoutMs$value);
            sb.append(", connectTimeoutMsSupplier$value=");
            sb.append(this.connectTimeoutMsSupplier$value);
            sb.append(", readTimeoutMsSupplier$value=");
            sb.append(this.readTimeoutMsSupplier$value);
            sb.append(", streamBufferSize$value=");
            return a.k(sb, this.streamBufferSize$value, ")");
        }
    }

    public static int $default$connectTimeoutMs() {
        return 30000;
    }

    public static Supplier<Integer> $default$connectTimeoutMsSupplier() {
        return null;
    }

    public static int $default$readTimeoutMs() {
        return 30000;
    }

    public static Supplier<Integer> $default$readTimeoutMsSupplier() {
        return null;
    }

    public static RetryPolicy<HttpResponse> $default$retryPolicy() {
        return new NoRetryPolicy();
    }

    public static int $default$streamBufferSize() {
        return 1024;
    }

    public UrlConnectionHttpClient(RetryPolicy<HttpResponse> retryPolicy, int i, int i2, Supplier<Integer> supplier, Supplier<Integer> supplier2, int i3) {
        this.retryPolicy = retryPolicy;
        this.connectTimeoutMs = i;
        this.readTimeoutMs = i2;
        this.connectTimeoutMsSupplier = supplier;
        this.readTimeoutMsSupplier = supplier2;
        this.streamBufferSize = i3;
    }

    public static UrlConnectionHttpClientBuilder builder() {
        return new UrlConnectionHttpClientBuilder();
    }

    private static HttpRequest constructHttpRequest(HttpClient.HttpMethod httpMethod, URL url, Map<String, String> map, byte[] bArr) {
        Map<String, String> map2;
        HttpClient.HttpMethod httpMethod2 = HttpClient.HttpMethod.PATCH;
        if (httpMethod2 == httpMethod) {
            httpMethod = HttpClient.HttpMethod.POST;
            HashMap hashMap = new HashMap(map);
            hashMap.put("X-HTTP-Method-Override", httpMethod2.name());
            map2 = hashMap;
        } else {
            map2 = map;
        }
        return new HttpRequest(url, map2, httpMethod.name(), bArr, null);
    }

    private String convertStreamToString(InputStream inputStream) {
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            char[] cArr = new char[this.streamBufferSize];
            StringBuilder sb = new StringBuilder();
            while (true) {
                int read = bufferedReader.read(cArr);
                if (read <= -1) {
                    return sb.toString();
                }
                sb.append(cArr, 0, read);
            }
        } finally {
            safeCloseStream(inputStream);
        }
    }

    public HttpResponse executeHttpSend(HttpRequest httpRequest, k0.a<HttpResponse> aVar) {
        InputStream errorStream;
        HttpURLConnection httpURLConnection = setupConnection(httpRequest);
        httpURLConnection.setRequestMethod(httpRequest.getRequestMethod());
        httpURLConnection.setUseCaches(true);
        setRequestBody(httpURLConnection, httpRequest.getRequestContent(), httpRequest.getmRequestHeaders().get("Content-Type"));
        try {
            try {
                try {
                    errorStream = httpURLConnection.getInputStream();
                } catch (SocketTimeoutException e) {
                    throw e;
                }
            } catch (IOException unused) {
                errorStream = httpURLConnection.getErrorStream();
            }
            try {
                HttpResponse httpResponse = new HttpResponse(new Date(httpURLConnection.getDate()), httpURLConnection.getResponseCode(), errorStream == null ? "" : convertStreamToString(errorStream), httpURLConnection.getHeaderFields());
                aVar.accept(httpResponse);
                safeCloseStream(errorStream);
                return httpResponse;
            } catch (Throwable th) {
                th = th;
                aVar.accept((Object) null);
                safeCloseStream(errorStream);
                throw th;
            }
        } catch (Throwable th2) {
            th = th2;
            errorStream = null;
            aVar.accept((Object) null);
            safeCloseStream(errorStream);
            throw th;
        }
    }

    private Integer getConnectTimeoutMs() {
        Supplier<Integer> supplier = this.connectTimeoutMsSupplier;
        return Integer.valueOf(supplier == null ? this.connectTimeoutMs : supplier.get().intValue());
    }

    public static UrlConnectionHttpClient getDefaultInstance() {
        UrlConnectionHttpClient urlConnectionHttpClient = defaultReference.get();
        if (urlConnectionHttpClient != null) {
            return urlConnectionHttpClient;
        }
        AtomicReference<UrlConnectionHttpClient> atomicReference = defaultReference;
        UrlConnectionHttpClient build = builder().build();
        while (!atomicReference.compareAndSet(null, build) && atomicReference.get() == null) {
        }
        return defaultReference.get();
    }

    private Integer getReadTimeoutMs() {
        Supplier<Integer> supplier = this.readTimeoutMsSupplier;
        return Integer.valueOf(supplier == null ? this.readTimeoutMs : supplier.get().intValue());
    }

    public static void recordHttpTelemetryEventEnd(HttpResponse httpResponse) {
        HttpEndEvent httpEndEvent = new HttpEndEvent();
        if (httpResponse != null) {
            httpEndEvent.putStatusCode(httpResponse.getStatusCode());
        }
        Telemetry.emit(httpEndEvent);
    }

    private static void recordHttpTelemetryEventStart(String str, URL url, String str2) {
        Telemetry.emit(new HttpStartEvent().putMethod(str).putPath(url).putRequestIdHeader(str2));
    }

    private static void safeCloseStream(Closeable closeable) {
        if (closeable == null) {
            return;
        }
        try {
            closeable.close();
        } catch (IOException unused) {
        }
    }

    private static void setRequestBody(HttpURLConnection httpURLConnection, byte[] bArr, String str) {
        OutputStream outputStream;
        if (bArr == null) {
            return;
        }
        httpURLConnection.setDoOutput(true);
        if (!StringUtil.isEmpty(str)) {
            httpURLConnection.setRequestProperty("Content-Type", str);
        }
        httpURLConnection.setRequestProperty("Content-Length", String.valueOf(bArr.length));
        try {
            outputStream = httpURLConnection.getOutputStream();
            try {
                outputStream.write(bArr);
                safeCloseStream(outputStream);
            } catch (Throwable th) {
                th = th;
                safeCloseStream(outputStream);
                throw th;
            }
        } catch (Throwable th2) {
            th = th2;
            outputStream = null;
        }
    }

    private HttpURLConnection setupConnection(HttpRequest httpRequest) {
        HttpURLConnection createHttpURLConnection = HttpUrlConnectionFactory.createHttpURLConnection(httpRequest.getRequestUrl());
        for (Map.Entry<String, String> entry : httpRequest.getmRequestHeaders().entrySet()) {
            createHttpURLConnection.setRequestProperty(entry.getKey(), entry.getValue());
        }
        createHttpURLConnection.setConnectTimeout(getConnectTimeoutMs().intValue());
        createHttpURLConnection.setReadTimeout(getReadTimeoutMs().intValue());
        createHttpURLConnection.setInstanceFollowRedirects(true);
        createHttpURLConnection.setUseCaches(false);
        createHttpURLConnection.setDoInput(true);
        return createHttpURLConnection;
    }

    public static <T> Supplier<T> supplierOf(final T t) {
        return new Supplier<T>() {
            @Override
            public T get() {
                return (T) t;
            }
        };
    }

    @Override
    public HttpResponse delete(URL url, Map<String, String> map, byte[] bArr) {
        return method(HttpClient.HttpMethod.POST.name(), url, map, bArr);
    }

    @Override
    public HttpResponse get(URL url, Map<String, String> map) {
        return method(HttpClient.HttpMethod.GET.name(), url, map, (byte[]) null);
    }

    @Override
    public HttpResponse head(URL url, Map<String, String> map) {
        return method(HttpClient.HttpMethod.HEAD.name(), url, map, (byte[]) null);
    }

    @Override
    public HttpResponse method(String str, URL url, Map<String, String> map, byte[] bArr) {
        return method(HttpClient.HttpMethod.validateAndNormalizeMethod(str), url, map, bArr);
    }

    @Override
    public HttpResponse options(URL url, Map<String, String> map) {
        return method(HttpClient.HttpMethod.OPTIONS.name(), url, map, (byte[]) null);
    }

    @Override
    public HttpResponse patch(URL url, Map<String, String> map, byte[] bArr) {
        return method(HttpClient.HttpMethod.PATCH.name(), url, map, bArr);
    }

    @Override
    public HttpResponse post(URL url, Map<String, String> map, byte[] bArr) {
        return method(HttpClient.HttpMethod.POST.name(), url, map, bArr);
    }

    @Override
    public HttpResponse put(URL url, Map<String, String> map, byte[] bArr) {
        return method(HttpClient.HttpMethod.PUT.name(), url, map, bArr);
    }

    @Override
    public HttpResponse trace(URL url, Map<String, String> map) {
        return method(HttpClient.HttpMethod.TRACE.name(), url, map, (byte[]) null);
    }

    @Override
    public HttpResponse method(HttpClient.HttpMethod httpMethod, URL url, Map<String, String> map, byte[] bArr) {
        recordHttpTelemetryEventStart(httpMethod.name(), url, map.get("client-request-id"));
        final HttpRequest constructHttpRequest = constructHttpRequest(httpMethod, url, map, bArr);
        return this.retryPolicy.attempt(new Callable<HttpResponse>() {
            @Override
            public HttpResponse call() {
                return UrlConnectionHttpClient.this.executeHttpSend(constructHttpRequest, new k0.a<HttpResponse>() {
                    public void accept(HttpResponse httpResponse) {
                        UrlConnectionHttpClient.recordHttpTelemetryEventEnd(httpResponse);
                    }
                });
            }
        });
    }
}