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.proxy;
020
021import java.io.Serializable;
022
023import lucee.commons.lang.StringUtil;
024
025public class ProxyDataImpl implements ProxyData,Serializable {
026        
027        public static final ProxyData NO_PROXY = new ProxyDataImpl();
028        
029        private String server;
030        private int port=-1;
031        private String username;
032        private String password;
033        
034
035        public ProxyDataImpl(String server, int port, String username, String password) {
036                if(!StringUtil.isEmpty(server,true))this.server = server;
037                if(port>0)this.port = port;
038                if(!StringUtil.isEmpty(username,true))this.username = username;
039                if(!StringUtil.isEmpty(password,true))this.password = password;
040        }
041        public ProxyDataImpl() {}
042        
043        public void release() {
044                server=null;
045                port=-1;
046                username=null;
047                password=null;
048        }
049        
050        /**
051         * @return the password
052         */
053        public String getPassword() {
054                return password;
055        }
056        /**
057         * @param password the password to set
058         */
059        public void setPassword(String password) {
060                this.password = password;
061        }
062        /**
063         * @return the port
064         */
065        public int getPort() {
066                return port;
067        }
068        /**
069         * @param port the port to set
070         */
071        public void setPort(int port) {
072                this.port = port;
073        }
074        /**
075         * @return the server
076         */
077        public String getServer() {
078                return server;
079        }
080        /**
081         * @param server the server to set
082         */
083        public void setServer(String server) {
084                this.server = server;
085        }
086        /**
087         * @return the username
088         */
089        public String getUsername() {
090                return username;
091        }
092        /**
093         * @param username the username to set
094         */
095        public void setUsername(String username) {
096                this.username = username;
097        }
098        
099        public boolean equals(Object obj){
100                if(obj==this) return true;
101                if(!(obj instanceof ProxyData)) return false;
102                
103                ProxyData other=(ProxyData) obj;
104                
105                return _eq(other.getServer(),server) && _eq(other.getUsername(),username) && _eq(other.getPassword(),password) && other.getPort()==port;
106                
107        }
108        
109        private boolean _eq(String left, String right) {
110                if(left==null) return right==null;
111                return left.equals(right);
112        }
113        
114        public static boolean isValid(ProxyData pd){
115                if(pd==null || pd.equals(NO_PROXY)) return false;
116                return true;
117        }
118        public static boolean hasCredentials(ProxyData data) {
119                return StringUtil.isEmpty(data.getUsername(),true);
120        }
121        public static ProxyData getInstance(String proxyserver, int proxyport, String proxyuser, String proxypassword) {
122                if(StringUtil.isEmpty(proxyserver,true)) return null;
123                return new ProxyDataImpl(proxyserver,proxyport,proxyuser,proxypassword);
124        }
125        
126        public String toString(){
127                return "server:"+server+";port:"+port+";user:"+username+";pass:"+password;
128        }
129}