正在查看: Petal Maps v4.7.0.310001 应用的 AuthUtil.java JAVA 源代码文件
本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。
正在查看: Petal Maps v4.7.0.310001 应用的 AuthUtil.java JAVA 源代码文件
本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。
package com.huawei.hiai.tts.network;
import android.content.Context;
import android.text.TextUtils;
import com.huawei.hiai.pdk.unifiedaccess.HttpConfig;
import com.huawei.hiai.tts.utils.TLog;
import com.huawei.secure.android.common.ssl.SecureSSLSocketFactory;
import com.huawei.secure.android.common.ssl.SecureSSLSocketFactoryNew;
import com.huawei.secure.android.common.ssl.SecureX509TrustManager;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.ConnectionPool;
import okhttp3.Dispatcher;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okio.ByteString;
import org.jetbrains.annotations.NotNull;
public class AuthUtil {
private static final int ALIVE_TIME = 5;
private static final long DEFAULT_CONNECTION_TIME_OUT = 500;
private static final long DEFAULT_READ_TIME_OUT = 5000;
private static final long DEFAULT_WRITE_TIME_OUT = 5000;
private static final int MAX_CONNECTION_POOL_NUM = 5;
private static final int PING_INTERVAL_SECONDS = 10;
private static final String TAG = "AuthUtil";
private WebSocket mWebSocket;
private OkHttpClient sOkHttpClient;
public static class SingleHolder {
private static final AuthUtil INSTANCE = new AuthUtil();
private SingleHolder() {
}
}
private Request.Builder addHeader(Map<String, String> map) {
Request.Builder builder = new Request.Builder();
if (map == null || map.isEmpty()) {
TLog.e(TAG, "post addHeader headerMap is null or empty");
return builder;
}
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if (key != null && value != null) {
try {
builder.addHeader(key, value);
} catch (IllegalArgumentException e) {
String message = e.getMessage();
if (message == null) {
TLog.d(TAG, "addHeader IllegalArgumentException: Unexpected char, errorMsg is null");
} else {
if (message.contains(value)) {
message = message.replace(value, "*");
}
TLog.e(TAG, "addHeader: " + message);
}
}
}
}
return builder;
}
private Optional<Request.Builder> addValidUrl(Request.Builder builder, String str) {
TLog.i(TAG, "addValidUrl url is empty: " + TextUtils.isEmpty(str));
if (builder != null) {
try {
if (!TextUtils.isEmpty(str)) {
return Optional.ofNullable(builder.url(str));
}
} catch (IllegalArgumentException unused) {
TLog.e(TAG, "addValidUrl IllegalArgumentException: Expected URL scheme 'http' or 'https'");
return Optional.empty();
}
}
return Optional.empty();
}
public static AuthUtil getInstance() {
return SingleHolder.INSTANCE;
}
public void cancelAll() {
this.sOkHttpClient.dispatcher().cancelAll();
}
public void cancelWs() {
WebSocket webSocket = this.mWebSocket;
if (webSocket != null) {
webSocket.cancel();
}
}
public void closeWs(int i, String str) {
WebSocket webSocket = this.mWebSocket;
if (webSocket != null) {
webSocket.close(i, str);
}
}
public void createOkHttpClient(Context context) {
try {
SecureX509TrustManager secureX509TrustManager = new SecureX509TrustManager(context);
OkHttpClient.Builder protocols = new OkHttpClient.Builder().sslSocketFactory(new SecureSSLSocketFactoryNew(secureX509TrustManager), secureX509TrustManager).hostnameVerifier(SecureSSLSocketFactory.STRICT_HOSTNAME_VERIFIER).protocols(Collections.unmodifiableList(Arrays.asList(Protocol.HTTP_1_1, Protocol.HTTP_2)));
TimeUnit timeUnit = TimeUnit.MILLISECONDS;
OkHttpClient.Builder dispatcher = protocols.connectTimeout(DEFAULT_CONNECTION_TIME_OUT, timeUnit).readTimeout(5000L, timeUnit).writeTimeout(5000L, timeUnit).retryOnConnectionFailure(true).dispatcher(new Dispatcher());
TimeUnit timeUnit2 = TimeUnit.SECONDS;
this.sOkHttpClient = dispatcher.connectionPool(new ConnectionPool(5, 5L, timeUnit2)).pingInterval(10L, timeUnit2).eventListener(new NetworkMeterEventListener()).build();
} catch (IOException e) {
TLog.d(TAG, "createOkHttpClient IOException: " + e.getMessage());
TLog.e(TAG, "createOkHttpClient IOException");
} catch (IllegalArgumentException e2) {
TLog.d(TAG, "createOkHttpClient IllegalAccessException: " + e2.getMessage());
TLog.e(TAG, "createOkHttpClient IllegalAccessException");
} catch (KeyManagementException e3) {
TLog.d(TAG, "createOkHttpClient KeyManagementException: " + e3.getMessage());
TLog.e(TAG, "createOkHttpClient KeyManagementException");
} catch (KeyStoreException e4) {
TLog.d(TAG, "createOkHttpClient KeyStoreException: " + e4.getMessage());
TLog.e(TAG, "createOkHttpClient KeyStoreException");
} catch (NoSuchAlgorithmException e5) {
TLog.d(TAG, "createOkHttpClient NoSuchAlgorithmException: " + e5.getMessage());
TLog.e(TAG, "createOkHttpClient NoSuchAlgorithmException");
} catch (CertificateException e6) {
TLog.d(TAG, "createOkHttpClient CertificateException: " + e6.getMessage());
TLog.e(TAG, "createOkHttpClient CertificateException");
}
}
public void evictAll() {
this.sOkHttpClient.connectionPool().evictAll();
}
public Response get(String str) throws IOException {
TLog.d(TAG, "pre connect before access");
Optional<Request.Builder> addValidUrl = addValidUrl(new Request.Builder(), str);
if (!addValidUrl.isPresent()) {
throw new IOException("Request builder is null");
}
return this.sOkHttpClient.newCall(addValidUrl.get().get().build()).execute();
}
public Response post(String str, String str2, Map<String, String> map, long j, boolean z) throws IOException {
MultipartBody create;
TLog.i(TAG, "POST start");
Optional<Request.Builder> addValidUrl = addValidUrl(addHeader(map), str);
if (!addValidUrl.isPresent()) {
throw new IOException("Request builder is null");
}
if (z) {
MultipartBody.Builder type = new MultipartBody.Builder("hivoice-boundary").setType(MultipartBody.FORM);
type.addPart(Headers.of(new String[]{HttpConfig.CONTENT_DISPOSITION, "form-data; name=\"json\""}), RequestBody.create(MediaType.parse(HttpConfig.APPLICATION_JSON), str2));
create = type.build();
} else {
create = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), str2);
}
Call newCall = this.sOkHttpClient.newCall(addValidUrl.get().post(create).build());
final CompletableFuture completableFuture = new CompletableFuture();
try {
newCall.enqueue(new Callback() {
public void onFailure(@NotNull Call call, @NotNull IOException iOException) {
TLog.e(AuthUtil.TAG, "call enqueue onFailure");
completableFuture.completeExceptionally(iOException);
}
public void onResponse(@NotNull Call call, @NotNull Response response) {
TLog.i(AuthUtil.TAG, "call enqueue onResponse");
completableFuture.complete(response);
}
});
Response response = (Response) completableFuture.get(j, TimeUnit.MILLISECONDS);
TLog.i(TAG, "POST complete, return value");
return response;
} catch (IllegalStateException e) {
e = e;
TLog.e(TAG, "call enqueue failed, throw exception for next step");
throw new IOException(e);
} catch (InterruptedException e2) {
e = e2;
TLog.e(TAG, "call enqueue failed, throw exception for next step");
throw new IOException(e);
} catch (ExecutionException e3) {
e = e3;
TLog.e(TAG, "call enqueue failed, throw exception for next step");
throw new IOException(e);
} catch (TimeoutException e4) {
TLog.e(TAG, "call enqueue failed, throw exception for next step after timeout: " + j);
throw new IOException(e4);
}
}
public boolean sendWs(String str, Map<String, String> map, String str2, WebSocketListener webSocketListener) {
TLog.i(TAG, "ws connect");
Optional<Request.Builder> addValidUrl = addValidUrl(addHeader(map), str);
if (!addValidUrl.isPresent()) {
return false;
}
WebSocket newWebSocket = this.sOkHttpClient.newWebSocket(addValidUrl.get().build(), webSocketListener);
this.mWebSocket = newWebSocket;
return newWebSocket.send(str2);
}
private AuthUtil() {
}
public boolean sendWs(ByteString byteString) {
WebSocket webSocket = this.mWebSocket;
if (webSocket == null) {
return false;
}
return webSocket.send(byteString);
}
public boolean sendWs(String str) {
WebSocket webSocket = this.mWebSocket;
if (webSocket == null) {
return false;
}
return webSocket.send(str);
}
}