Skip to content

Commit

Permalink
✨ custom exception for retrieve non existing key/file, ♻️ and Path in…
Browse files Browse the repository at this point in the history
…sted of string for copy parameters
  • Loading branch information
Azeot committed Jul 4, 2024
1 parent 69e213f commit eb39589
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
14 changes: 14 additions & 0 deletions src/main/java/net/optionfactory/storage/DataNotFoundException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package net.optionfactory.storage;

public class DataNotFoundException extends RuntimeException {
public DataNotFoundException() {
}

public DataNotFoundException(String message) {
super(message);
}

public DataNotFoundException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public InputStream retrieve(Path name) {
try {
return Files.newInputStream(base.resolve(name));
} catch (IOException e) {
throw new RuntimeException(e);
throw new DataNotFoundException("Could not find %s".formatted(name), e);
}
}

Expand Down Expand Up @@ -112,7 +112,7 @@ private List<Path> retrieveListing(Path path) {
}

@Override
public void copy(String sourceName, String targetName) {
public void copy(Path sourceName, Path targetName) {
try {
Files.copy(base.resolve(sourceName), base.resolve(targetName));
} catch (IOException ex) {
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/net/optionfactory/storage/S3Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import software.amazon.awssdk.services.s3.model.DeleteObjectsRequest;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.ListObjectsV2Request;
import software.amazon.awssdk.services.s3.model.NoSuchKeyException;
import software.amazon.awssdk.services.s3.model.ObjectCannedACL;
import software.amazon.awssdk.services.s3.model.ObjectIdentifier;
import software.amazon.awssdk.services.s3.model.PutObjectAclRequest;
Expand Down Expand Up @@ -139,9 +140,11 @@ public InputStream retrieve(Path name) {
.key(name.toString())
.build();
return s3.getObject(request);
} catch (NoSuchKeyException ex) {
throw new DataNotFoundException("Key %s not found in S3 bucket %s".formatted(name, bucket), ex);
} catch (SdkException ex) {
logger.error("Unable to retrieve {} from S3 bucket {}", name, bucket, ex);
throw new IllegalStateException(String.format("Unable to retrieve %s from S3 bucket %s", name, bucket), ex);
throw new IllegalStateException("Unable to retrieve %s from S3 bucket %s".formatted(name, bucket), ex);
}
}

Expand All @@ -159,15 +162,17 @@ public Path cacheLocally(InputStream inputStream) {
}

@Override
public void copy(String sourceName, String targetName) {
public void copy(Path sourceName, Path targetName) {
try {
final var request = CopyObjectRequest.builder()
.sourceBucket(bucket)
.destinationBucket(bucket)
.sourceKey(sourceName)
.destinationKey(targetName)
.sourceKey(sourceName.toString())
.destinationKey(targetName.toString())
.build();
s3.copyObject(request);
} catch (NoSuchKeyException ex) {
throw new DataNotFoundException("Key %s not found in S3 bucket %s".formatted(sourceName, bucket), ex);
} catch (SdkException ex) {
logger.error("Unable to copy {} to {} from S3 bucket {}", sourceName, targetName, bucket, ex);
throw new IllegalStateException(String.format("Unable to copy %s to %s from S3 bucket %s", sourceName, targetName, bucket), ex);
Expand Down Expand Up @@ -232,7 +237,7 @@ private List<Path> adaptKeyToPath(ListObjectsV2Iterable objects) {
.toList();
}

public String contentTypeForFile(Path file) {
private String contentTypeForFile(Path file) {
try {
return Optional.ofNullable(tika.detect(file))
.orElse(DEFAULT_CONTENT_TYPE);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/optionfactory/storage/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface Storage {

List<Path> list(Path path);

void copy(String sourceName, String targetName);
void copy(Path sourceName, Path targetName);

String absoluteUrl(String... paths);

Expand Down

0 comments on commit eb39589

Please sign in to comment.