Decouple PathHelper and SFTPEngine, introduce Canonicalizer interface

This commit is contained in:
Shikhar Bhushan
2012-05-12 11:06:36 +01:00
parent 431be8e7c7
commit d2b9248535
3 changed files with 27 additions and 14 deletions

View File

@@ -21,13 +21,18 @@ public class PathHelper {
public static final String DEFAULT_PATH_SEPARATOR = "/"; public static final String DEFAULT_PATH_SEPARATOR = "/";
private final SFTPEngine engine; private final Canonicalizer canonicalizer;
private final String pathSep; private final String pathSep;
private String dotDir; private String dotDir;
public PathHelper(SFTPEngine engine, String pathSep) { public interface Canonicalizer {
this.engine = engine; String canonicalize(String path)
throws IOException;
}
public PathHelper(Canonicalizer canonicalizer, String pathSep) {
this.canonicalizer = canonicalizer;
this.pathSep = pathSep; this.pathSep = pathSep;
} }
@@ -56,14 +61,14 @@ public class PathHelper {
if (lastSlash == -1) // Relative path if (lastSlash == -1) // Relative path
if (path.equals("..")) if (path.equals(".."))
return getComponents(canon(path)); return getComponents(canonicalizer.canonicalize(path));
else else
return getComponents(getDotDir(), path); return getComponents(getDotDir(), path);
final String name = path.substring(lastSlash + pathSep.length()); final String name = path.substring(lastSlash + pathSep.length());
if (name.equals(".") || name.equals("..")) if (name.equals(".") || name.equals(".."))
return getComponents(canon(path)); return getComponents(canonicalizer.canonicalize(path));
else { else {
final String parent = path.substring(0, lastSlash); final String parent = path.substring(0, lastSlash);
return getComponents(parent, name); return getComponents(parent, name);
@@ -72,12 +77,7 @@ public class PathHelper {
private synchronized String getDotDir() private synchronized String getDotDir()
throws IOException { throws IOException {
return (dotDir != null) ? dotDir : (dotDir = canon(".")); return (dotDir != null) ? dotDir : (dotDir = canonicalizer.canonicalize("."));
}
private String canon(String path)
throws IOException {
return engine.canonicalize(path);
} }
} }

View File

@@ -61,7 +61,13 @@ public class SFTPEngine
sub = ssh.startSession().startSubsystem("sftp"); sub = ssh.startSession().startSubsystem("sftp");
out = sub.getOutputStream(); out = sub.getOutputStream();
reader = new PacketReader(this); reader = new PacketReader(this);
pathHelper = new PathHelper(this, pathSep); pathHelper = new PathHelper(new PathHelper.Canonicalizer() {
@Override
public String canonicalize(String path)
throws IOException {
return SFTPEngine.this.canonicalize(path);
}
}, pathSep);
} }
public SFTPEngine init() public SFTPEngine init()

View File

@@ -17,13 +17,20 @@ public class SFTPClientTest {
@Before @Before
public void setPathHelper() throws Exception { public void setPathHelper() throws Exception {
PathHelper helper = new PathHelper(sftpEngine, DEFAULT_PATH_SEPARATOR); PathHelper helper = new PathHelper(new PathHelper.Canonicalizer() {
@Override
public String canonicalize(String path)
throws IOException {
if (path.equals("."))
return "/workingdirectory";
return path;
}
}, DEFAULT_PATH_SEPARATOR);
when(sftpEngine.getPathHelper()).thenReturn(helper); when(sftpEngine.getPathHelper()).thenReturn(helper);
} }
@Before @Before
public void setRemoteWorkingDirectory() throws IOException { public void setRemoteWorkingDirectory() throws IOException {
when(sftpEngine.canonicalize(".")).thenReturn("/workingdirectory");
FileAttributes isADirectory = new FileAttributes.Builder().withType(FileMode.Type.DIRECTORY).build(); FileAttributes isADirectory = new FileAttributes.Builder().withType(FileMode.Type.DIRECTORY).build();
when(sftpEngine.stat("/workingdirectory")).thenReturn(isADirectory); when(sftpEngine.stat("/workingdirectory")).thenReturn(isADirectory);
} }