-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMain.java
executable file
·242 lines (220 loc) · 9.4 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright 2016-2017 Cisco Systems Inc
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package com.ciscowebex.iossdk.example.pns;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import cn.teaey.apns4j.Apns4j;
import cn.teaey.apns4j.network.ApnsChannel;
import cn.teaey.apns4j.network.ApnsChannelFactory;
import cn.teaey.apns4j.network.ApnsGateway;
import cn.teaey.apns4j.protocol.ApnsPayload;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class Main {
private enum CertType {
DEV, PROD;
public ApnsGateway getGateway() {
return this == DEV ? ApnsGateway.DEVELOPMENT : ApnsGateway.PRODUCTION;
}
}
private enum NotificationType {
Message, VoIP;
public static NotificationType fromShort(short x) {
NotificationType[] types = NotificationType.values();
if (x >= 0 && x < types.length) {
return types[x];
}
return null;
}
public String getCertPassword(CertType type) {
if (this == VoIP || type == CertType.DEV) {
return "1111";
}
return "123123";
}
public Map<CertType, String> getCerts() {
Map<CertType, String> certs = new HashMap<>(2);
if (this == NotificationType.Message) {
certs.put(CertType.PROD, ".jdk/jre/lib/security/messages_prod.p12");
certs.put(CertType.DEV, ".jdk/jre/lib/security/messages_dev.p12");
}
else if (this == NotificationType.VoIP) {
certs.put(CertType.DEV, ".jdk/jre/lib/security/voip.p12");
certs.put(CertType.PROD, ".jdk/jre/lib/security/voip.p12");
}
return certs;
}
public String getTitle() {
return this == VoIP ? "Incoming Call" : "New Message";
}
public String getBody(WebhookNotification webhook) {
return this == VoIP ? webhook.getActorId() : webhook.getData().getPersonEmail();
}
}
@Value("${spring.datasource.url}")
private String dbUrl;
@Autowired
private DataSource dataSource;
public static void main(String[] args) throws Exception {
SpringApplication.run(Main.class, args);
}
@RequestMapping("/webhook")
public ResponseEntity<?> doWebhook(@RequestBody WebhookNotification notification) {
try {
System.out.println(new ObjectMapper().writeValueAsString(notification));
}
catch (JsonProcessingException e) {
System.out.println(e);
}
String to = null;
NotificationType type = NotificationType.Message;
String resource = notification.getResource();
if ("messages".equals(resource)) {
String from = notification.getActorId();
String createdBy = notification.getCreatedBy();
if (createdBy != null && !createdBy.equals(from)) {
to = createdBy;
}
type = NotificationType.Message;
}
else if ("callMemberships".equals(resource)) {
to = notification.getData().getPersonId();
type = NotificationType.VoIP;
}
if (to != null && to.length() > 0) {
for (Map.Entry<CertType, String> cert : type.getCerts().entrySet()) {
ApnsChannelFactory apnsChannelFactory = Apns4j.newChannelFactoryBuilder().apnsGateway(cert.getKey().getGateway()).keyStoreMeta(cert.getValue()).keyStorePwd(type.getCertPassword(cert.getKey())).build();
System.out.println(cert.getKey().getGateway() + ", " + cert.getValue());
ApnsChannel apnsChannel = apnsChannelFactory.newChannel();
try (Connection connection = dataSource.getConnection()) {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT token, type FROM device_tokens WHERE personId = '" + to + "';");
while (rs.next()) {
String token = rs.getString("token");
if (token != null && token.length() > 0 && type == NotificationType.fromShort(rs.getShort("type")) ) {
ApnsPayload apnsPayload = Apns4j.newPayload().alertTitle(type.getTitle()).alertBody(type.getBody(notification)).sound("default");
System.out.println(token + ":" + apnsPayload);
apnsChannel.send(token, apnsPayload);
System.out.println("Done");
}
}
} catch (Exception e) {
System.out.println(e);
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
}
apnsChannel.close();
}
}
return new ResponseEntity(HttpStatus.NO_CONTENT);
}
@RequestMapping(value = "/register", method = RequestMethod.POST)
ResponseEntity<?> doRegister(@RequestBody DeviceRegistration reg) {
if (reg.getMsgToken() == null || reg.getVoipToken() == null || reg.getPersonId() == null || reg.getEmail() == null) {
return new ResponseEntity(HttpStatus.BAD_REQUEST);
}
System.out.println(reg.getEmail() + ": " + reg.getMsgToken() + ", " + reg.getVoipToken());
try (Connection connection = dataSource.getConnection()) {
Statement stmt = connection.createStatement();
stmt.executeUpdate("CREATE TABLE IF NOT EXISTS device_tokens (token varchar(255) NOT NULL PRIMARY KEY, email varchar(255) NOT NULL, personId varchar(255) NOT NULL, type smallint NOT NULL DEFAULT 0);");
stmt.executeUpdate("INSERT INTO device_tokens (personId, email, token, type) VALUES ('" + reg.getPersonId() + "', '" + reg.getEmail() + "', '" + reg.getVoipToken() + "', 1) ON CONFLICT (token) DO UPDATE SET email = EXCLUDED.email, personId = EXCLUDED.personId;");
stmt.executeUpdate("INSERT INTO device_tokens (personId, email, token, type) VALUES ('" + reg.getPersonId() + "', '" + reg.getEmail() + "', '" + reg.getMsgToken() + "', 0) ON CONFLICT (token) DO UPDATE SET email = EXCLUDED.email, personId = EXCLUDED.personId;");
return new ResponseEntity(HttpStatus.NO_CONTENT);
}
catch (Exception e) {
System.out.println(e);
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@RequestMapping(value = "/register/{token}", method = RequestMethod.DELETE)
ResponseEntity<?> doUnregister(@PathVariable("token") String token) {
if (token == null) {
return new ResponseEntity(HttpStatus.BAD_REQUEST);
}
System.out.println("Device token: " + token);
try (Connection connection = dataSource.getConnection()) {
Statement stmt = connection.createStatement();
stmt.executeUpdate("DELETE FROM device_tokens WHERE token = '" + token + "';");
return new ResponseEntity(HttpStatus.NO_CONTENT);
}
catch (Exception e) {
System.out.println(e);
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@RequestMapping(value = "/register", method = RequestMethod.GET)
ResponseEntity<?> doLookup(@RequestParam("email") String email) {
if (email == null || email.length() <= 0) {
return new ResponseEntity(HttpStatus.BAD_REQUEST);
}
System.out.println("Email: " + email);
try (Connection connection = dataSource.getConnection()) {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT token FROM device_tokens WHERE email = '" + email + "';");
ArrayList<String> output = new ArrayList<String>();
while (rs.next()) {
String token = rs.getString("token");
if (token != null && token.length() > 0) {
output.add(token);
}
}
return ResponseEntity.ok(output);
} catch (Exception e) {
System.out.println(e);
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@RequestMapping("/")
String index() {
return "Webex iOS SDK Push Notification Server Example";
}
@Bean
public DataSource dataSource() throws SQLException {
if (dbUrl == null || dbUrl.isEmpty()) {
return new HikariDataSource();
} else {
HikariConfig config = new HikariConfig();
config.setJdbcUrl(dbUrl);
return new HikariDataSource(config);
}
}
}