001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.net.ftp;
020
021import lucee.runtime.net.proxy.Proxy;
022import org.apache.commons.net.ftp.FTP;
023import org.apache.commons.net.ftp.FTPClient;
024
025import java.io.IOException;
026import java.net.InetAddress;
027
028
029/**
030 * Wrap a Client and a Connection
031 */
032public final class FTPWrap {
033
034    private FTPConnectionPro conn;
035    private AFTPClient client;
036    private InetAddress address;
037    private long lastAccess=0;
038
039    /**
040         * @return the lastAccess
041         */
042        public long getLastAccess() {
043                return lastAccess;
044        }
045
046
047
048        /**
049         * @param lastAccess the lastAccess to set
050         */
051        public void setLastAccess(long lastAccess) {
052                this.lastAccess = lastAccess;
053        }
054
055
056
057        /**
058     * 
059     * @param connection
060     * @throws IOException
061     */
062    public FTPWrap(FTPConnectionPro connection) throws IOException {
063        this.conn=connection;
064        this.address = InetAddress.getByName(connection.getServer());
065        connect();        
066    }
067    
068    
069    
070    /**
071     * @return Returns the connection.
072     */
073    public FTPConnection getConnection() {
074        return conn;
075    }
076
077    /**
078     * @return Returns the client.
079     */
080    public AFTPClient getClient() {
081        return client;
082    }
083
084    /**
085     * @throws IOException
086     * 
087     */
088    public void reConnect() throws IOException { 
089        try {
090            if(client!=null && client.isConnected())client.disconnect();
091        }
092        catch(IOException ioe) {}
093        connect();
094    }
095    
096    public void reConnect(short transferMode) throws IOException { 
097        if(transferMode!=conn.getTransferMode())
098                ((FTPConnectionImpl)conn).setTransferMode(transferMode);
099        reConnect();
100    }
101
102    /**
103     * connects the client
104     * @throws IOException
105     */
106    private void connect() throws IOException { 
107        
108        client=AFTPClient.getInstance(conn.secure(), address, conn.getPort(), 
109                        conn.getUsername(),conn.getPassword(), conn.getFingerprint(), conn.getStopOnError());
110        
111        setConnectionSettings(client,conn);
112        
113        // transfer mode
114        if(conn.getTransferMode()==FTPConstant.TRANSFER_MODE_ASCCI) getClient().setFileType(FTP.ASCII_FILE_TYPE);
115        else if(conn.getTransferMode()==FTPConstant.TRANSFER_MODE_BINARY) getClient().setFileType(FTP.BINARY_FILE_TYPE);
116        
117        
118        
119        // Connect
120        try {
121                Proxy.start(
122                        conn.getProxyServer(), 
123                        conn.getProxyPort(), 
124                        conn.getProxyUser(), 
125                        conn.getProxyPassword()
126            );
127                client.connect();
128        }
129        finally {
130                Proxy.end();
131        }
132    }
133
134
135
136        static void setConnectionSettings(AFTPClient client, FTPConnection conn) {
137                if(client==null) return;
138                
139                // timeout
140        client.setTimeout(conn.getTimeout()*1000);
141        
142        // passive/active Mode
143                int mode = client.getDataConnectionMode();
144        if(conn.isPassive()) {
145                if(FTPClient.PASSIVE_LOCAL_DATA_CONNECTION_MODE!=mode)
146                        client.enterLocalPassiveMode();
147        }
148        else {
149                if(FTPClient.ACTIVE_LOCAL_DATA_CONNECTION_MODE!=mode)
150                        client.enterLocalActiveMode();
151        }
152        }
153}