Code sample - Forcing a server to work only the office hours

magine that for some weird reason your FTP server must accept connections only during certain hours: from 10:00 till 17:00. To implement this behaviour we will extend ControlConnector. The best place to insert the fix is into the configure method – where a connector configures a new connection before it goes into a connection pool:

public class TimedControlConnector extends GenericControlConnector {

  public void configure(ControlConnection connection) {
    Date cur = new Date();
    boolean timeRight = cur.compareTo(TIME_1000) > 0 &&
                                      cur.compareTo(TIME_1700) < 0;
    if (!timeRight) {
      Reply reply = (Reply) ObjectFactory.getObject(ObjectName.REPLY);
      reply.setCode("421");
      reply.setText("Service not available, closing control connection.");
      connection.reply(reply);
      connection.poison();
    } else {
      super.configure(connection);
    }
  }
}
  

We will replace the default controlConnector with a new implementation in the beans.xml file as follows:

<bean id="controlConnector"
      class="com.mysite.coloradoftp.TimedControlConnector"/>
  

Now if a user tries to connect outside of the office hours she will see 421 reply and her connection will be terminated.

Note: I recommend to extend IntelControlConnector from the Intelligent Pack plug-in rather than GenericControlConnector as shown here. But this is just an example.

sitemap