1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
49 getCtx().closeSockets();
50 DataChannelInfo info = null;
51 SocketProvider socketProvider = new PassiveModeSocketProvider(getCtx(), getPreferredProtocol());
52 info = socketProvider.init();
53 getCtx().setDataSocketProvider(socketProvider);
54
55
56 out(createResponseMessage(info.getProtocolIdx(), info.getAddress(), info.getPort()));
57
58
59
60
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 }