Skip to content
Closed
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
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ cache:
jdk:
- oraclejdk10
- oraclejdk8
- openjdk7

matrix:
fast_finish: true
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@
</distributionManagement>

<dependencies>
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>com.esotericsoftware.kryo</groupId>
<artifactId>kryo</artifactId>
Expand Down
111 changes: 77 additions & 34 deletions src/main/java/loci/common/Location.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -93,8 +91,9 @@ protected class ListingsResult {

// -- Fields --

private boolean isURL = true;
private boolean isURL = false;
private URL url;
private URI uri;
private File file;

// -- Constructors --
Expand All @@ -107,22 +106,7 @@ protected class ListingsResult {
* @see #getMappedFile(String)
*/
public Location(String pathname) {
LOGGER.trace("Location({})", pathname);
if (pathname.contains("://")) {
// Avoid expensive exception handling in case when path is
// obviously not an URL
try {
url = new URL(getMappedId(pathname));
}
catch (MalformedURLException e) {
LOGGER.trace("Location is not a URL", e);
isURL = false;
}
} else {
LOGGER.trace("Location is not a URL");
isURL = false;
}
if (!isURL) file = new File(getMappedId(pathname));
this((String) null, pathname);
}

/**
Expand All @@ -145,7 +129,41 @@ public Location(File file) {
* @param child the relative path name
*/
public Location(String parent, String child) {
this(parent + File.separator + child);
LOGGER.trace("Location({}, {})", parent, child);

String mapped = null;
String pathname = null;

// First handle possible URIs
if (child != null && child.contains("://")) {
// Avoid expensive exception handling in case when path is
// obviously not an URL
try {
mapped = getMappedId(child);
pathname = child;
uri = new URI(mapped);
isURL = true;
url = uri.toURL();
}
catch (URISyntaxException | MalformedURLException e) {
// TODO: this should possibly throw
// possibly leaves url null
}
}

// If not a URI, then deal with relative vs. absolute paths
if (pathname == null) {
if (parent != null) {
// TODO: in some cases child here may be null
pathname = parent + File.separator + child;
} else {
pathname = child;
}
mapped = getMappedId(pathname);
}

if (!isURL) file = new File(mapped);

}

/**
Expand Down Expand Up @@ -395,7 +413,10 @@ public static IRandomAccess getHandle(String id, boolean writable,
LOGGER.trace("no handle was mapped for this ID");
String mapId = getMappedId(id);

if (id.startsWith("http://") || id.startsWith("https://")) {
if (id.startsWith("s3://")) {
handle = new S3Handle(mapId);
}
else if (id.startsWith("http://") || id.startsWith("https://")) {
handle = new URLHandle(mapId);
}
else if (allowArchiveHandles && ZipHandle.isZipFile(mapId)) {
Expand Down Expand Up @@ -464,6 +485,17 @@ public String[] list(boolean noHiddenFiles) {
final List<String> files = new ArrayList<String>();
if (isURL) {
try {
if (url == null) {
// Likely s3
if (!exists()) {
return null;
}
if (uri.toString().endsWith("/")) {
return new String[0];
} else {
return null;
}
}
URLConnection c = url.openConnection();
InputStream is = c.getInputStream();
boolean foundEnd = false;
Expand Down Expand Up @@ -539,7 +571,8 @@ public String[] list(boolean noHiddenFiles) {
*/
public boolean canRead() {
LOGGER.trace("canRead()");
return isURL ? (isDirectory() || isFile() || exists()) : file.canRead();
// Note: isFile calls exist
return isURL ? (isDirectory() || isFile()) : file.canRead();
}

/**
Expand Down Expand Up @@ -642,7 +675,9 @@ public boolean exists() {
LOGGER.trace("exists()");
if (isURL) {
try {
url.getContent();
// TODO: existence should almost certainly be cached.
IRandomAccess handle = getHandle(uri.toString());
handle.length();
return true;
}
catch (IOException e) {
Expand Down Expand Up @@ -675,7 +710,7 @@ public Location getAbsoluteFile() {
*/
public String getAbsolutePath() {
LOGGER.trace("getAbsolutePath()");
return isURL ? url.toExternalForm() : file.getAbsolutePath();
return isURL ? uri.normalize().toString() : file.getAbsolutePath();
}

/**
Expand Down Expand Up @@ -715,9 +750,8 @@ public String getCanonicalPath() throws IOException {
public String getName() {
LOGGER.trace("getName()");
if (isURL) {
String name = url.getFile();
name = name.substring(name.lastIndexOf("/") + 1);
return name;
// TODO: we should just store new File(uri) in file
return new File(uri.getPath()).getName();
}
return file.getName();
}
Expand Down Expand Up @@ -757,7 +791,7 @@ public Location getParentFile() {
* @see java.io.File#getPath()
*/
public String getPath() {
return isURL ? url.getHost() + url.getPath() : file.getPath();
return isURL ? uri.getHost() + uri.getPath() : file.getPath();
}

/**
Expand All @@ -769,7 +803,7 @@ public String getPath() {
*/
public boolean isAbsolute() {
LOGGER.trace("isAbsolute()");
return isURL ? true : file.isAbsolute();
return isURL ? uri.isAbsolute() : file.isAbsolute();
}

/**
Expand All @@ -781,8 +815,13 @@ public boolean isAbsolute() {
public boolean isDirectory() {
LOGGER.trace("isDirectory()");
if (isURL) {
String[] list = list();
return list != null;
if ("s3".equals(uri.getScheme())) {
return uri.toString().endsWith("/") && exists();
} else {
// TODO: this should be removed as well.
String[] list = list();
return list != null;
}
}
return file.isDirectory();
}
Expand Down Expand Up @@ -829,7 +868,11 @@ public long lastModified() {
LOGGER.trace("lastModified()");
if (isURL) {
try {
return url.openConnection().getLastModified();
if (url != null) {
return url.openConnection().getLastModified();
} else {
return 0;
}
}
catch (IOException e) {
LOGGER.trace("Could not determine URL's last modification time", e);
Expand Down Expand Up @@ -908,7 +951,7 @@ public URL toURL() throws MalformedURLException {
*/
@Override
public String toString() {
return isURL ? url.toString() : file.toString();
return isURL ? uri.toString() : file.toString();
}

}
Loading