18 package org.turro.push.service;
20 import java.net.MalformedURLException;
22 import static java.nio.charset.StandardCharsets.UTF_8;
23 import java.security.NoSuchAlgorithmException;
24 import java.security.NoSuchProviderException;
25 import java.security.PublicKey;
26 import java.security.spec.InvalidKeySpecException;
27 import org.apache.commons.codec.binary.Base64;
28 import org.bouncycastle.jce.interfaces.ECPublicKey;
29 import org.turro.push.security.ServerKeys;
37 private final String endpoint;
39 private final ECPublicKey userPublicKey;
41 private final byte[] userAuth;
43 private final byte[] payload;
49 private final int ttl;
51 private static final int ONE_DAY_DURATION_IN_SECONDS = 86400;
52 private static int DEFAULT_TTL = 28 * ONE_DAY_DURATION_IN_SECONDS;
54 public Notification(String endpoint, ECPublicKey userPublicKey,
byte[] userAuth,
byte[] payload,
int ttl,
Urgency urgency, String topic) {
55 this.endpoint = endpoint;
56 this.userPublicKey = userPublicKey;
57 this.userAuth = userAuth;
58 this.payload = payload;
60 this.urgency = urgency;
64 public Notification(String endpoint, PublicKey userPublicKey,
byte[] userAuth,
byte[] payload,
int ttl) {
65 this(endpoint, (ECPublicKey) userPublicKey, userAuth, payload, ttl,
null,
null);
68 public Notification(String endpoint, String userPublicKey, String userAuth,
byte[] payload,
int ttl)
throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
72 public Notification(String endpoint, PublicKey userPublicKey,
byte[] userAuth,
byte[] payload) {
73 this(endpoint, userPublicKey, userAuth, payload, DEFAULT_TTL);
76 public Notification(String endpoint, String userPublicKey, String userAuth,
byte[] payload)
throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
80 public Notification(String endpoint, String userPublicKey, String userAuth, String payload)
throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
81 this(endpoint,
ServerKeys.
loadPublicKey(userPublicKey), Base64.decodeBase64(userAuth), payload.getBytes(UTF_8));
84 public Notification(String endpoint, String userPublicKey, String userAuth, String payload,
Urgency urgency)
throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
85 this(endpoint,
ServerKeys.
loadPublicKey(userPublicKey), Base64.decodeBase64(userAuth), payload.getBytes(UTF_8));
86 this.urgency = urgency;
89 public Notification(
Subscription subscription, String payload)
throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
90 this(subscription.endpoint, subscription.keys.p256dh, subscription.keys.auth, payload);
93 public Notification(
Subscription subscription, String payload,
Urgency urgency)
throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
94 this(subscription.endpoint, subscription.keys.p256dh, subscription.keys.auth, payload);
95 this.urgency = urgency;
103 return userPublicKey;
119 return urgency !=
null;
123 return topic !=
null;
127 return getEndpoint().indexOf(
"https://android.googleapis.com/gcm/send") == 0;
131 return getEndpoint().indexOf(
"https://fcm.googleapis.com/fcm/send") == 0;
146 public String
getOrigin() throws MalformedURLException {
149 return url.getProtocol() +
"://" + url.getHost();
156 public static class NotificationBuilder {
158 private String endpoint =
null;
159 private ECPublicKey userPublicKey =
null;
160 private byte[] userAuth =
null;
161 private byte[] payload =
null;
162 private int ttl = DEFAULT_TTL;
163 private Urgency urgency =
null;
164 private String topic =
null;
166 private NotificationBuilder() {
170 return new Notification(endpoint, userPublicKey, userAuth, payload, ttl, urgency, topic);
173 public NotificationBuilder endpoint(String endpoint) {
174 this.endpoint = endpoint;
178 public NotificationBuilder userPublicKey(PublicKey publicKey) {
179 this.userPublicKey = (ECPublicKey) publicKey;
183 public NotificationBuilder userPublicKey(String publicKey)
throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
184 this.userPublicKey = (ECPublicKey) ServerKeys.loadPublicKey(publicKey);
188 public NotificationBuilder userPublicKey(
byte[] publicKey)
throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
189 this.userPublicKey = (ECPublicKey) ServerKeys.loadPublicKey(publicKey);
193 public NotificationBuilder userAuth(String userAuth) {
194 this.userAuth = Base64.decodeBase64(userAuth);
198 public NotificationBuilder userAuth(
byte[] userAuth) {
199 this.userAuth = userAuth;
203 public NotificationBuilder payload(
byte[] payload) {
204 this.payload = payload;
208 public NotificationBuilder payload(String payload) {
209 this.payload = payload.getBytes(UTF_8);
213 public NotificationBuilder ttl(
int ttl) {
218 public NotificationBuilder urgency(Urgency urgency) {
219 this.urgency = urgency;
223 public NotificationBuilder topic(String topic) {
static PublicKey loadPublicKey(String encodedPublicKey)
Notification(String endpoint, String userPublicKey, String userAuth, byte[] payload)
Notification(String endpoint, PublicKey userPublicKey, byte[] userAuth, byte[] payload, int ttl)
Notification(String endpoint, String userPublicKey, String userAuth, String payload)
static NotificationBuilder builder()
Notification(Subscription subscription, String payload, Urgency urgency)
Notification(String endpoint, String userPublicKey, String userAuth, String payload, Urgency urgency)
Notification(Subscription subscription, String payload)
Notification(String endpoint, String userPublicKey, String userAuth, byte[] payload, int ttl)
Notification(String endpoint, PublicKey userPublicKey, byte[] userAuth, byte[] payload)
ECPublicKey getUserPublicKey()
Notification(String endpoint, ECPublicKey userPublicKey, byte[] userAuth, byte[] payload, int ttl, Urgency urgency, String topic)