package com.tencent.wxop.stat; import android.content.Context; import com.tencent.wxop.stat.common.StatLogger; import com.thoughtworks.xstream.XStream; import com.ubt.jimu.base.util.FileUtil; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.InputStream; import java.nio.ByteBuffer; import java.util.Arrays; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import java.util.zip.GZIPOutputStream; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ByteArrayEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.util.EntityUtils; import org.json.JSONObject; /* loaded from: classes.dex */ class i { private static StatLogger d = com.tencent.wxop.stat.common.l.b(); private static i e = null; private static Context f = null; DefaultHttpClient a; com.tencent.wxop.stat.common.e b; StringBuilder c = new StringBuilder(FileUtil.ZIP_BUFFER_SIZE); private long g; private i(Context context) { this.a = null; this.b = null; this.g = 0L; try { f = context.getApplicationContext(); this.g = System.currentTimeMillis() / 1000; this.b = new com.tencent.wxop.stat.common.e(); if (StatConfig.isDebugEnable()) { try { Logger.getLogger("org.apache.http.wire").setLevel(Level.FINER); Logger.getLogger("org.apache.http.headers").setLevel(Level.FINER); System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog"); System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true"); System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "debug"); System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "debug"); System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.headers", "debug"); } catch (Throwable unused) { } } BasicHttpParams basicHttpParams = new BasicHttpParams(); HttpConnectionParams.setStaleCheckingEnabled(basicHttpParams, false); HttpConnectionParams.setConnectionTimeout(basicHttpParams, XStream.PRIORITY_VERY_HIGH); HttpConnectionParams.setSoTimeout(basicHttpParams, XStream.PRIORITY_VERY_HIGH); this.a = new DefaultHttpClient(basicHttpParams); this.a.setKeepAliveStrategy(new j(this)); } catch (Throwable th) { d.e(th); } } static Context a() { return f; } static void a(Context context) { f = context.getApplicationContext(); } private void a(JSONObject jSONObject) { try { String optString = jSONObject.optString("mid"); if (com.tencent.a.a.a.a.h.b(optString)) { if (StatConfig.isDebugEnable()) { d.i("update mid:" + optString); } com.tencent.a.a.a.a.g.a(f).a(optString); } if (!jSONObject.isNull("cfg")) { StatConfig.a(f, jSONObject.getJSONObject("cfg")); } if (jSONObject.isNull("ncts")) { return; } int i = jSONObject.getInt("ncts"); int currentTimeMillis = (int) (i - (System.currentTimeMillis() / 1000)); if (StatConfig.isDebugEnable()) { d.i("server time:" + i + ", diff time:" + currentTimeMillis); } com.tencent.wxop.stat.common.l.x(f); com.tencent.wxop.stat.common.l.a(f, currentTimeMillis); } catch (Throwable th) { d.w(th); } } static i b(Context context) { if (e == null) { synchronized (i.class) { if (e == null) { e = new i(context); } } } return e; } void a(com.tencent.wxop.stat.event.e eVar, h hVar) { b(Arrays.asList(eVar.g()), hVar); } void a(List list, h hVar) { Throwable th; ByteArrayOutputStream byteArrayOutputStream; HttpResponse execute; HttpEntity entity; int statusCode; long contentLength; if (list == null || list.isEmpty()) { return; } int size = list.size(); list.get(0); try { this.c.delete(0, this.c.length()); this.c.append("["); for (int i = 0; i < size; i++) { this.c.append(list.get(i).toString()); if (i != size - 1) { this.c.append(","); } } this.c.append("]"); String sb = this.c.toString(); int length = sb.length(); String str = StatConfig.getStatReportUrl() + "/?index=" + this.g; this.g++; if (StatConfig.isDebugEnable()) { d.i("[" + str + "]Send request(" + length + "bytes), content:" + sb); } HttpPost httpPost = new HttpPost(str); httpPost.addHeader("Accept-Encoding", "gzip"); httpPost.setHeader("Connection", "Keep-Alive"); httpPost.removeHeaders("Cache-Control"); HttpHost a = a.a(f).a(); httpPost.addHeader("Content-Encoding", "rc4"); if (a == null) { this.a.getParams().removeParameter("http.route.default-proxy"); } else { if (StatConfig.isDebugEnable()) { d.d("proxy:" + a.toHostString()); } httpPost.addHeader("X-Content-Encoding", "rc4"); this.a.getParams().setParameter("http.route.default-proxy", a); httpPost.addHeader("X-Online-Host", StatConfig.k); httpPost.addHeader("Accept", "*/*"); httpPost.addHeader("Content-Type", "json"); } byteArrayOutputStream = new ByteArrayOutputStream(length); byte[] bytes = sb.getBytes("UTF-8"); int length2 = bytes.length; if (length > StatConfig.o) { httpPost.removeHeaders("Content-Encoding"); String str2 = "rc4,gzip"; httpPost.addHeader("Content-Encoding", str2); if (a != null) { httpPost.removeHeaders("X-Content-Encoding"); httpPost.addHeader("X-Content-Encoding", str2); } byteArrayOutputStream.write(new byte[4]); GZIPOutputStream gZIPOutputStream = new GZIPOutputStream(byteArrayOutputStream); gZIPOutputStream.write(bytes); gZIPOutputStream.close(); bytes = byteArrayOutputStream.toByteArray(); ByteBuffer.wrap(bytes, 0, 4).putInt(length2); if (StatConfig.isDebugEnable()) { d.d("before Gzip:" + length2 + " bytes, after Gzip:" + bytes.length + " bytes"); } } httpPost.setEntity(new ByteArrayEntity(com.tencent.wxop.stat.common.f.a(bytes))); execute = this.a.execute(httpPost); entity = execute.getEntity(); statusCode = execute.getStatusLine().getStatusCode(); contentLength = entity.getContentLength(); if (StatConfig.isDebugEnable()) { d.i("http recv response status code:" + statusCode + ", content length:" + contentLength); } } catch (Throwable th2) { th = th2; } if (contentLength <= 0) { d.e("Server response no data."); if (hVar != null) { hVar.b(); } EntityUtils.toString(entity); return; } if (contentLength > 0) { InputStream content = entity.getContent(); DataInputStream dataInputStream = new DataInputStream(content); byte[] bArr = new byte[(int) entity.getContentLength()]; dataInputStream.readFully(bArr); content.close(); dataInputStream.close(); Header firstHeader = execute.getFirstHeader("Content-Encoding"); if (firstHeader != null) { if (firstHeader.getValue().equalsIgnoreCase("gzip,rc4")) { bArr = com.tencent.wxop.stat.common.f.b(com.tencent.wxop.stat.common.l.a(bArr)); } else if (firstHeader.getValue().equalsIgnoreCase("rc4,gzip")) { bArr = com.tencent.wxop.stat.common.l.a(com.tencent.wxop.stat.common.f.b(bArr)); } else if (firstHeader.getValue().equalsIgnoreCase("gzip")) { bArr = com.tencent.wxop.stat.common.l.a(bArr); } else if (firstHeader.getValue().equalsIgnoreCase("rc4")) { bArr = com.tencent.wxop.stat.common.f.b(bArr); } } String str3 = new String(bArr, "UTF-8"); if (StatConfig.isDebugEnable()) { d.i("http get response data:" + str3); } JSONObject jSONObject = new JSONObject(str3); if (statusCode == 200) { a(jSONObject); if (hVar != null) { if (jSONObject.optInt("ret") == 0) { hVar.a(); } else { d.error("response error data."); hVar.b(); } } content.close(); } else { d.error("Server response error code:" + statusCode + ", error:" + new String(bArr, "UTF-8")); if (hVar != null) { hVar.b(); } content.close(); } } else { EntityUtils.toString(entity); } byteArrayOutputStream.close(); th = null; if (th != null) { d.error(th); if (hVar != null) { try { hVar.b(); } catch (Throwable th3) { d.e(th3); } } if (th instanceof OutOfMemoryError) { System.gc(); this.c = null; this.c = new StringBuilder(2048); } a.a(f).d(); } } void b(List list, h hVar) { com.tencent.wxop.stat.common.e eVar = this.b; if (eVar != null) { eVar.a(new k(this, list, hVar)); } } }