java - Apache Mina and JSch can't set home dir -
i'm trying write unit test using apache mina , jsch , i'm hitting problem i'm sure has way i've set filesystem on mina.
here the mina setup code:
sshd = sshserver.setupdefaultserver(); sshd.setport(8002); sshd.setfilesystemfactory(new nativefilesystemfactory() { @override public void setcreatehome(boolean createhome) { super.setcreatehome(true); } @override public filesystemview createfilesystemview(final session session) { string username = session.getusername(); // create home if not exist string homedirstr = "/home/testusr"; file homedir = new file(homedirstr); if ((!homedir.exists()) && (!homedir.mkdirs())) { system.out.println("cannot create user home :: " + homedirstr); } return new nativefilesystemview(session.getusername(), false) { @override public string getvirtualuserdir() { return "/home/testusr"; } }; }; }); sshd.setsubsystemfactories(arrays.<namedfactory<command>>aslist(new sftpsubsystem.factory())); sshd.setcommandfactory(new scpcommandfactory()); sshd.setkeypairprovider(new simplegeneratorhostkeyprovider("hostkey.ser")); list<namedfactory<userauth>> userauthfactories = new arraylist<namedfactory<userauth>>(); userauthfactories.add(new userauthnone.factory()); sshd.setuserauthfactories(userauthfactories); sshd.setpublickeyauthenticator(new publickeyauthenticator() { public boolean authenticate(string username, publickey key, serversession session) { return true; } }); sshd.start(); jsch code:
jsch jsch = new jsch(); string apppublickey = "c:\\conf\\test_private"; jsch.addidentity(new file(apppublickey).getabsolutepath()); com.jcraft.jsch.session session = jsch.getsession("testusr","localhost", 8002); session.setconfig("stricthostkeychecking", "no"); session.settimeout(30000); session.connect(); channel channel = session.openchannel("sftp"); channel.connect(); channelsftp sftpchannel = (channelsftp) channel; string filename = "c:\\temp\\test.tar.gz"; file f = new file(filename); sftpchannel.put(new fileinputstream(f), "/home/testusr"); exception:
4: @ com.jcraft.jsch.channelsftp.gethome(channelsftp.java:2403) @ com.jcraft.jsch.channelsftp.getcwd(channelsftp.java:2412) @ com.jcraft.jsch.channelsftp.remoteabsolutepath(channelsftp.java:2904) @ com.jcraft.jsch.channelsftp.put(channelsftp.java:517) @ com.jcraft.jsch.channelsftp.put(channelsftp.java:492) @ com.sftpservicetest.sendfile(sftpservicetest.java:183) @ com.sftpservicetest.main(sftpservicetest.java:218) caused by: java.io.ioexception: inputstream closed @ com.jcraft.jsch.channelsftp.fill(channelsftp.java:2871) @ com.jcraft.jsch.channelsftp.header(channelsftp.java:2895) @ com.jcraft.jsch.channelsftp._realpath(channelsftp.java:2315) @ com.jcraft.jsch.channelsftp.gethome(channelsftp.java:2397) ... 6 more i can't seem find examples of creating filesystemview. hoping can me out, i've been stuck on days!
thanks in advance.
this how ended setting jsch & mina in unit test:
@runwith(springjunit4classrunner.class) @contextconfiguration(locations = { "classpath:context.xml" }) public class sftptaskletlettertest extends abstractaomtest {
@autowired private sftptaskletletter cut; private sshserver sshd = null; @before public void setup() throws exception { sshd = createandstartsshserver(); } @after public void teardown() throws exception { sshd.stop(); } @test public void testputandgetfile() throws exception { jsch jsch = new jsch(); hashtable<string, string> config = new hashtable<>(); jsch.setconfig(config); session session = jsch.getsession("assentisftp", "127.0.0.1", 22); userinfo ui = new myuserinfo(); session.setuserinfo(ui); session.connect(); // channel channel = session.openchannel("session"); // works channel channel = session.openchannel("sftp"); channel.connect(); channelsftp sftpchannel = (channelsftp) channel; final string testfilecontents = "some file contents"; string uploadedfilename = "uploadfile"; sftpchannel.put(new bytearrayinputstream(testfilecontents.getbytes()), uploadedfilename); string downloadedfilename = "downloadfile"; sftpchannel.get(uploadedfilename, downloadedfilename); file downloadedfile = new file(downloadedfilename); asserttrue(downloadedfile.exists()); if (sftpchannel.isconnected()) { sftpchannel.exit(); } if (session.isconnected()) { session.disconnect(); } } public static sshserver createandstartsshserver() throws ioexception { sshserver result = sshserver.setupdefaultserver(); result.setport(22); result.setkeypairprovider(new simplegeneratorhostkeyprovider("hostkey.ser")); result.setpasswordauthenticator(new passwordauthenticator() { public boolean authenticate(string username, string password, serversession session) { return "assentisftp".equals(username); } }); result.setsubsystemfactories(arrays.<namedfactory<command>> aslist(new sftpsubsystem.factory())); result.start(); return result; } @override protected mandatortype getmandator() { return mandatortype.man; } public static class myuserinfo implements userinfo { @override public string getpassphrase() { return ""; } @override public string getpassword() { return ""; } @override public boolean promptpassword(string message) { return true; } @override public boolean promptpassphrase(string message) { return true; } @override public boolean promptyesno(string message) { return true; } @override public void showmessage(string message) { } } }
Comments
Post a Comment