001    /**
002     *
003     * Copyright 2003-2006 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package javax.mail;
019    
020    import java.net.MalformedURLException;
021    import java.net.URI;
022    import java.net.URISyntaxException;
023    import java.net.URL;
024    
025    /**
026     * @version $Rev: 421852 $ $Date: 2006-07-14 03:02:19 -0700 (Fri, 14 Jul 2006) $
027     */
028    public class URLName {
029        private String file;
030        private String host;
031        private String password;
032        private int port;
033        private String protocol;
034        private String ref;
035        private String username;
036        protected String fullURL;
037        private int hashCode;
038    
039        public URLName(String url) {
040            parseString(url);
041        }
042    
043        protected void parseString(String url) {
044            URI uri;
045            try {
046                if (url == null) {
047                    uri = null;
048                } else {
049                    uri = new URI(url);
050                }
051            } catch (URISyntaxException e) {
052                uri = null;
053            }
054            if (uri == null) {
055                protocol = null;
056                host = null;
057                port = -1;
058                file = null;
059                ref = null;
060                username = null;
061                password = null;
062                return;
063            }
064    
065            protocol = checkBlank(uri.getScheme());
066            host = checkBlank(uri.getHost());
067            port = uri.getPort();
068            file = checkBlank(uri.getPath());
069            ref = checkBlank(uri.getFragment());
070            String userInfo = checkBlank(uri.getUserInfo());
071            if (userInfo == null) {
072                username = null;
073                password = null;
074            } else {
075                int pos = userInfo.indexOf(':');
076                if (pos == -1) {
077                    username = userInfo;
078                    password = null;
079                } else {
080                    username = userInfo.substring(0, pos);
081                    password = userInfo.substring(pos + 1);
082                }
083            }
084            updateFullURL();
085        }
086    
087        public URLName(String protocol, String host, int port, String file, String username, String password) {
088            this.protocol = checkBlank(protocol);
089            this.host = checkBlank(host);
090            this.port = port;
091            if (file == null || file.length() == 0) {
092                this.file = null;
093                ref = null;
094            } else {
095                int pos = file.indexOf('#');
096                if (pos == -1) {
097                    this.file = file;
098                    ref = null;
099                } else {
100                    this.file = file.substring(0, pos);
101                    ref = file.substring(pos + 1);
102                }
103            }
104            this.username = checkBlank(username);
105            if (this.username != null) {
106                this.password = checkBlank(password);
107            } else {
108                this.password = null;
109            }
110            updateFullURL();
111        }
112    
113        public URLName(URL url) {
114            protocol = checkBlank(url.getProtocol());
115            host = checkBlank(url.getHost());
116            port = url.getPort();
117            file = checkBlank(url.getFile());
118            ref = checkBlank(url.getRef());
119            String userInfo = checkBlank(url.getUserInfo());
120            if (userInfo == null) {
121                username = null;
122                password = null;
123            } else {
124                int pos = userInfo.indexOf(':');
125                if (pos == -1) {
126                    username = userInfo;
127                    password = null;
128                } else {
129                    username = userInfo.substring(0, pos);
130                    password = userInfo.substring(pos + 1);
131                }
132            }
133            updateFullURL();
134        }
135    
136        private static String checkBlank(String target) {
137            if (target == null || target.length() == 0) {
138                return null;
139            } else {
140                return target;
141            }
142        }
143    
144        private void updateFullURL() {
145            hashCode = 0;
146            StringBuffer buf = new StringBuffer(100);
147            if (protocol != null) {
148                buf.append(protocol).append(':');
149                if (host != null) {
150                    buf.append("//");
151                    if (username != null) {
152                        buf.append(username);
153                        if (password != null) {
154                            buf.append(':').append(password);
155                        }
156                        buf.append('@');
157                    }
158                    buf.append(host);
159                    if (port != -1) {
160                        buf.append(':').append(port);
161                    }
162                    if (file != null) {
163                        buf.append(file);
164                    }
165                    hashCode = buf.toString().hashCode();
166                    if (ref != null) {
167                        buf.append('#').append(ref);
168                    }
169                }
170            }
171            fullURL = buf.toString();
172        }
173    
174        public boolean equals(Object o) {
175            if (o instanceof URLName == false) {
176                return false;
177            }
178            URLName other = (URLName) o;
179            // check same protocol - false if either is null
180            if (protocol == null || other.protocol == null || !protocol.equals(other.protocol)) {
181                return false;
182            }
183    
184            if (port != other.port) {
185                return false;
186            }
187    
188            // check host - false if not (both null or both equal)
189            return areSame(host, other.host) && areSame(file, other.file) && areSame(username, other.username) && areSame(password, other.password);
190        }
191    
192        private static boolean areSame(String s1, String s2) {
193            if (s1 == null) {
194                return s2 == null;
195            } else {
196                return s1.equals(s2);
197            }
198        }
199    
200        public int hashCode() {
201            return hashCode;
202        }
203    
204        public String toString() {
205            return fullURL;
206        }
207    
208        public String getFile() {
209            return file;
210        }
211    
212        public String getHost() {
213            return host;
214        }
215    
216        public String getPassword() {
217            return password;
218        }
219    
220        public int getPort() {
221            return port;
222        }
223    
224        public String getProtocol() {
225            return protocol;
226        }
227    
228        public String getRef() {
229            return ref;
230        }
231    
232        public URL getURL() throws MalformedURLException {
233            return new URL(fullURL);
234        }
235    
236        public String getUsername() {
237            return username;
238        }
239    }