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.commons.io.res.type.ftp;
020
021import lucee.commons.lang.StringUtil;
022
023public final class FTPConnectionData {
024
025        public String username="";
026        public String password="";
027        public String host="localhost";
028        public int port=0;
029
030    private String proxyserver;
031    private int proxyport;
032    private String proxyuser;
033    private String proxypassword;
034        
035        
036        public String load(String path) {
037                username="";
038                password="";
039                host=null;
040                port=21;
041                // TODO impl proxy
042                
043                int atIndex=path.indexOf('@');
044                int slashIndex=path.indexOf('/');
045                if(slashIndex==-1){
046                        slashIndex=path.length();
047                        path+="/";
048                }
049                int index;
050                
051                // username/password
052                if(atIndex!=-1) {
053                        index=path.indexOf(':');
054                        if(index!=-1 && index<atIndex) {
055                                username=path.substring(0,index);
056                                password=path.substring(index+1,atIndex);
057                        }
058                        else username=path.substring(0,atIndex);
059                }
060                // host port
061                if(slashIndex>atIndex+1) {
062                        index=path.indexOf(':',atIndex+1);
063                        if(index!=-1 && index>atIndex && index<slashIndex) {
064                                host=path.substring(atIndex+1,index);
065                                port=Integer.parseInt(path.substring(index+1,slashIndex));
066                        }
067                        else host=path.substring(atIndex+1,slashIndex);
068                }
069                //if(slashIndex==-1)return "/";
070                return path.substring(slashIndex);
071        }
072
073
074
075        @Override
076        public String toString() {
077                return "username:"+username+";password:"+password+";hostname:"+host+";port:"+port;
078        }
079
080
081
082        public String key() {
083                if(StringUtil.isEmpty(username))
084                                return host+_port();
085                return username+":"+password+"@"+host+_port();
086        }
087
088
089
090        private String _port() {
091                if(port>0) return ":"+port;
092                return "";
093        }
094
095
096
097        public boolean hasProxyData() {
098                return getProxyserver()!=null;
099        }
100
101
102
103        /**
104         * @return the proxypassword
105         */
106        public String getProxypassword() {
107                return proxypassword;
108        }
109
110
111
112        /**
113         * @return the proxyport
114         */
115        public int getProxyport() {
116                return proxyport;
117        }
118
119
120
121        /**
122         * @return the proxyserver
123         */
124        public String getProxyserver() {
125                return proxyserver;
126        }
127
128
129
130        /**
131         * @return the proxyuser
132         */
133        public String getProxyuser() {
134                return proxyuser;
135        }
136        @Override
137        public boolean equals(Object obj) {
138                if(this==obj)return true;
139                if(!(obj instanceof FTPConnectionData)) return false;
140                return key().equals(((FTPConnectionData)obj).key());
141        }
142}