View Javadoc

1   /*
2    * ------------------------------------------------------------------------------
3    * Hermes FTP Server
4    * Copyright (c) 2005-2007 Lars Behnke
5    * ------------------------------------------------------------------------------
6    * 
7    * This file is part of Hermes FTP Server.
8    * 
9    * Hermes FTP Server is free software; you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as published by
11   * the Free Software Foundation; either version 2 of the License, or
12   * (at your option) any later version.
13   * 
14   * Hermes FTP Server is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public License
20   * along with Hermes FTP Server; if not, write to the Free Software
21   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22   * ------------------------------------------------------------------------------
23   */
24  
25  package net.sf.hermesftp.cmd;
26  
27  import java.io.IOException;
28  
29  import net.sf.hermesftp.exception.FtpCmdException;
30  import net.sf.hermesftp.exception.FtpIllegalProtocolVersion;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  
35  /***
36   * @author Behnke
37   */
38  public abstract class AbstractFtpCmdPasv extends AbstractFtpCmd {
39  
40      private static Log log = LogFactory.getLog(AbstractFtpCmdPasv.class);
41  
42      /***
43       * {@inheritDoc}
44       */
45      public void execute() throws FtpCmdException {
46          try {
47  
48              /* Set up socket provider */
49              getCtx().closeSockets();
50              DataChannelInfo info = null;
51              SocketProvider socketProvider = new PassiveModeSocketProvider(getCtx(), getPreferredProtocol());
52              info = socketProvider.init();
53              getCtx().setDataSocketProvider(socketProvider);
54  
55              /* Send connection parameters */
56              out(createResponseMessage(info.getProtocolIdx(), info.getAddress(), info.getPort()));
57  
58              /*
59               * Connecting the client (ServerSocket.accept()) is deferred until data channel is
60               * needed.
61               */
62  
63          } catch (FtpIllegalProtocolVersion e) {
64              log.error(e.toString());
65              msgOut(MSG522);
66          } catch (IOException e) {
67              log.error(e.toString());
68              msgOut(MSG425);
69          } catch (RuntimeException e) {
70              log.error(e.toString());
71              msgOut(MSG501);
72          }
73      }
74  
75      /***
76       * {@inheritDoc}
77       */
78      public boolean isAuthenticationRequired() {
79          return true;
80      }
81  
82      /***
83       * Returns the preferred protocol version (1=IPv4, 2=IPv6, 0=undefined).
84       * 
85       * @return The protocol version.
86       */
87      protected abstract int getPreferredProtocol();
88  
89      /***
90       * Returns the reponse string encoding ip address, port and protocol type. Example: 229 Entering
91       * Extended Passive Mode (|||6000|).
92       * 
93       * @param protocol The protocol index (1=IPv4, 2=IPv6).
94       * @param addr The address.
95       * @param port The port.
96       * @return The string encoding the connection data in an appropriate format.
97       */
98      protected abstract String createResponseMessage(int protocol, String addr, int port);
99  
100 }