foo://example.com:8042/over/there?name=ferret#nose \_/ \______________/\_________/ \_________/ \__/ | | | | | scheme authority path query fragment
The URL class in Java is too stringent to accept our use cases here, namely you can not create a URL class that excludes the scheme and authority portions of a URL. However, Java does include a URI class which is suitable for our needs. The URLBuilder class doesn't help you build the scheme or authority parts of a URL. The URI class in java.net handles that part perfectly well. This class helps you with the path, query, and fragment parts of a URL.
Since this class relies on the URI class to handle the scheme and authority parts of a URI, we frequently refer to them together. We've chosen the term base
. It's short and descriptive.
Here's a few examples of using URLBuilder to construct URL's. Here's an easy one:
Here's one with a query string: URI base = new URI("http://www.example.com"); URLBuilder urlb = new URLBuilder(base); urlb.add("yellow").add("brick"); urlb.add("road.html"); assertEquals("http://www.example.com/yellow/brick/road.html",urlb.toString());
All of the {@link #add(Object)} and {@link #addQuery(Object,Object)} methodsdo their thing, but also return URI base = new URI("http://www.example.com"); URLBuilder urlb = new URLBuilder(base); urlb.add("people.xml").addQuery("title","manager"); assertEquals("http://www.example.com/people.xml?title=manager",urlb.toString());
this
. Which means you can chain the methods together like we have done in both of our examples so far. We don't have to have a base, we can create just the path component and the query string:.
@version $LastChangedRevision$ URLBuilder urlb = new URLBuilder(); urlb.add("people.xml") urlb.addQuery("title","manager"); assertEquals("people.xml?title=manager",urlb.toString());
$LastChangedDate$
@author $LastChangedBy$
Part | Description | Default | Example |
---|---|---|---|
protocol | The protocol. If specified, must not be null / empty. | The protocol from {@link GWT#getHostPageBaseURL()} | http, https |
host | The hostname. Can include a port. If specified, must not be null / empty. | The host from {@link GWT#getHostPageBaseURL()} | 127.0.0.1, www.esoves.de:8080 |
port | The port number. If specified must be something between {@link #MIN_PORT}and {@link #MAX_PORT} | The port from {@link GWT#getHostPageBaseURL()} | 80, 8080 |
context | The context path. If specified, must not be null / empty. In case the context starts with a '/' there won't be a double '//' in the URL. | The context from {@link GWT#getHostPageBaseURL()} | foo, /bar, / |
module | Some kind of REST module. If you have many resources you might want to subdivide your resources in different modules like 'admin', 'export', etc. If specified, must not be null / empty. | nothing | admin, common, export |
path | The actual resource paths. At least one path must be provided. In case the context starts with a '/' there won't be a double '//' in the URL. | nothing | [user, 0815], [/users, 0815, /groups, 12] |
query | Query parameters. You can specify 0-n values for one parameter. If specified, the name must not be null / empty. | nothing | [foo=[bar], options=[a, b, x]] |
Here are some examples of URLs you can build with this class:
new UrlBuilder().toUrl()
→ http://localhost/ new UrlBuilder().toUrl()
→ http://localhost/ new UrlBuilder().toUrl()
→ http://localhost/ new UrlBuilder().toUrl()
→ http://localhost/ Simple class that encapsulates the process of building up a URL from a path fragment and a zero or more parameters. Parameters can be single valued, array valued or collection valued. In the case of arrays and collections, each value in the array or collection will be added as a separate URL parameter (with the same name). The assembled URL can then be retrieved by calling toString().
While not immediately obvious, it is possible to add a single parameter with multiple values by invoking the addParameter() method that uses varargs, and supplying a Collection as the single parameter value to the method.
@author Tim Fennell @since Stripes 1.1.2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|