ApacheFtpServer - Custom command extension

方法一:

修改源码,加载自定义的命令工厂类。

I'm a new user of Apache FtpServer. I want to add custom command. Author say on the features page "All the FTP messages are customizable." but did not talk about new custom command. I could simply add a command using below code:

            CommandFactoryFactory  cf = new CommandFactoryFactory();
            cf.addCommand("NEW_CUSTOM_COMMAND", new NewCustomCommand());
            CommandFactory commandFactory = cf.createCommandFactory();
            FtpServerFactory serverFactory = new FtpServerFactory();
            serverFactory.setCommandFactory(commandFactory);

            public class NewCustomCommand implements Command{
                  public void execute(FtpIoSession session, FtpServerContext context,
                              FtpRequest request) throws IOException, FtpException {
                        // main code
                  }
            }


It's worked. We have a ftp server. It^s implemented in house on TCP / IP server. We use custom commands for executing program, using bundle job etc. Apache FTP Server has talended feature like resume file transfer, customizable user managment, simple handling connection timeouts. So we decide to replace ours by yours.


方法二:

当与spring结合的时候,可以将自定义命令添加到ftpserver全配置文件(第96行-第100行)。

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.     <!--  
  3.         Licensed to the Apache Software Foundation (ASF) under one or more  
  4.         contributor license agreements. See the NOTICE file distributed with  
  5.         this work for additional information regarding copyright ownership.  
  6.         The ASF licenses this file to you under the Apache License, Version  
  7.         2.0 (the "License"); you may not use this file except in compliance  
  8.         with the License. You may obtain a copy of the License at  
  9.         http://www.apache.org/licenses/LICENSE-2.0 Unless required by  
  10.         applicable law or agreed to in writing, software distributed under the  
  11.         License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR  
  12.         CONDITIONS OF ANY KIND, either express or implied. See the License for  
  13.         the specific language governing permissions and limitations under the  
  14.         License.  
  15.     -->  
  16. <server xmlns="http://mina.apache.org/ftpserver/spring/v1"  
  17.     xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  18.     xsi:schemaLocation="  
  19.        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  20.        http://mina.apache.org/ftpserver/spring/v1 http://mina.apache.org/ftpserver/ftpserver-1.0.xsd      
  21.        "  
  22.     id="myServer">  
  23.     <!--  
  24.         Use this section to define custom listeners, or to redefine the  
  25.         default listener, aptly named "default"  
  26.     -->  
  27.     <listeners>  
  28.         <nio-listener name="default" port="2222" implicit-ssl="true"  
  29.             idle-timeout="60" local-address="1.2.3.4">  
  30.             <ssl>  
  31.                 <keystore file="mykeystore.jks" password="secret"  
  32.                     key-password="otherSecret" />  
  33.                 <truststore file="mytruststore.jks" password="secret" />  
  34.             </ssl>  
  35.             <data-connection idle-timeout="60">  
  36.                 <active enabled="true" local-address="1.2.3.4" local-port="2323"  
  37.                     ip-check="true" />  
  38.                 <passive ports="123-125" address="1.2.3.4" external-address="1.2.3.4" />  
  39.             </data-connection>  
  40.             <blacklist>1.2.3.0/161.2.4.0/161.2.3.4</blacklist>  
  41.         </nio-listener>  
  42.     </listeners>  
  43.     <!--  
  44.         Use this section to define your Ftplets, they are configured like  
  45.         regular Spring beans  
  46.     -->  
  47.     <ftplets>  
  48.         <ftplet name="ftplet1">  
  49.             <beans:bean class="org.apache.ftpserver.examples.MyFtplet">  
  50.                 <beans:property name="foo" value="123" />  
  51.             </beans:bean>  
  52.         </ftplet>  
  53.     </ftplets>  
  54.     <!-- The user manager, choose one -->  
  55.     <file-user-manager file="users.properties"  
  56.         encrypt-passwords="true" />  
  57.     <!--<db-user-manager>  
  58.         <data-source>  
  59.             <beans:bean class="some.datasoure.class" />  
  60.         </data-source>  
  61.         <insert-user>INSERT INTO FTP_USER (userid, userpassword,  
  62.             homedirectory, enableflag, writepermission, idletime, uploadrate,  
  63.             downloadrate) VALUES ('{userid}''{userpassword}',  
  64.             '{homedirectory}',  
  65.             {enableflag}, {writepermission}, {idletime},  
  66.             {uploadrate},  
  67.             {downloadrate})  
  68.         </insert-user>  
  69.             <update-user>UPDATE FTP_USER SET  
  70.                 userpassword='{userpassword}',homedirectory='{homedirectory}',enableflag={enableflag},writepermission={writepermission},idletime={idletime},uploadrate={uploadrate},downloadrate={downloadrate}  
  71.                 WHERE userid='{userid}'  
  72.         </update-user>  
  73.             <delete-user>DELETE FROM FTP_USER WHERE userid = '{userid}'  
  74.         </delete-user>  
  75.             <select-user>SELECT userid, userpassword, homedirectory,  
  76.                 enableflag, writepermission, idletime, uploadrate, downloadrate,  
  77.                 maxloginnumber, maxloginperip FROM  
  78.                 FTP_USER WHERE userid = '{userid}'  
  79.         </select-user>  
  80.             <select-all-users>SELECT userid FROM FTP_USER ORDER BY userid  
  81.         </select-all-users>  
  82.             <is-admin>SELECT userid FROM FTP_USER WHERE userid='{userid}'  
  83.                 AND  
  84.                 userid='admin'  
  85.         </is-admin>  
  86.             <authenticate>SELECT userpassword from FTP_USER WHERE  
  87.                 userid='{userid}'</authenticate>  
  88.     </db-user-manager> -->  
  89.     <!-- The file system -->  
  90.     <native-filesystem case-insensitive="false"  
  91.         create-home="true" />  
  92.     <!--  
  93.         Use this section to define custom commands. Custom commands can also  
  94.         override already existing commands  
  95.     -->  
  96.     <commands use-default="false">  
  97.         <command name="MYHELP">  
  98.             <beans:bean class="org.apache.ftpserver.examples.MYHELP" />  
  99.         </command>  
  100.     </commands>  
  101.     <!-- Define the available languages -->  
  102.     <messages languages="se, no ,da" />  
  103. </server> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值