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:[email protected]:81/path/index.html?q=books#fragment");
JURI uri = JURI.create(new URI("http://user:[email protected]: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"
uri.navigate("../relative/path").toString(); // -> "blah/relative/path""
The navigate method tries to mimic browser behaviour: 'What happens if you are on the current URI and click on the (relative or absolute) link:
uri = JURI.parse("http://example.com?c=d#asdf");
uri.navigate("/a/b.html?a=b#hash").toString(); // -> "http://example.com/a/b.html?a=b#hash"
uri.navigate("../c.html").toString(); // -> "http://example.com/c.html"
uri.navigate("g.html").toString(); // -> "http://example.com/g.html"
uri.navigate("#anchor").toString(); // -> "http://example.com/g.html#anchor"
uri.navigate("/a/b.html#anchor").toString(); // -> "http://example.com/a/b.html#anchor"
uri.navigate("http://www.google.com/search?q=2").toString() // -> "http://www.google.com/search?q=2"
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.
Before building copy gradle.properties.example
to gradle.properties
.
Build using gradle build
.
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.