com.google.android.exoplayer2.upstream.HttpDataSource.InvalidResponseCodeException Java Examples

The following examples show how to use com.google.android.exoplayer2.upstream.HttpDataSource.InvalidResponseCodeException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ChunkedTrackBlacklistUtil.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Blacklists {@code trackSelectionIndex} in {@code trackSelection} for
 * {@code blacklistDurationMs} if calling {@link #shouldBlacklist(Exception)} for {@code e}
 * returns true. Else does nothing. Note that blacklisting will fail if the track is the only
 * non-blacklisted track in the selection.
 *
 * @param trackSelection The track selection.
 * @param trackSelectionIndex The index in the selection to consider blacklisting.
 * @param e The error to inspect.
 * @param blacklistDurationMs The duration to blacklist the track for, if it is blacklisted.
 * @return Whether the track was blacklisted.
 */
public static boolean maybeBlacklistTrack(TrackSelection trackSelection, int trackSelectionIndex,
    Exception e, long blacklistDurationMs) {
  if (shouldBlacklist(e)) {
    boolean blacklisted = trackSelection.blacklist(trackSelectionIndex, blacklistDurationMs);
    int responseCode = ((InvalidResponseCodeException) e).responseCode;
    if (blacklisted) {
      Log.w(TAG, "Blacklisted: duration=" + blacklistDurationMs + ", responseCode="
          + responseCode + ", format=" + trackSelection.getFormat(trackSelectionIndex));
    } else {
      Log.w(TAG, "Blacklisting failed (cannot blacklist last enabled track): responseCode="
          + responseCode + ", format=" + trackSelection.getFormat(trackSelectionIndex));
    }
    return blacklisted;
  }
  return false;
}
 
Example #2
Source File: DefaultDashChunkSource.java    From K-Sonic with MIT License 6 votes vote down vote up
@Override
public boolean onChunkLoadError(Chunk chunk, boolean cancelable, Exception e) {
  if (!cancelable) {
    return false;
  }
  // Workaround for missing segment at the end of the period
  if (!manifest.dynamic && chunk instanceof MediaChunk
      && e instanceof InvalidResponseCodeException
      && ((InvalidResponseCodeException) e).responseCode == 404) {
    RepresentationHolder representationHolder =
        representationHolders[trackSelection.indexOf(chunk.trackFormat)];
    int segmentCount = representationHolder.getSegmentCount();
    if (segmentCount != DashSegmentIndex.INDEX_UNBOUNDED && segmentCount != 0) {
      int lastAvailableSegmentNum = representationHolder.getFirstSegmentNum() + segmentCount - 1;
      if (((MediaChunk) chunk).getNextChunkIndex() > lastAvailableSegmentNum) {
        missingLastSegment = true;
        return true;
      }
    }
  }
  // Blacklist if appropriate.
  return ChunkedTrackBlacklistUtil.maybeBlacklistTrack(trackSelection,
      trackSelection.indexOf(chunk.trackFormat), e);
}
 
Example #3
Source File: ChunkedTrackBlacklistUtil.java    From K-Sonic with MIT License 6 votes vote down vote up
/**
 * Blacklists {@code trackSelectionIndex} in {@code trackSelection} for
 * {@code blacklistDurationMs} if calling {@link #shouldBlacklist(Exception)} for {@code e}
 * returns true. Else does nothing. Note that blacklisting will fail if the track is the only
 * non-blacklisted track in the selection.
 *
 * @param trackSelection The track selection.
 * @param trackSelectionIndex The index in the selection to consider blacklisting.
 * @param e The error to inspect.
 * @param blacklistDurationMs The duration to blacklist the track for, if it is blacklisted.
 * @return Whether the track was blacklisted.
 */
public static boolean maybeBlacklistTrack(TrackSelection trackSelection, int trackSelectionIndex,
    Exception e, long blacklistDurationMs) {
  if (shouldBlacklist(e)) {
    boolean blacklisted = trackSelection.blacklist(trackSelectionIndex, blacklistDurationMs);
    int responseCode = ((InvalidResponseCodeException) e).responseCode;
    if (blacklisted) {
      Log.w(TAG, "Blacklisted: duration=" + blacklistDurationMs + ", responseCode="
          + responseCode + ", format=" + trackSelection.getFormat(trackSelectionIndex));
    } else {
      Log.w(TAG, "Blacklisting failed (cannot blacklist last enabled track): responseCode="
          + responseCode + ", format=" + trackSelection.getFormat(trackSelectionIndex));
    }
    return blacklisted;
  }
  return false;
}
 
Example #4
Source File: ChunkedTrackBlacklistUtil.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Blacklists {@code trackSelectionIndex} in {@code trackSelection} for
 * {@code blacklistDurationMs} if calling {@link #shouldBlacklist(Exception)} for {@code e}
 * returns true. Else does nothing. Note that blacklisting will fail if the track is the only
 * non-blacklisted track in the selection.
 *
 * @param trackSelection The track selection.
 * @param trackSelectionIndex The index in the selection to consider blacklisting.
 * @param e The error to inspect.
 * @param blacklistDurationMs The duration to blacklist the track for, if it is blacklisted.
 * @return Whether the track was blacklisted.
 */
public static boolean maybeBlacklistTrack(TrackSelection trackSelection, int trackSelectionIndex,
    Exception e, long blacklistDurationMs) {
  if (shouldBlacklist(e)) {
    boolean blacklisted = trackSelection.blacklist(trackSelectionIndex, blacklistDurationMs);
    int responseCode = ((InvalidResponseCodeException) e).responseCode;
    if (blacklisted) {
      Log.w(TAG, "Blacklisted: duration=" + blacklistDurationMs + ", responseCode="
          + responseCode + ", format=" + trackSelection.getFormat(trackSelectionIndex));
    } else {
      Log.w(TAG, "Blacklisting failed (cannot blacklist last enabled track): responseCode="
          + responseCode + ", format=" + trackSelection.getFormat(trackSelectionIndex));
    }
    return blacklisted;
  }
  return false;
}
 
Example #5
Source File: HttpMediaDrmCallback.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
private static @Nullable String getRedirectUrl(InvalidResponseCodeException exception) {
  Map<String, List<String>> headerFields = exception.headerFields;
  if (headerFields != null) {
    List<String> locationHeaders = headerFields.get("Location");
    if (locationHeaders != null && !locationHeaders.isEmpty()) {
      return locationHeaders.get(0);
    }
  }
  return null;
}
 
Example #6
Source File: DefaultLoadErrorHandlingPolicy.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Blacklists resources whose load error was an {@link InvalidResponseCodeException} with response
 * code HTTP 404 or 410. The duration of the blacklisting is {@link #DEFAULT_TRACK_BLACKLIST_MS}.
 */
@Override
public long getBlacklistDurationMsFor(
    int dataType, long loadDurationMs, IOException exception, int errorCount) {
  if (exception instanceof InvalidResponseCodeException) {
    int responseCode = ((InvalidResponseCodeException) exception).responseCode;
    return responseCode == 404 // HTTP 404 Not Found.
            || responseCode == 410 // HTTP 410 Gone.
        ? DEFAULT_TRACK_BLACKLIST_MS
        : C.TIME_UNSET;
  }
  return C.TIME_UNSET;
}
 
Example #7
Source File: DefaultDashChunkSource.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean onChunkLoadError(
    Chunk chunk, boolean cancelable, Exception e, long blacklistDurationMs) {
  if (!cancelable) {
    return false;
  }
  if (playerTrackEmsgHandler != null
      && playerTrackEmsgHandler.maybeRefreshManifestOnLoadingError(chunk)) {
    return true;
  }
  // Workaround for missing segment at the end of the period
  if (!manifest.dynamic && chunk instanceof MediaChunk
      && e instanceof InvalidResponseCodeException
      && ((InvalidResponseCodeException) e).responseCode == 404) {
    RepresentationHolder representationHolder =
        representationHolders[trackSelection.indexOf(chunk.trackFormat)];
    int segmentCount = representationHolder.getSegmentCount();
    if (segmentCount != DashSegmentIndex.INDEX_UNBOUNDED && segmentCount != 0) {
      long lastAvailableSegmentNum = representationHolder.getFirstSegmentNum() + segmentCount - 1;
      if (((MediaChunk) chunk).getNextChunkIndex() > lastAvailableSegmentNum) {
        missingLastSegment = true;
        return true;
      }
    }
  }
  return blacklistDurationMs != C.TIME_UNSET
      && trackSelection.blacklist(trackSelection.indexOf(chunk.trackFormat), blacklistDurationMs);
}
 
Example #8
Source File: HttpMediaDrmCallback.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
private static @Nullable String getRedirectUrl(InvalidResponseCodeException exception) {
  Map<String, List<String>> headerFields = exception.headerFields;
  if (headerFields != null) {
    List<String> locationHeaders = headerFields.get("Location");
    if (locationHeaders != null && !locationHeaders.isEmpty()) {
      return locationHeaders.get(0);
    }
  }
  return null;
}
 
Example #9
Source File: DefaultLoadErrorHandlingPolicy.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Blacklists resources whose load error was an {@link InvalidResponseCodeException} with response
 * code HTTP 404 or 410. The duration of the blacklisting is {@link #DEFAULT_TRACK_BLACKLIST_MS}.
 */
@Override
public long getBlacklistDurationMsFor(
    int dataType, long loadDurationMs, IOException exception, int errorCount) {
  if (exception instanceof InvalidResponseCodeException) {
    int responseCode = ((InvalidResponseCodeException) exception).responseCode;
    return responseCode == 404 // HTTP 404 Not Found.
            || responseCode == 410 // HTTP 410 Gone.
        ? DEFAULT_TRACK_BLACKLIST_MS
        : C.TIME_UNSET;
  }
  return C.TIME_UNSET;
}
 
Example #10
Source File: DefaultDashChunkSource.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean onChunkLoadError(
    Chunk chunk, boolean cancelable, Exception e, long blacklistDurationMs) {
  if (!cancelable) {
    return false;
  }
  if (playerTrackEmsgHandler != null
      && playerTrackEmsgHandler.maybeRefreshManifestOnLoadingError(chunk)) {
    return true;
  }
  // Workaround for missing segment at the end of the period
  if (!manifest.dynamic && chunk instanceof MediaChunk
      && e instanceof InvalidResponseCodeException
      && ((InvalidResponseCodeException) e).responseCode == 404) {
    RepresentationHolder representationHolder =
        representationHolders[trackSelection.indexOf(chunk.trackFormat)];
    int segmentCount = representationHolder.getSegmentCount();
    if (segmentCount != DashSegmentIndex.INDEX_UNBOUNDED && segmentCount != 0) {
      long lastAvailableSegmentNum = representationHolder.getFirstSegmentNum() + segmentCount - 1;
      if (((MediaChunk) chunk).getNextChunkIndex() > lastAvailableSegmentNum) {
        missingLastSegment = true;
        return true;
      }
    }
  }
  return blacklistDurationMs != C.TIME_UNSET
      && trackSelection.blacklist(trackSelection.indexOf(chunk.trackFormat), blacklistDurationMs);
}
 
Example #11
Source File: HttpMediaDrmCallback.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
private static @Nullable String getRedirectUrl(InvalidResponseCodeException exception) {
  Map<String, List<String>> headerFields = exception.headerFields;
  if (headerFields != null) {
    List<String> locationHeaders = headerFields.get("Location");
    if (locationHeaders != null && !locationHeaders.isEmpty()) {
      return locationHeaders.get(0);
    }
  }
  return null;
}
 
Example #12
Source File: LoadErrorHandlingPolicy.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Blacklists resources whose load error was an {@link InvalidResponseCodeException} with
 * response code HTTP 404 or 410. The duration of the blacklisting is {@link
 * ChunkedTrackBlacklistUtil#DEFAULT_TRACK_BLACKLIST_MS}.
 */
@Override
public long getBlacklistDurationMsFor(
    Loadable loadable, long loadDurationMs, IOException exception, int errorCount) {
  if (exception instanceof InvalidResponseCodeException) {
    int responseCode = ((InvalidResponseCodeException) exception).responseCode;
    return responseCode == 404 // HTTP 404 Not Found.
            || responseCode == 410 // HTTP 410 Gone.
        ? ChunkedTrackBlacklistUtil.DEFAULT_TRACK_BLACKLIST_MS
        : C.TIME_UNSET;
  }
  return C.TIME_UNSET;
}
 
Example #13
Source File: DefaultDashChunkSource.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean onChunkLoadError(Chunk chunk, boolean cancelable, Exception e) {
  if (!cancelable) {
    return false;
  }
  if (playerTrackEmsgHandler != null
      && playerTrackEmsgHandler.maybeRefreshManifestOnLoadingError(chunk)) {
    return true;
  }
  // Workaround for missing segment at the end of the period
  if (!manifest.dynamic && chunk instanceof MediaChunk
      && e instanceof InvalidResponseCodeException
      && ((InvalidResponseCodeException) e).responseCode == 404) {
    RepresentationHolder representationHolder =
        representationHolders[trackSelection.indexOf(chunk.trackFormat)];
    int segmentCount = representationHolder.getSegmentCount();
    if (segmentCount != DashSegmentIndex.INDEX_UNBOUNDED && segmentCount != 0) {
      long lastAvailableSegmentNum = representationHolder.getFirstSegmentNum() + segmentCount - 1;
      if (((MediaChunk) chunk).getNextChunkIndex() > lastAvailableSegmentNum) {
        missingLastSegment = true;
        return true;
      }
    }
  }
  // Blacklist if appropriate.
  return ChunkedTrackBlacklistUtil.maybeBlacklistTrack(trackSelection,
      trackSelection.indexOf(chunk.trackFormat), e);
}
 
Example #14
Source File: HttpMediaDrmCallback.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private static String getRedirectUrl(InvalidResponseCodeException exception) {
  Map<String, List<String>> headerFields = exception.headerFields;
  if (headerFields != null) {
    List<String> locationHeaders = headerFields.get("Location");
    if (locationHeaders != null && !locationHeaders.isEmpty()) {
      return locationHeaders.get(0);
    }
  }
  return null;
}
 
Example #15
Source File: HttpMediaDrmCallback.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private static byte[] executePost(HttpDataSource.Factory dataSourceFactory, String url,
    byte[] data, Map<String, String> requestProperties) throws IOException {
  HttpDataSource dataSource = dataSourceFactory.createDataSource();
  if (requestProperties != null) {
    for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) {
      dataSource.setRequestProperty(requestProperty.getKey(), requestProperty.getValue());
    }
  }

  int manualRedirectCount = 0;
  while (true) {
    DataSpec dataSpec =
        new DataSpec(
            Uri.parse(url),
            data,
            /* absoluteStreamPosition= */ 0,
            /* position= */ 0,
            /* length= */ C.LENGTH_UNSET,
            /* key= */ null,
            DataSpec.FLAG_ALLOW_GZIP);
    DataSourceInputStream inputStream = new DataSourceInputStream(dataSource, dataSpec);
    try {
      return Util.toByteArray(inputStream);
    } catch (InvalidResponseCodeException e) {
      // For POST requests, the underlying network stack will not normally follow 307 or 308
      // redirects automatically. Do so manually here.
      boolean manuallyRedirect =
          (e.responseCode == 307 || e.responseCode == 308)
              && manualRedirectCount++ < MAX_MANUAL_REDIRECTS;
      url = manuallyRedirect ? getRedirectUrl(e) : null;
      if (url == null) {
        throw e;
      }
    } finally {
      Util.closeQuietly(inputStream);
    }
  }
}
 
Example #16
Source File: LoadErrorHandlingPolicy.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Blacklists resources whose load error was an {@link InvalidResponseCodeException} with
 * response code HTTP 404 or 410. The duration of the blacklisting is {@link
 * ChunkedTrackBlacklistUtil#DEFAULT_TRACK_BLACKLIST_MS}.
 */
@Override
public long getBlacklistDurationMsFor(
    Loadable loadable, long loadDurationMs, IOException exception, int errorCount) {
  if (exception instanceof InvalidResponseCodeException) {
    int responseCode = ((InvalidResponseCodeException) exception).responseCode;
    return responseCode == 404 // HTTP 404 Not Found.
            || responseCode == 410 // HTTP 410 Gone.
        ? ChunkedTrackBlacklistUtil.DEFAULT_TRACK_BLACKLIST_MS
        : C.TIME_UNSET;
  }
  return C.TIME_UNSET;
}
 
Example #17
Source File: DefaultDashChunkSource.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean onChunkLoadError(Chunk chunk, boolean cancelable, Exception e) {
  if (!cancelable) {
    return false;
  }
  if (playerTrackEmsgHandler != null
      && playerTrackEmsgHandler.maybeRefreshManifestOnLoadingError(chunk)) {
    return true;
  }
  // Workaround for missing segment at the end of the period
  if (!manifest.dynamic && chunk instanceof MediaChunk
      && e instanceof InvalidResponseCodeException
      && ((InvalidResponseCodeException) e).responseCode == 404) {
    RepresentationHolder representationHolder =
        representationHolders[trackSelection.indexOf(chunk.trackFormat)];
    int segmentCount = representationHolder.getSegmentCount();
    if (segmentCount != DashSegmentIndex.INDEX_UNBOUNDED && segmentCount != 0) {
      long lastAvailableSegmentNum = representationHolder.getFirstSegmentNum() + segmentCount - 1;
      if (((MediaChunk) chunk).getNextChunkIndex() > lastAvailableSegmentNum) {
        missingLastSegment = true;
        return true;
      }
    }
  }
  // Blacklist if appropriate.
  return ChunkedTrackBlacklistUtil.maybeBlacklistTrack(trackSelection,
      trackSelection.indexOf(chunk.trackFormat), e);
}
 
Example #18
Source File: HttpMediaDrmCallback.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private static String getRedirectUrl(InvalidResponseCodeException exception) {
  Map<String, List<String>> headerFields = exception.headerFields;
  if (headerFields != null) {
    List<String> locationHeaders = headerFields.get("Location");
    if (locationHeaders != null && !locationHeaders.isEmpty()) {
      return locationHeaders.get(0);
    }
  }
  return null;
}
 
Example #19
Source File: HttpMediaDrmCallback.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private static byte[] executePost(HttpDataSource.Factory dataSourceFactory, String url,
    byte[] data, Map<String, String> requestProperties) throws IOException {
  HttpDataSource dataSource = dataSourceFactory.createDataSource();
  if (requestProperties != null) {
    for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) {
      dataSource.setRequestProperty(requestProperty.getKey(), requestProperty.getValue());
    }
  }

  int manualRedirectCount = 0;
  while (true) {
    DataSpec dataSpec =
        new DataSpec(
            Uri.parse(url),
            data,
            /* absoluteStreamPosition= */ 0,
            /* position= */ 0,
            /* length= */ C.LENGTH_UNSET,
            /* key= */ null,
            DataSpec.FLAG_ALLOW_GZIP);
    DataSourceInputStream inputStream = new DataSourceInputStream(dataSource, dataSpec);
    try {
      return Util.toByteArray(inputStream);
    } catch (InvalidResponseCodeException e) {
      // For POST requests, the underlying network stack will not normally follow 307 or 308
      // redirects automatically. Do so manually here.
      boolean manuallyRedirect =
          (e.responseCode == 307 || e.responseCode == 308)
              && manualRedirectCount++ < MAX_MANUAL_REDIRECTS;
      url = manuallyRedirect ? getRedirectUrl(e) : null;
      if (url == null) {
        throw e;
      }
    } finally {
      Util.closeQuietly(inputStream);
    }
  }
}
 
Example #20
Source File: DefaultLoadErrorHandlingPolicy.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
/**
 * Blacklists resources whose load error was an {@link InvalidResponseCodeException} with response
 * code HTTP 404 or 410. The duration of the blacklisting is {@link #DEFAULT_TRACK_BLACKLIST_MS}.
 */
@Override
public long getBlacklistDurationMsFor(
    int dataType, long loadDurationMs, IOException exception, int errorCount) {
  if (exception instanceof InvalidResponseCodeException) {
    int responseCode = ((InvalidResponseCodeException) exception).responseCode;
    return responseCode == 404 // HTTP 404 Not Found.
            || responseCode == 410 // HTTP 410 Gone.
            || responseCode == 416 // HTTP 416 Range Not Satisfiable.
        ? DEFAULT_TRACK_BLACKLIST_MS
        : C.TIME_UNSET;
  }
  return C.TIME_UNSET;
}
 
Example #21
Source File: DefaultDashChunkSource.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onChunkLoadError(
    Chunk chunk, boolean cancelable, Exception e, long blacklistDurationMs) {
  if (!cancelable) {
    return false;
  }
  if (playerTrackEmsgHandler != null
      && playerTrackEmsgHandler.maybeRefreshManifestOnLoadingError(chunk)) {
    return true;
  }
  // Workaround for missing segment at the end of the period
  if (!manifest.dynamic && chunk instanceof MediaChunk
      && e instanceof InvalidResponseCodeException
      && ((InvalidResponseCodeException) e).responseCode == 404) {
    RepresentationHolder representationHolder =
        representationHolders[trackSelection.indexOf(chunk.trackFormat)];
    int segmentCount = representationHolder.getSegmentCount();
    if (segmentCount != DashSegmentIndex.INDEX_UNBOUNDED && segmentCount != 0) {
      long lastAvailableSegmentNum = representationHolder.getFirstSegmentNum() + segmentCount - 1;
      if (((MediaChunk) chunk).getNextChunkIndex() > lastAvailableSegmentNum) {
        missingLastSegment = true;
        return true;
      }
    }
  }
  return blacklistDurationMs != C.TIME_UNSET
      && trackSelection.blacklist(trackSelection.indexOf(chunk.trackFormat), blacklistDurationMs);
}
 
Example #22
Source File: HttpMediaDrmCallback.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
private static byte[] executePost(
    HttpDataSource.Factory dataSourceFactory,
    String url,
    byte[] data,
    @Nullable Map<String, String> requestProperties)
    throws IOException {
  HttpDataSource dataSource = dataSourceFactory.createDataSource();
  if (requestProperties != null) {
    for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) {
      dataSource.setRequestProperty(requestProperty.getKey(), requestProperty.getValue());
    }
  }

  int manualRedirectCount = 0;
  while (true) {
    DataSpec dataSpec =
        new DataSpec(
            Uri.parse(url),
            data,
            /* absoluteStreamPosition= */ 0,
            /* position= */ 0,
            /* length= */ C.LENGTH_UNSET,
            /* key= */ null,
            DataSpec.FLAG_ALLOW_GZIP);
    DataSourceInputStream inputStream = new DataSourceInputStream(dataSource, dataSpec);
    try {
      return Util.toByteArray(inputStream);
    } catch (InvalidResponseCodeException e) {
      // For POST requests, the underlying network stack will not normally follow 307 or 308
      // redirects automatically. Do so manually here.
      boolean manuallyRedirect =
          (e.responseCode == 307 || e.responseCode == 308)
              && manualRedirectCount++ < MAX_MANUAL_REDIRECTS;
      String redirectUrl = manuallyRedirect ? getRedirectUrl(e) : null;
      if (redirectUrl == null) {
        throw e;
      }
      url = redirectUrl;
    } finally {
      Util.closeQuietly(inputStream);
    }
  }
}
 
Example #23
Source File: HttpMediaDrmCallback.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
private static byte[] executePost(
    HttpDataSource.Factory dataSourceFactory,
    String url,
    byte[] data,
    @Nullable Map<String, String> requestProperties)
    throws IOException {
  HttpDataSource dataSource = dataSourceFactory.createDataSource();
  if (requestProperties != null) {
    for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) {
      dataSource.setRequestProperty(requestProperty.getKey(), requestProperty.getValue());
    }
  }

  int manualRedirectCount = 0;
  while (true) {
    DataSpec dataSpec =
        new DataSpec(
            Uri.parse(url),
            data,
            /* absoluteStreamPosition= */ 0,
            /* position= */ 0,
            /* length= */ C.LENGTH_UNSET,
            /* key= */ null,
            DataSpec.FLAG_ALLOW_GZIP);
    DataSourceInputStream inputStream = new DataSourceInputStream(dataSource, dataSpec);
    try {
      return Util.toByteArray(inputStream);
    } catch (InvalidResponseCodeException e) {
      // For POST requests, the underlying network stack will not normally follow 307 or 308
      // redirects automatically. Do so manually here.
      boolean manuallyRedirect =
          (e.responseCode == 307 || e.responseCode == 308)
              && manualRedirectCount++ < MAX_MANUAL_REDIRECTS;
      String redirectUrl = manuallyRedirect ? getRedirectUrl(e) : null;
      if (redirectUrl == null) {
        throw e;
      }
      url = redirectUrl;
    } finally {
      Util.closeQuietly(inputStream);
    }
  }
}
 
Example #24
Source File: HttpMediaDrmCallback.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
private static byte[] executePost(
    HttpDataSource.Factory dataSourceFactory,
    String url,
    @Nullable byte[] httpBody,
    @Nullable Map<String, String> requestProperties)
    throws IOException {
  HttpDataSource dataSource = dataSourceFactory.createDataSource();
  if (requestProperties != null) {
    for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) {
      dataSource.setRequestProperty(requestProperty.getKey(), requestProperty.getValue());
    }
  }

  int manualRedirectCount = 0;
  while (true) {
    DataSpec dataSpec =
        new DataSpec(
            Uri.parse(url),
            DataSpec.HTTP_METHOD_POST,
            httpBody,
            /* absoluteStreamPosition= */ 0,
            /* position= */ 0,
            /* length= */ C.LENGTH_UNSET,
            /* key= */ null,
            DataSpec.FLAG_ALLOW_GZIP);
    DataSourceInputStream inputStream = new DataSourceInputStream(dataSource, dataSpec);
    try {
      return Util.toByteArray(inputStream);
    } catch (InvalidResponseCodeException e) {
      // For POST requests, the underlying network stack will not normally follow 307 or 308
      // redirects automatically. Do so manually here.
      boolean manuallyRedirect =
          (e.responseCode == 307 || e.responseCode == 308)
              && manualRedirectCount++ < MAX_MANUAL_REDIRECTS;
      String redirectUrl = manuallyRedirect ? getRedirectUrl(e) : null;
      if (redirectUrl == null) {
        throw e;
      }
      url = redirectUrl;
    } finally {
      Util.closeQuietly(inputStream);
    }
  }
}
 
Example #25
Source File: ChunkedTrackBlacklistUtil.java    From TelePlus-Android with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns whether a loading error is an {@link InvalidResponseCodeException} with
 * {@link InvalidResponseCodeException#responseCode} equal to 404 or 410.
 *
 * @param e The loading error.
 * @return Wheter the loading error is an {@link InvalidResponseCodeException} with
 *     {@link InvalidResponseCodeException#responseCode} equal to 404 or 410.
 */
public static boolean shouldBlacklist(Exception e) {
  if (e instanceof InvalidResponseCodeException) {
    int responseCode = ((InvalidResponseCodeException) e).responseCode;
    return responseCode == 404 || responseCode == 410;
  }
  return false;
}
 
Example #26
Source File: ChunkedTrackBlacklistUtil.java    From K-Sonic with MIT License 3 votes vote down vote up
/**
 * Returns whether a loading error is an {@link InvalidResponseCodeException} with
 * {@link InvalidResponseCodeException#responseCode} equal to 404 or 410.
 *
 * @param e The loading error.
 * @return Wheter the loading error is an {@link InvalidResponseCodeException} with
 *     {@link InvalidResponseCodeException#responseCode} equal to 404 or 410.
 */
public static boolean shouldBlacklist(Exception e) {
  if (e instanceof InvalidResponseCodeException) {
    int responseCode = ((InvalidResponseCodeException) e).responseCode;
    return responseCode == 404 || responseCode == 410;
  }
  return false;
}
 
Example #27
Source File: ChunkedTrackBlacklistUtil.java    From TelePlus-Android with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns whether a loading error is an {@link InvalidResponseCodeException} with
 * {@link InvalidResponseCodeException#responseCode} equal to 404 or 410.
 *
 * @param e The loading error.
 * @return Wheter the loading error is an {@link InvalidResponseCodeException} with
 *     {@link InvalidResponseCodeException#responseCode} equal to 404 or 410.
 */
public static boolean shouldBlacklist(Exception e) {
  if (e instanceof InvalidResponseCodeException) {
    int responseCode = ((InvalidResponseCodeException) e).responseCode;
    return responseCode == 404 || responseCode == 410;
  }
  return false;
}