Skip to content

Utility class for constructing syntactically correct HTTP URLs using a fluent method-chaining API

License

Notifications You must be signed in to change notification settings

Widen/urlbuilder

Repository files navigation

UrlBuilder

Build Status Maven Central

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.

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

Installation

With Gradle:

compile 'com.widen:urlbuilder:{version}'

Other dependency managers should be similar.

Usage

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 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 the Apache Version 2.0 license. See the license file for details.