Class ApnsClientBuilder

java.lang.Object
com.eatthepath.pushy.apns.ApnsClientBuilder

public class ApnsClientBuilder extends Object
An ApnsClientBuilder constructs new ApnsClient instances. Callers must specify the APNs server to which clients connect and must provide either TLS credentials or a signing key (but not both) before building a client. Client builders may be reused to generate multiple clients, and their settings may be changed from one client to the next. APNs client builders are not thread-safe, and should not be shared between threads.
Author:
Jon Chambers
  • Field Details

    • DEFAULT_IDLE_PING_INTERVAL

      public static final Duration DEFAULT_IDLE_PING_INTERVAL
      The default idle time in milliseconds after which the client will send a PING frame to the APNs server.
      Since:
      0.11
    • PRODUCTION_APNS_HOST

      public static final String PRODUCTION_APNS_HOST
      The hostname for the production APNs gateway.
      Since:
      0.5
      See Also:
    • DEVELOPMENT_APNS_HOST

      public static final String DEVELOPMENT_APNS_HOST
      The hostname for the development APNs gateway.
      Since:
      0.5
      See Also:
    • DEFAULT_APNS_PORT

      public static final int DEFAULT_APNS_PORT
      The default (HTTPS) port for communication with the APNs gateway.
      Since:
      0.5
      See Also:
    • ALTERNATE_APNS_PORT

      public static final int ALTERNATE_APNS_PORT

      An alternative port for communication with the APNs gateway. According to Apple's documentation:

      You can alternatively use port 2197 when communicating with APNs. You might do this, for example, to allow APNs traffic through your firewall but to block other HTTPS traffic.
      Since:
      0.5
      See Also:
  • Constructor Details

    • ApnsClientBuilder

      public ApnsClientBuilder()
  • Method Details

    • setApnsServer

      public ApnsClientBuilder setApnsServer(String hostname)
      Sets the hostname of the server to which the client under construction will connect. Apple provides a production and development environment.
      Parameters:
      hostname - the hostname of the server to which the client under construction should connect
      Returns:
      a reference to this builder
      Since:
      0.11
      See Also:
    • setApnsServer

      public ApnsClientBuilder setApnsServer(String hostname, int port)
      Sets the hostname and port of the server to which the client under construction will connect. Apple provides a production and development environment, both of which listen for traffic on the default HTTPS port (443) and an alternate port (2197), which callers may use to work around firewall or proxy restrictions.
      Parameters:
      hostname - the hostname of the server to which the client under construction should connect
      port - the port to which the client under contruction should connect
      Returns:
      a reference to this builder
      Since:
      0.11
      See Also:
    • setClientCredentials

      public ApnsClientBuilder setClientCredentials(File p12File, String p12Password) throws SSLException, IOException

      Sets the TLS credentials for the client under construction using the contents of the given PKCS#12 file. Clients constructed with TLS credentials will use TLS-based authentication when sending push notifications. The PKCS#12 file must contain a certificate/private key pair.

      Clients may not have both TLS credentials and a signing key.

      Parameters:
      p12File - a PKCS#12-formatted file containing the certificate and private key to be used to identify the client to the APNs server
      p12Password - the password to be used to decrypt the contents of the given PKCS#12 file; passwords may be blank (i.e. ""), but must not be null
      Returns:
      a reference to this builder
      Throws:
      SSLException - if the given PKCS#12 file could not be loaded or if any other SSL-related problem arises when constructing the context
      IOException - if any IO problem occurred while attempting to read the given PKCS#12 file, or the PKCS#12 file could not be found
      Since:
      0.8
    • setClientCredentials

      public ApnsClientBuilder setClientCredentials(InputStream p12InputStream, String p12Password) throws SSLException, IOException

      Sets the TLS credentials for the client under construction using the data from the given PKCS#12 input stream. Clients constructed with TLS credentials will use TLS-based authentication when sending push notifications. The PKCS#12 data must contain a certificate/private key pair.

      Clients may not have both TLS credentials and a signing key.

      Parameters:
      p12InputStream - an input stream to a PKCS#12-formatted file containing the certificate and private key to be used to identify the client to the APNs server
      p12Password - the password to be used to decrypt the contents of the given PKCS#12 file; passwords may be blank (i.e. ""), but must not be null
      Returns:
      a reference to this builder
      Throws:
      SSLException - if the given PKCS#12 file could not be loaded or if any other SSL-related problem arises when constructing the context
      IOException - if any IO problem occurred while attempting to read the given PKCS#12 input stream
      Since:
      0.8
    • setClientCredentials

      public ApnsClientBuilder setClientCredentials(X509Certificate clientCertificate, PrivateKey privateKey, String privateKeyPassword)

      Sets the TLS credentials for the client under construction. Clients constructed with TLS credentials will use TLS-based authentication when sending push notifications.

      Clients may not have both TLS credentials and a signing key.

      Parameters:
      clientCertificate - the certificate to be used to identify the client to the APNs server
      privateKey - the private key for the client certificate
      privateKeyPassword - the password to be used to decrypt the private key; may be null if the private key does not require a password
      Returns:
      a reference to this builder
      Since:
      0.8
    • setSigningKey

      public ApnsClientBuilder setSigningKey(ApnsSigningKey signingKey)

      Sets the signing key for the client under construction. Clients constructed with a signing key will use token-based authentication when sending push notifications.

      Clients may not have both a signing key and TLS credentials.

      Parameters:
      signingKey - the signing key to be used by the client under construction
      Returns:
      a reference to this builder
      Since:
      0.10
      See Also:
    • setTokenExpiration

      public ApnsClientBuilder setTokenExpiration(Duration tokenExpiration)

      Sets the duration after which authentication tokens should expire and be regenerated from the signing key for clients using token-based authentication. Has no effect for clients using TLS-based authentication.

      At the time of writing, the APNs server will treat tokens as "expired" after 60 minutes. The default expiration duration for clients using token-based authentication is 50 minutes. Callers should not set a non-default value unless the upstream behavior changes.

      Parameters:
      tokenExpiration - the duration after which authentication tokens should expire
      Returns:
      a reference to this builder
      Since:
      0.13.11
    • setTrustedServerCertificateChain

      public ApnsClientBuilder setTrustedServerCertificateChain(File certificatePemFile)

      Sets the trusted certificate chain for the client under construction using the contents of the given PEM file. If not set (or null), the client will use the JVM's default trust manager.

      Callers will generally not need to set a trusted server certificate chain in normal operation, but may wish to do so for certificate pinning or connecting to a mock server for integration testing or benchmarking.

      Parameters:
      certificatePemFile - a PEM file containing one or more trusted certificates
      Returns:
      a reference to this builder
      Since:
      0.8
    • setTrustedServerCertificateChain

      public ApnsClientBuilder setTrustedServerCertificateChain(InputStream certificateInputStream)

      Sets the trusted certificate chain for the client under construction using the contents of the given PEM input stream. If not set (or null), the client will use the JVM's default trust manager.

      Callers will generally not need to set a trusted server certificate chain in normal operation, but may wish to do so for certificate pinning or connecting to a mock server for integration testing or benchmarking.

      Parameters:
      certificateInputStream - an input stream to PEM-formatted data containing one or more trusted certificates
      Returns:
      a reference to this builder
      Since:
      0.8
    • setTrustedServerCertificateChain

      public ApnsClientBuilder setTrustedServerCertificateChain(X509Certificate... certificates)

      Sets the trusted certificate chain for the client under construction. If not set (or null), the client will use the JVM's default trust manager.

      Callers will generally not need to set a trusted server certificate chain in normal operation, but may wish to do so for certificate pinning or connecting to a mock server for integration testing or benchmarking.

      Parameters:
      certificates - one or more trusted certificates
      Returns:
      a reference to this builder
      Since:
      0.8
    • setHostnameVerificationEnabled

      public ApnsClientBuilder setHostnameVerificationEnabled(boolean hostnameVerificationEnabled)

      Enables or disables hostname verification for the client under construction. When enabled, the client will verify that the name of the server to which it's connected matches the name in the certificate presented by the server. Hostname verification is enabled by default.

      Callers should almost never disable hostname verification. This method is provided only to facilitate uncommon testing and benchmarking use cases.

      Parameters:
      hostnameVerificationEnabled - true to enable hostname verification or false otherwise
      Returns:
      a reference to this builder
      Since:
      0.15.0
    • setEventLoopGroup

      public ApnsClientBuilder setEventLoopGroup(EventLoopGroup eventLoopGroup)

      Sets the event loop group to be used by the client under construction. If not set (or if null), the client will create and manage its own event loop group.

      Generally speaking, callers don't need to set event loop groups for clients, but it may be useful to specify an event loop group under certain circumstances. In particular, specifying an event loop group that is shared among multiple ApnsClient instances can keep thread counts manageable. Regardless of the number of concurrent ApnsClient instances, callers may also wish to specify an event loop group to take advantage of certain platform-specific optimizations (e.g. epoll or KQueue event loop groups).

      Parameters:
      eventLoopGroup - the event loop group to use for this client, or null to let the client manage its own event loop group
      Returns:
      a reference to this builder
      Since:
      0.8
    • setConcurrentConnections

      public ApnsClientBuilder setConcurrentConnections(int concurrentConnections)
      Sets the maximum number of concurrent connections the client under construction may attempt to maintain to the APNs server. By default, clients will attempt to maintain a single connection to the APNs server.
      Parameters:
      concurrentConnections - the maximum number of concurrent connections the client under construction may attempt to maintain
      Returns:
      a reference to this builder
      Since:
      0.11
    • setMetricsListener

      public ApnsClientBuilder setMetricsListener(ApnsClientMetricsListener metricsListener)
      Sets the metrics listener for the client under construction. Metrics listeners gather information that describes the performance and behavior of a client, and are completely optional.
      Parameters:
      metricsListener - the metrics listener for the client under construction, or null if this client should not report metrics to a listener
      Returns:
      a reference to this builder
      Since:
      0.8
    • setProxyHandlerFactory

      public ApnsClientBuilder setProxyHandlerFactory(ProxyHandlerFactory proxyHandlerFactory)
      Sets the proxy handler factory to be used to construct proxy handlers when establishing a new connection to the APNs gateway. A client's proxy handler factory may be null, in which case the client will connect to the gateway directly and will not use a proxy. By default, clients will not use a proxy.
      Parameters:
      proxyHandlerFactory - the proxy handler factory to be used to construct proxy handlers, or null if this client should not use a proxy
      Returns:
      a reference to this builder
      Since:
      0.8
    • setConnectionTimeout

      public ApnsClientBuilder setConnectionTimeout(Duration timeout)
      Sets the maximum amount of time, in milliseconds, that the client under construction will wait to establish a connection with the APNs server before the connection attempt is considered a failure.
      Parameters:
      timeout - the maximum amount of time to wait for a connection attempt to complete
      Returns:
      a reference to this builder
      Since:
      0.8
    • setIdlePingInterval

      public ApnsClientBuilder setIdlePingInterval(Duration idlePingInterval)
      Sets the amount of idle time (in milliseconds) after which the client under construction will send a PING frame to the APNs server. By default, clients will send a PING frame after an idle period of DEFAULT_IDLE_PING_INTERVAL.
      Parameters:
      idlePingInterval - the amount of idle time after which the client will send a PING frame
      Returns:
      a reference to this builder
      Since:
      0.10
    • setGracefulShutdownTimeout

      public ApnsClientBuilder setGracefulShutdownTimeout(Duration gracefulShutdownTimeout)
      Sets the amount of time clients should wait for in-progress requests to complete before closing a connection during a graceful shutdown.
      Parameters:
      gracefulShutdownTimeout - the amount of time to wait for in-progress requests to complete before closing a connection
      Returns:
      a reference to this builder
      Since:
      0.8
      See Also:
    • setFrameLogger

      public ApnsClientBuilder setFrameLogger(Http2FrameLogger frameLogger)
      Sets the HTTP/2 frame logger for the client under construction. HTTP/2 frame loggers log all HTTP/2 frames sent to or from the client to the logging system of your choice via SLF4J. Frame logging is extremely verbose and is recommended only for debugging purposes.
      Parameters:
      frameLogger - the frame logger to be used by the client under construction or null if the client should not log individual HTTP/2 frames
      Returns:
      a reference to this builder
      Since:
      0.12
      See Also:
    • setUseAlpn

      public ApnsClientBuilder setUseAlpn(boolean useAlpn)

      Sets whether this client should perform application-layer protocol negotiation (ALPN) when connecting to an APNs server. APNs servers always use HTTP/2, and so protocol negotiation is generally not necessary (i.e. clients have prior knowledge that the server supports HTTP/2). By default, clients will not use ALPN. Most callers will not need to change this setting, and generally should not unless they have a specific reason to do so (e.g. when working with certain reverse proxies).

      Please note that ALPN support was added to OpenJDK in version 8u252, and prior versions do not include native ALPN support.

      Parameters:
      useAlpn - if true, then the client under construction will perform application-layer protocol negotiation when connecting to the APNs server; otherwise, the client will start an HTTP/2 connecting with prior knowledge
      Returns:
      a reference to this builder
      Since:
      0.15.3
      See Also:
    • build

      public ApnsClient build() throws SSLException
      Constructs a new ApnsClient with the previously-set configuration.
      Returns:
      a new ApnsClient instance with the previously-set configuration
      Throws:
      SSLException - if an SSL context could not be created for the new client for any reason
      IllegalStateException - if this method is called without specifying an APNs server address, if this method is called without providing TLS credentials or a signing key, or if this method is called with both TLS credentials and a signing key
      Since:
      0.8