Skip to content

Commit

Permalink
refactor(S3Path): extract methods to use in getPath to improve code…
Browse files Browse the repository at this point in the history
… maintainance and readability; solves unused assignment warning on `part`
  • Loading branch information
guicamest committed Nov 1, 2023
1 parent b351710 commit 2da3ec0
Showing 1 changed file with 35 additions and 21 deletions.
56 changes: 35 additions & 21 deletions src/main/java/software/amazon/nio/spi/s3/S3Path.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,35 +70,21 @@ private S3Path from(String path){
* @return a new S3Path
*/
static S3Path getPath(S3FileSystem fsForBucket, String first, String... more) {
if(fsForBucket == null) throw new IllegalArgumentException("The S3FileSystem may not be null");
if(first == null ){
if(fsForBucket == null) throw new IllegalArgumentException("The S3FileSystem may not be null");
if(first == null) {
throw new IllegalArgumentException("first element of the path may not be null");
}

S3NioSpiConfiguration configuration = fsForBucket.configuration();

first = first.trim();

if((first.isEmpty()) && !(more == null || more.length == 0)) throw new IllegalArgumentException("The first element of the path may not be empty when more exists");
if( first.isEmpty() && !(more == null || more.length == 0)) throw new IllegalArgumentException("The first element of the path may not be empty when more exists");
if(first.startsWith(fsForBucket.provider().getScheme()+":/")) {
first = first.substring(fsForBucket.provider().getScheme().length()+2);

String part = null;
if (configuration.getCredentials() != null) {
AwsCredentials credentials = configuration.getCredentials();
part = credentials.accessKeyId() + ':' + credentials.secretAccessKey();
if (first.startsWith('/' + part)) {
first = PATH_SEPARATOR + first.substring(part.length()+2);
}
}
part = configuration.getEndpoint();
if (!part.isEmpty() && first.startsWith(PATH_SEPARATOR + part)) {
first = first.substring(part.length()+1);
}
part = configuration.getBucketName();
if (first.startsWith(PATH_SEPARATOR + part)) {
first = first.substring(part.length()+1);
}
first = removeScheme(first, fsForBucket.provider().getScheme());
first = removeCredentials(first, configuration);
first = removeEndpoint(first, configuration.getEndpoint());
first = removeBucket(first, configuration.getBucketName());
}

return new S3Path(fsForBucket, PosixLikePathRepresentation.of(first, more));
Expand Down Expand Up @@ -861,4 +847,32 @@ public boolean hasNext() {
}
}

private static String removeScheme(String path, String scheme){
return path.substring(scheme.length()+2);
}

private static String removeCredentials(String first, S3NioSpiConfiguration configuration) {
if (configuration.getCredentials() != null) {
AwsCredentials credentials = configuration.getCredentials();
String credentialsAsString = credentials.accessKeyId() + ':' + credentials.secretAccessKey();
if (first.startsWith('/' + credentialsAsString)) {
first = PATH_SEPARATOR + first.substring(credentialsAsString.length()+2);
}
}
return first;
}

private static String removeEndpoint(String first, String endpoint) {
if (!endpoint.isEmpty() && first.startsWith(PATH_SEPARATOR + endpoint)) {
first = first.substring(endpoint.length()+1);
}
return first;
}

private static String removeBucket(String first, String part) {
if (first.startsWith(PATH_SEPARATOR + part)) {
first = first.substring(part.length()+1);
}
return first;
}
}

0 comments on commit 2da3ec0

Please sign in to comment.