currently working on sftp protocol.i have created sftp client using jsch library , sftp server using apache mina sshd library.i have made connection between them , can send files sftp server.now working on creating sftp server side file handler handles incoming files.as example let sftp server can receive files sftp client in implementation there no way notify when file arrived server.i go server root folder , see if there files available.that how know if files arrived.
i implement when files arrive server notify user files arrived , files content.(file name , other details).but problem new apache mina sshd api.i have gone through documentation couldn't figured out.
please know if there implemented listeners handle incoming files in apache mina sshd server or if not how can implement own listener incoming files.
sftp server code
public class sftpserverstarter { private sshserver sshd; private final static logger logger = loggerfactory.getlogger(sftpserverstarter.class); public void start(){ sshd = sshserver.setupdefaultserver(); sshd.setport(22); sshd.sethost("localhost"); sshd.setpasswordauthenticator(new mypasswordauthenticator()); sshd.setpublickeyauthenticator(new mypublickeyauthenticator()); sshd.setkeypairprovider(new simplegeneratorhostkeyprovider()); sshd.setsubsystemfactories(arrays.<namedfactory<command>>aslist(new sftpsubsystem.factory())); sshd.setcommandfactory(new scpcommandfactory()); sshd.setfilesystemfactory(new virtualfilesystemfactory("c:/root")); try { logger.info("starting ..."); sshd.start(); logger.info("started"); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); logger.info("can not start server"); } } }
i found solution not coming apache mina sshd api.here concept.we can monitor server root directory file changes , if there file changed in server folder trigger event.there plenty of api available.in code snippet use org.apache.commons.io.monitor.
sftpfilelistner class
public static void startmonitor(string rootfolder) throws exception { //every 5 seconds start monitoring final long pollinginterval = 5 * 1000; file folder = new file(rootfolder); if (!folder.exists()) { throw new runtimeexception("error : server root directory not found: " + rootfolder); } filealterationobserver observer = new filealterationobserver(folder); filealterationmonitor monitor = new filealterationmonitor(pollinginterval); filealterationlistener listener = new filealterationlisteneradaptor() { @override public void onfilecreate(file file) { try { system.out.println("[sftpfilelistner] received :"+ file.getname()); system.out.println("[sftpfilelistner] received file path :"+ file.getcanonicalpath()); } catch (ioexception e) { throw new runtimeexception("error: unrecoverable error when creating files " + e.getmessage(),e); } } }; observer.addlistener(listener); monitor.addobserver(observer); monitor.start(); } after create monitor class can call implemented method in sftp server class.
sftp server class
//pass server root directory sftpfilelistner.startmonitor("c:/root");
Comments
Post a Comment