package com.meizu.cloud.pushsdk.a.a; import android.content.Context; import android.text.TextUtils; import android.util.Base64; import com.heytap.mcssdk.a.a; import com.meizu.cloud.pushinternal.DebugLogger; import com.netease.nim.highavailable.lava.base.http.HttpHeaders; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.HashMap; import java.util.Map; import java.util.zip.GZIPOutputStream; import org.json.JSONException; import org.json.JSONObject; public class b { private static final String a = "b"; private static final Object b = new Object(); private static b c; /* renamed from: d reason: collision with root package name */ private Context f1522d; private b(Context context) { this.f1522d = context; try { System.setProperty("sun.net.http.allowRestrictedHeaders", "true"); } catch (Exception e) { e.printStackTrace(); } a.a(context); } public static b a(Context context) { if (c == null) { synchronized (b) { if (c == null) { c = new b(context); } } } return c; } private Map<String, String> a(Map<String, String> map) { if (map == null) { map = new HashMap<>(); } byte[] c2 = a.a().c(); if (c2 == null || c2.length <= 0) { byte[] b2 = a.a().b(); if (b2 != null && b2.length > 0) { String str = new String(a.a().b()); String str2 = a; DebugLogger.d(str2, "attach x_a_key: " + str); map.put("X-A-Key", str); } } else { String str3 = new String(c2); String str4 = a; DebugLogger.d(str4, "attach x_s_key: " + str3); map.put("X-S-Key", str3); } return map; } /* JADX WARNING: Removed duplicated region for block: B:12:0x001a A[SYNTHETIC, Splitter:B:12:0x001a] */ private void a(HttpURLConnection httpURLConnection, byte[] bArr) throws IOException { Throwable th; GZIPOutputStream gZIPOutputStream = null; try { GZIPOutputStream gZIPOutputStream2 = new GZIPOutputStream(httpURLConnection.getOutputStream()); try { gZIPOutputStream2.write(bArr); gZIPOutputStream2.flush(); try { gZIPOutputStream2.close(); } catch (Exception unused) { } } catch (Throwable th2) { th = th2; gZIPOutputStream = gZIPOutputStream2; if (gZIPOutputStream != null) { try { gZIPOutputStream.close(); } catch (Exception unused2) { } } throw th; } } catch (Throwable th3) { th = th3; if (gZIPOutputStream != null) { } throw th; } } private void a(URLConnection uRLConnection) { try { String headerField = uRLConnection.getHeaderField("X-S-Key"); String str = a; DebugLogger.d(str, "get x_s_key = " + headerField); if (!TextUtils.isEmpty(headerField)) { a.a().a(headerField); } } catch (NullPointerException unused) { } } private byte[] a(InputStream inputStream) throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); while (true) { try { int read = inputStream.read(); if (read == -1) { break; } byteArrayOutputStream.write(read); } finally { try { byteArrayOutputStream.close(); } catch (IOException unused) { } } } return byteArrayOutputStream.toByteArray(); } /* JADX WARNING: Code restructure failed: missing block: B:37:0x0101, code lost: if (r9 == null) goto L_0x0142; */ /* JADX WARNING: Code restructure failed: missing block: B:38:0x0103, code lost: r9.disconnect(); */ /* JADX WARNING: Code restructure failed: missing block: B:48:0x012c, code lost: if (r9 != null) goto L_0x0103; */ /* JADX WARNING: Removed duplicated region for block: B:45:0x0127 A[SYNTHETIC, Splitter:B:45:0x0127] */ /* JADX WARNING: Removed duplicated region for block: B:52:0x0133 A[SYNTHETIC, Splitter:B:52:0x0133] */ /* JADX WARNING: Removed duplicated region for block: B:56:0x013a */ private c b(String str, Map<String, String> map, String str2) throws IOException { Throwable th; Exception e; InputStream inputStream; byte[] bArr; c cVar = null; InputStream inputStream2 = null; cVar = null; cVar = null; cVar = null; if (str2 != null) { byte[] a2 = a.a().a(str2.getBytes()); String str3 = a2 != null ? new String(Base64.encode(a2, 2)) : null; try { HttpURLConnection httpURLConnection = (HttpURLConnection) new URL("http://norma-external-collect.meizu.com/push/android/external/add.do").openConnection(); httpURLConnection.setRequestMethod(str); httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); httpURLConnection.setUseCaches(false); httpURLConnection.setConnectTimeout(30000); httpURLConnection.setReadTimeout(30000); httpURLConnection.setRequestProperty("Connection", "keep-alive"); httpURLConnection.setRequestProperty("Charset", "UTF-8"); httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); httpURLConnection.setRequestProperty(HttpHeaders.CONTENT_ENCODING, "gzip"); if (map != null && map.size() > 0) { for (Map.Entry<String, String> entry : map.entrySet()) { httpURLConnection.setRequestProperty(entry.getKey(), entry.getValue()); } } try { a(httpURLConnection, str3.getBytes()); int responseCode = httpURLConnection.getResponseCode(); String str4 = a; DebugLogger.d(str4, "code = " + responseCode); a(httpURLConnection); b(httpURLConnection); inputStream = httpURLConnection.getInputStream(); if (inputStream != null) { try { bArr = a(inputStream); if (bArr != null) { String str5 = new String(bArr); DebugLogger.d(str4, "body = " + str5); try { new JSONObject(str5).getInt(a.j); } catch (JSONException e2) { e2.printStackTrace(); } } } catch (Exception e3) { e = e3; try { DebugLogger.e(a, "realStringPartRequest error " + e.getMessage()); if (inputStream != null) { } } catch (Throwable th2) { th = th2; inputStream2 = inputStream; if (inputStream2 != null) { } if (httpURLConnection != null) { } throw th; } } } else { bArr = null; } cVar = bArr != null ? new c(responseCode, new String(bArr)) : new c(responseCode, null); if (inputStream != null) { try { inputStream.close(); } catch (IOException unused) { } } } catch (Exception e4) { e = e4; inputStream = null; DebugLogger.e(a, "realStringPartRequest error " + e.getMessage()); if (inputStream != null) { try { inputStream.close(); } catch (IOException unused2) { } } } catch (Throwable th3) { th = th3; if (inputStream2 != null) { try { inputStream2.close(); } catch (IOException unused3) { } } if (httpURLConnection != null) { httpURLConnection.disconnect(); } throw th; } } catch (MalformedURLException e5) { e5.printStackTrace(); } } return cVar; } private void b(URLConnection uRLConnection) { try { String headerField = uRLConnection.getHeaderField("Key-Timeout"); String str = a; DebugLogger.d(str, "get keyTimeout = " + headerField); } catch (NullPointerException unused) { } } public c a(String str, Map<String, String> map, String str2) { try { return b(str, a(map), str2); } catch (IOException e) { e.printStackTrace(); return null; } } }