diff --git a/zeppelin-interpreter/src/main/java/org/apache/zeppelin/conf/ZeppelinConfiguration.java b/zeppelin-interpreter/src/main/java/org/apache/zeppelin/conf/ZeppelinConfiguration.java index cbd894eaa3e..75391deebd0 100644 --- a/zeppelin-interpreter/src/main/java/org/apache/zeppelin/conf/ZeppelinConfiguration.java +++ b/zeppelin-interpreter/src/main/java/org/apache/zeppelin/conf/ZeppelinConfiguration.java @@ -20,6 +20,8 @@ import com.google.common.annotations.VisibleForTesting; import java.io.File; import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import java.util.Arrays; import java.util.HashMap; @@ -583,7 +585,7 @@ public String getRelativeDir(ConfVars c) { } public String getRelativeDir(String path) { - if (path != null && path.startsWith(File.separator) || isWindowsPath(path)) { + if (path != null && (path.startsWith(File.separator) || isWindowsPath(path) || isPathWithScheme(path))) { return path; } else { return getString(ConfVars.ZEPPELIN_HOME) + File.separator + path; @@ -602,6 +604,14 @@ public boolean isWindowsPath(String path){ return path.matches("^[A-Za-z]:\\\\.*"); } + public boolean isPathWithScheme(String path){ + try { + return StringUtils.isNotBlank(new URI(path).getScheme()); + } catch (URISyntaxException e) { + return false; + } + } + public boolean isAnonymousAllowed() { if (anonymousAllowed == null) { anonymousAllowed = this.getShiroPath().equals(StringUtils.EMPTY); diff --git a/zeppelin-zengine/src/test/java/org/apache/zeppelin/conf/ZeppelinConfigurationTest.java b/zeppelin-zengine/src/test/java/org/apache/zeppelin/conf/ZeppelinConfigurationTest.java index c730e5f4b26..001860b77dc 100644 --- a/zeppelin-zengine/src/test/java/org/apache/zeppelin/conf/ZeppelinConfigurationTest.java +++ b/zeppelin-zengine/src/test/java/org/apache/zeppelin/conf/ZeppelinConfigurationTest.java @@ -16,9 +16,10 @@ */ package org.apache.zeppelin.conf; -import junit.framework.Assert; + import org.apache.commons.configuration.ConfigurationException; import org.apache.zeppelin.conf.ZeppelinConfiguration.ConfVars; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -78,6 +79,30 @@ public void isWindowsPathTestFalse() throws ConfigurationException { Assert.assertFalse(isIt); } + @Test + public void isPathWithSchemeTestTrue() throws ConfigurationException { + + ZeppelinConfiguration conf = new ZeppelinConfiguration(this.getClass().getResource("/zeppelin-site.xml")); + Boolean isIt = conf.isPathWithScheme("hdfs://hadoop.example.com/zeppelin/notebook"); + Assert.assertTrue(isIt); + } + + @Test + public void isPathWithSchemeTestFalse() throws ConfigurationException { + + ZeppelinConfiguration conf = new ZeppelinConfiguration(this.getClass().getResource("/zeppelin-site.xml")); + Boolean isIt = conf.isPathWithScheme("~/test/file.xml"); + Assert.assertFalse(isIt); + } + + @Test + public void isPathWithInvalidSchemeTest() throws ConfigurationException { + + ZeppelinConfiguration conf = new ZeppelinConfiguration(this.getClass().getResource("/zeppelin-site.xml")); + Boolean isIt = conf.isPathWithScheme("c:\\test\\file.txt"); + Assert.assertFalse(isIt); + } + @Test public void getNotebookDirTest() throws ConfigurationException { ZeppelinConfiguration conf = new ZeppelinConfiguration(this.getClass().getResource("/zeppelin-site.xml"));