1 /* 2 * Copyright 2007 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 */ 16 package org.mockftpserver.stub.command; 17 18 import org.mockftpserver.core.command.Command; 19 import org.mockftpserver.core.command.CommandHandler; 20 import org.mockftpserver.core.command.InvocationRecord; 21 import org.mockftpserver.core.command.ReplyCodes; 22 import org.mockftpserver.core.session.Session; 23 import org.mockftpserver.core.util.Assert; 24 import org.mockftpserver.core.util.PortParser; 25 26 import java.io.IOException; 27 import java.net.InetAddress; 28 29 /** 30 * CommandHandler for the PASV (Passove Mode) command. Request the Session to switch to passive 31 * data connection mode. Return a reply code of 227, along with response text of the form: 32 * "<i>Entering Passive Mode. (h1,h2,h3,h4,p1,p2)</i>", where <i>h1..h4</i> are the 4 33 * bytes of the 32-bit internet host address of the server, and <i>p1..p2</i> are the 2 34 * bytes of the 16-bit TCP port address of the data connection on the server to which 35 * the client must connect. See RFC959 for more information. 36 * <p/> 37 * Each invocation record stored by this CommandHandler contains no data elements. 38 * 39 * @author Chris Mair 40 * @version $Revision$ - $Date$ 41 */ 42 public class PasvCommandHandler extends AbstractStubCommandHandler implements CommandHandler { 43 44 /** 45 * Constructor. Initialize the replyCode. 46 */ 47 public PasvCommandHandler() { 48 setReplyCode(ReplyCodes.PASV_OK); 49 } 50 51 /** 52 * @throws IOException 53 * @see org.mockftpserver.core.command.CommandHandler#handleCommand(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session) 54 */ 55 public void handleCommand(Command command, Session session, InvocationRecord invocationRecord) 56 throws IOException { 57 58 int port = session.switchToPassiveMode(); 59 InetAddress server = session.getServerHost(); 60 61 Assert.isTrue(port > -1, "The server-side port is invalid: " + port); 62 LOG.debug("server=" + server + " port=" + port); 63 String hostAndPort = "(" + PortParser.convertHostAndPortToCommaDelimitedBytes(server, port) + ")"; 64 65 sendReply(session, hostAndPort); 66 } 67 68 } 69