Class Forwarded.Builder

java.lang.Object
org.apache.juneau.http.header.Forwarded.Builder
Enclosing class:
Forwarded

public static class Forwarded.Builder extends Object
Fluent builder for assembling an HTTP Forwarded header value.

Composes the forwarding information defined by RFC 7239 into the key=value;key=value, key=value wire format: ;-separated parameters within a single forwarded element (one proxy hop) and ,-separated elements across hops. Typed setters cover the four registered parameters (by, for, host, proto); param(String, String) is a generic escape hatch. Values are emitted as bare tokens when possible and quoted automatically when they are not valid tokens (e.g. IPv6 node identifiers such as "[2001:db8::1]:8080"). Call next() to start a new hop.

Example:

String value = Forwarded.create() .forValue("192.0.2.60") .proto("http") .by("203.0.113.43") .next() .forValue("198.51.100.17") .build(); // => "for=192.0.2.60;proto=http;by=203.0.113.43, for=198.51.100.17"

See Also:
Since:
10.0.0
  • Constructor Details

    • Builder

      public Builder()
      Constructor.
  • Method Details

    • by

      public Forwarded.Builder by(String value)
      Sets the by parameter (the interface where the request came in to the proxy) on the current element.
      Parameters:
      value - The node identifier. Must not be null.
      Returns:
      This object.
    • forValue

      Sets the for parameter (the client that initiated the request) on the current element.
      Parameters:
      value - The node identifier. Must not be null.
      Returns:
      This object.
    • host

      public Forwarded.Builder host(String value)
      Sets the host parameter (the original Host request header) on the current element.
      Parameters:
      value - The host. Must not be null.
      Returns:
      This object.
    • proto

      public Forwarded.Builder proto(String value)
      Sets the proto parameter (the protocol used to make the request) on the current element.
      Parameters:
      value - The protocol (e.g. "http", "https"). Must not be null.
      Returns:
      This object.
    • param

      public Forwarded.Builder param(String name, String value)
      Generic escape hatch for setting any forwarded parameter by name on the current element.

      Replaces any previously-registered value for the same parameter while preserving its original position. The value is emitted as a bare token when valid, otherwise as an escaped quoted-string.

      Parameters:
      name - The parameter name. Must not be null or blank.
      value - The parameter value. Must not be null.
      Returns:
      This object.
      Throws:
      IllegalArgumentException - If name is null or blank.
    • next

      Commits the current forwarded element and begins a new one (the next proxy hop).
      Returns:
      This object.
    • build

      public String build()
      Builds the rendered Forwarded header value.

      Empty elements (hops with no parameters) are omitted. Returns an empty string when nothing has been registered.

      Returns:
      The header value. Never null.
    • toHeader

      public Forwarded toHeader()
      Builds a Forwarded header bean directly from this builder.
      Returns:
      A Forwarded carrying build(). Never null.
    • toString

      public String toString()
      Overrides:
      toString in class Object