diff options
author | Tarun Gupta <tarun1995gupta@gmail.com> | 2015-05-26 17:43:14 (GMT) |
---|---|---|
committer | Tarun Gupta <tarun1995gupta@gmail.com> | 2015-05-27 17:39:37 (GMT) |
commit | a790b9a2cf6c138e6fecf614f346993153aabc82 (patch) | |
tree | c5f314cab0c2778686cade97c3b6a1d502dfe8c5 /src/com | |
parent | 9416cec9c245e8998f29071635be4e416946e1a4 (diff) | |
download | stroke-a790b9a2cf6c138e6fecf614f346993153aabc82.zip stroke-a790b9a2cf6c138e6fecf614f346993153aabc82.tar.bz2 |
Add the functionality for processing of URL.
Adds functionalities for URL processing such as extracting parameters from URL, decoding URL.
License:
This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details.
Test-Information:
Corresponding tests are added for each function which passes.
Change-Id: I1d54b86167b4f368c365f16c63641a6e6a91cbb8
Diffstat (limited to 'src/com')
-rw-r--r-- | src/com/isode/stroke/base/URL.java | 245 |
1 files changed, 245 insertions, 0 deletions
diff --git a/src/com/isode/stroke/base/URL.java b/src/com/isode/stroke/base/URL.java new file mode 100644 index 0000000..f210ee4 --- /dev/null +++ b/src/com/isode/stroke/base/URL.java @@ -0,0 +1,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 ""; + } + } +}
\ No newline at end of file |