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