Interface HttpClient.Builder

Enclosing interface:
HttpClient

public static interface HttpClient.Builder
A builder of HTTP clients.
  • Method Details

    • setUserAgent

      HttpClient.Builder setUserAgent(String userAgent)
      Sets the User-Agent header.

      Defaults to "Pkl/$version ($os; $flavor)".

    • setConnectTimeout

      HttpClient.Builder setConnectTimeout(Duration timeout)
      Sets the timeout for connecting to a server.

      Defaults to 60 seconds.

    • setRequestTimeout

      HttpClient.Builder setRequestTimeout(Duration timeout)
      Sets the timeout for the interval between sending a request and receiving response headers.

      Defaults to 60 seconds. To set a timeout for a specific request, use HttpRequest.Builder.timeout(java.time.Duration).

    • addCertificates

      HttpClient.Builder addCertificates(Path path)
      Adds a CA certificate file to the client's trust store.

      The given file must contain X.509 certificates in PEM format.

      If no CA certificates are added via this method nor addCertificates(byte[]), the built-in CA certificates of the Pkl native executable or JVM are used.

    • addCertificates

      HttpClient.Builder addCertificates(byte[] certificateBytes)
      Adds CA certificate bytes to the client's trust store.

      The given cert must be an X.509 certificate in PEM format.

      If no CA certificates are added via this method nor addCertificates(Path), the built-in CA certificates of the Pkl native executable or JVM are used.

    • setTestPort

      HttpClient.Builder setTestPort(int port)
      Sets a test server's listening port.

      If set, requests that specify port 0 will be modified to use the given port. This is an internal test option.

    • setProxySelector

      HttpClient.Builder setProxySelector(ProxySelector proxySelector)
      Sets the proxy selector to use when establishing connections.

      Defaults to: ProxySelector.getDefault().

    • setProxy

      HttpClient.Builder setProxy(@Nullable @Nullable URI proxyAddress, List<String> noProxy)
      Configures HTTP connections to connect to the provided proxy address.

      The provided proxyAddress must have scheme http, not contain userInfo, and not have a path segment.

      If proxyAddress is null, uses the proxy address provided by ProxySelector.getDefault().

      NOTE: Due to a limitation in the JDK, this does not configure the proxy server used for certificate revocation checking. To configure the certificate revocation checker, the result of ProxySelector.getDefault() needs to be changed either by setting system properties, or via ProxySelector.setDefault(java.net.ProxySelector).

      Throws:
      IllegalArgumentException - if `proxyAddress` is invalid.
    • setRewrites

      HttpClient.Builder setRewrites(Map<URI,URI> rewrites)
      Removes any existing rewrites, then adds the given rewrites.

      A rewrite changes outbound HTTP URLs by replacing a source prefix with a targert prefix.

      Each rewrite URI must start with http:// or https://, and end with /.

      Each key describes the prefix of a request, and each value describes the replacement prefix.

      This can be useful for setting up mirroring of packages, which are fetched over HTTPS.

      In the case of multiple matches, the longest prefix is used.

      The URL hostname is case-insensitive.

      Throws:
      IllegalArgumentException - if rewrites is invalid.
      Since:
      0.29.0
    • addRewrite

      HttpClient.Builder addRewrite(URI sourcePrefix, URI targetPrefix)
      Adds a rewrite rule.
      Throws:
      IllegalArgumentException - if sourcePrefix or targetPrefix is invalid.
      Since:
      0.29.0
      See Also:
    • build

      HttpClient build()
      Creates a new HttpClient from the current state of this builder.
      Throws:
      HttpClientInitException - if an error occurs while initializing the client
    • buildLazily

      HttpClient buildLazily()
      Returns an HTTPClient wrapper that defers building the actual HTTP client until the wrapper's HttpClient.send(java.net.http.HttpRequest, java.net.http.HttpResponse.BodyHandler<T>) method is called.

      Note: When using this method, any exception thrown when building the actual HTTP client is equally deferred.