BrightSide Workbench Full Report + Source Code
Notification.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.push.service;
19 
20 import java.net.MalformedURLException;
21 import java.net.URL;
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;
30 
35 public class Notification {
36 
37  private final String endpoint;
38 
39  private final ECPublicKey userPublicKey;
40 
41  private final byte[] userAuth;
42 
43  private final byte[] payload;
44 
45  private Urgency urgency;
46 
47  private String topic;
48 
49  private final int ttl;
50 
51  private static final int ONE_DAY_DURATION_IN_SECONDS = 86400;
52  private static int DEFAULT_TTL = 28 * ONE_DAY_DURATION_IN_SECONDS;
53 
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;
59  this.ttl = ttl;
60  this.urgency = urgency;
61  this.topic = topic;
62  }
63 
64  public Notification(String endpoint, PublicKey userPublicKey, byte[] userAuth, byte[] payload, int ttl) {
65  this(endpoint, (ECPublicKey) userPublicKey, userAuth, payload, ttl, null, null);
66  }
67 
68  public Notification(String endpoint, String userPublicKey, String userAuth, byte[] payload, int ttl) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
69  this(endpoint, ServerKeys.loadPublicKey(userPublicKey), Base64.decodeBase64(userAuth), payload, ttl);
70  }
71 
72  public Notification(String endpoint, PublicKey userPublicKey, byte[] userAuth, byte[] payload) {
73  this(endpoint, userPublicKey, userAuth, payload, DEFAULT_TTL);
74  }
75 
76  public Notification(String endpoint, String userPublicKey, String userAuth, byte[] payload) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
77  this(endpoint, ServerKeys.loadPublicKey(userPublicKey), Base64.decodeBase64(userAuth), payload);
78  }
79 
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));
82  }
83 
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;
87  }
88 
89  public Notification(Subscription subscription, String payload) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
90  this(subscription.endpoint, subscription.keys.p256dh, subscription.keys.auth, payload);
91  }
92 
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;
96  }
97 
98  public String getEndpoint() {
99  return endpoint;
100  }
101 
102  public ECPublicKey getUserPublicKey() {
103  return userPublicKey;
104  }
105 
106  public byte[] getUserAuth() {
107  return userAuth;
108  }
109 
110  public byte[] getPayload() {
111  return payload;
112  }
113 
114  public boolean hasPayload() {
115  return getPayload().length > 0;
116  }
117 
118  public boolean hasUrgency() {
119  return urgency != null;
120  }
121 
122  public boolean hasTopic() {
123  return topic != null;
124  }
125 
126  public boolean isGcm() {
127  return getEndpoint().indexOf("https://android.googleapis.com/gcm/send") == 0;
128  }
129 
130  public boolean isFcm() {
131  return getEndpoint().indexOf("https://fcm.googleapis.com/fcm/send") == 0;
132  }
133 
134  public int getTTL() {
135  return ttl;
136  }
137 
138  public Urgency getUrgency() {
139  return urgency;
140  }
141 
142  public String getTopic() {
143  return topic;
144  }
145 
146  public String getOrigin() throws MalformedURLException {
147  URL url = new URL(getEndpoint());
148 
149  return url.getProtocol() + "://" + url.getHost();
150  }
151 
152  public static NotificationBuilder builder() {
153  return new Notification.NotificationBuilder();
154  }
155 
156  public static class NotificationBuilder {
157 
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;
165 
166  private NotificationBuilder() {
167  }
168 
169  public Notification build() {
170  return new Notification(endpoint, userPublicKey, userAuth, payload, ttl, urgency, topic);
171  }
172 
173  public NotificationBuilder endpoint(String endpoint) {
174  this.endpoint = endpoint;
175  return this;
176  }
177 
178  public NotificationBuilder userPublicKey(PublicKey publicKey) {
179  this.userPublicKey = (ECPublicKey) publicKey;
180  return this;
181  }
182 
183  public NotificationBuilder userPublicKey(String publicKey) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
184  this.userPublicKey = (ECPublicKey) ServerKeys.loadPublicKey(publicKey);
185  return this;
186  }
187 
188  public NotificationBuilder userPublicKey(byte[] publicKey) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
189  this.userPublicKey = (ECPublicKey) ServerKeys.loadPublicKey(publicKey);
190  return this;
191  }
192 
193  public NotificationBuilder userAuth(String userAuth) {
194  this.userAuth = Base64.decodeBase64(userAuth);
195  return this;
196  }
197 
198  public NotificationBuilder userAuth(byte[] userAuth) {
199  this.userAuth = userAuth;
200  return this;
201  }
202 
203  public NotificationBuilder payload(byte[] payload) {
204  this.payload = payload;
205  return this;
206  }
207 
208  public NotificationBuilder payload(String payload) {
209  this.payload = payload.getBytes(UTF_8);
210  return this;
211  }
212 
213  public NotificationBuilder ttl(int ttl) {
214  this.ttl = ttl;
215  return this;
216  }
217 
218  public NotificationBuilder urgency(Urgency urgency) {
219  this.urgency = urgency;
220  return this;
221  }
222 
223  public NotificationBuilder topic(String topic) {
224  this.topic = topic;
225  return this;
226  }
227  }
228 
229 }
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)
Notification(String endpoint, ECPublicKey userPublicKey, byte[] userAuth, byte[] payload, int ttl, Urgency urgency, String topic)