Class ApnsClientBuilder
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 Summary
Modifier and TypeFieldDescriptionstatic final int
An alternative port for communication with the APNs gateway.static final int
The default (HTTPS) port for communication with the APNs gateway.static final Duration
The default idle time in milliseconds after which the client will send a PING frame to the APNs server.static final String
The hostname for the development APNs gateway.static final String
The hostname for the production APNs gateway. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionbuild()
Constructs a newApnsClient
with the previously-set configuration.setApnsServer
(String hostname) Sets the hostname of the server to which the client under construction will connect.setApnsServer
(String hostname, int port) Sets the hostname and port of the server to which the client under construction will connect.setClientCredentials
(File p12File, String p12Password) Sets the TLS credentials for the client under construction using the contents of the given PKCS#12 file.setClientCredentials
(InputStream p12InputStream, String p12Password) Sets the TLS credentials for the client under construction using the data from the given PKCS#12 input stream.setClientCredentials
(X509Certificate clientCertificate, PrivateKey privateKey, String privateKeyPassword) Sets the TLS credentials for the client under construction.setConcurrentConnections
(int concurrentConnections) Sets the maximum number of concurrent connections the client under construction may attempt to maintain to the APNs server.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.setEventLoopGroup
(EventLoopGroup eventLoopGroup) Sets the event loop group to be used by the client under construction.setFrameLogger
(Http2FrameLogger frameLogger) Sets the HTTP/2 frame logger for the client under construction.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.setHostnameVerificationEnabled
(boolean hostnameVerificationEnabled) Enables or disables hostname verification for the client under construction.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.setMetricsListener
(ApnsClientMetricsListener metricsListener) Sets the metrics listener for the client under construction.setProxyHandlerFactory
(ProxyHandlerFactory proxyHandlerFactory) Sets the proxy handler factory to be used to construct proxy handlers when establishing a new connection to the APNs gateway.setSigningKey
(ApnsSigningKey signingKey) Sets the signing key for the client under construction.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.setTrustedServerCertificateChain
(File certificatePemFile) Sets the trusted certificate chain for the client under construction using the contents of the given PEM file.setTrustedServerCertificateChain
(InputStream certificateInputStream) Sets the trusted certificate chain for the client under construction using the contents of the given PEM input stream.setTrustedServerCertificateChain
(X509Certificate... certificates) Sets the trusted certificate chain for the client under construction.setUseAlpn
(boolean useAlpn) Sets whether this client should perform application-layer protocol negotiation (ALPN) when connecting to an APNs server.
-
Field Details
-
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
The hostname for the production APNs gateway.- Since:
- 0.5
- See Also:
-
DEVELOPMENT_APNS_HOST
The hostname for the development APNs gateway.- Since:
- 0.5
- See Also:
-
DEFAULT_APNS_PORT
public static final int DEFAULT_APNS_PORTThe default (HTTPS) port for communication with the APNs gateway.- Since:
- 0.5
- See Also:
-
ALTERNATE_APNS_PORT
public static final int ALTERNATE_APNS_PORTAn 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
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
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 connectport
- 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 serverp12Password
- the password to be used to decrypt the contents of the given PKCS#12 file; passwords may be blank (i.e.""
), but must not benull
- 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 contextIOException
- 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 serverp12Password
- the password to be used to decrypt the contents of the given PKCS#12 file; passwords may be blank (i.e.""
), but must not benull
- 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 contextIOException
- 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 serverprivateKey
- the private key for the client certificateprivateKeyPassword
- the password to be used to decrypt the private key; may benull
if the private key does not require a password- Returns:
- a reference to this builder
- Since:
- 0.8
-
setSigningKey
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
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
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
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
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
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 orfalse
otherwise- Returns:
- a reference to this builder
- Since:
- 0.15.0
-
setEventLoopGroup
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 concurrentApnsClient
instances, callers may also wish to specify an event loop group to take advantage of certain platform-specific optimizations (e.g.epoll
orKQueue
event loop groups).- Parameters:
eventLoopGroup
- the event loop group to use for this client, ornull
to let the client manage its own event loop group- Returns:
- a reference to this builder
- Since:
- 0.8
-
setConcurrentConnections
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
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, ornull
if this client should not report metrics to a listener- Returns:
- a reference to this builder
- Since:
- 0.8
-
setProxyHandlerFactory
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 benull
, 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, ornull
if this client should not use a proxy- Returns:
- a reference to this builder
- Since:
- 0.8
-
setConnectionTimeout
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
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 ofDEFAULT_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
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
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 ornull
if the client should not log individual HTTP/2 frames- Returns:
- a reference to this builder
- Since:
- 0.12
- See Also:
-
setUseAlpn
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
- iftrue
, 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
Constructs a newApnsClient
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 reasonIllegalStateException
- 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
-