EprtCommandHandler.java revision 3e469b93fd10bc09ea2c088516168bf6a5cbaa43
1/*
2 * Copyright 2009 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.mockftpserver.stub.command;
17
18import org.mockftpserver.core.command.Command;
19import org.mockftpserver.core.command.CommandHandler;
20import org.mockftpserver.core.command.InvocationRecord;
21import org.mockftpserver.core.command.ReplyCodes;
22import org.mockftpserver.core.session.Session;
23import org.mockftpserver.core.util.PortParser;
24import org.mockftpserver.core.util.HostAndPort;
25
26import java.net.InetAddress;
27import java.net.UnknownHostException;
28
29/**
30 * CommandHandler for the EPRT command. Send back a reply code of 200.
31 * <p/>
32 * Each invocation record stored by this CommandHandler includes the following data element key/values:
33 * <ul>
34 * <li>{@link #HOST_KEY} ("host") - the client data host (InetAddress) submitted on the invocation (from parameters 1-4)
35 * <li>{@link #PORT_KEY} ("port") - the port number (Integer) submitted on the invocation (from parameter 5-6)
36 * </ul>
37 * See RFC2428 for more information.
38 *
39 * @author Chris Mair
40 * @version $Revision$ - $Date$
41 */
42public class EprtCommandHandler extends AbstractStubCommandHandler implements CommandHandler {
43
44    public static final String HOST_KEY = "host";
45    public static final String PORT_KEY = "port";
46
47    /**
48     * Constructor. Initialize the replyCode.
49     */
50    public EprtCommandHandler() {
51        setReplyCode(ReplyCodes.EPRT_OK);
52    }
53
54    /**
55     * Handle the command
56     *
57     * @throws java.net.UnknownHostException
58     * @see org.mockftpserver.core.command.CommandHandler#handleCommand(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session)
59     */
60    public void handleCommand(Command command, Session session, InvocationRecord invocationRecord) throws UnknownHostException {
61        String parameter = command.getRequiredParameter(0);
62
63        HostAndPort client = PortParser.parseExtendedAddressHostAndPort(parameter);
64        LOG.debug("host=" + client.host + " port=" + client.port);
65        session.setClientDataHost(client.host);
66        session.setClientDataPort(client.port);
67        invocationRecord.set(HOST_KEY, client.host);
68        invocationRecord.set(PORT_KEY, new Integer(client.port));
69        sendReply(session);
70    }
71
72}