package com.qiniu.android.common; import android.os.Process; import com.qiniu.android.collect.LogHandler; import com.qiniu.android.common.Zone; import com.qiniu.android.http.Client; import com.qiniu.android.http.CompletionHandler; import com.qiniu.android.http.DnsPrefetcher; import com.qiniu.android.http.ResponseInfo; import com.qiniu.android.storage.UpToken; import com.qiniu.android.utils.UrlSafeBase64; import java.net.URI; import java.util.Iterator; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import org.json.JSONException; import org.json.JSONObject; public final class AutoZone extends Zone { private Client client = new Client(); private String ucServer; private Map<ZoneIndex, ZoneInfo> zones = new ConcurrentHashMap(); public static class ZoneIndex { public final String accessKey; public final String bucket; public ZoneIndex(String str, String str2) { this.accessKey = str; this.bucket = str2; } public static ZoneIndex getFromToken(String str) { String[] split = str.split(":"); try { return new ZoneIndex(split[0], new JSONObject(new String(UrlSafeBase64.decode(split[2]), "utf-8")).getString("scope").split(":")[0]); } catch (Exception e14) { e14.printStackTrace(); return null; } } public boolean equals(Object obj) { if (obj != this) { if (obj != null && (obj instanceof ZoneIndex)) { ZoneIndex zoneIndex = (ZoneIndex) obj; if (!zoneIndex.accessKey.equals(this.accessKey) || !zoneIndex.bucket.equals(this.bucket)) { return false; } } return false; } return true; } public int hashCode() { return (this.accessKey.hashCode() * 37) + this.bucket.hashCode(); } } public AutoZone(boolean z14) { if (z14) { this.ucServer = "https://uc.qbox.me"; } else { this.ucServer = "http://uc.qbox.me"; } } private void getZoneJsonAsync(LogHandler logHandler, ZoneIndex zoneIndex, CompletionHandler completionHandler) { this.client.asyncGet(logHandler, this.ucServer + "/v2/query?ak=" + zoneIndex.accessKey + "&bucket=" + zoneIndex.bucket, null, UpToken.NULL, completionHandler); } private ResponseInfo getZoneJsonSync(LogHandler logHandler, ZoneIndex zoneIndex) { return this.client.syncGet(logHandler, this.ucServer + "/v2/query?ak=" + zoneIndex.accessKey + "&bucket=" + zoneIndex.bucket, null); } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private void setTarget_region_id(ZoneInfo zoneInfo) { if (zoneInfo == null || zoneInfo.upDomainsList.size() <= 0) { return; } if (zoneInfo.upDomainsList.contains(FixedZone.arrayzone0[0])) { DnsPrefetcher.target_region_id = "z0"; } else if (zoneInfo.upDomainsList.contains(FixedZone.arrayzone1[0])) { DnsPrefetcher.target_region_id = "z1"; } else if (zoneInfo.upDomainsList.contains(FixedZone.arrayzone2[0])) { DnsPrefetcher.target_region_id = "z2"; } else if (zoneInfo.upDomainsList.contains(FixedZone.arrayZoneAs0[0])) { DnsPrefetcher.target_region_id = "as0"; } else if (zoneInfo.upDomainsList.contains(FixedZone.arrayzoneNa0[0])) { DnsPrefetcher.target_region_id = "na"; } } @Override // com.qiniu.android.common.Zone public synchronized void frozenDomain(String str) { if (str != null) { String host = URI.create(str).getHost(); ZoneInfo zoneInfo = null; Iterator<Map.Entry<ZoneIndex, ZoneInfo>> it3 = this.zones.entrySet().iterator(); while (true) { if (!it3.hasNext()) { break; } ZoneInfo value = it3.next().getValue(); if (value.upDomainsList.contains(host)) { zoneInfo = value; break; } } if (zoneInfo != null) { zoneInfo.frozenDomain(host); } } } @Override // com.qiniu.android.common.Zone public void preQuery(LogHandler logHandler, String str, Zone.QueryHandler queryHandler) { preQueryIndex(logHandler, ZoneIndex.getFromToken(str), queryHandler); } public void preQueryIndex(LogHandler logHandler, final ZoneIndex zoneIndex, final Zone.QueryHandler queryHandler) { if (zoneIndex == null) { queryHandler.onFailure(-5); return; } ZoneInfo zoneInfo = this.zones.get(zoneIndex); if (zoneInfo != null) { setTarget_region_id(zoneInfo); queryHandler.onSuccess(); return; } logHandler.send("tid", Long.valueOf((long) Process.myTid())); getZoneJsonAsync(logHandler, zoneIndex, new CompletionHandler() { /* class com.qiniu.android.common.AutoZone.AnonymousClass1 */ @Override // com.qiniu.android.http.CompletionHandler public void complete(ResponseInfo responseInfo, JSONObject jSONObject) { if (!responseInfo.isOK() || jSONObject == null) { queryHandler.onFailure(responseInfo.statusCode); return; } try { ZoneInfo buildFromJson = ZoneInfo.buildFromJson(jSONObject); AutoZone.this.setTarget_region_id(buildFromJson); AutoZone.this.zones.put(zoneIndex, buildFromJson); queryHandler.onSuccess(); } catch (JSONException e14) { e14.printStackTrace(); queryHandler.onFailure(-1); } } }); } public ZoneInfo queryByToken(String str) { try { String[] split = str.split(":"); return zoneInfo(split[0], new JSONObject(new String(UrlSafeBase64.decode(split[2]), "utf-8")).getString("scope").split(":")[0]); } catch (Exception e14) { e14.printStackTrace(); return null; } } @Override // com.qiniu.android.common.Zone public synchronized String upHost(String str, boolean z14, String str2) { ZoneInfo queryByToken = queryByToken(str); if (queryByToken == null) { return null; } return super.upHost(queryByToken, z14, str2); } public ZoneInfo zoneInfo(String str, String str2) { return this.zones.get(new ZoneIndex(str, str2)); } @Override // com.qiniu.android.common.Zone public boolean preQuery(LogHandler logHandler, String str) { return preQueryIndex(logHandler, ZoneIndex.getFromToken(str)); } public boolean preQueryIndex(LogHandler logHandler, ZoneIndex zoneIndex) { logHandler.send("tid", Long.valueOf((long) Process.myTid())); if (zoneIndex != null) { if (this.zones.get(zoneIndex) != null) { return true; } try { JSONObject jSONObject = getZoneJsonSync(logHandler, zoneIndex).response; if (jSONObject == null) { return false; } this.zones.put(zoneIndex, ZoneInfo.buildFromJson(jSONObject)); return true; } catch (JSONException e14) { e14.printStackTrace(); } } return false; } }