UrlBuilder builds URLs from their parts. Give it a host, path segments, and query parameters, and it places the slashes,
adds the ? and & separators, and percent-encodes each value. It is a small Java library with a fluent, chainable
API.
Made with ❤️ by Widen.
Building a URL by hand looks simple. It breaks as soon as a value differs from what you expected:
String host = "files.example.com";
String folder = "/reports/2024/";
String fileName = "Q3 summary #2.pdf";
String search = "cats & dogs";
int page = 2;
String url = "https://" + host + "/" + folder + "/" + fileName + "?q=" + search + "&page=" + page;
// https://files.example.com//reports/2024//Q3 summary #2.pdf?q=cats & dogs&page=2That URL has four bugs:
- The slashes double up, because
folderalready starts and ends with one. - The spaces stay unencoded.
- The
#starts a fragment, so the server never receives2.pdfor the query string. - The
&in the search text ends theqparameter early.
UrlBuilder takes the same values and gets all four right:
String url = new UrlBuilder(host, folder)
.usingSsl()
.addPathSegment(fileName)
.addParameter("q", search)
.addParameter("page", page)
.toString();
// https://files.example.com/reports/2024/Q3%20summary%20%232.pdf?q=cats%20%26%20dogs&page=2The JDK classes parse and validate a URL, but they do little to build one. The obvious attempt still gets this URL wrong:
new URI("https", host, "/" + folder + "/" + fileName, "q=" + search + "&page=" + page, null).toString()
// https://files.example.com//reports/2024//Q3%20summary%20%232.pdf?q=cats%20&%20dogs&page=2The path is encoded now, but the slashes still double up, and the & in the search text still ends the q parameter. A
correct version that uses only the JDK looks like this:
// Nothing in the JDK joins path segments, so fix the slashes yourself
String path = ("/" + folder + "/" + fileName).replaceAll("/+", "/");
// URLEncoder writes a space as "+", which is correct only for form data
String query = "q=" + URLEncoder.encode(search, "UTF-8").replace("+", "%20") + "&page=" + page;
// The URI constructor would encode the "%" signs in the query again, so add the query afterward
String url = new URI("https", host, path, null).toASCIIString() + "?" + query;
// https://files.example.com/reports/2024/Q3%20summary%20%232.pdf?q=cats%20%26%20dogs&page=2The example works around the first five of these problems:
- No JDK class joins path segments. You place and trim the slashes yourself.
- The multi-argument
URIconstructors leave&,=, and+in the query unencoded. Those characters are legal in a query string, and the constructor cannot tell a separator from part of a value. You encode every name and value yourself. - Those constructors always encode
%. They encode the%in a value you already encoded, so%26becomes%2526. That is why the example adds the query afterward. URLEncoderimplements HTML form encoding, not URL encoding. It writes a space as+, and a path reads+as a literal plus sign.URI.toString()leaves characters outside ASCII unencoded.toASCIIString()encodes them.URLencodes nothing and validates little.new URL("https://files.example.com/a b")succeeds and keeps the space. ItsequalsandhashCodelook up the host name on the network. Its constructors have been deprecated since Java 20.- Neither class can change a URL. Both are immutable and hold the query as one string, so adding or removing a parameter means taking that string apart yourself.
- The constructors and
URLEncoderthrow checked exceptions.new URI(...),new URL(...), andURLEncoder.encode(String, String)each declare one.
UrlBuilder does that work for you and returns a URI or URL when an API needs one. See
Getting a URI or URL.
UrlBuilder requires Java 8 or later. If you are moving from version 2, read the upgrade notes in the changelog first.
implementation("com.widen:urlbuilder:{version}")<dependency>
<groupId>com.widen</groupId>
<artifactId>urlbuilder</artifactId>
<version>{version}</version>
</dependency>Start with a host and a path, then add to them. The methods that add to the URL return the builder, so the calls chain.
toString() writes the URL.
new UrlBuilder("my.host.com", "foo")
.addPathSegment("bar")
.addParameter("a", "b")
.toString()
// http://my.host.com/foo/bar?a=bYou never add or trim a slash. The builder joins segments with exactly one slash, whatever the input contains.
new UrlBuilder("my.host.com", "/reports/")
.addPathSegment("/2024/")
.addPathSegment("summary.pdf")
.toString()
// http://my.host.com/reports/2024/summary.pdfA value that contains slashes adds several segments. addPrefixedPathSegment adds to the front of the path, and
includeTrailingSlash() ends the path with a slash.
new UrlBuilder("my.host.com", "users/42")
.addPrefixedPathSegment("api/v2")
.includeTrailingSlash()
.toString()
// http://my.host.com/api/v2/users/42/Pass plain, unencoded text. The builder encodes each path segment and each parameter name and value when it writes the URL, including characters outside ASCII.
new UrlBuilder("my.host.com", "foo & bar")
.addParameter("1&2", "3&4")
.addParameter("a", "b&c")
.toString()
// http://my.host.com/foo%20&%20bar?1%262=3%264&a=b%26c
new UrlBuilder("my.host.com", "café")
.addParameter("q", "naïve 😀")
.toString()
// http://my.host.com/caf%C3%A9?q=na%C3%AFve%20%F0%9F%98%80The path and the query string follow different rules. An & is legal in a path, so it stays as it is. In a parameter it
would end the value, so it becomes %26. Controlling encoding describes the rules and how to
change them.
addParameter accepts any object and uses its toString(). You can add several parameters from a map, and you can
repeat a name.
Map<String, Object> filters = new LinkedHashMap<>();
filters.put("status", "open");
filters.put("limit", 25);
new UrlBuilder("my.host.com", "issues")
.addParameters(filters)
.addParameter("tag", "bug")
.addParameter("tag", "ui")
.toString()
// http://my.host.com/issues?status=open&limit=25&tag=bug&tag=uiA null value adds the name alone, and a blank name adds nothing.
new UrlBuilder("my.host.com", "issues")
.addParameter("archived", null)
.toString()
// http://my.host.com/issues?archivedclearParameter("name") removes a parameter, and clearParameters() removes them all.
new UrlBuilder("my.host.com", "secure/path").usingSsl().toString()
// https://my.host.com/secure/path
new UrlBuilder("localhost", 8080, "api/health").toString()
// http://localhost:8080/api/health
new UrlBuilder("https://localhost:8443/app").toString()
// https://localhost:8443/appThe builder leaves the default port of the scheme out of the URL: 80 for http and 443 for https. Port 443 turns
HTTPS on and port 80 turns it off, so the port wins when it conflicts with the scheme.
new UrlBuilder("https://localhost:80/app").toString()
// http://localhost/app
new UrlBuilder("http://localhost:443/app").toString()
// https://localhost/appA later usingSsl(boolean) call changes the scheme and keeps the port.
new UrlBuilder("localhost", 80, "app").usingSsl().toString()
// https://localhost:80/appnew UrlBuilder("my.host.com", "docs/guide").withFragment("install").toString()
// http://my.host.com/docs/guide#installThe builder writes the fragment as given, without encoding it.
Pass a complete URL to the constructor to take it apart. Then change it and write it back out.
UrlBuilder builder = new UrlBuilder("https://example.com/path/to/resource?existing=param&page=1");
builder.addParameter("new", "value").clearParameter("page").toString()
// https://example.com/path/to/resource?existing=param&new=valueThe builder also returns the parts.
builder.getHostname() // example.com
builder.getPath() // /path/to/resource
builder.getPathSegments() // [path, to, resource]
builder.getQueryParameters() // {existing=[param], new=[value]}
builder.isSslEnabled() // trueThe constructor decodes percent-encoded text in the path and the query string, so the getters return plain text. It
reads a + in the query string as a space, as HTML forms write it and servers read it, so ?q=a+b is written back as
?q=a%20b. A + in the path stays a plus. It throws UrlBuilder.NonParsableUrl for a string that java.net.URL
cannot parse, and IllegalArgumentException for a percent-encoded sequence that is not UTF-8, such as /caf%E9,
because it cannot write that URL back unchanged.
The builder writes a fully qualified URL by default. It can also leave out the scheme, or both the scheme and the host.
UrlBuilder builder = new UrlBuilder("my.host.com", "foo");
builder.modeFullyQualified().toString()
// http://my.host.com/foo
builder.modeProtocolRelative().toString()
// //my.host.com/foo
builder.modeHostnameRelative().toString()
// /fooA fully qualified or protocol-relative URL needs a hostname, and toString() throws IllegalArgumentException without
one. A builder made with the no-argument constructor starts in hostname-relative mode.
new UrlBuilder().withPath("search").addParameter("q", "shoes").toString()
// /search?q=shoesUrlBuilder builder = new UrlBuilder("my.host.com", "path");
URI uri = builder.toURI();
URL url = builder.toURL();The default encoders follow RFC 3986:
PathSegmentEncoderencodes path segments. It leaves@,:, and the sub-delimiters!$&'()*+,;=as they are.QueryParameterEncoderencodes parameter names and values. It encodes everything except letters, digits, and-._~.
You can change the encoding in three ways:
usingPathEncoder(Encoder)andusingQueryEncoder(Encoder)set an encoder of your own.addParameter(key, value, encoder)sets the encoder for one parameter.NoEncodingEncoderpasses text through unchanged, which suits a value that is already encoded.usingLegacyEncoding()restores the encoding of version 2.x throughLegacyEncoder.
new UrlBuilder("my.host.com", "login")
.addParameter("next", "%2Fhome%3Ftab%3D1", NoEncodingEncoder.INSTANCE)
.toString()
// http://my.host.com/login?next=%2Fhome%3Ftab%3D1You can set the path and query encoders at any point before toString(). They apply to the whole URL, including a URL
the builder parsed. A parameter added with its own encoder keeps it.
S3UrlBuilder builds S3 URLs and
signs them, so you can share a private object through a link that expires.
// Unsigned URL of a public object
new S3UrlBuilder("my-bucket", "path/to/file.txt")
.inRegion("us-west-2")
.toString()
// https://my-bucket.s3.us-west-2.amazonaws.com/path/to/file.txt
// Signed URL that works for one hour
new S3UrlBuilder("my-bucket", "private/file.txt")
.inRegion("us-east-1")
.usingCredentials(accessKey, secretKey)
.expireIn(Duration.ofHours(1))
.toString()
// https://my-bucket.s3.us-east-1.amazonaws.com/private/file.txt?X-Amz-Algorithm=AWS4-HMAC-SHA256&...&X-Amz-Signature=...
// Signed URL that downloads the object under another file name
new S3UrlBuilder("my-bucket", "documents/report-2024-q1.pdf")
.inRegion("us-east-1")
.usingCredentials(accessKey, secretKey)
.expireIn(Duration.ofMinutes(30))
.withAttachmentFilename("Quarterly Report.pdf")
.toString()The details:
- Key: the builder takes the key as literal text and encodes every character except letters, digits,
-._~, and/, as the AWS SDKs do. Soa%20b.txtnames that object and nota b.txt, and S3 does not read the+ina+b.txtas a space. The builder drops leading slashes and keeps every other slash, becausefolder/anda//bare keys of their own. After the first slash of a run it writes%2F, as the AWS SDKs do, soa//bbecomesa/%2Fband no HTTP client or proxy can merge the slashes.CloudfrontUrlBuilderencodes its key the same way, for an S3 origin; CloudFront checks the signature against the path as written and passes%2Fon to S3. - Expiration:
expireIn(Duration)counts from the moment the URL is generated, andexpireAt(Instant)sets a fixed time. The later call replaces the earlier one. - Credentials: a signed URL needs an expiration and
usingCredentials, which also accepts an STS session token. - Signature: the builder signs with AWS Signature Version 4 (SigV4) by default. A signed URL needs the bucket's
region, from
inRegionorwithEndpoint(endpoint, region); without one,toString()throwsIllegalStateException. The region must be the bucket's own, or S3 rejects the URL with a 400. A SigV4 URL lasts from 1 second to 7 days, andtoString()throwsIllegalArgumentExceptionfor an expiration outside that range.usingSignatureVersion2()is a deprecated opt-in to Signature Version 2 for buckets and S3-compatible services that still accept it; it needs no region and accepts any expiration. - Response headers:
withAttachmentFilenamesets theContent-Dispositionheader that makes a browser download the file, andwithContentTypesets theContent-Typeheader. Both require a signed URL. - Endpoint:
inRegion("us-west-2")selects a regional endpoint, which ends in.amazonaws.com.cnfor a China region, andwithEndpoint(endpoint, region)names any S3-compatible service and the region it signs for.withEndpoint(endpoint)clears the region. - Scheme: the URL uses HTTPS by default, and
usingSsl(false)writeshttp. - Bucket placement: the bucket goes in the hostname by default. The table lists all three placements. The default
moves the bucket to the path where the hostname cannot work: the name is not a DNS name, the endpoint is an IP
address, or the name contains a period and the URL uses HTTPS, because the endpoint's wildcard certificate does not
match
my.bucket.s3.amazonaws.com. A path-style URL reaches a bucket outsideus-east-1only through its regional endpoint, so callinRegionfor such a bucket.
| Method | Result |
|---|---|
usingBucketInHostname() (default) |
https://my-bucket.s3.amazonaws.com/key.txt |
usingBucketVirtualHost(), for a bucket served from its own domain |
https://assets.example.com/key.txt |
usingBucketInPath(), for a path-style request |
https://s3.amazonaws.com/my-bucket/key.txt |
CloudfrontUrlBuilder builds a
CloudFront signed URL with a canned policy, which gives temporary access to a
private distribution. It needs the ID of a CloudFront key pair and the matching RSA private key.
PrivateKey privateKey = CloudfrontPrivateKeyUtils.fromPemString(pemString);
// Signed URL that works for two hours
new CloudfrontUrlBuilder("d1234.cloudfront.net", "videos/movie.mp4", "APKAEIBAERJR2EXAMPLE", privateKey)
.expireIn(Duration.ofHours(2))
.toString()
// Signed URL that downloads the file under another name
new CloudfrontUrlBuilder("d1234.cloudfront.net", "files/document.pdf", "APKAEIBAERJR2EXAMPLE", privateKey)
.expireAt(Instant.now().plus(Duration.ofDays(1)))
.withAttachmentFilename("download.pdf")
.toString()
// Signed URL that expires at an exact time in a time zone
ZonedDateTime endOfSale = LocalDateTime.of(2030, 6, 30, 17, 0).atZone(ZoneId.of("America/Chicago"));
new CloudfrontUrlBuilder("d1234.cloudfront.net", "files/document.pdf", "APKAEIBAERJR2EXAMPLE", privateKey)
.expireAt(endOfSale.toInstant())
.toString()The details:
- Expiration: a CloudFront URL always needs one, and
toString()throws without it.expireInandexpireAtwork as they do for S3. - Private key:
fromPemStringreads a PKCS#1 or PKCS#8 key in PEM form, andfromDerBinaryreads a PKCS#8 key in DER form from anInputStream. - Key: the builder encodes the key as
S3UrlBuilderdoes, and throwsIllegalArgumentExceptionfor a key that is blank or only slashes, which would sign the root of the distribution. - Scheme: the URL uses HTTPS by default, and
withSsl(false)writeshttp. The signature covers the scheme, so request the URL with the scheme it was signed with. - Parameters:
addParameteradds a query parameter that the signature covers. It rejectsExpires,Signature,Key-Pair-Id, andPolicy, which CloudFront reads the signature from.
UrlSigner adds your own signature scheme, such as an HMAC over
the URL, to any UrlBuilder. When the builder writes the URL, it calls the signer with a SigningContext. The signer
signs context.getStringToSign(), which is the complete URL with every query parameter, and returns the signature as a
map of parameter names to values. The builder appends each entry to the URL. A value can be any object: the builder
writes its toString() and encodes it like any other parameter.
String SECRET_KEY = "my-secret-key";
UrlBuilder builder = new UrlBuilder("cdn.example.com", "/videos/movie.mp4");
builder.addParameter("user", "john");
builder.usingUrlSigner(context -> {
// NOTE: use a strong hash like HmacSHA256 in production
String signature = Integer.toHexString((context.getStringToSign() + SECRET_KEY).hashCode());
return Collections.singletonMap("signature", signature);
});
String signedUrl = builder.toString();
// http://cdn.example.com/videos/movie.mp4?user=john&signature=abc123...A class lets several builders share one signer. In these examples, hmacSha256 stands for a helper of your own that
returns the HMAC as text.
HmacSigningExampleTest has a working
version of that helper.
public class HmacUrlSigner implements UrlSigner {
private final String secretKey;
public HmacUrlSigner(String secretKey) {
this.secretKey = secretKey;
}
@Override
public Map<String, String> sign(SigningContext context) {
String signature = hmacSha256(context.getStringToSign(), secretKey);
return Collections.singletonMap("signature", signature);
}
}
HmacUrlSigner signer = new HmacUrlSigner("my-secret");
UrlBuilder builder = new UrlBuilder("cdn.example.com", "/media/video.mp4");
builder.usingUrlSigner(signer);
String signedUrl = builder.toString();A signing scheme is yours to design. These choices matter most.
-
What the signature covers. Sign the complete URL, including every query parameter. That is the convention, and it stops anyone from changing any part of the link. Sign less only for a reason, such as omitting the host so that one link works on several domains. A narrower scope leaves more of the link open to change: a signature over the path alone lets anyone change the parameters. The context returns each scope in one call.
Scope Text to sign Example Whole URL context.getStringToSign()https://cdn.example.com/a/b.mp4?user=john&expires=1795127806Path and parameters context.getEncodedPathAndQuery()/a/b.mp4?user=john&expires=1795127806Path only context.getEncodedPath()/a/b.mp4The whole URL is the URL as the builder writes it. A protocol-relative URL has no scheme to sign, and a hostname-relative URL has no scheme, host, or port, so its signature stays valid on any host. Use a fully qualified URL when the signature must cover the host.
-
Where extra values go. An expiration, a key ID, or a user ID must be in the URL, so the verifier can read it, and covered by the signature, so nobody can change it. Add each one with
context.addParameterbefore you read the text to sign; once you have read it,addParameterthrowsIllegalStateException. The signature does not cover a parameter thatsignreturns, because the builder appends it after signing. Return only the signature. -
Whether the link expires. A link with no expiration works for as long as the key does, for anyone who obtains it. Add an expiration unless the link must be permanent.
-
A shared secret or a key pair. An HMAC uses one secret on both sides, so every service that can verify a link can also create one. A signature made with a private key, such as RSA or ECDSA, is verified with the public key, so a verifier cannot create links. CloudFront signed URLs work that way.
-
How to replace a key. Plan for it before a key leaks. Add a signed key ID parameter that tells the verifier which key to use, so the old key and the new key both work during the change.
-
What the verifier rebuilds. The verifier must reproduce the signed text byte for byte. The simplest rule is "the URL as it arrived, without the signature parameter". Anything between the signer and the verifier that reorders or re-encodes query parameters breaks that rule, so check what your proxies and CDN do to a URL.
To make a signed URL expire, add the expiration to the URL before you sign it. The signature then covers the expiration, and the verifier can read it.
public class ExpiringHmacSigner implements UrlSigner {
private final String secretKey;
private final Duration validFor;
public ExpiringHmacSigner(String secretKey, Duration validFor) {
this.secretKey = secretKey;
this.validFor = validFor;
}
@Override
public Map<String, ?> sign(SigningContext context) {
// Added before signing, so the signature covers the expiration
context.addParameter("expires", Instant.now().plus(validFor).getEpochSecond());
String signature = hmacSha256(context.getStringToSign(), secretKey);
return Collections.singletonMap("signature", signature);
}
}
// A URL that expires in one hour
ExpiringHmacSigner signer = new ExpiringHmacSigner("my-secret", Duration.ofHours(1));
UrlBuilder builder = new UrlBuilder("api.example.com", "/v1/data");
builder.usingUrlSigner(signer);
String signedUrl = builder.toString();
// http://api.example.com/v1/data?expires=1795127806&signature=...The service that receives a signed URL finds the text the signer signed, checks the signature, and then checks the
expiration. Because the signer signed the whole URL, the signed text is the URL without its signature parameter.
The verifier takes that text from the URL as it arrived. Parsing the URL and writing it again can change it, for example
by dropping an empty path segment, and the verifier would then accept a URL that differs from the signed one. The signer
writes its expires parameter and then its signature parameter last, so the verifier accepts only a URL that ends
with both. It reads the expiration from that place, so an expires parameter that the caller put earlier in the URL
changes nothing.
public class ExpiringHmacVerifier {
// The signer writes its expiration and then its signature last, so a URL it signed ends with both
private static final Pattern SIGNED_URL =
Pattern.compile("(?<signedText>.*[?&]expires=(?<expires>\\d+))&signature=(?<signature>[^&#]*)");
private final String secretKey;
public ExpiringHmacVerifier(String secretKey) {
this.secretKey = secretKey;
}
public boolean isValid(String signedUrl) {
Matcher url = SIGNED_URL.matcher(signedUrl);
if (!url.matches()) {
return false;
}
// The signer signed everything before the signature parameter. Take that text from the URL as it
// arrived: parsing the URL and writing it again can change it.
// Check the signature first: until it matches, the expiration is untrusted input.
String expected = hmacSha256(url.group("signedText"), secretKey);
// MessageDigest.isEqual compares in constant time, so the comparison leaks nothing about the signature
if (!MessageDigest.isEqual(expected.getBytes(StandardCharsets.UTF_8),
url.group("signature").getBytes(StandardCharsets.UTF_8))) {
return false;
}
Instant expiresAt = Instant.ofEpochSecond(Long.parseLong(url.group("expires")));
return Instant.now().isBefore(expiresAt);
}
}
ExpiringHmacVerifier verifier = new ExpiringHmacVerifier("my-secret");
boolean valid = verifier.isValid(signedUrl);The verifier compares the signature as it appears in the URL, so hmacSha256 must return text that the builder writes
without encoding, such as hexadecimal or URL-safe Base64.
The SigningContext describes the URL before signing:
getStringToSign()returns the text to sign: the complete unsigned URL, including the parameters you add through the context.addParameter(name, value)adds a query parameter to the URL before signing, so the signature covers it.addParameters(map)adds several at once, in the map's order.getUrl()returns the same URL asgetStringToSign(), for a signer that needs the URL for another purpose, such as the resource in a CloudFront policy.getProtocol()returnshttporhttps, or an empty string for a relative URL.getHostname()returns the host.getPort()returns the port, ornullwhen the port is unset or is the default for the scheme: 80 forhttpand 443 forhttps.getEncodedPath()returns the encoded path.getEncodedQuery()returns the encoded query string.getEncodedPathAndQuery()returns the encoded path and query string together, without the scheme or host.getParameters()returns the unencoded query parameters, as a map from each name to the list of its values.getFragment()returns the fragment, which is not part of the text to sign.isSsl()returns whether the URL uses HTTPS.getGenerationMode()returns the generation mode: fully qualified, protocol relative, or hostname relative.
- Keep signers stateless. A signer shared between builders must be safe to call from several threads.
- Leave the fragment out. A browser never sends it to the server.
- Pass unencoded values. The builder encodes the parameters a signer adds or returns, so it encodes a percent-encoded value twice.
The tests show each feature in use:
- UrlBuilderTest covers general usage.
- UrlBuilderPathTest covers path handling.
- UrlBuilderQueryParamsTest covers query parameters.
- UrlBuilderParsingTest covers parsing a URL.
- UrlBuilderPortSslTest covers ports and HTTPS.
- UrlBuilderModesTest covers relative URLs.
- UrlBuilderSigningTest covers signing.
Licensed under the Apache Version 2.0 license. See the license file for details.