package com.meizu.cloud.pushsdk.a.a; import android.content.Context; import android.text.TextUtils; import android.util.Base64; import com.coloros.mcssdk.mode.CommandMessage; import com.meizu.cloud.pushinternal.DebugLogger; import com.yanzhenjie.kalle.Headers; 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 { /* renamed from: a reason: collision with root package name */ private static final String f5862a = "b"; private static final Object b = new Object(); private static b c; private Context d; private b(Context context) { this.d = 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 = f5862a; DebugLogger.d(str2, "attach x_a_key: " + str); map.put("X-A-Key", str); } } else { String str3 = new String(c2); String str4 = f5862a; 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 = f5862a; 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:36:0x0104, code lost: if (r9 == null) goto L_0x0145; */ /* JADX WARNING: Code restructure failed: missing block: B:37:0x0106, code lost: r9.disconnect(); */ /* JADX WARNING: Code restructure failed: missing block: B:48:0x0130, code lost: if (r9 != null) goto L_0x0106; */ /* JADX WARNING: Removed duplicated region for block: B:45:0x012b A[SYNTHETIC, Splitter:B:45:0x012b] */ /* JADX WARNING: Removed duplicated region for block: B:51:0x0136 A[SYNTHETIC, Splitter:B:51:0x0136] */ /* JADX WARNING: Removed duplicated region for block: B:55:0x013d */ private c b(String str, Map<String, String> map, String str2) throws IOException { InputStream inputStream; Throwable th; Exception e; byte[] bArr; c 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", Headers.VALUE_APPLICATION_URLENCODED); httpURLConnection.setRequestProperty("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(); DebugLogger.d(f5862a, "code = " + responseCode); a(httpURLConnection); b(httpURLConnection); inputStream = httpURLConnection.getInputStream(); if (inputStream != null) { try { bArr = a(inputStream); if (bArr != null) { String str4 = new String(bArr); DebugLogger.d(f5862a, "body = " + str4); try { new JSONObject(str4).getInt(CommandMessage.CODE); } catch (JSONException e2) { e2.printStackTrace(); } } } catch (Exception e3) { e = e3; try { DebugLogger.e(f5862a, "realStringPartRequest error " + e.getMessage()); if (inputStream != null) { } } catch (Throwable th2) { th = th2; if (inputStream != null) { try { inputStream.close(); } catch (IOException unused) { } } if (httpURLConnection != null) { httpURLConnection.disconnect(); } 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 unused2) { } } } catch (Exception e4) { e = e4; inputStream = null; DebugLogger.e(f5862a, "realStringPartRequest error " + e.getMessage()); if (inputStream != null) { try { inputStream.close(); } catch (IOException unused3) { } } } catch (Throwable th3) { th = th3; inputStream = null; if (inputStream != null) { } if (httpURLConnection != null) { } throw th; } } catch (MalformedURLException e5) { e5.printStackTrace(); } } return cVar; } private void b(URLConnection uRLConnection) { try { String headerField = uRLConnection.getHeaderField("Key-Timeout"); String str = f5862a; 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; } } }