Class SafeFetchPolicy

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

public final class SafeFetchPolicy extends Object
What a SafeHttpFetcher may read and how far it goes: the schemes and ports a URL may use, whether internal addresses may be reached at all, the content types accepted, the most bytes read, the most redirects followed, the timeouts, and the HTTP client's own bounds. Built with SafeFetchPolicyBuilder; immutable.

The resolver and the two exemptions are the seams of the tests, so that a stub server on loopback can stand in for a public one while every other internal address stays refused. The resolver can be set by anyone, since the guard judges every address it answers; the exemptions are set only from this package, so a production policy holds none. The body limit and the accepted content types are ceilings: a SafeFetchRequest narrows them, never widens them.

  • Field Details

    • HTTP_SCHEMES

      public static final Set<String> HTTP_SCHEMES
      The scheme set a policy may draw from: the HTTP client speaks nothing else.
    • DEFAULT_PORTS

      public static final Set<Integer> DEFAULT_PORTS
      The ports a URL may reach unless the policy says otherwise.
    • DEFAULT_MAX_BYTES

      public static final long DEFAULT_MAX_BYTES
      Largest body read unless the policy says otherwise, in bytes.
      See Also:
    • DEFAULT_MAX_REDIRECTS

      public static final int DEFAULT_MAX_REDIRECTS
      Most redirects followed unless the policy says otherwise.
      See Also:
    • DEFAULT_CONNECT

      public static final Duration DEFAULT_CONNECT
      Longest wait for a connection unless the policy says otherwise.
    • DEFAULT_READ

      public static final Duration DEFAULT_READ
      Longest wait between two reads unless the policy says otherwise.
    • DEFAULT_TOTAL

      public static final Duration DEFAULT_TOTAL
      Longest read of a URL, redirects included, unless the policy says otherwise.
    • DEFAULT_NAME

      public static final String DEFAULT_NAME
      The name of a fetcher built without one; names its deadline thread.
      See Also:
    • DEFAULT_USER_AGENT

      public static final String DEFAULT_USER_AGENT
      The User-Agent sent unless the policy says otherwise.
      See Also:
  • Method Details

    • builder

      public static SafeFetchPolicyBuilder builder()
      Returns:
      a builder holding the defaults
    • getName

      public String getName()
      Returns:
      the fetcher's name, which names its deadline thread
    • getUserAgent

      public String getUserAgent()
      Returns:
      the User-Agent sent with every request
    • getAllowedSchemes

      public Set<String> getAllowedSchemes()
      Returns:
      the schemes a URL may use, lower-case, within HTTP_SCHEMES
    • getAllowedPorts

      public Set<Integer> getAllowedPorts()
      Returns:
      the ports a URL may reach, implicit default ports included; empty when isAnyPortAllowed()
    • isAnyPortAllowed

      public boolean isAnyPortAllowed()
      Returns:
      whether a URL may reach any port
    • isInternalAddressesAllowed

      public boolean isInternalAddressesAllowed()
      Returns:
      whether internal addresses may be reached: a deployment's opt-out, false by default
    • getAcceptedContentTypes

      public Set<String> getAcceptedContentTypes()
      Returns:
      the media types an answer may declare, lower-case and without parameters; empty when any is accepted
    • getMaxBytes

      public long getMaxBytes()
      Returns:
      the largest body read, in bytes, unless the request says otherwise
    • getMaxRedirects

      public int getMaxRedirects()
      Returns:
      the most redirects followed
    • getConnectTimeout

      public Duration getConnectTimeout()
      Returns:
      the longest wait for a connection
    • getReadTimeout

      public Duration getReadTimeout()
      Returns:
      the longest wait between two reads
    • getTotalTimeout

      public Duration getTotalTimeout()
      Returns:
      the longest read of a URL, redirects included
    • getMaxConnectionsTotal

      public int getMaxConnectionsTotal()
      Returns:
      the most connections the fetcher keeps open
    • getMaxConnectionsPerRoute

      public int getMaxConnectionsPerRoute()
      Returns:
      the most connections the fetcher keeps open to one host
    • getResolver

      public HostResolver getResolver()
      Returns:
      name resolution; the JDK's unless a test says otherwise
    • getExemptHosts

      public Set<String> getExemptHosts()
      Returns:
      host names whose addresses are read as public; empty in production
    • getExemptAddresses

      public Set<InetAddress> getExemptAddresses()
      Returns:
      addresses read as public; empty in production