public record DownloadUrl()

in baremaps-core/src/main/java/org/apache/baremaps/workflow/tasks/DownloadUrl.java [27:77]


public record DownloadUrl(String url, Path path, boolean replaceExisting) implements Task {

  private static final String PROTOCOL_FTP = "ftp";
  private static final String PROTOCOL_HTTP = "http";
  private static final String PROTOCOL_HTTPS = "https";

  public DownloadUrl(String url, Path path) {
    this(url, path, false);
  }

  private static final Logger logger = LoggerFactory.getLogger(DownloadUrl.class);

  @Override
  public void execute(WorkflowContext context) throws Exception {
    var targetUrl = new URL(url);
    var targetPath = path.toAbsolutePath();

    if (Files.exists(targetPath) && !replaceExisting) {
      logger.info("Skipping download of {} to {}", url, path);
      return;
    }

    if (isHttp(targetUrl)) {
      var get = (HttpURLConnection) targetUrl.openConnection();
      get.setInstanceFollowRedirects(true);
      get.setRequestMethod("GET");
      urlDownloadToFile(get, targetPath);
      get.disconnect();
    } else if (isFtp(targetUrl)) {
      urlDownloadToFile(targetUrl.openConnection(), targetPath);
    } else {
      throw new IllegalArgumentException("Unsupported URL protocol (supported: http(s)/ftp)");
    }
  }

  private static boolean isHttp(URL url) {
    return url.getProtocol().equalsIgnoreCase(PROTOCOL_HTTP) ||
        url.getProtocol().equalsIgnoreCase(PROTOCOL_HTTPS);
  }

  private static boolean isFtp(URL url) {
    return url.getProtocol().equalsIgnoreCase(PROTOCOL_FTP);
  }

  private static void urlDownloadToFile(URLConnection url, Path targetPath) throws IOException {
    try (var inputStream = url.getInputStream()) {
      Files.createDirectories(targetPath.toAbsolutePath().getParent());
      Files.copy(inputStream, targetPath, StandardCopyOption.REPLACE_EXISTING);
    }
  }
}