iodataconnectionfactory.java

来自「JAVA FTP 上传下载 的源文件」· Java 代码 · 共 475 行 · 第 1/2 页

JAVA
475
字号
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements.  See the NOTICE file * distributed with this work for additional information * regarding copyright ownership.  The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License.  You may obtain a copy of the License at * *  http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied.  See the License for the * specific language governing permissions and limitations * under the License. */package org.apache.ftpserver.impl;import java.net.InetAddress;import java.net.InetSocketAddress;import java.net.ServerSocket;import java.net.Socket;import java.net.SocketAddress;import java.net.UnknownHostException;import javax.net.ssl.SSLContext;import javax.net.ssl.SSLSocket;import javax.net.ssl.SSLSocketFactory;import org.apache.ftpserver.DataConnectionConfiguration;import org.apache.ftpserver.DataConnectionException;import org.apache.ftpserver.ftplet.DataConnection;import org.apache.ftpserver.ftplet.FtpException;import org.apache.ftpserver.ssl.ClientAuth;import org.apache.ftpserver.ssl.SslConfiguration;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** * <strong>Internal class, do not use directly.</strong> *  * We can get the FTP data connection using this class. It uses either PORT or * PASV command. * * @author The Apache MINA Project (dev@mina.apache.org) * @version $Rev$, $Date$ */public class IODataConnectionFactory implements ServerDataConnectionFactory {    private final Logger LOG = LoggerFactory            .getLogger(IODataConnectionFactory.class);    private FtpServerContext serverContext;    private Socket dataSoc;    ServerSocket servSoc;    InetAddress address;    int port = 0;    long requestTime = 0L;    boolean passive = false;    boolean secure = false;    private boolean isZip = false;    InetAddress serverControlAddress;    FtpIoSession session;    public IODataConnectionFactory(final FtpServerContext serverContext,            final FtpIoSession session) {        this.session = session;        this.serverContext = serverContext;        if (session.getListener().getDataConnectionConfiguration()                .isImplicitSsl()) {            secure = true;        }    }    /**     * Close data socket.     */    public synchronized void closeDataConnection() {        // close client socket if any        if (dataSoc != null) {            try {                dataSoc.close();            } catch (Exception ex) {                LOG.warn("FtpDataConnection.closeDataSocket()", ex);            }            dataSoc = null;        }        // close server socket if any        if (servSoc != null) {            try {                servSoc.close();            } catch (Exception ex) {                LOG.warn("FtpDataConnection.closeDataSocket()", ex);            }            FtpServerContext ctx = serverContext;            if (ctx != null) {                DataConnectionConfiguration dcc = session.getListener()                        .getDataConnectionConfiguration();                if (dcc != null) {                    dcc.releasePassivePort(port);                }            }            servSoc = null;        }        // reset request time        requestTime = 0L;    }    /**     * Port command.     */    public synchronized void initActiveDataConnection(            final InetSocketAddress address) {        // close old sockets if any        closeDataConnection();        // set variables        passive = false;        this.address = address.getAddress();        port = address.getPort();        requestTime = System.currentTimeMillis();    }    private SslConfiguration getSslConfiguration() {        DataConnectionConfiguration dataCfg = session.getListener()                .getDataConnectionConfiguration();        SslConfiguration configuration = dataCfg.getSslConfiguration();        // fall back if no configuration has been provided on the data connection config        if (configuration == null) {            configuration = session.getListener().getSslConfiguration();        }        return configuration;    }    /**     * Initiate a data connection in passive mode (server listening). It returns     * the success flag.     */    public synchronized InetSocketAddress initPassiveDataConnection()            throws DataConnectionException {        LOG.debug("Initiating passive data connection");        // close old sockets if any        closeDataConnection();        // get the passive port        int passivePort = session.getListener()                .getDataConnectionConfiguration().requestPassivePort();        if (passivePort == -1) {            servSoc = null;            throw new DataConnectionException(                    "Cannot find an available passive port.");        }        // open passive server socket and get parameters        try {            DataConnectionConfiguration dataCfg = session.getListener()                    .getDataConnectionConfiguration();            String passiveAddress = dataCfg.getPassiveAddress();            if (passiveAddress == null) {                address = serverControlAddress;            } else {                address = resolveAddress(dataCfg.getPassiveAddress());            }            if (secure) {                LOG                        .debug(                                "Opening SSL passive data connection on address \"{}\" and port {}",                                address, passivePort);                SslConfiguration ssl = getSslConfiguration();                if (ssl == null) {                    throw new DataConnectionException(                            "Data connection SSL required but not configured.");                }                // this method does not actually create the SSL socket, due to a JVM bug                 // (https://issues.apache.org/jira/browse/FTPSERVER-241).                // Instead, it creates a regular                // ServerSocket that will be wrapped as a SSL socket in createDataSocket()                servSoc = new ServerSocket(passivePort, 0, address);                LOG                        .debug(                                "SSL Passive data connection created on address \"{}\" and port {}",                                address, passivePort);            } else {                LOG                        .debug(                                "Opening passive data connection on address \"{}\" and port {}",                                address, passivePort);                servSoc = new ServerSocket(passivePort, 0, address);                LOG                        .debug(                                "Passive data connection created on address \"{}\" and port {}",                                address, passivePort);            }            port = servSoc.getLocalPort();            servSoc.setSoTimeout(dataCfg.getIdleTime() * 1000);            // set different state variables            passive = true;            requestTime = System.currentTimeMillis();            return new InetSocketAddress(address, port);        } catch (Exception ex) {            servSoc = null;            closeDataConnection();            throw new DataConnectionException(                    "Failed to initate passive data connection: "                            + ex.getMessage(), ex);        }    }    /*

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?