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            @Override
131            public String getPassword() {
132                    if(password==null && hasAuthentication()) return "";
133                    return password;
134            }
135            @Override
136            public int getPort() {
137                    return port;
138            }
139            @Override
140            public String getHostName() {
141                    return hostName;
142            }
143            @Override
144            public String getUsername() {
145                    return username;
146            }
147            @Override
148            public boolean hasAuthentication() {
149                    return username!=null && username.length()>0;
150            }
151            
152            @Override
153            public String toString() {
154                    if(username!=null) {
155                            return username+":"+password+"@"+hostName+":"+port;
156                    }
157                    return hostName+":"+port;
158            }
159    
160        @Override
161        public Server cloneReadOnly() {
162            ServerImpl s = new ServerImpl(hostName, port,username, password,tls,ssl);
163            s.readOnly=true;
164            return s;
165        }
166    
167        @Override
168        public boolean isReadOnly() {
169            return readOnly;
170        }
171    
172        @Override
173        public boolean verify() throws SMTPException {
174            return SMTPVerifier.verify(hostName,username,password,port);
175        }
176    
177            public boolean isTLS() {
178                    return tls;
179            }
180    
181            public boolean isSSL() {
182                    return ssl;
183            }
184    
185            public void setSSL(boolean ssl) {
186                    this.ssl=ssl;
187            }
188    
189            public void setTLS(boolean tls) {
190                    this.tls=tls;
191            }
192    }