001    package railo.runtime.net.mail;
002    
003    import railo.commons.lang.StringUtil;
004    import railo.runtime.exp.ExpressionException;
005    import railo.runtime.op.Caster;
006    
007    
008    /**
009     * 
010     */
011    public final class ServerImpl implements Server {
012            
013            private String hostName;
014            private String username;
015            private String password;
016            private int port=DEFAULT_PORT;
017            private boolean readOnly=false;
018            private boolean tls;
019            private boolean ssl;
020            //private static Pattern[] patterns=new Pattern[3];
021        
022            //[user:password@]server[:port],[
023            /*static {
024                    patterns[0]=Pattern.compile("^([^:\\s)]+)\\s*:\\s*([^@\\s)]+)\\s*@\\s*([^:\\s)]+)\\s*:\\s*(.+)$");
025                    patterns[1]=Pattern.compile("^([^:\\s)]+)\\s*:\\s*([^@\\s)]+)\\s*@\\s*(.+)$");
026                    patterns[2]=Pattern.compile("^([^:\\s)]+)\\s*:\\s*(.+)$");
027                    
028            }*/
029            
030            public static ServerImpl getInstance(String host, int defaultPort,String defaultUsername,String defaultPassword, boolean defaultTls, boolean defaultSsl) throws MailException {
031                    
032                    String userpass,user=defaultUsername,pass=defaultPassword,tmp;
033                    int port=defaultPort;
034                    
035                    // [user:password@]server[:port]
036                    int index=host.indexOf('@');
037                            
038                    // username:password
039                    if(index!=-1) {
040                            userpass=host.substring(0,index);
041                            host=host.substring(index+1);
042                            
043                            index=userpass.indexOf(':');
044                            if(index!=-1) {
045                                    user=userpass.substring(0,index).trim();
046                                    pass=userpass.substring(index+1).trim();
047                            }
048                            else user=userpass.trim();
049            }
050    
051                    // server:port
052                    index=host.indexOf(':');
053                    if(index!=-1) {
054                            tmp=host.substring(index+1).trim();
055                            if(!StringUtil.isEmpty(tmp)){
056                                    try {
057                                            port=Caster.toIntValue(tmp);
058                                    } catch (ExpressionException e) {
059                                            throw new MailException("port definition is invalid ["+tmp+"]");
060                                    }
061                            }
062                            host=host.substring(0,index).trim();
063                    }
064                    else host=host.trim();
065    
066                            
067                    return new ServerImpl(host,port,user,pass,defaultTls,defaultSsl);
068            }
069            
070    
071            /*public ServerImpl(String server,int port) {
072                    this.hostName=server;
073                    this.port=port;
074            }*/
075            
076            public ServerImpl(String hostName,int port,String username,String password, boolean tls, boolean ssl) {
077                    this.hostName=hostName;
078                    this.username=username;
079                    this.password=password;
080                    this.port=port;
081                    this.tls=tls;
082                    this.ssl=ssl;
083            }
084            
085            /*public ServerImpl(String strServer) throws MailException {
086                    strServer=strServer.trim();
087                    boolean hasMatch=false;
088                    outer:for(int i=0;i<patterns.length;i++) {
089                            Pattern p = patterns[i];
090                            Matcher m = p.matcher(strServer);
091                            
092                            if(m.matches()) {
093                                    try {
094                                            switch(m.groupCount()) {
095                                                    case 2:
096                                                            hostName=m.group(1).trim();
097                                                            port=Caster.toIntValue(m.group(2).trim());
098                                                    break;
099                                                    case 4:
100                                                            username=m.group(1).trim();
101                                                            password=m.group(2).trim();
102                                                            hostName=m.group(3).trim();
103                                                            port=Caster.toIntValue(m.group(4).trim());
104                                                    break;
105                                            }
106                                    }
107                                    catch(ExpressionException e) {
108                                            throw new MailException(e.getMessage());
109                                    }
110                                    hasMatch=true;
111                                    break outer;
112                            }
113                    }
114                    if(!hasMatch) hostName=strServer;
115            }*/
116            
117            /*public static Server[] factory(String strServers) throws MailException {
118                    StringTokenizer tokens=new StringTokenizer(strServers,",;");
119                    ArrayList list=new ArrayList();
120                    
121                    while(tokens.hasMoreTokens()) {
122                            list.add(new ServerImpl(tokens.nextToken()));
123                    }
124                    Server[] pairs=(Server[])list.toArray(new Server[list.size()]);
125                    return pairs;
126                    
127                    
128            }*/
129    
130            /**
131         * @see railo.runtime.net.mail.Server#getPassword()
132         */
133            public String getPassword() {
134                    if(password==null && hasAuthentication()) return "";
135                    return password;
136            }
137            /**
138         * @see railo.runtime.net.mail.Server#getPort()
139         */
140            public int getPort() {
141                    return port;
142            }
143            /**
144         * @see railo.runtime.net.mail.Server#getHostName()
145         */
146            public String getHostName() {
147                    return hostName;
148            }
149            /**
150         * @see railo.runtime.net.mail.Server#getUsername()
151         */
152            public String getUsername() {
153                    return username;
154            }
155            /**
156         * @see railo.runtime.net.mail.Server#hasAuthentication()
157         */
158            public boolean hasAuthentication() {
159                    return username!=null && username.length()>0;
160            }
161            
162            /**
163             * @see java.lang.Object#toString()
164             */
165            public String toString() {
166                    if(username!=null) {
167                            return username+":"+password+"@"+hostName+":"+port;
168                    }
169                    return hostName+":"+port;
170            }
171    
172        /**
173         * @see railo.runtime.net.mail.Server#cloneReadOnly()
174         */
175        public Server cloneReadOnly() {
176            ServerImpl s = new ServerImpl(hostName, port,username, password,tls,ssl);
177            s.readOnly=true;
178            return s;
179        }
180    
181        /**
182         * @see railo.runtime.net.mail.Server#isReadOnly()
183         */
184        public boolean isReadOnly() {
185            return readOnly;
186        }
187    
188        /**
189         * @see railo.runtime.net.mail.Server#verify()
190         */
191        public boolean verify() throws SMTPException {
192            return SMTPVerifier.verify(hostName,username,password,port);
193        }
194    
195            public boolean isTLS() {
196                    return tls;
197            }
198    
199            public boolean isSSL() {
200                    return ssl;
201            }
202    
203            public void setSSL(boolean ssl) {
204                    this.ssl=ssl;
205            }
206    
207            public void setTLS(boolean tls) {
208                    this.tls=tls;
209            }
210    }