- java.lang.Object
-
- com.zerodeplibs.webpush.httpclient.PreparerBuilder<T>
-
- Type Parameters:
T- the type of "Preparer" which is build by this builder instance.
public abstract class PreparerBuilder<T> extends Object
The builder class of "Preparer".
"Preparer"s are the components which are used for requesting the delivery of a push message.
Typically, a subclass of this class provides methods creating an object that represents an HTTP request. In order to do this, "Preparer"s
- extract and set a URL of a push service from a given push subscription,
- generate and set proper HTTP header fields(e.g. the "Authorization" header field) with given information and
- encrypt a given push message and put it into the request body.
- Author:
- Tomoki Sato
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classPreparerBuilder.RequestPreparationInfoRepresents information which is required for an application to request the delivery of a push message.
-
Constructor Summary
Constructors Modifier Constructor Description protectedPreparerBuilder()Creates a newPreparerBuilder.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Tbuild(VAPIDKeyPair vapidKeyPair)Creates a new "Preparer" by constructing aPreparerBuilder.RequestPreparationInfoand then callingbuildInternal(RequestPreparationInfo).protected abstract TbuildInternal(PreparerBuilder.RequestPreparationInfo requestPreparationInfo)Creates a new "Preparer" with the givenrequestPreparationInfo.PreparerBuilder<T>pushMessage(byte[] messageBytes)Specifies a push message.PreparerBuilder<T>pushMessage(String messageText)Specifies a push message.PreparerBuilder<T>pushSubscription(PushSubscription pushSubscription)Specifies aPushSubscription.PreparerBuilder<T>topic(String topic)Specifies a value for the Topic header field.PreparerBuilder<T>ttl(long ttl, TimeUnit timeUnit)Specifies a value for the TTL header field with atimeUnit.PreparerBuilder<T>urgency(Urgency.UrgencyOption urgencyOption)Specifies a value for the Urgency header field.PreparerBuilder<T>urgencyHigh()Specifies the "high" urgency for the Urgency header field.PreparerBuilder<T>urgencyLow()Specifies the "low" urgency for the Urgency header field.PreparerBuilder<T>urgencyNormal()Specifies the "normal" urgency for the Urgency header field.PreparerBuilder<T>urgencyVeryLow()Specifies the "very-low" urgency for the Urgency header field.PreparerBuilder<T>vapidJWTAdditionalClaim(String name, String value)Specifies an additional claim.PreparerBuilder<T>vapidJWTExpirationTime(Instant expirationTime)Specifies the time at which a JWT for VAPID expires.PreparerBuilder<T>vapidJWTExpiresAfter(int expiresAfter, TimeUnit timeUnit)Specifies the time after which a JWT for VAPID expires.PreparerBuilder<T>vapidJWTSubject(String subject)Specifies a subject.
-
-
-
Constructor Detail
-
PreparerBuilder
protected PreparerBuilder()
Creates a newPreparerBuilder.
-
-
Method Detail
-
pushSubscription
public PreparerBuilder<T> pushSubscription(PushSubscription pushSubscription)
Specifies aPushSubscription.- Parameters:
pushSubscription- a push subscription.- Returns:
- this object.
- Throws:
NullPointerException- if one of the push subscription's fields(other than expirationTime) is null.
-
vapidJWTExpiresAfter
public PreparerBuilder<T> vapidJWTExpiresAfter(int expiresAfter, TimeUnit timeUnit)
Specifies the time after which a JWT for VAPID expires.- Parameters:
expiresAfter- the time after which a JWT for VAPID expires.timeUnit- the unit of the givenexpiresAfter.- Returns:
- this object.
- Throws:
IllegalStateException- if the methods for specifying expiration time are called more than once.- See Also:
VAPIDJWTParam.Builder.expiresAfter(int, TimeUnit)
-
vapidJWTExpirationTime
public PreparerBuilder<T> vapidJWTExpirationTime(Instant expirationTime)
Specifies the time at which a JWT for VAPID expires.- Parameters:
expirationTime- the time at which a JWT for VAPID expires.- Returns:
- this object.
- Throws:
IllegalStateException- if the methods for specifying expiration time are called more than once.- See Also:
VAPIDJWTParam.Builder.expirationTime(Instant)
-
vapidJWTSubject
public PreparerBuilder<T> vapidJWTSubject(String subject)
Specifies a subject.- Parameters:
subject- a subject.- Returns:
- this object.
- See Also:
VAPIDJWTParam.Builder.subject(String)
-
vapidJWTAdditionalClaim
public PreparerBuilder<T> vapidJWTAdditionalClaim(String name, String value)
Specifies an additional claim.- Parameters:
name- the name of an additional claim.value- the value of an additional claim.- Returns:
- this object.
- Throws:
IllegalArgumentException- if one of the "reserved" names is given.- See Also:
VAPIDJWTParam.Builder.additionalClaim(String, Object)
-
pushMessage
public PreparerBuilder<T> pushMessage(byte[] messageBytes)
Specifies a push message.- Parameters:
messageBytes- the octet sequence representing a push message.- Returns:
- this object.
- Throws:
IllegalArgumentException- if the given octet sequence is null or empty.- See Also:
PushMessage.of(byte[])
-
pushMessage
public PreparerBuilder<T> pushMessage(String messageText)
Specifies a push message. The given text is encoded by using UTF-8.- Parameters:
messageText- the text representing a push message.- Returns:
- this object.
- Throws:
IllegalArgumentException- if the given text is null or empty.- See Also:
PushMessage.ofUTF8(String)
-
ttl
public PreparerBuilder<T> ttl(long ttl, TimeUnit timeUnit)
Specifies a value for the TTL header field with atimeUnit.- Parameters:
ttl- a time duration for the TTL header field in the giventimeUnit.timeUnit- the unit of thettlargument.- Returns:
- this object.
- Throws:
IllegalArgumentException- if thettlis negative.- See Also:
TTL
-
urgency
public PreparerBuilder<T> urgency(Urgency.UrgencyOption urgencyOption)
Specifies a value for the Urgency header field.- Parameters:
urgencyOption- an urgency value for the Urgency header field.- Returns:
- this object.
- See Also:
Urgency,urgencyHigh(),urgencyNormal(),urgencyLow(),urgencyVeryLow()
-
urgencyHigh
public PreparerBuilder<T> urgencyHigh()
Specifies the "high" urgency for the Urgency header field.- Returns:
- this object.
- See Also:
Urgency
-
urgencyNormal
public PreparerBuilder<T> urgencyNormal()
Specifies the "normal" urgency for the Urgency header field.- Returns:
- this object.
- See Also:
Urgency
-
urgencyLow
public PreparerBuilder<T> urgencyLow()
Specifies the "low" urgency for the Urgency header field.- Returns:
- this object.
- See Also:
Urgency
-
urgencyVeryLow
public PreparerBuilder<T> urgencyVeryLow()
Specifies the "very-low" urgency for the Urgency header field.- Returns:
- this object.
- See Also:
Urgency
-
topic
public PreparerBuilder<T> topic(String topic)
Specifies a value for the Topic header field.- Parameters:
topic- a topic.- Returns:
- this object
- Throws:
IllegalArgumentException- if the given topic doesn't meet the constraints.- See Also:
Topic
-
build
public T build(VAPIDKeyPair vapidKeyPair)
Creates a new "Preparer" by constructing a
PreparerBuilder.RequestPreparationInfoand then callingbuildInternal(RequestPreparationInfo).To do that, this method internally
- generates a JWT for VAPID with the given
vapidKeyPair - performs message encryption(if the push message exists)
There is a default value for each of the following fields. If the value isn't specified, the default value below is used.
- The expiration time of the JWT for VAPID (the "exp" claim): 3 minutes
- The TTL header field: 24 hours
- The Urgency header filed: "normal"
- Parameters:
vapidKeyPair- a key pair used to sign the JWT for VAPID.- Returns:
- a new "Preparer".
- Throws:
IllegalArgumentException- if the 'keys.p256dh' of the push subscription is invalid as a base64url string or the 'keys.auth' is invalid as a base64url string.IllegalStateException- if the push subscription isn't specified.InvalidECPublicKeyException- if the public key extracted from the 'keys.p256dh' of the push subscription is invalid.MalformedUncompressedBytesException- if the 'keys.p256dh' of the push subscription doesn't start with 0x04 or the length isn't 65 bytes.MalformedURLRuntimeException- if the endpoint url of the push subscription is malformed.
- generates a JWT for VAPID with the given
-
buildInternal
protected abstract T buildInternal(PreparerBuilder.RequestPreparationInfo requestPreparationInfo)
Creates a new "Preparer" with the givenrequestPreparationInfo.- Parameters:
requestPreparationInfo- a requestPreparationInfo.- Returns:
- a new "Preparer".
-
-