Class SafeFetchPolicyBuilder

java.lang.Object
io.meeds.commons.http.SafeFetchPolicyBuilder

public final class SafeFetchPolicyBuilder extends Object
Builds a SafeFetchPolicy, starting from the defaults and refusing what no policy may hold: a scheme the HTTP client does not speak, a port outside 1-65535, a non-positive limit or timeout, a timeout over an hour.
  • Method Details

    • name

      public SafeFetchPolicyBuilder name(String fetcherName)
      Names the fetcher, and so its deadline thread.
      Parameters:
      fetcherName - the name
      Returns:
      this builder
    • userAgent

      public SafeFetchPolicyBuilder userAgent(String agent)
      Sets the User-Agent sent with every request.
      Parameters:
      agent - the header value
      Returns:
      this builder
    • allowedSchemes

      public SafeFetchPolicyBuilder allowedSchemes(Collection<String> schemes)
      Sets the schemes a URL may use, within SafeFetchPolicy.HTTP_SCHEMES.
      Parameters:
      schemes - the schemes, case ignored
      Returns:
      this builder
    • httpsOnly

      public SafeFetchPolicyBuilder httpsOnly()
      Allows https only.
      Returns:
      this builder
    • allowedPorts

      public SafeFetchPolicyBuilder allowedPorts(Collection<Integer> ports)
      Sets the ports a URL may reach, the implicit default ports included.
      Parameters:
      ports - the ports, each in 1-65535
      Returns:
      this builder
    • allowedPorts

      public SafeFetchPolicyBuilder allowedPorts(String ports, Collection<Integer> fallback)
      Reads a comma-separated list of ports, ignoring what is not one — a word, a number outside 1-65535, however many digits it has — as a deployment property states them; falls back to the given set when the list names no usable port.
      Parameters:
      ports - the list, such as 80,443,8443
      fallback - the ports when the list names none
      Returns:
      this builder
    • anyPort

      public SafeFetchPolicyBuilder anyPort()
      Lets a URL reach any port. Stated, never implied: an empty port set is refused, not read as this.
      Returns:
      this builder
    • internalAddressesAllowed

      public SafeFetchPolicyBuilder internalAddressesAllowed(boolean allowed)
      Lets the fetcher reach internal addresses: a deployment's opt-out.
      Parameters:
      allowed - whether they may be reached
      Returns:
      this builder
    • acceptedContentTypes

      public SafeFetchPolicyBuilder acceptedContentTypes(Collection<String> contentTypes)
      Sets the media types an answer may declare; an answer declaring another, or none, is refused before its body is read. Empty, the default, accepts any. A request may narrow this set, never widen it.
      Parameters:
      contentTypes - the media types, without parameters, case ignored
      Returns:
      this builder
    • maxBytes

      public SafeFetchPolicyBuilder maxBytes(long bytes)
      Sets the largest body read, in bytes: a ceiling a request may lower, never raise.
      Parameters:
      bytes - the limit, positive
      Returns:
      this builder
    • maxRedirects

      public SafeFetchPolicyBuilder maxRedirects(int redirects)
      Sets the most redirects followed; zero follows none.
      Parameters:
      redirects - the limit, zero or more
      Returns:
      this builder
    • connectTimeout

      public SafeFetchPolicyBuilder connectTimeout(Duration timeout)
      Sets the longest wait for a connection.
      Parameters:
      timeout - the wait, positive and at most one hour
      Returns:
      this builder
    • readTimeout

      public SafeFetchPolicyBuilder readTimeout(Duration timeout)
      Sets the longest wait between two reads.
      Parameters:
      timeout - the wait, positive and at most one hour
      Returns:
      this builder
    • totalTimeout

      public SafeFetchPolicyBuilder totalTimeout(Duration timeout)
      Sets the longest read of a URL, redirects included, enforced by cancelling the request.
      Parameters:
      timeout - the deadline, positive and at most one hour
      Returns:
      this builder
    • maxConnections

      public SafeFetchPolicyBuilder maxConnections(int total, int perRoute)
      Sets the HTTP client's connection pool bounds.
      Parameters:
      total - the most connections kept open, positive
      perRoute - the most connections kept open to one host, positive
      Returns:
      this builder
    • resolver

      public SafeFetchPolicyBuilder resolver(HostResolver hostResolver)
      Replaces name resolution: the seam of the tests, a table of names. Every address it answers is still judged by the guard, so it cannot open what the policy refuses.
      Parameters:
      hostResolver - the resolver
      Returns:
      this builder
    • build

      public SafeFetchPolicy build()
      Returns:
      the policy