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

com.sap.hana.datalake.files.directaccess.wasb.WasbSignedUrl Maven / Gradle / Ivy

Go to download

An implementation of org.apache.hadoop.fs.FileSystem targeting SAP HANA Data Lake Files.

There is a newer version: 3.0.27
Show newest version
// © 2023 SAP SE or an SAP affiliate company. All rights reserved.
package com.sap.hana.datalake.files.directaccess.wasb;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.sap.hana.datalake.files.HdlfsConstants;
import com.sap.hana.datalake.files.HdlfsFileSystemCapabilities.DirectAccessSignedUrl;
import com.sap.hana.datalake.files.directaccess.BaseSignedUrl;

/* package-private */ class WasbSignedUrl extends BaseSignedUrl {

  private static final Pattern URL_EXPIRES_PARAMETER_PATTERN = Pattern.compile("^https?://.+(?:\\?|\\?.+&)se=(\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z)(&.*|#.*|$)");
  private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZone(ZoneOffset.UTC);

  private final Instant expirationTime;

  public static WasbSignedUrl from(final DirectAccessSignedUrl directAccessSignedUrl, final int expirationSafetyMargin) {
    return new WasbSignedUrl(directAccessSignedUrl, expirationSafetyMargin);
  }

  /* package-private */ WasbSignedUrl(final DirectAccessSignedUrl directAccessSignedUrl,
          final int expirationSafetyMarginSeconds) {
    super(directAccessSignedUrl, expirationSafetyMarginSeconds);
    this.expirationTime = this.getExpirationTimeFromSignedUrl(this.url);
  }

  public Instant getExpirationTime() {
    return this.expirationTime;
  }

  @Override
  public boolean isExpired() {
    return this.expirationTime.isBefore(Instant.now());
  }

  private Instant getExpirationTimeFromSignedUrl(final String signedUrl) {
    final String decodedUrl;

    try {
      decodedUrl = URLDecoder.decode(signedUrl, String.valueOf(HdlfsConstants.DEFAULT_CHARSET));
    } catch (final UnsupportedEncodingException ex) {
      throw new RuntimeException("Error occurred while decoding the signed url", ex);
    }

    final Matcher expirationTimeMatcher = URL_EXPIRES_PARAMETER_PATTERN.matcher(decodedUrl);

    if (!expirationTimeMatcher.matches()) {
      throw new IllegalArgumentException("Signed URL does not contain an expiration time");
    }

    final String expirationDate = expirationTimeMatcher.group(1);
    final Instant expirationTime = Instant.from(DATE_FORMATTER.parse(expirationDate));

    final Instant expirationTimeWithSafetyMargin = expirationTime.minusSeconds(this.expirationSafetyMarginSeconds);

    // do not apply safety margin if that means the URL would not be valid for at least one second
    if (expirationTimeWithSafetyMargin.isAfter(Instant.now().plusSeconds(1))) {
      return expirationTimeWithSafetyMargin;
    } else {
      return expirationTime;
    }
  }

}

// © 2023 SAP SE or an SAP affiliate company. All rights reserved.




© 2015 - 2025 Weber Informatics LLC | Privacy Policy