1 /**
2 *
3 * Copyright 2003-2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package javax.mail;
19
20 import java.net.MalformedURLException;
21 import java.net.URI;
22 import java.net.URISyntaxException;
23 import java.net.URL;
24
25 /**
26 * @version $Rev: 46019 $ $Date: 2004-09-14 02:56:06 -0700 (Tue, 14 Sep 2004) $
27 */
28 public class URLName {
29 private String file;
30 private String host;
31 private String password;
32 private int port;
33 private String protocol;
34 private String ref;
35 private String username;
36 protected String fullURL;
37 private int hashCode;
38
39 public URLName(String url) {
40 parseString(url);
41 }
42
43 protected void parseString(String url) {
44 URI uri;
45 try {
46 if (url == null) {
47 uri = null;
48 } else {
49 uri = new URI(url);
50 }
51 } catch (URISyntaxException e) {
52 uri = null;
53 }
54 if (uri == null) {
55 protocol = null;
56 host = null;
57 port = -1;
58 file = null;
59 ref = null;
60 username = null;
61 password = null;
62 return;
63 }
64
65 protocol = checkBlank(uri.getScheme());
66 host = checkBlank(uri.getHost());
67 port = uri.getPort();
68 file = checkBlank(uri.getPath());
69 ref = checkBlank(uri.getFragment());
70 String userInfo = checkBlank(uri.getUserInfo());
71 if (userInfo == null) {
72 username = null;
73 password = null;
74 } else {
75 int pos = userInfo.indexOf(':');
76 if (pos == -1) {
77 username = userInfo;
78 password = null;
79 } else {
80 username = userInfo.substring(0, pos);
81 password = userInfo.substring(pos + 1);
82 }
83 }
84 updateFullURL();
85 }
86
87 public URLName(String protocol, String host, int port, String file, String username, String password) {
88 this.protocol = checkBlank(protocol);
89 this.host = checkBlank(host);
90 this.port = port;
91 if (file == null || file.length() == 0) {
92 this.file = null;
93 ref = null;
94 } else {
95 int pos = file.indexOf('#');
96 if (pos == -1) {
97 this.file = file;
98 ref = null;
99 } 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
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
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 }