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 **/
019
020package lucee.runtime.net.ftp;
021
022
023
024
025/**
026 *  
027 */
028public final class FTPConnectionImpl implements FTPConnectionPro {
029    
030    private final String name;
031    private final String server;
032    private final String username;
033    private final String password;
034    private final int port;
035    private final int timeout;
036    private short transferMode;
037    private final boolean passive;
038    private final String proxyserver;
039    private final int proxyport;
040    private final String proxyuser;
041    private final String proxypassword;
042    private final String fingerprint;
043        private final boolean stopOnError;
044        private final boolean secure;
045
046    /**
047     * @param name
048     * @param server
049     * @param username
050     * @param password
051     * @param port
052     * @param timeout
053     * @param transferMode
054     * @param passive
055     * @param proxyserver
056     */
057    public FTPConnectionImpl(String name, String server, String username, String password,int port, int timeout, short transferMode,boolean passive, 
058                String proxyserver,int proxyport,String proxyuser, String proxypassword, 
059                String fingerprint, boolean stopOnError, boolean secure) {
060        this.name=name==null?null:name.toLowerCase().trim();
061        this.server=server;
062        this.username=username;
063        this.password=password;
064        this.port=port;
065        this.timeout=timeout;
066        this.transferMode=transferMode;
067        this.passive=passive;
068        
069        this.proxyserver=proxyserver;
070        this.proxyport=proxyport;
071        this.proxyuser=proxyuser;
072        this.proxypassword=proxypassword;
073        this.fingerprint=fingerprint;
074        this.stopOnError=stopOnError;
075        this.secure=secure;
076    }
077    @Override
078    public String getName() {
079        return name;
080    }
081    @Override
082    public String getPassword() {
083        return password;
084    }
085    @Override
086    public String getServer() {
087        return server;
088    }
089    @Override
090    public String getUsername() {
091        return username;
092    }
093    @Override
094    public boolean hasLoginData() {
095        return server!=null;// && username!=null && password!=null;
096    }
097    @Override
098    public boolean hasName() {
099        return name!=null;
100    }
101    @Override
102    public int getPort() {
103        return port;
104    }
105    @Override
106    public int getTimeout() {
107        return timeout;
108    }
109    @Override
110    public short getTransferMode() {
111        return transferMode;
112    }
113    
114
115        public void setTransferMode(short transferMode) {
116                this.transferMode=transferMode;
117        }
118    
119    @Override
120    public boolean isPassive() {
121        return passive;
122    }
123    @Override
124    public boolean loginEquals(FTPConnection conn) {
125        return 
126                server.equalsIgnoreCase(conn.getServer()) && 
127                username.equals(conn.getUsername()) && 
128                password.equals(conn.getPassword());
129    }
130    
131        @Override
132        public String getProxyPassword() {
133                return proxypassword;
134        }
135        
136        @Override
137        public int getProxyPort() {
138                return proxyport;
139        }
140        
141        @Override
142        public String getProxyServer() {
143                return proxyserver;
144        }
145        
146        @Override
147        public String getProxyUser() {
148                return proxyuser;
149        }
150        
151        public boolean equal(Object o){
152                if(!(o instanceof FTPConnection)) return false;
153                FTPConnection other=(FTPConnection) o;
154                
155                if(neq(other.getPassword(),getPassword())) return false;
156                if(neq(other.getProxyPassword(),getProxyPassword())) return false;
157                if(neq(other.getProxyServer(),getProxyServer())) return false;
158                if(neq(other.getProxyUser(),getProxyUser())) return false;
159                if(neq(other.getServer(),getServer())) return false;
160                if(neq(other.getUsername(),getUsername())) return false;
161                
162                if(other.getPort()!=getPort()) return false;
163                if(other.getProxyPort()!=getProxyPort()) return false;
164                //if(other.getTimeout()!=getTimeout()) return false;
165                if(other.getTransferMode()!=getTransferMode()) return false;
166                
167                return true;
168        }
169        
170        private boolean neq(String left, String right) {
171                if(left==null) left="";
172                if(right==null) right="";
173                
174                return !left.equals(right);
175        }
176        @Override
177        public boolean secure() {
178                return secure;
179        }
180        @Override
181        public boolean getStopOnError() {
182                return stopOnError;
183        }
184        @Override
185        public String getFingerprint() {
186                return fingerprint;
187        }
188        
189}