URI parsing and manipulation for Java.
Pass any URL into the constructor (similiar to java.net.URI but without the bugs in parsing parameters, user info, etc.):
JURI uri = JURI.parse("http://user:pass@www.test.com:81/path/index.html?q=books#fragment");
JURI uri = JURI.create(new URI("http://user:pass@www.test.com:81/path/index.html?q=books#fragment"));
Use property methods to get at the various parts (like java.net.URI but more convenient):
uri.getScheme() // http
uri.getUser() // user
uri.getPassword() // pass
uri.getHost() // www.test.com
uri.getPort() // 81
uri.getPath() // /path/index.html
uri.getPathSegments() // ["path","index.html"]
uri.getQueryParameters() // {"q": "books"} as Map<String, Collecion<String>>
uri.getFragment() // fragment
JURI can modify and extend your URIs. Setters and other modifying methods are chainable:
JURI.parse(uri)
.setScheme(scheme)
.setUserInfo(user, password)
.setHost(host)
.setPort(port)
.addPathSegment(directory)
.addQueryParameter(key, value)
.replaceQueryParameter(key, value)
.setFragment()
.toString();
Do magic with query parameters:
uri.addQueryParameter("q", "value");
uri.replaceQueryParameter("q", "value");
uri.removeQueryParameter("q");
uri.clearQueryParameters();
JURI.parse("...?q=X&q=Y").replaceQueryParameter("q", "value").toString() // -> "...?q=value"
Read, set and alter paths:
JURI uri = JURI.parse("blah/blub")
uri.addPathSegment("sub%20dir")
uri.getRawPath() // -> "blah/blub/sub%20dir"
uri.getPath() // -> "blah/blub/sub dir"
No need to worry about including the list of authors in your software product.
- Guava for escaping.
- A lib for Nullable etc.
- StringUtils from Apache lang-commons.
- slf4j-api for logging.
Why should I use JURI instead of java.net.URI? URI helps when parsing URIs. Not when building or mutating them.
Why should I use JURI instead of java.net.URL? URL does many things. And many unexpected things. An example: What do you expect this line to do:
url1.equals(url2);
Do you expect it to do blocking DNS requests and compare IP adresses? If using java.net.URL you better should...
Slightly inspired by jsuri.