18 package org.turro.push;
20 import java.io.IOException;
21 import java.io.StringReader;
22 import java.security.GeneralSecurityException;
23 import java.security.NoSuchAlgorithmException;
24 import java.security.NoSuchProviderException;
25 import java.security.Security;
26 import java.security.spec.InvalidKeySpecException;
27 import java.util.List;
29 import java.util.concurrent.ExecutionException;
30 import java.util.logging.Level;
31 import java.util.logging.Logger;
32 import javax.json.Json;
33 import javax.json.JsonObject;
34 import javax.json.JsonReader;
35 import org.turro.string.Strings;
36 import org.apache.hc.core5.http.HttpResponse;
37 import org.bouncycastle.jce.provider.BouncyCastleProvider;
38 import org.jose4j.lang.JoseException;
39 import org.turro.auth.Authentication;
40 import org.turro.collections.KeyValueMap;
41 import org.turro.elephant.context.ElephantContext;
42 import org.turro.elephant.context.IConstructor;
43 import org.turro.elephant.db.ElephantPU;
44 import org.turro.elephant.direct.AbstractDirect;
45 import org.turro.elephant.direct.DirectContent;
46 import org.turro.elephant.entities.db.PushSubscription;
47 import org.turro.lock.Initializer;
48 import org.turro.push.security.ServerKeys;
49 import org.turro.push.service.Notification;
50 import org.turro.push.service.PushService;
51 import org.turro.push.service.Subscription;
52 import org.turro.sql.SqlClause;
58 @DirectContent(identifier=
"push-subscription")
63 public static void pushMessage(String idContact, String payload) {
64 subscriptions(idContact).forEach(pushSubscription -> {
65 pushMessage(pushSubscription, payload);
76 HttpResponse response = pushService.
send(notification);
77 if(REMOVE_RESPONSES.contains(response.getCode())) {
78 removeSubscription(pushSubscription);
80 }
catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidKeySpecException ex) {
82 }
catch (GeneralSecurityException | IOException | JoseException | ExecutionException | InterruptedException ex) {
99 if(map.containsKey(
"json")) {
100 JsonReader reader = Json.createReader(
new StringReader(map.get(
"json")));
101 JsonObject subs = reader.readObject();
102 JsonObject keys = subs.getJsonObject(
"keys");
105 subscription.
setEndpoint(subs.getString(
"endpoint"));
110 .
message(
"Successfully subscribed on this device")
115 private static List<PushSubscription> subscriptions(String idContact) {
116 return SqlClause.select(
"ps").from(
"PushSubscription ps")
117 .startIf(!Strings.isBlank(idContact))
118 .where().equal(
"ps.contactId", idContact)
124 private static void removeSubscription(PushSubscription subscription) {
125 new ElephantPU().deleteObject(subscription);
130 private static final Initializer<PushService> PUSH_SERVICE =
new Initializer<>();
133 return PUSH_SERVICE.instance(() -> {
134 Security.addProvider(
new BouncyCastleProvider());
139 "mailto:lluis@turro.org");
140 }
catch (GeneralSecurityException ex) {
147 private static final Set<Integer> REMOVE_RESPONSES = Set.of(401, 404, 410);
static IContact getIContact()
static String getServerUrl(String scheme)
void setEndpoint(String endpoint)
void setPublicKey(String publicKey)
void setContactId(String contactId)
void setPrivateKey(String privateKey)
static PushMessage title(String title)
PushMessage click(String target)
PushMessage message(String message)
void doExecute(IConstructor constructor, KeyValueMap map)
static PushService instance()
static void pushMessage(String idContact, String payload)
static void pushMessage(PushSubscription pushSubscription, String payload)
static String publicKey()
static String privateKey()
HttpResponse send(Notification notification)