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