package net.sourceforge.javautil.network.ssh;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import net.sourceforge.javautil.common.exception.ThrowableManagerRegistry;
import net.sourceforge.javautil.common.io.VirtualArtifact;
import net.sourceforge.javautil.common.io.VirtualArtifactException;
import net.sourceforge.javautil.common.io.VirtualDirectory;
import net.sourceforge.javautil.common.io.VirtualFile;
import net.sourceforge.javautil.common.io.VirtualPath;
import net.sourceforge.javautil.common.io.remote.RemoteDirectory;
import net.sourceforge.javautil.common.io.remote.RemoteLocation;
import net.sourceforge.javautil.common.io.remote.impl.RemoteDirectoryAbstract;
/**
* A directory implementation for browsing secure FTP locations.
*
* @author elponderador
* @author $Author$
* @version $Id$
*/
public class SecureFTPDirectory extends RemoteDirectoryAbstract {
private final VirtualPath path;
private SftpATTRS atts;
public SecureFTPDirectory(SecureFTP location, RemoteDirectory owner, VirtualPath path, SftpATTRS atts) {
super(location, owner, path.getPartCount() == 0 ? "" : path.getPart(path.getPartCount()-1));
this.atts = atts;
this.path = path;
}
public VirtualDirectory createDirectory(String name) {
return this.getDirectoryFor(this.getAttributes(name), name);
}
public VirtualFile createFile(String name) {
return this.getFileFor(this.getAttributes(name), name);
}
public VirtualArtifact getArtifact(String name) {
return this.getFor(this.getAttributes(name), name);
}
public Iterator<VirtualArtifact> getArtifacts() {
if (atts == null) return Collections.EMPTY_LIST.iterator();
List<VirtualArtifact> artifacts = new ArrayList<VirtualArtifact>();
Vector<LsEntry> entries = ((SecureFTP)location).getFiles(path);
for (int e=0; e<entries.size(); e++) {
LsEntry entry = entries.get(e);
if (".".equals( entry.getFilename() ) || "..".equals( entry.getFilename() )) continue;
artifacts.add( this.getFor(entry.getAttrs(), entry.getFilename()) );
}
return artifacts.iterator();
}
public long getLength() { return atts == null ? -1 : atts.getSize(); }
public boolean makeDirectory() {
if (this.atts != null) return true;
try {
((SecureFTP)location).makeDirectory(path);
this.atts = ((SecureFTP)location).getAttributes(path);
return atts != null;
} catch (Exception e) {
ThrowableManagerRegistry.caught(e);
return false;
}
}
public boolean remove(String name) {
try {
((SecureFTP)location).removeDirectory(path);
this.atts = null;
return true;
} catch (Exception e) {
ThrowableManagerRegistry.caught(e);
return false;
}
}
public long getLastModified() { return atts == null ? -1 : atts.getMTime() * 1000; }
public boolean isExists() { return this.atts != null; }
protected SftpATTRS getAttributes (String name) {
return ((SecureFTP)location).getAttributes(path.append(name));
}
protected VirtualArtifact getFor (SftpATTRS atts, String name) {
if (atts == null) return null;
if (atts.isDir()) return this.getDirectoryFor(atts, name);
return this.getFileFor(atts, name);
}
protected VirtualFile getFileFor (SftpATTRS atts, String name) {
if (atts != null && atts.isDir()) throw new VirtualArtifactException(this, "A directory already exists by this name: " + name);
return new SecureFTPFile((SecureFTP)location, this, path.append(name), atts);
}
protected VirtualDirectory getDirectoryFor (SftpATTRS atts, String name) {
if (atts != null && !atts.isDir()) throw new VirtualArtifactException(this, "A file already exists by this name: " + name);
return new SecureFTPDirectory((SecureFTP)location, this, path.append(name), atts);
}
}