1/* 2 * Copyright 2008 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.apache.log4j.Logger; 19import org.mockftpserver.core.command.Command; 20import org.mockftpserver.core.command.CommandHandler; 21import org.mockftpserver.core.command.InvocationRecord; 22import org.mockftpserver.core.command.ReplyCodes; 23import org.mockftpserver.core.session.Session; 24import org.mockftpserver.core.util.PortParser; 25 26import java.net.InetAddress; 27import java.net.UnknownHostException; 28 29/** 30 * CommandHandler for the PORT 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 * 38 * @author Chris Mair 39 * @version $Revision$ - $Date$ 40 */ 41public final class PortCommandHandler extends AbstractStubCommandHandler implements CommandHandler { 42 43 public static final String HOST_KEY = "host"; 44 public static final String PORT_KEY = "port"; 45 private static final Logger LOG = Logger.getLogger(PortCommandHandler.class); 46 47 /** 48 * Constructor. Initialize the replyCode. 49 */ 50 public PortCommandHandler() { 51 setReplyCode(ReplyCodes.PORT_OK); 52 } 53 54 /** 55 * Handle the command 56 * 57 * @throws 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 InetAddress host = PortParser.parseHost(command.getParameters()); 62 int port = PortParser.parsePortNumber(command.getParameters()); 63 LOG.debug("host=" + host + " port=" + port); 64 session.setClientDataHost(host); 65 session.setClientDataPort(port); 66 invocationRecord.set(HOST_KEY, host); 67 invocationRecord.set(PORT_KEY, new Integer(port)); 68 sendReply(session); 69 } 70 71} 72