com.sap.hana.datalake.files.directaccess.gcs.GcsSignedUrl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sap-hdlfs Show documentation
Show all versions of sap-hdlfs Show documentation
An implementation of org.apache.hadoop.fs.FileSystem targeting SAP HANA Data Lake Files.
// © 2023 SAP SE or an SAP affiliate company. All rights reserved.
package com.sap.hana.datalake.files.directaccess.gcs;
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.HdlfsFileSystemCapabilities.DirectAccessSignedUrl;
import com.sap.hana.datalake.files.directaccess.BaseSignedUrl;
/* package-private */ class GcsSignedUrl extends BaseSignedUrl {
private static final Pattern URL_DATE_PARAMETER_PATTERN = Pattern.compile("^https?://.+(?:\\?|\\?.+&)X-Goog-Date=(\\d{8}T\\d{6}Z)(&.*|#.*|$)");
private static final Pattern URL_EXPIRES_PARAMETER_PATTERN = Pattern.compile("^https?://.+(?:\\?|\\?.+&)X-Goog-Expires=(\\d+)(&.*|#.*|$)");
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'").withZone(ZoneOffset.UTC);
private final long expirationTimeSeconds;
public static GcsSignedUrl from(final DirectAccessSignedUrl directAccessSignedUrl,
final int expirationSafetyMargin) {
return new GcsSignedUrl(directAccessSignedUrl, expirationSafetyMargin);
}
/* package-private */ GcsSignedUrl(final DirectAccessSignedUrl directAccessSignedUrl, final int expirationSafetyMarginSeconds) {
super(directAccessSignedUrl, expirationSafetyMarginSeconds);
this.expirationTimeSeconds = this.getExpirationTimeFromSignedUrl(this.url);
}
public long getExpirationTimeSeconds() {
return this.expirationTimeSeconds;
}
@Override
public boolean isExpired() {
return this.expirationTimeSeconds < System.currentTimeMillis();
}
private long getExpirationTimeFromSignedUrl(final String signedUrl) {
final Matcher creationDateMatcher = URL_DATE_PARAMETER_PATTERN.matcher(signedUrl);
final Matcher expirationTimeMatcher = URL_EXPIRES_PARAMETER_PATTERN.matcher(signedUrl);
if (!creationDateMatcher.matches()) {
throw new IllegalArgumentException("Signed URL does not contain a creation date");
}
if (!expirationTimeMatcher.matches()) {
throw new IllegalArgumentException("Signed URL does not contain an expiration time");
}
final String creationDate = creationDateMatcher.group(1);
final long expirationTime = Long.parseLong(expirationTimeMatcher.group(1));
final long signedUrlValiditySeconds;
// do not apply safety margin if that means the URL would not be valid for at least one second
if (expirationTime - this.expirationSafetyMarginSeconds > 1) {
signedUrlValiditySeconds = expirationTime - this.expirationSafetyMarginSeconds;
} else {
signedUrlValiditySeconds = expirationTime;
}
final Instant creationInstant = Instant.from(DATE_FORMATTER.parse(creationDate));
final Instant expirationInstant = creationInstant.plusSeconds(signedUrlValiditySeconds);
return expirationInstant.toEpochMilli();
}
}
// © 2023 SAP SE or an SAP affiliate company. All rights reserved.
© 2015 - 2025 Weber Informatics LLC | Privacy Policy