Utility class for constructing syntactically correct HTTP URLs using a fluent method-chaining API. It strives simply to be more robust than manually constructing URLs by string concatenation.
Made with ❤️ by Widen.
- Automatic slash management in paths. Slashes will be de-duped or added as necessary when using addPathSegment
- Automatic URL encoding for both path segments (preserving slashes) and query parameters
- Encoder is user replaceable; two implementations are provided:
- Default BuiltinEncoder uses
java.net.UrlEncoder
- NoEncodingEncoder uses text as-is
- Use
usingEncoder(Encoder encoder)
to set default;addParameter(String key, Object value, Encoder encoder)
can be used to override Encoder for a single parameter
- Default BuiltinEncoder uses
- Encoder is user replaceable; two implementations are provided:
- Options for generation of fully-qualified, hostname relative, or protocol relative URLs
- Fluent method-chaining API
- More examples in UrlBuilderTest
With Gradle:
compile 'com.widen:urlbuilder:{version}'
Other dependency managers should be similar.
new UrlBuilder("my.host.com", "foo").addPathSegment("bar").addParameter("a", "b").toString()
produces http://my.host.com/foo/bar?a=b
new UrlBuilder("my.host.com", "foo & bar").addParameter("1&2", "3&4").addParameter("a", "b&c").toString()
produces http://my.host.com/foo%20%26%20bar?1%262=3%264&a=b%26c
S3 Flavored UrlBuilder
S3UrlBuilder
provides specialized functionality building for S3 URLsexpireIn
andexpireAt
for time-bombing S3 links- All bucket reference methods supported:
- virtual bucket (
http://bucket.example.com/key.txt
) - bucket in path (
https://s3.amazonaws.com/bucket.example.com/key.txt
) - hostname (
http://bucket.example.com.s3.amazonaws.com/key.txt
)
- virtual bucket (
withAttachmentFilename(String filename)
generates requiredContent-Disposition
header for browser file download prompt
Cloudfront Flavored UrlBuilder
CloudfrontUrlBuilder
provides specialized functionality for building CloudFront URLsexpireIn
andexpireAt
for time-bombing CloudFront linkswithAttachmentFilename(String filename)
generates requiredContent-Disposition
header for browser file download prompt
Licensed under the Apache Version 2.0 license. See the license file for details.