Utility class for constructing syntactically correct HTTP URLs using a fluent method-chaining API. It strives simply to be more robust then manually constructing URLs by string concatenation.
Java
Latest commit 5e6c3b6 Jan 4, 2017 @sagebind sagebind Bump release version
Permalink
Failed to load latest commit information.
.idea
gradle/wrapper
src/com/widen/util
test-resources
test/com/widen/util
.editorconfig
.gitignore
LICENSE.md
README.md
build.gradle
gradlew
gradlew.bat

README.md

UrlBuilder

Utility class for constructing syntactically correct HTTP URLs using a fluent method-chaining API. It strives simply to be more robust then manually constructing URLs by string concatenation.

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

Features

  • 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
  • Options for generation of fully-qualified, hostname relative, or protocol relative URLs
  • Fluent method-chaining API
  • More examples in UrlBuilderTest

S3 Flavored UrlBuilder

  • S3UrlBuilder provides specialized functionality building for S3 URLs
  • expireIn and expireAt 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)
  • withAttachmentFilename(String filename) generates required Content-Disposition header for browser file download prompt

Cloudfront Flavored UrlBuilder

  • CloudfrontUrlBuilder provides specialized functionality for building CloudFront URLs
  • expireIn and expireAt for time-bombing CloudFront links
  • withAttachmentFilename(String filename) generates required Content-Disposition header for browser file download prompt

License

Licensed under Apache, Version 2.0. See the license file for details.