package com.mbridge.msdk.playercommon.exoplayer2.drm; import android.net.Uri; import android.text.TextUtils; import com.baidu.speech.utils.auth.HttpClientUtil; import com.mbridge.msdk.playercommon.exoplayer2.C; import com.mbridge.msdk.playercommon.exoplayer2.drm.ExoMediaDrm; import com.mbridge.msdk.playercommon.exoplayer2.upstream.DataSourceInputStream; import com.mbridge.msdk.playercommon.exoplayer2.upstream.DataSpec; import com.mbridge.msdk.playercommon.exoplayer2.upstream.HttpDataSource; import com.mbridge.msdk.playercommon.exoplayer2.util.Assertions; import com.mbridge.msdk.playercommon.exoplayer2.util.Util; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; public final class HttpMediaDrmCallback implements MediaDrmCallback { private static final int MAX_MANUAL_REDIRECTS = 5; private final HttpDataSource.Factory dataSourceFactory; private final String defaultLicenseUrl; private final boolean forceDefaultLicenseUrl; private final Map<String, String> keyRequestProperties; public HttpMediaDrmCallback(String str, HttpDataSource.Factory factory) { this(str, false, factory); } public HttpMediaDrmCallback(String str, boolean z, HttpDataSource.Factory factory) { this.dataSourceFactory = factory; this.defaultLicenseUrl = str; this.forceDefaultLicenseUrl = z; this.keyRequestProperties = new HashMap(); } public final void setKeyRequestProperty(String str, String str2) { Assertions.checkNotNull(str); Assertions.checkNotNull(str2); synchronized (this.keyRequestProperties) { this.keyRequestProperties.put(str, str2); } } public final void clearKeyRequestProperty(String str) { Assertions.checkNotNull(str); synchronized (this.keyRequestProperties) { this.keyRequestProperties.remove(str); } } public final void clearAllKeyRequestProperties() { synchronized (this.keyRequestProperties) { this.keyRequestProperties.clear(); } } @Override // com.mbridge.msdk.playercommon.exoplayer2.drm.MediaDrmCallback public final byte[] executeProvisionRequest(UUID uuid, ExoMediaDrm.ProvisionRequest provisionRequest) throws IOException { return executePost(this.dataSourceFactory, provisionRequest.getDefaultUrl() + "&signedRequest=" + Util.fromUtf8Bytes(provisionRequest.getData()), new byte[0], null); } @Override // com.mbridge.msdk.playercommon.exoplayer2.drm.MediaDrmCallback public final byte[] executeKeyRequest(UUID uuid, ExoMediaDrm.KeyRequest keyRequest, String str) throws Exception { String str2; String defaultUrl = keyRequest.getDefaultUrl(); if (!TextUtils.isEmpty(defaultUrl)) { str = defaultUrl; } if (this.forceDefaultLicenseUrl || TextUtils.isEmpty(str)) { str = this.defaultLicenseUrl; } HashMap hashMap = new HashMap(); if (C.PLAYREADY_UUID.equals(uuid)) { str2 = "text/xml"; } else { str2 = C.CLEARKEY_UUID.equals(uuid) ? "application/json" : HttpClientUtil.APPLICATION_OCTET_STREAM; } hashMap.put("Content-Type", str2); if (C.PLAYREADY_UUID.equals(uuid)) { hashMap.put("SOAPAction", "http://schemas.microsoft.com/DRM/2007/03/protocols/AcquireLicense"); } synchronized (this.keyRequestProperties) { hashMap.putAll(this.keyRequestProperties); } return executePost(this.dataSourceFactory, str, keyRequest.getData(), hashMap); } /* JADX INFO: finally extract failed */ /* JADX WARNING: Removed duplicated region for block: B:26:0x006b A[Catch:{ InvalidResponseCodeException -> 0x0051, all -> 0x004f }] */ /* JADX WARNING: Removed duplicated region for block: B:27:0x0070 */ /* JADX WARNING: Removed duplicated region for block: B:29:0x0073 A[LOOP:1: B:7:0x002e->B:29:0x0073, LOOP_END] */ /* JADX WARNING: Removed duplicated region for block: B:35:0x007c A[SYNTHETIC] */ private static byte[] executePost(HttpDataSource.Factory factory, String str, byte[] bArr, Map<String, String> map) throws IOException { boolean z; int i; String redirectUrl; HttpDataSource createDataSource = factory.createDataSource(); if (map != null) { for (Map.Entry<String, String> entry : map.entrySet()) { createDataSource.setRequestProperty(entry.getKey(), entry.getValue()); } } String str2 = str; int i2 = 0; while (true) { DataSourceInputStream dataSourceInputStream = new DataSourceInputStream(createDataSource, new DataSpec(Uri.parse(str2), bArr, 0, 0, -1, null, 1)); try { byte[] byteArray = Util.toByteArray(dataSourceInputStream); Util.closeQuietly(dataSourceInputStream); return byteArray; } catch (HttpDataSource.InvalidResponseCodeException e) { if (e.responseCode == 307 || e.responseCode == 308) { i = i2 + 1; if (i2 < 5) { z = true; redirectUrl = !z ? getRedirectUrl(e) : null; if (redirectUrl == null) { Util.closeQuietly(dataSourceInputStream); i2 = i; str2 = redirectUrl; } else { throw e; } } else { i2 = i; } } i = i2; z = false; if (!z) { } if (redirectUrl == null) { } } catch (Throwable th) { Util.closeQuietly(dataSourceInputStream); throw th; } } } private static String getRedirectUrl(HttpDataSource.InvalidResponseCodeException invalidResponseCodeException) { List<String> list; Map<String, List<String>> map = invalidResponseCodeException.headerFields; if (map == null || (list = map.get("Location")) == null || list.isEmpty()) { return null; } return list.get(0); } }