@@ -45,6 +45,9 @@ public class Notification {
45
45
*/
46
46
private final int ttl ;
47
47
48
+ private static final int ONE_DAY_DURATION_IN_SECONDS = 86400 ;
49
+ private static int DEFAULT_TTL = 28 * ONE_DAY_DURATION_IN_SECONDS ;
50
+
48
51
49
52
public Notification (String endpoint , ECPublicKey userPublicKey , byte [] userAuth , byte [] payload , int ttl , Urgency urgency ) {
50
53
this .endpoint = endpoint ;
@@ -59,8 +62,12 @@ public Notification(String endpoint, PublicKey userPublicKey, byte[] userAuth, b
59
62
this (endpoint , (ECPublicKey ) userPublicKey , userAuth , payload , ttl , null );
60
63
}
61
64
65
+ public Notification (String endpoint , String userPublicKey , String userAuth , byte [] payload , int ttl ) throws NoSuchAlgorithmException , NoSuchProviderException , InvalidKeySpecException {
66
+ this (endpoint , Utils .loadPublicKey (userPublicKey ), Base64Encoder .decode (userAuth ), payload , ttl );
67
+ }
68
+
62
69
public Notification (String endpoint , PublicKey userPublicKey , byte [] userAuth , byte [] payload ) {
63
- this (endpoint , userPublicKey , userAuth , payload , 2419200 );
70
+ this (endpoint , userPublicKey , userAuth , payload , DEFAULT_TTL );
64
71
}
65
72
66
73
public Notification (String endpoint , String userPublicKey , String userAuth , byte [] payload ) throws NoSuchAlgorithmException , NoSuchProviderException , InvalidKeySpecException {
@@ -135,4 +142,59 @@ public String getOrigin() throws MalformedURLException {
135
142
136
143
return url .getProtocol () + "://" + url .getHost ();
137
144
}
145
+
146
+ public static NotificationBuilder builder () {
147
+ return new Notification .NotificationBuilder ();
148
+ }
149
+
150
+ public static class NotificationBuilder {
151
+ private String endpoint = null ;
152
+ private ECPublicKey userPublicKey = null ;
153
+ private byte [] userAuth = null ;
154
+ private byte [] payload = null ;
155
+ private int ttl = DEFAULT_TTL ;
156
+
157
+ private NotificationBuilder () {
158
+ }
159
+
160
+ public Notification build () {
161
+ return new Notification (endpoint , userPublicKey , userAuth , payload , ttl );
162
+ }
163
+
164
+ public NotificationBuilder endpoint (String endpoint ) {
165
+ this .endpoint = endpoint ;
166
+ return this ;
167
+ }
168
+
169
+ public NotificationBuilder userPublicKey (PublicKey publicKey ) {
170
+ this .userPublicKey = (ECPublicKey ) publicKey ;
171
+ return this ;
172
+ }
173
+
174
+ public NotificationBuilder userPublicKey (String publicKey ) throws NoSuchAlgorithmException , NoSuchProviderException , InvalidKeySpecException {
175
+ this .userPublicKey = (ECPublicKey ) Utils .loadPublicKey (publicKey );
176
+ return this ;
177
+ }
178
+
179
+ public NotificationBuilder userAuth (String userAuth ) {
180
+ this .userAuth = Base64Encoder .decode (userAuth );
181
+ return this ;
182
+ }
183
+
184
+ public NotificationBuilder userAuth (byte [] userAuth ) {
185
+ this .userAuth = userAuth ;
186
+ return this ;
187
+ }
188
+
189
+ public NotificationBuilder payload (byte [] payload ) {
190
+ this .payload = payload ;
191
+ return this ;
192
+ }
193
+
194
+ public NotificationBuilder ttl (int ttl ) {
195
+ this .ttl = ttl ;
196
+ return this ;
197
+ }
198
+ }
199
+
138
200
}
0 commit comments