summaryrefslogtreecommitdiffstats
blob: f210ee4503d97c8a9063c382029fe73558f96bdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
 * Copyright (c) 2010-2011 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */
/*
 * Copyright (c) 2015 Tarun Gupta.
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

package com.isode.stroke.base;

import com.isode.stroke.base.NotNull;
import java.net.URLDecoder;
import java.io.UnsupportedEncodingException;

public class URL {

	private String scheme;
	private String user;
	private String password;
	private String host;
	private Integer port = null;
	private String path;
	private boolean empty;

	/**
	* Default constructor.
	*/
	public URL() {
		this("", "", "", "", null, "", true);
	}

	/**
	* Parameterized Constructor
	* @param scheme notnull
	* @param host notnull
	* @param path notnull
	*/
	public URL(String scheme, String host, String path) {
		this(scheme, "", "", host, null, path, false);
	}

	/**
	* Parameterized Constructor
	* @param scheme notnull
	* @param host notnull
	* @param port
	* @param path notnul
	*/
	public URL(String scheme, String host, int port, String path) {
		this(scheme, "", "", host, port, path, false);
	}

	/**
	* Parameterized Constructor
	* @param scheme notnull
	* @param user notnull
	* @param password notnull
	* @param host notnull
	* @param port
	* @param path notnul
	* @param empty notnull
	*/
	public URL(String scheme, String user, String password, String host, Integer port, String path, boolean empty) {
		NotNull.exceptIfNull(scheme, "scheme");
		NotNull.exceptIfNull(user, "user");
		NotNull.exceptIfNull(password, "password");
		NotNull.exceptIfNull(host, "host");
		NotNull.exceptIfNull(path, "path");
		NotNull.exceptIfNull(empty, "empty");
		this.scheme = scheme;
		this.user = user;
		this.password = password;
		this.host = host;
		this.port = port;
		this.path = path;
		this.empty = empty;
	}

	/**
	* Whether the URL is empty.
	*/
	public boolean isEmpty() {
		return empty;
	}

	/**
	* Scheme used for the URL (http, https etc.)
	*/
	public String getScheme() {
		return scheme;
	}

	/**
	* Hostname
	*/
	public String getHost() {
		return host;
	}

	/**
	* Port number
	*/
	public Integer getPort() {
		return port;
	}

	/**
	* Path
	*/
	public String getPath() {
		return path;
	}

	/**
	* @return Complete URL with all its parameters and format.
	*/
	public String toString() {
		if (empty) {
			return "";
		}
		String result = scheme + "://";
		if (!user.isEmpty()) {
			result += user;
			if (!password.isEmpty()) {
				result += ":" + password;
			}
			result += "@";
		}
		result += host;
		if (port != null) {
			result += ":";
			result += Integer.toString(port);
		}
		result += path;
		return result;
	}

	/**
	* @return port number based on scheme.
	*/
	public static int getPortOrDefaultPort(URL url) {
		NotNull.exceptIfNull(url, "url");
		if (url.getPort() != null) {
			return url.getPort();
		}
		else if (url.getScheme() == "http") {
			return 80;
		}
		else if (url.getScheme() == "https") {
			return 443;
		}
		else {
			System.err.println("Unknown scheme: " + url.getScheme());
			return 80;
		}
	}

	/**
	* @param URL String which has to be processed for extraction of parameters.
	* @return URL object after extracting all parameters from urlString.
	*/
	public static URL fromString(String urlString) {
		NotNull.exceptIfNull(urlString, "urlString");
		int colonIndex = urlString.indexOf(':');
		if (colonIndex == -1) {
			return new URL();
		}
		String scheme = urlString.substring(0, colonIndex);

		// Authority
		if (urlString.length() > colonIndex + 2 && urlString.charAt(colonIndex+1) == '/' && urlString.charAt(colonIndex+2) == '/') {
			int authorityIndex = colonIndex + 3;
			int slashIndex = urlString.indexOf('/', authorityIndex);
			String authority = "";
			String path = "";
			if (slashIndex == -1) {
				authority = urlString.substring(authorityIndex);
				path = "";
			}
			else {
				authority = urlString.substring(authorityIndex, slashIndex);
				path = unescape(urlString.substring(slashIndex));
			}

			int atIndex = authority.indexOf('@');
			String userInfo = "";
			String hostAndPort = "";
			if (atIndex != -1) {
				userInfo = authority.substring(0, atIndex);
				hostAndPort = authority.substring(atIndex + 1);
			}
			else {
				userInfo = "";
				hostAndPort = authority;
			}

			String host = "";
			Integer port = null;
			colonIndex = hostAndPort.indexOf(':');
			if (colonIndex != -1) {
				host = unescape(hostAndPort.substring(0, colonIndex));
				try {
					port = Integer.valueOf(hostAndPort.substring(colonIndex + 1));
				}
				catch (NumberFormatException e) {
					return new URL();
				}
			}
			else {
				host = unescape(hostAndPort);
			}

			if (port != null) {
				return new URL(scheme, host, port, path);
			}
			else {
				return new URL(scheme, host, path);
			}
		}
		else {
			// We don't support URLs without authorities yet
			return new URL();
		}
	}

	/**
	* @return URL String after decoding hexadecimal bits.
	*/
	public static String unescape(String str) {
		try {
			return URLDecoder.decode(str, "UTF-8");
		}
		catch(IllegalArgumentException e) {
			System.err.println(e.getMessage());
			return "";
		}
		catch(UnsupportedEncodingException e) {
			System.err.println(e.getMessage());
			return "";
		}
	}
}