All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.files.util.HeaderUtils Maven / Gradle / Ivy

Go to download

The Files.com Java client library provides convenient access to the Files.com API from JVM based applications.

There is a newer version: 1.4.123
Show newest version
package com.files.util;

import com.files.FilesConfig;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class HeaderUtils {
  // This is NOT an ISO Date format, but an HTTP Date per https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Date
  public static final DateFormat HTTP_DATE_FORMAT = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");

  private static Integer cappedRetrySeconds(int retrySeconds) {
    return retrySeconds <= FilesConfig.getInstance().getMaximumRetrySeconds() ? retrySeconds : null;
  }

  public static Integer retryAfterSeconds(Map> headers) {
    if (headers.containsKey("Retry-After")) {
      String retryValue = headers.get("Retry-After").get(0);
      try {
        return cappedRetrySeconds(Integer.parseInt(retryValue));
      } catch (NumberFormatException e) {
        try {
          // `toSeconds` rounds toward zero, so add 1 to round up and always delay enough time
          return cappedRetrySeconds((int) TimeUnit.MILLISECONDS.toSeconds(HTTP_DATE_FORMAT.parse(retryValue).getTime() - Date.from(Instant.now()).getTime()) + 1);
        } catch (ParseException e2) {
          return null;
        }
      }
    }

    return null;
  }

  private HeaderUtils() {
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy