Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More robust S3Path.resolve #104

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ language: java
jdk:
- openjdk7
- oraclejdk8
env:
global:
- secure: "cJdVQLH5yLcMiafZH1BRubHlsd/P8k4IqVToYQBK8GXx47brewSBb1PL+LlRqKiC83N5uFkkF6Md1gASf8twagg2HjJigfUaHZtlZmtVQw1aGAoLcsns0EZny26Gn29q/K+A3FAtydB9jCnJCt3ZKSD6aT/RT7x5v3nm8C7g7iU="
- secure: "peM3iQKIGKttf76uUs1vSm6L7A0+sk4it0WrhctQ5CWSFuSHl8pZUKhT+3ESkUL7FnNfG8USb/2EdlhwljH8Dg/e09jQ3OwhOPhbX8pgsn2k4SFJ1+IkB6iAcV/o5DlN87CTRcixInInQLXsSIswiUbhaPLI0dE6xJJG93XvSc8="
- secure: tlMPSRwLNtMX6sAPle28uItIUoWuWet2EDaWZprKPJsY5u/AmjOigDH6dJCwYFi7t1eWeueYeNgzQjKiVGe1kia00WoEaUwPo+P3C1hCjkKSfjLwMxqLR4VvEj/8FDRBoncwryNWWCCxSxEXL6OCGCUqCqy0eye2KaEV9RJCNoA=
- secure: bXjOngFzBAcI5uiJyxAx2S9rptFK+Sp0Pa0LQKsPz/lFxCPhgJdhHGqEvpkzxAi41jmR/nTFB/F//a7lWUK3QfHQV9zU39T+nA17wjmpds2+1xYNTAea5ow3bBVelBL0MoIs+sNlwQhIFR7daVpTfnVEJNXCipEzJmLrKptWs7E=
before_install:
- sudo sed -i 's/security.provider.9/#security.provider.9/g' $JAVA_HOME/jre/lib/security/java.security
before_script:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This project provides a first API implementation, little optimized, but "complet
<dependency>
<groupId>com.upplication</groupId>
<artifactId>s3fs</artifactId>
<version>2.2.1</version>
<version>2.2.2</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.upplication</groupId>
<artifactId>s3fs</artifactId>
<packaging>jar</packaging>
<version>2.2.1</version>
<version>2.2.2</version>
<name>s3fs</name>
<description>S3 filesystem provider for Java 7</description>
<url>https://github.com/Upplication/Amazon-S3-FileSystem-NIO2</url>
Expand Down
39 changes: 27 additions & 12 deletions src/main/java/com/upplication/s3fs/S3Path.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
package com.upplication.s3fs;

import com.google.common.base.*;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.upplication.s3fs.attribute.S3BasicFileAttributes;
import static com.google.common.collect.Iterables.concat;
import static java.lang.String.format;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.file.*;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.Iterator;
import java.util.List;

import static com.google.common.collect.Iterables.*;
import static java.lang.String.format;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.upplication.s3fs.attribute.S3BasicFileAttributes;

public class S3Path implements Path {

Expand Down Expand Up @@ -353,20 +358,30 @@ public Path normalize() {

@Override
public Path resolve(Path other) {
String otherUri = "";
if (other.isAbsolute()) {
Preconditions.checkArgument(other instanceof S3Path, "other must be an instance of %s", S3Path.class.getName());
Preconditions.checkArgument(other instanceof S3Path, "other must be an instance of %s or be relative", S3Path.class.getName());
return other;
} else if (!(other instanceof S3Path)) {
int nameCount = other.getNameCount();
for (int i = 0; i < nameCount; i++) {
if (i > 0)
otherUri += PATH_SEPARATOR;
otherUri += other.getName(i);
}
} else {
S3Path otherS3Path = (S3Path) other;
otherUri = otherS3Path.uri;
}

S3Path otherS3Path = (S3Path) other;
StringBuilder pathBuilder = new StringBuilder();

if (this.isAbsolute()) {
pathBuilder.append(PATH_SEPARATOR + this.fileStore.name() + PATH_SEPARATOR);
}
pathBuilder.append(this.uri);
if (!otherS3Path.uri.isEmpty())
pathBuilder.append(PATH_SEPARATOR + otherS3Path.uri);
if (!otherUri.isEmpty())
pathBuilder.append(PATH_SEPARATOR + otherUri);

return new S3Path(this.fileSystem, pathBuilder.toString());
}
Expand Down Expand Up @@ -465,7 +480,7 @@ public URI toUri() {
return URI.create("s3://" + normalizeURI(builder.toString()));
}
else {
return URI.create(this.uri);
return URI.create(uri);
}
}

Expand Down
54 changes: 54 additions & 0 deletions src/test/java/com/upplication/s3fs/Path/S3PathTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.upplication.s3fs.Path;

import com.upplication.s3fs.AmazonS3Factory;
import com.upplication.s3fs.S3FileSystem;
import com.upplication.s3fs.S3FileSystemProvider;
import com.upplication.s3fs.S3Path;
Expand All @@ -9,8 +10,10 @@
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.util.HashMap;

Expand Down Expand Up @@ -232,4 +235,55 @@ public void registerWatchService() throws IOException {
S3Path path = forPath("/buck/file");
path.register(null, new WatchEvent.Kind<?>[0], new WatchEvent.Modifier[0]);
}
@Test
public void testResolve() throws IOException {
S3FileSystem fileSystem2 = null;
try {
HashMap<String, String> environments = new HashMap<>();
environments.put(AmazonS3Factory.ACCESS_KEY, "accesskey");
environments.put(AmazonS3Factory.SECRET_KEY, "secretaccesskey");

fileSystem2 = (S3FileSystem) FileSystems.newFileSystem(URI.create("s3://accesskey:secretaccesskey@s3.test.amazonaws.com/bucket"), environments);

//good
S3Path parent = (S3Path) fileSystem2.provider().getPath(URI.create("s3://accesskey:secretaccesskey@s3.test.amazonaws.com/bucket"));
S3Path child = (S3Path) fileSystem2.provider().getPath(URI.create("s3://accesskey:secretaccesskey@s3.test.amazonaws.com/bucket/rabbit"));
S3Path resolved = (S3Path) parent.resolve(child);

assertEquals(child, resolved);

resolved = (S3Path) parent.resolve("s3://accesskey:secretaccesskey@s3.test.amazonaws.com/bucket/rabbit");
assertEquals(child, resolved);

resolved = (S3Path) parent.resolve("rabbit");
assertEquals(child, resolved);

resolved = (S3Path) parent.resolve(Paths.get("rabbit")); //unixPath
assertEquals(child, resolved);

resolved = (S3Path) parent.resolve(Paths.get("./rabbit")); //unixPath
assertEquals("s3://accesskey:secretaccesskey@s3.test.amazonaws.com/bucket/./rabbit", resolved.toString());

resolved = (S3Path) parent.resolve(Paths.get("./rabbit in space")); //unixPath
assertEquals("s3://accesskey:secretaccesskey@s3.test.amazonaws.com/bucket/./rabbit%20in%20space", resolved.toString());

try {
parent.resolve(Paths.get("/tmp"));
fail("expect IllegalArgumentException");
} catch (IllegalArgumentException e) {
//ignore
assertEquals("other must be an instance of com.upplication.s3fs.S3Path or be relative", e.getMessage());
}

//bad
S3Path parent2 = fileSystem2.getPath("s3://accesskey:secretaccesskey@s3.test.amazonaws.com/bucket");
S3Path child2 = fileSystem2.getPath("s3://accesskey:secretaccesskey@s3.test.amazonaws.com/bucket/rabbit");
S3Path resolved2 = (S3Path) parent2.resolve(child2);
assertEquals("s3:/accesskey:secretaccesskey@s3.test.amazonaws.com/bucket/s3:/accesskey:secretaccesskey@s3.test.amazonaws.com/bucket/rabbit", resolved2.toString());

} finally {
if (fileSystem2 != null)
fileSystem2.close();
}
}
}
18 changes: 18 additions & 0 deletions src/test/java/com/upplication/s3fs/Path/ToUriTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ public void toUriWithEndSlash() {
assertEquals(S3_GLOBAL_URI_TEST + "bucket/folder/", s3Path.toUri().toString());
}

@Test
public void toUriWithSpaces() {
S3Path s3Path = getPath("/bucket/with spaces");

assertEquals(S3_GLOBAL_URI_TEST.resolve("bucket/with%20spaces") , s3Path.toUri());
}

@Test
public void toUriWithNotEndSlash() {
S3Path s3Path = getPath("/bucket/file");
Expand All @@ -70,6 +77,17 @@ public void toUriRelative() {

S3Path path = new S3Path(fileSystem, "bla");
assertEquals(URI.create("bla"), path.toUri());

S3Path withSpaces = new S3Path(fileSystem, "with space");
assertEquals(URI.create("with%20space"), withSpaces.toUri());
}

@Test
public void toUriRelativeWithSpaces() {
S3FileSystem fileSystem = s3fsProvider.getFileSystem(S3_GLOBAL_URI_TEST);

S3Path path = new S3Path(fileSystem, "with space");
assertEquals(URI.create("with%20space"), path.toUri());
}

@Test
Expand Down