SlideShare a Scribd company logo
1 of 26
Download to read offline
Creating a Facebook Clone - Part XLV
@Service
public class NotificationService {
private static final String PUSHURL =
"https://push.codenameone.com/push/push";
private final static Logger logger = LoggerFactory.
getLogger(NotificationService.class);
@Autowired
private NotificationRepository notifications;
@Autowired
private APIKeys keys;
@Autowired
private UserRepository users;
public void updatePushKey(String auth, String key) {
User u = users.findByAuthtoken(auth).get(0);
u.setPushKey(key);
users.save(u);
}
NotificationService
@Service
public class NotificationService {
private static final String PUSHURL =
"https://push.codenameone.com/push/push";
private final static Logger logger = LoggerFactory.
getLogger(NotificationService.class);
@Autowired
private NotificationRepository notifications;
@Autowired
private APIKeys keys;
@Autowired
private UserRepository users;
public void updatePushKey(String auth, String key) {
User u = users.findByAuthtoken(auth).get(0);
u.setPushKey(key);
users.save(u);
}
NotificationService
@Service
public class NotificationService {
private static final String PUSHURL =
"https://push.codenameone.com/push/push";
private final static Logger logger = LoggerFactory.
getLogger(NotificationService.class);
@Autowired
private NotificationRepository notifications;
@Autowired
private APIKeys keys;
@Autowired
private UserRepository users;
public void updatePushKey(String auth, String key) {
User u = users.findByAuthtoken(auth).get(0);
u.setPushKey(key);
users.save(u);
}
NotificationService
@Autowired
private NotificationRepository notifications;
@Autowired
private APIKeys keys;
@Autowired
private UserRepository users;
public void updatePushKey(String auth, String key) {
User u = users.findByAuthtoken(auth).get(0);
u.setPushKey(key);
users.save(u);
}
public void removePushKey(String key) {
List<User> userList = users.findByPushKey(key);
if(!userList.isEmpty()) {
User u = userList.get(0);
u.setPushKey(null);
users.save(u);
}
}
NotificationService
@Autowired
private NotificationRepository notifications;
@Autowired
private APIKeys keys;
@Autowired
private UserRepository users;
public void updatePushKey(String auth, String key) {
User u = users.findByAuthtoken(auth).get(0);
u.setPushKey(key);
users.save(u);
}
public void removePushKey(String key) {
List<User> userList = users.findByPushKey(key);
if(!userList.isEmpty()) {
User u = userList.get(0);
u.setPushKey(null);
users.save(u);
}
}
NotificationService
@Autowired
private NotificationRepository notifications;
@Autowired
private APIKeys keys;
@Autowired
private UserRepository users;
public void updatePushKey(String auth, String key) {
User u = users.findByAuthtoken(auth).get(0);
u.setPushKey(key);
users.save(u);
}
public void removePushKey(String key) {
List<User> userList = users.findByPushKey(key);
if(!userList.isEmpty()) {
User u = userList.get(0);
u.setPushKey(null);
users.save(u);
}
}
NotificationService
users.save(u);
}
}
public void sendNotification(User u, NotificationDAO nd) {
Notification n = new Notification();
n.setDate(System.currentTimeMillis());
n.setReaction(nd.getReaction());
n.setReactionColor(nd.getReactionColor());
n.setText(nd.getText());
n.setUser(u);
n.setWasRead(nd.isWasRead());
notifications.save(n);
if(u.getPushKey() != null) {
sendPushNotification(u.getPushKey(), nd.getText(), 1);
}
}
public List<NotificationDAO> listNotifications(String authtoken,
int page, int amount) {
Page<Notification> notificationsPage = notifications.
findNotificationsForUser(
authtoken, PageRequest.of(page, amount,
Sort.by(Sort.Direction.DESC, "date")));
NotificationService
}
return resp;
}
private String sendPushImpl(String deviceId,
String messageBody, int type) throws IOException {
HttpURLConnection connection = (HttpURLConnection)
new URL(PUSHURL).openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
String cert;
String pass;
boolean production = keys.get("push.itunes_production",
"false").equals("true");
if(production) {
cert = keys.get("push.itunes_prodcert");
pass = keys.get("push.itunes_prodpass");
} else {
cert = keys.get("push.itunes_devcert");
pass = keys.get("push.itunes_devpass");
}
String query = "token=" + keys.get("push.token") +
NotificationService
}
return resp;
}
private String sendPushImpl(String deviceId,
String messageBody, int type) throws IOException {
HttpURLConnection connection = (HttpURLConnection)
new URL(PUSHURL).openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
String cert;
String pass;
boolean production = keys.get("push.itunes_production",
"false").equals("true");
if(production) {
cert = keys.get("push.itunes_prodcert");
pass = keys.get("push.itunes_prodpass");
} else {
cert = keys.get("push.itunes_devcert");
pass = keys.get("push.itunes_devpass");
}
String query = "token=" + keys.get("push.token") +
NotificationService
}
return resp;
}
private String sendPushImpl(String deviceId,
String messageBody, int type) throws IOException {
HttpURLConnection connection = (HttpURLConnection)
new URL(PUSHURL).openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
String cert;
String pass;
boolean production = keys.get("push.itunes_production",
"false").equals("true");
if(production) {
cert = keys.get("push.itunes_prodcert");
pass = keys.get("push.itunes_prodpass");
} else {
cert = keys.get("push.itunes_devcert");
pass = keys.get("push.itunes_devpass");
}
String query = "token=" + keys.get("push.token") +
NotificationService
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
String cert;
String pass;
boolean production = keys.get("push.itunes_production",
"false").equals("true");
if(production) {
cert = keys.get("push.itunes_prodcert");
pass = keys.get("push.itunes_prodpass");
} else {
cert = keys.get("push.itunes_devcert");
pass = keys.get("push.itunes_devpass");
}
String query = "token=" + keys.get("push.token") +
"&device=" + URLEncoder.encode(deviceId, "UTF-8") +
"&type=" + type + "&auth=" +
URLEncoder.encode(keys.get("push.gcm_key"), "UTF-8") +
"&certPassword=" + URLEncoder.encode(pass, "UTF-8") +
"&cert=" + URLEncoder.encode(cert, "UTF-8") +
"&body=" + URLEncoder.encode(messageBody, "UTF-8") +
"&production=" + production;
try (OutputStream output = connection.getOutputStream()) {
output.write(query.getBytes("UTF-8"));
NotificationService
if(production) {
cert = keys.get("push.itunes_prodcert");
pass = keys.get("push.itunes_prodpass");
} else {
cert = keys.get("push.itunes_devcert");
pass = keys.get("push.itunes_devpass");
}
String query = "token=" + keys.get("push.token") +
"&device=" + URLEncoder.encode(deviceId, "UTF-8") +
"&type=" + type + "&auth=" +
URLEncoder.encode(keys.get("push.gcm_key"), "UTF-8") +
"&certPassword=" + URLEncoder.encode(pass, "UTF-8") +
"&cert=" + URLEncoder.encode(cert, "UTF-8") +
"&body=" + URLEncoder.encode(messageBody, "UTF-8") +
"&production=" + production;
try (OutputStream output = connection.getOutputStream()) {
output.write(query.getBytes("UTF-8"));
}
int c = connection.getResponseCode();
if(c == 200) {
try (InputStream i = connection.getInputStream()) {
return new String(readInputStream(i));
}
}
NotificationService
}
String query = "token=" + keys.get("push.token") +
"&device=" + URLEncoder.encode(deviceId, "UTF-8") +
"&type=" + type + "&auth=" +
URLEncoder.encode(keys.get("push.gcm_key"), "UTF-8") +
"&certPassword=" + URLEncoder.encode(pass, "UTF-8") +
"&cert=" + URLEncoder.encode(cert, "UTF-8") +
"&body=" + URLEncoder.encode(messageBody, "UTF-8") +
"&production=" + production;
try (OutputStream output = connection.getOutputStream()) {
output.write(query.getBytes("UTF-8"));
}
int c = connection.getResponseCode();
if(c == 200) {
try (InputStream i = connection.getInputStream()) {
return new String(readInputStream(i));
}
}
logger.error("Error response code from push server: " + c);
return null;
}
@Async
public void sendPushNotification(String deviceId,
NotificationService
}
String query = "token=" + keys.get("push.token") +
"&device=" + URLEncoder.encode(deviceId, "UTF-8") +
"&type=" + type + "&auth=" +
URLEncoder.encode(keys.get("push.gcm_key"), "UTF-8") +
"&certPassword=" + URLEncoder.encode(pass, "UTF-8") +
"&cert=" + URLEncoder.encode(cert, "UTF-8") +
"&body=" + URLEncoder.encode(messageBody, "UTF-8") +
"&production=" + production;
try (OutputStream output = connection.getOutputStream()) {
output.write(query.getBytes("UTF-8"));
}
int c = connection.getResponseCode();
if(c == 200) {
try (InputStream i = connection.getInputStream()) {
return new String(readInputStream(i));
}
}
logger.error("Error response code from push server: " + c);
return null;
}
@Async
public void sendPushNotification(String deviceId,
NotificationService
logger.error("Error response code from push server: " + c);
return null;
}
@Async
public void sendPushNotification(String deviceId,
String messageBody, int type) {
try {
String json = sendPushImpl(deviceId, messageBody, type);
if(json != null) {
JsonParser parser = JsonParserFactory.getJsonParser();
if(json.startsWith("[")) {
List<Object> lst = parser.parseList(json);
for(Object o : lst) {
if(o instanceof Map) {
Map entry = (Map)o;
String status = (String)entry.get("status");
if(status != null) {
if(status.equals("error") ||
status.equals("inactive")) {
removePushKey((String)
entry.get("id"));
}
}
NotificationService
logger.error("Error response code from push server: " + c);
return null;
}
@Async
public void sendPushNotification(String deviceId,
String messageBody, int type) {
try {
String json = sendPushImpl(deviceId, messageBody, type);
if(json != null) {
JsonParser parser = JsonParserFactory.getJsonParser();
if(json.startsWith("[")) {
List<Object> lst = parser.parseList(json);
for(Object o : lst) {
if(o instanceof Map) {
Map entry = (Map)o;
String status = (String)entry.get("status");
if(status != null) {
if(status.equals("error") ||
status.equals("inactive")) {
removePushKey((String)
entry.get("id"));
}
}
NotificationService
logger.error("Error response code from push server: " + c);
return null;
}
@Async
public void sendPushNotification(String deviceId,
String messageBody, int type) {
try {
String json = sendPushImpl(deviceId, messageBody, type);
if(json != null) {
JsonParser parser = JsonParserFactory.getJsonParser();
if(json.startsWith("[")) {
List<Object> lst = parser.parseList(json);
for(Object o : lst) {
if(o instanceof Map) {
Map entry = (Map)o;
String status = (String)entry.get("status");
if(status != null) {
if(status.equals("error") ||
status.equals("inactive")) {
removePushKey((String)
entry.get("id"));
}
}
NotificationService
logger.error("Error response code from push server: " + c);
return null;
}
@Async
public void sendPushNotification(String deviceId,
String messageBody, int type) {
try {
String json = sendPushImpl(deviceId, messageBody, type);
if(json != null) {
JsonParser parser = JsonParserFactory.getJsonParser();
if(json.startsWith("[")) {
List<Object> lst = parser.parseList(json);
for(Object o : lst) {
if(o instanceof Map) {
Map entry = (Map)o;
String status = (String)entry.get("status");
if(status != null) {
if(status.equals("error") ||
status.equals("inactive")) {
removePushKey((String)
entry.get("id"));
}
}
NotificationService
@Async
public void sendPushNotification(String deviceId,
String messageBody, int type) {
try {
String json = sendPushImpl(deviceId, messageBody, type);
if(json != null) {
JsonParser parser = JsonParserFactory.getJsonParser();
if(json.startsWith("[")) {
List<Object> lst = parser.parseList(json);
for(Object o : lst) {
if(o instanceof Map) {
Map entry = (Map)o;
String status = (String)entry.get("status");
if(status != null) {
if(status.equals("error") ||
status.equals("inactive")) {
removePushKey((String)
entry.get("id"));
}
}
}
}
} else {
NotificationService
@Async
public void sendPushNotification(String deviceId,
String messageBody, int type) {
try {
String json = sendPushImpl(deviceId, messageBody, type);
if(json != null) {
JsonParser parser = JsonParserFactory.getJsonParser();
if(json.startsWith("[")) {
List<Object> lst = parser.parseList(json);
for(Object o : lst) {
if(o instanceof Map) {
Map entry = (Map)o;
String status = (String)entry.get("status");
if(status != null) {
if(status.equals("error") ||
status.equals("inactive")) {
removePushKey((String)
entry.get("id"));
}
}
}
}
} else {
NotificationService
Map entry = (Map)o;
String status = (String)entry.get("status");
if(status != null) {
if(status.equals("error") ||
status.equals("inactive")) {
removePushKey((String)
entry.get("id"));
}
}
}
}
} else {
Map<String, Object> m = parser.parseMap(json);
if(m.containsKey("error")) {
logger.error("Error message from server: " +
m.get("error"));
}
}
}
} catch(IOException err) {
logger.error("Error during connection to push server", err);
}
}
NotificationService
}
} catch(IOException err) {
logger.error("Error during connection to push server", err);
}
}
private static byte[] readInputStream(InputStream i)
throws IOException {
try(ByteArrayOutputStream b = new ByteArrayOutputStream()) {
copy(i, b);
return b.toByteArray();
}
}
private static void copy(InputStream i, OutputStream o)
throws IOException {
byte[] buffer = new byte[8192];
int size = i.read(buffer);
while(size > -1) {
o.write(buffer, 0, size);
size = i.read(buffer);
}
}
}
NotificationService
@RequestMapping(method=RequestMethod.GET, value="/notifications")
public List<NotificationDAO> listNotifications(
@RequestHeader(name="auth", required=true) String auth,
@RequestParam(name="page") int page,
@RequestParam(name="size") int size) {
return notifications.listNotifications(auth, page, size);
}
@RequestMapping(method=RequestMethod.POST, value="/contacts")
public String uploadContacts(
@RequestHeader(name="auth", required=true) String auth,
@RequestBody List<ShadowUserDAO> contacts) {
users.uploadContacts(auth, contacts);
return "OK";
}
@RequestMapping(method=RequestMethod.GET, value="/updatePush")
public String updatePushKey(
@RequestHeader(name="auth", required=true) String auth,
String key) {
notifications.updatePushKey(auth, key);
return "OK";
}
}
UserWebService
@SpringBootApplication
@EnableAsync
public class FacebookCloneServerApplication {
public static void main(String[] args) {
SpringApplication.run(FacebookCloneServerApplication.class, args);
}
@Bean
public Executor asyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(2);
executor.setMaxPoolSize(2);
executor.setQueueCapacity(5000);
executor.setThreadNamePrefix("PushThread-");
executor.initialize();
return executor;
}
}
FacebookCloneServerApplication
@SpringBootApplication
@EnableAsync
public class FacebookCloneServerApplication {
public static void main(String[] args) {
SpringApplication.run(FacebookCloneServerApplication.class, args);
}
@Bean
public Executor asyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(2);
executor.setMaxPoolSize(2);
executor.setQueueCapacity(5000);
executor.setThreadNamePrefix("PushThread-");
executor.initialize();
return executor;
}
}
FacebookCloneServerApplication

More Related Content

Similar to Creating a Facebook Clone - Part XLV.pdf

Trip itparsing
Trip itparsingTrip itparsing
Trip itparsingCapIpad
 
Laporan multiclient chatting client server
Laporan multiclient chatting client serverLaporan multiclient chatting client server
Laporan multiclient chatting client servertrilestari08
 
WordPress Realtime - WordCamp São Paulo 2015
WordPress Realtime - WordCamp São Paulo 2015WordPress Realtime - WordCamp São Paulo 2015
WordPress Realtime - WordCamp São Paulo 2015Fernando Daciuk
 
AuthN deep.dive—ASP.NET Authentication Internals.pdf
AuthN deep.dive—ASP.NET Authentication Internals.pdfAuthN deep.dive—ASP.NET Authentication Internals.pdf
AuthN deep.dive—ASP.NET Authentication Internals.pdfondrejl1
 
Cnam azure 2014 mobile services
Cnam azure 2014   mobile servicesCnam azure 2014   mobile services
Cnam azure 2014 mobile servicesAymeric Weinbach
 
Creating a Whatsapp Clone - Part XI.pdf
Creating a Whatsapp Clone - Part XI.pdfCreating a Whatsapp Clone - Part XI.pdf
Creating a Whatsapp Clone - Part XI.pdfShaiAlmog1
 
Creating a Facebook Clone - Part XXVII - Transcript.pdf
Creating a Facebook Clone - Part XXVII - Transcript.pdfCreating a Facebook Clone - Part XXVII - Transcript.pdf
Creating a Facebook Clone - Part XXVII - Transcript.pdfShaiAlmog1
 
Creating a Facebook Clone - Part XLVI.pdf
Creating a Facebook Clone - Part XLVI.pdfCreating a Facebook Clone - Part XLVI.pdf
Creating a Facebook Clone - Part XLVI.pdfShaiAlmog1
 
Creating a Whatsapp Clone - Part XIII.pdf
Creating a Whatsapp Clone - Part XIII.pdfCreating a Whatsapp Clone - Part XIII.pdf
Creating a Whatsapp Clone - Part XIII.pdfShaiAlmog1
 
DAOFactory.javaDAOFactory.javapublicclassDAOFactory{ this .docx
DAOFactory.javaDAOFactory.javapublicclassDAOFactory{ this .docxDAOFactory.javaDAOFactory.javapublicclassDAOFactory{ this .docx
DAOFactory.javaDAOFactory.javapublicclassDAOFactory{ this .docxtheodorelove43763
 
13 networking, mobile services, and authentication
13   networking, mobile services, and authentication13   networking, mobile services, and authentication
13 networking, mobile services, and authenticationWindowsPhoneRocks
 
How to test complex SaaS applications - The family july 2014
How to test complex SaaS applications - The family july 2014How to test complex SaaS applications - The family july 2014
How to test complex SaaS applications - The family july 2014Guillaume POTIER
 
Android Testing
Android TestingAndroid Testing
Android TestingEvan Lin
 
Creating a Facebook Clone - Part XXVIII - Transcript.pdf
Creating a Facebook Clone - Part XXVIII - Transcript.pdfCreating a Facebook Clone - Part XXVIII - Transcript.pdf
Creating a Facebook Clone - Part XXVIII - Transcript.pdfShaiAlmog1
 
Lesson_07_Spring_Security_Register_NEW.pdf
Lesson_07_Spring_Security_Register_NEW.pdfLesson_07_Spring_Security_Register_NEW.pdf
Lesson_07_Spring_Security_Register_NEW.pdfScott Anderson
 
Parse - a mobile backend platform
Parse - a mobile backend platformParse - a mobile backend platform
Parse - a mobile backend platformCarlotta Tatti
 
VPN Access Runbook
VPN Access RunbookVPN Access Runbook
VPN Access RunbookTaha Shakeel
 
Lesson07-UsernamePasswordAuthenticationFilter.pdf
Lesson07-UsernamePasswordAuthenticationFilter.pdfLesson07-UsernamePasswordAuthenticationFilter.pdf
Lesson07-UsernamePasswordAuthenticationFilter.pdfScott Anderson
 
Creating an Uber Clone - Part XIII - Transcript.pdf
Creating an Uber Clone - Part XIII - Transcript.pdfCreating an Uber Clone - Part XIII - Transcript.pdf
Creating an Uber Clone - Part XIII - Transcript.pdfShaiAlmog1
 

Similar to Creating a Facebook Clone - Part XLV.pdf (20)

Trip itparsing
Trip itparsingTrip itparsing
Trip itparsing
 
Laporan multiclient chatting client server
Laporan multiclient chatting client serverLaporan multiclient chatting client server
Laporan multiclient chatting client server
 
WordPress Realtime - WordCamp São Paulo 2015
WordPress Realtime - WordCamp São Paulo 2015WordPress Realtime - WordCamp São Paulo 2015
WordPress Realtime - WordCamp São Paulo 2015
 
Webauthn Tutorial
Webauthn TutorialWebauthn Tutorial
Webauthn Tutorial
 
AuthN deep.dive—ASP.NET Authentication Internals.pdf
AuthN deep.dive—ASP.NET Authentication Internals.pdfAuthN deep.dive—ASP.NET Authentication Internals.pdf
AuthN deep.dive—ASP.NET Authentication Internals.pdf
 
Cnam azure 2014 mobile services
Cnam azure 2014   mobile servicesCnam azure 2014   mobile services
Cnam azure 2014 mobile services
 
Creating a Whatsapp Clone - Part XI.pdf
Creating a Whatsapp Clone - Part XI.pdfCreating a Whatsapp Clone - Part XI.pdf
Creating a Whatsapp Clone - Part XI.pdf
 
Creating a Facebook Clone - Part XXVII - Transcript.pdf
Creating a Facebook Clone - Part XXVII - Transcript.pdfCreating a Facebook Clone - Part XXVII - Transcript.pdf
Creating a Facebook Clone - Part XXVII - Transcript.pdf
 
Creating a Facebook Clone - Part XLVI.pdf
Creating a Facebook Clone - Part XLVI.pdfCreating a Facebook Clone - Part XLVI.pdf
Creating a Facebook Clone - Part XLVI.pdf
 
Creating a Whatsapp Clone - Part XIII.pdf
Creating a Whatsapp Clone - Part XIII.pdfCreating a Whatsapp Clone - Part XIII.pdf
Creating a Whatsapp Clone - Part XIII.pdf
 
DAOFactory.javaDAOFactory.javapublicclassDAOFactory{ this .docx
DAOFactory.javaDAOFactory.javapublicclassDAOFactory{ this .docxDAOFactory.javaDAOFactory.javapublicclassDAOFactory{ this .docx
DAOFactory.javaDAOFactory.javapublicclassDAOFactory{ this .docx
 
13 networking, mobile services, and authentication
13   networking, mobile services, and authentication13   networking, mobile services, and authentication
13 networking, mobile services, and authentication
 
How to test complex SaaS applications - The family july 2014
How to test complex SaaS applications - The family july 2014How to test complex SaaS applications - The family july 2014
How to test complex SaaS applications - The family july 2014
 
Android Testing
Android TestingAndroid Testing
Android Testing
 
Creating a Facebook Clone - Part XXVIII - Transcript.pdf
Creating a Facebook Clone - Part XXVIII - Transcript.pdfCreating a Facebook Clone - Part XXVIII - Transcript.pdf
Creating a Facebook Clone - Part XXVIII - Transcript.pdf
 
Lesson_07_Spring_Security_Register_NEW.pdf
Lesson_07_Spring_Security_Register_NEW.pdfLesson_07_Spring_Security_Register_NEW.pdf
Lesson_07_Spring_Security_Register_NEW.pdf
 
Parse - a mobile backend platform
Parse - a mobile backend platformParse - a mobile backend platform
Parse - a mobile backend platform
 
VPN Access Runbook
VPN Access RunbookVPN Access Runbook
VPN Access Runbook
 
Lesson07-UsernamePasswordAuthenticationFilter.pdf
Lesson07-UsernamePasswordAuthenticationFilter.pdfLesson07-UsernamePasswordAuthenticationFilter.pdf
Lesson07-UsernamePasswordAuthenticationFilter.pdf
 
Creating an Uber Clone - Part XIII - Transcript.pdf
Creating an Uber Clone - Part XIII - Transcript.pdfCreating an Uber Clone - Part XIII - Transcript.pdf
Creating an Uber Clone - Part XIII - Transcript.pdf
 

More from ShaiAlmog1

The Duck Teaches Learn to debug from the masters. Local to production- kill ...
The Duck Teaches  Learn to debug from the masters. Local to production- kill ...The Duck Teaches  Learn to debug from the masters. Local to production- kill ...
The Duck Teaches Learn to debug from the masters. Local to production- kill ...ShaiAlmog1
 
create-netflix-clone-06-client-ui.pdf
create-netflix-clone-06-client-ui.pdfcreate-netflix-clone-06-client-ui.pdf
create-netflix-clone-06-client-ui.pdfShaiAlmog1
 
create-netflix-clone-01-introduction_transcript.pdf
create-netflix-clone-01-introduction_transcript.pdfcreate-netflix-clone-01-introduction_transcript.pdf
create-netflix-clone-01-introduction_transcript.pdfShaiAlmog1
 
create-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdfcreate-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdfShaiAlmog1
 
create-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdfcreate-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdfShaiAlmog1
 
create-netflix-clone-01-introduction.pdf
create-netflix-clone-01-introduction.pdfcreate-netflix-clone-01-introduction.pdf
create-netflix-clone-01-introduction.pdfShaiAlmog1
 
create-netflix-clone-06-client-ui_transcript.pdf
create-netflix-clone-06-client-ui_transcript.pdfcreate-netflix-clone-06-client-ui_transcript.pdf
create-netflix-clone-06-client-ui_transcript.pdfShaiAlmog1
 
create-netflix-clone-03-server.pdf
create-netflix-clone-03-server.pdfcreate-netflix-clone-03-server.pdf
create-netflix-clone-03-server.pdfShaiAlmog1
 
create-netflix-clone-04-server-continued.pdf
create-netflix-clone-04-server-continued.pdfcreate-netflix-clone-04-server-continued.pdf
create-netflix-clone-04-server-continued.pdfShaiAlmog1
 
create-netflix-clone-05-client-model_transcript.pdf
create-netflix-clone-05-client-model_transcript.pdfcreate-netflix-clone-05-client-model_transcript.pdf
create-netflix-clone-05-client-model_transcript.pdfShaiAlmog1
 
create-netflix-clone-03-server_transcript.pdf
create-netflix-clone-03-server_transcript.pdfcreate-netflix-clone-03-server_transcript.pdf
create-netflix-clone-03-server_transcript.pdfShaiAlmog1
 
create-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdfcreate-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdfShaiAlmog1
 
create-netflix-clone-05-client-model.pdf
create-netflix-clone-05-client-model.pdfcreate-netflix-clone-05-client-model.pdf
create-netflix-clone-05-client-model.pdfShaiAlmog1
 
Creating a Whatsapp Clone - Part II.pdf
Creating a Whatsapp Clone - Part II.pdfCreating a Whatsapp Clone - Part II.pdf
Creating a Whatsapp Clone - Part II.pdfShaiAlmog1
 
Creating a Whatsapp Clone - Part IX - Transcript.pdf
Creating a Whatsapp Clone - Part IX - Transcript.pdfCreating a Whatsapp Clone - Part IX - Transcript.pdf
Creating a Whatsapp Clone - Part IX - Transcript.pdfShaiAlmog1
 
Creating a Whatsapp Clone - Part II - Transcript.pdf
Creating a Whatsapp Clone - Part II - Transcript.pdfCreating a Whatsapp Clone - Part II - Transcript.pdf
Creating a Whatsapp Clone - Part II - Transcript.pdfShaiAlmog1
 
Creating a Whatsapp Clone - Part V - Transcript.pdf
Creating a Whatsapp Clone - Part V - Transcript.pdfCreating a Whatsapp Clone - Part V - Transcript.pdf
Creating a Whatsapp Clone - Part V - Transcript.pdfShaiAlmog1
 
Creating a Whatsapp Clone - Part IV - Transcript.pdf
Creating a Whatsapp Clone - Part IV - Transcript.pdfCreating a Whatsapp Clone - Part IV - Transcript.pdf
Creating a Whatsapp Clone - Part IV - Transcript.pdfShaiAlmog1
 
Creating a Whatsapp Clone - Part IV.pdf
Creating a Whatsapp Clone - Part IV.pdfCreating a Whatsapp Clone - Part IV.pdf
Creating a Whatsapp Clone - Part IV.pdfShaiAlmog1
 
Creating a Whatsapp Clone - Part I - Transcript.pdf
Creating a Whatsapp Clone - Part I - Transcript.pdfCreating a Whatsapp Clone - Part I - Transcript.pdf
Creating a Whatsapp Clone - Part I - Transcript.pdfShaiAlmog1
 

More from ShaiAlmog1 (20)

The Duck Teaches Learn to debug from the masters. Local to production- kill ...
The Duck Teaches  Learn to debug from the masters. Local to production- kill ...The Duck Teaches  Learn to debug from the masters. Local to production- kill ...
The Duck Teaches Learn to debug from the masters. Local to production- kill ...
 
create-netflix-clone-06-client-ui.pdf
create-netflix-clone-06-client-ui.pdfcreate-netflix-clone-06-client-ui.pdf
create-netflix-clone-06-client-ui.pdf
 
create-netflix-clone-01-introduction_transcript.pdf
create-netflix-clone-01-introduction_transcript.pdfcreate-netflix-clone-01-introduction_transcript.pdf
create-netflix-clone-01-introduction_transcript.pdf
 
create-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdfcreate-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdf
 
create-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdfcreate-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdf
 
create-netflix-clone-01-introduction.pdf
create-netflix-clone-01-introduction.pdfcreate-netflix-clone-01-introduction.pdf
create-netflix-clone-01-introduction.pdf
 
create-netflix-clone-06-client-ui_transcript.pdf
create-netflix-clone-06-client-ui_transcript.pdfcreate-netflix-clone-06-client-ui_transcript.pdf
create-netflix-clone-06-client-ui_transcript.pdf
 
create-netflix-clone-03-server.pdf
create-netflix-clone-03-server.pdfcreate-netflix-clone-03-server.pdf
create-netflix-clone-03-server.pdf
 
create-netflix-clone-04-server-continued.pdf
create-netflix-clone-04-server-continued.pdfcreate-netflix-clone-04-server-continued.pdf
create-netflix-clone-04-server-continued.pdf
 
create-netflix-clone-05-client-model_transcript.pdf
create-netflix-clone-05-client-model_transcript.pdfcreate-netflix-clone-05-client-model_transcript.pdf
create-netflix-clone-05-client-model_transcript.pdf
 
create-netflix-clone-03-server_transcript.pdf
create-netflix-clone-03-server_transcript.pdfcreate-netflix-clone-03-server_transcript.pdf
create-netflix-clone-03-server_transcript.pdf
 
create-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdfcreate-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdf
 
create-netflix-clone-05-client-model.pdf
create-netflix-clone-05-client-model.pdfcreate-netflix-clone-05-client-model.pdf
create-netflix-clone-05-client-model.pdf
 
Creating a Whatsapp Clone - Part II.pdf
Creating a Whatsapp Clone - Part II.pdfCreating a Whatsapp Clone - Part II.pdf
Creating a Whatsapp Clone - Part II.pdf
 
Creating a Whatsapp Clone - Part IX - Transcript.pdf
Creating a Whatsapp Clone - Part IX - Transcript.pdfCreating a Whatsapp Clone - Part IX - Transcript.pdf
Creating a Whatsapp Clone - Part IX - Transcript.pdf
 
Creating a Whatsapp Clone - Part II - Transcript.pdf
Creating a Whatsapp Clone - Part II - Transcript.pdfCreating a Whatsapp Clone - Part II - Transcript.pdf
Creating a Whatsapp Clone - Part II - Transcript.pdf
 
Creating a Whatsapp Clone - Part V - Transcript.pdf
Creating a Whatsapp Clone - Part V - Transcript.pdfCreating a Whatsapp Clone - Part V - Transcript.pdf
Creating a Whatsapp Clone - Part V - Transcript.pdf
 
Creating a Whatsapp Clone - Part IV - Transcript.pdf
Creating a Whatsapp Clone - Part IV - Transcript.pdfCreating a Whatsapp Clone - Part IV - Transcript.pdf
Creating a Whatsapp Clone - Part IV - Transcript.pdf
 
Creating a Whatsapp Clone - Part IV.pdf
Creating a Whatsapp Clone - Part IV.pdfCreating a Whatsapp Clone - Part IV.pdf
Creating a Whatsapp Clone - Part IV.pdf
 
Creating a Whatsapp Clone - Part I - Transcript.pdf
Creating a Whatsapp Clone - Part I - Transcript.pdfCreating a Whatsapp Clone - Part I - Transcript.pdf
Creating a Whatsapp Clone - Part I - Transcript.pdf
 

Recently uploaded

Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 

Recently uploaded (20)

Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 

Creating a Facebook Clone - Part XLV.pdf

  • 1. Creating a Facebook Clone - Part XLV
  • 2. @Service public class NotificationService { private static final String PUSHURL = "https://push.codenameone.com/push/push"; private final static Logger logger = LoggerFactory. getLogger(NotificationService.class); @Autowired private NotificationRepository notifications; @Autowired private APIKeys keys; @Autowired private UserRepository users; public void updatePushKey(String auth, String key) { User u = users.findByAuthtoken(auth).get(0); u.setPushKey(key); users.save(u); } NotificationService
  • 3. @Service public class NotificationService { private static final String PUSHURL = "https://push.codenameone.com/push/push"; private final static Logger logger = LoggerFactory. getLogger(NotificationService.class); @Autowired private NotificationRepository notifications; @Autowired private APIKeys keys; @Autowired private UserRepository users; public void updatePushKey(String auth, String key) { User u = users.findByAuthtoken(auth).get(0); u.setPushKey(key); users.save(u); } NotificationService
  • 4. @Service public class NotificationService { private static final String PUSHURL = "https://push.codenameone.com/push/push"; private final static Logger logger = LoggerFactory. getLogger(NotificationService.class); @Autowired private NotificationRepository notifications; @Autowired private APIKeys keys; @Autowired private UserRepository users; public void updatePushKey(String auth, String key) { User u = users.findByAuthtoken(auth).get(0); u.setPushKey(key); users.save(u); } NotificationService
  • 5. @Autowired private NotificationRepository notifications; @Autowired private APIKeys keys; @Autowired private UserRepository users; public void updatePushKey(String auth, String key) { User u = users.findByAuthtoken(auth).get(0); u.setPushKey(key); users.save(u); } public void removePushKey(String key) { List<User> userList = users.findByPushKey(key); if(!userList.isEmpty()) { User u = userList.get(0); u.setPushKey(null); users.save(u); } } NotificationService
  • 6. @Autowired private NotificationRepository notifications; @Autowired private APIKeys keys; @Autowired private UserRepository users; public void updatePushKey(String auth, String key) { User u = users.findByAuthtoken(auth).get(0); u.setPushKey(key); users.save(u); } public void removePushKey(String key) { List<User> userList = users.findByPushKey(key); if(!userList.isEmpty()) { User u = userList.get(0); u.setPushKey(null); users.save(u); } } NotificationService
  • 7. @Autowired private NotificationRepository notifications; @Autowired private APIKeys keys; @Autowired private UserRepository users; public void updatePushKey(String auth, String key) { User u = users.findByAuthtoken(auth).get(0); u.setPushKey(key); users.save(u); } public void removePushKey(String key) { List<User> userList = users.findByPushKey(key); if(!userList.isEmpty()) { User u = userList.get(0); u.setPushKey(null); users.save(u); } } NotificationService
  • 8. users.save(u); } } public void sendNotification(User u, NotificationDAO nd) { Notification n = new Notification(); n.setDate(System.currentTimeMillis()); n.setReaction(nd.getReaction()); n.setReactionColor(nd.getReactionColor()); n.setText(nd.getText()); n.setUser(u); n.setWasRead(nd.isWasRead()); notifications.save(n); if(u.getPushKey() != null) { sendPushNotification(u.getPushKey(), nd.getText(), 1); } } public List<NotificationDAO> listNotifications(String authtoken, int page, int amount) { Page<Notification> notificationsPage = notifications. findNotificationsForUser( authtoken, PageRequest.of(page, amount, Sort.by(Sort.Direction.DESC, "date"))); NotificationService
  • 9. } return resp; } private String sendPushImpl(String deviceId, String messageBody, int type) throws IOException { HttpURLConnection connection = (HttpURLConnection) new URL(PUSHURL).openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); String cert; String pass; boolean production = keys.get("push.itunes_production", "false").equals("true"); if(production) { cert = keys.get("push.itunes_prodcert"); pass = keys.get("push.itunes_prodpass"); } else { cert = keys.get("push.itunes_devcert"); pass = keys.get("push.itunes_devpass"); } String query = "token=" + keys.get("push.token") + NotificationService
  • 10. } return resp; } private String sendPushImpl(String deviceId, String messageBody, int type) throws IOException { HttpURLConnection connection = (HttpURLConnection) new URL(PUSHURL).openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); String cert; String pass; boolean production = keys.get("push.itunes_production", "false").equals("true"); if(production) { cert = keys.get("push.itunes_prodcert"); pass = keys.get("push.itunes_prodpass"); } else { cert = keys.get("push.itunes_devcert"); pass = keys.get("push.itunes_devpass"); } String query = "token=" + keys.get("push.token") + NotificationService
  • 11. } return resp; } private String sendPushImpl(String deviceId, String messageBody, int type) throws IOException { HttpURLConnection connection = (HttpURLConnection) new URL(PUSHURL).openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); String cert; String pass; boolean production = keys.get("push.itunes_production", "false").equals("true"); if(production) { cert = keys.get("push.itunes_prodcert"); pass = keys.get("push.itunes_prodpass"); } else { cert = keys.get("push.itunes_devcert"); pass = keys.get("push.itunes_devpass"); } String query = "token=" + keys.get("push.token") + NotificationService
  • 12. connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); String cert; String pass; boolean production = keys.get("push.itunes_production", "false").equals("true"); if(production) { cert = keys.get("push.itunes_prodcert"); pass = keys.get("push.itunes_prodpass"); } else { cert = keys.get("push.itunes_devcert"); pass = keys.get("push.itunes_devpass"); } String query = "token=" + keys.get("push.token") + "&device=" + URLEncoder.encode(deviceId, "UTF-8") + "&type=" + type + "&auth=" + URLEncoder.encode(keys.get("push.gcm_key"), "UTF-8") + "&certPassword=" + URLEncoder.encode(pass, "UTF-8") + "&cert=" + URLEncoder.encode(cert, "UTF-8") + "&body=" + URLEncoder.encode(messageBody, "UTF-8") + "&production=" + production; try (OutputStream output = connection.getOutputStream()) { output.write(query.getBytes("UTF-8")); NotificationService
  • 13. if(production) { cert = keys.get("push.itunes_prodcert"); pass = keys.get("push.itunes_prodpass"); } else { cert = keys.get("push.itunes_devcert"); pass = keys.get("push.itunes_devpass"); } String query = "token=" + keys.get("push.token") + "&device=" + URLEncoder.encode(deviceId, "UTF-8") + "&type=" + type + "&auth=" + URLEncoder.encode(keys.get("push.gcm_key"), "UTF-8") + "&certPassword=" + URLEncoder.encode(pass, "UTF-8") + "&cert=" + URLEncoder.encode(cert, "UTF-8") + "&body=" + URLEncoder.encode(messageBody, "UTF-8") + "&production=" + production; try (OutputStream output = connection.getOutputStream()) { output.write(query.getBytes("UTF-8")); } int c = connection.getResponseCode(); if(c == 200) { try (InputStream i = connection.getInputStream()) { return new String(readInputStream(i)); } } NotificationService
  • 14. } String query = "token=" + keys.get("push.token") + "&device=" + URLEncoder.encode(deviceId, "UTF-8") + "&type=" + type + "&auth=" + URLEncoder.encode(keys.get("push.gcm_key"), "UTF-8") + "&certPassword=" + URLEncoder.encode(pass, "UTF-8") + "&cert=" + URLEncoder.encode(cert, "UTF-8") + "&body=" + URLEncoder.encode(messageBody, "UTF-8") + "&production=" + production; try (OutputStream output = connection.getOutputStream()) { output.write(query.getBytes("UTF-8")); } int c = connection.getResponseCode(); if(c == 200) { try (InputStream i = connection.getInputStream()) { return new String(readInputStream(i)); } } logger.error("Error response code from push server: " + c); return null; } @Async public void sendPushNotification(String deviceId, NotificationService
  • 15. } String query = "token=" + keys.get("push.token") + "&device=" + URLEncoder.encode(deviceId, "UTF-8") + "&type=" + type + "&auth=" + URLEncoder.encode(keys.get("push.gcm_key"), "UTF-8") + "&certPassword=" + URLEncoder.encode(pass, "UTF-8") + "&cert=" + URLEncoder.encode(cert, "UTF-8") + "&body=" + URLEncoder.encode(messageBody, "UTF-8") + "&production=" + production; try (OutputStream output = connection.getOutputStream()) { output.write(query.getBytes("UTF-8")); } int c = connection.getResponseCode(); if(c == 200) { try (InputStream i = connection.getInputStream()) { return new String(readInputStream(i)); } } logger.error("Error response code from push server: " + c); return null; } @Async public void sendPushNotification(String deviceId, NotificationService
  • 16. logger.error("Error response code from push server: " + c); return null; } @Async public void sendPushNotification(String deviceId, String messageBody, int type) { try { String json = sendPushImpl(deviceId, messageBody, type); if(json != null) { JsonParser parser = JsonParserFactory.getJsonParser(); if(json.startsWith("[")) { List<Object> lst = parser.parseList(json); for(Object o : lst) { if(o instanceof Map) { Map entry = (Map)o; String status = (String)entry.get("status"); if(status != null) { if(status.equals("error") || status.equals("inactive")) { removePushKey((String) entry.get("id")); } } NotificationService
  • 17. logger.error("Error response code from push server: " + c); return null; } @Async public void sendPushNotification(String deviceId, String messageBody, int type) { try { String json = sendPushImpl(deviceId, messageBody, type); if(json != null) { JsonParser parser = JsonParserFactory.getJsonParser(); if(json.startsWith("[")) { List<Object> lst = parser.parseList(json); for(Object o : lst) { if(o instanceof Map) { Map entry = (Map)o; String status = (String)entry.get("status"); if(status != null) { if(status.equals("error") || status.equals("inactive")) { removePushKey((String) entry.get("id")); } } NotificationService
  • 18. logger.error("Error response code from push server: " + c); return null; } @Async public void sendPushNotification(String deviceId, String messageBody, int type) { try { String json = sendPushImpl(deviceId, messageBody, type); if(json != null) { JsonParser parser = JsonParserFactory.getJsonParser(); if(json.startsWith("[")) { List<Object> lst = parser.parseList(json); for(Object o : lst) { if(o instanceof Map) { Map entry = (Map)o; String status = (String)entry.get("status"); if(status != null) { if(status.equals("error") || status.equals("inactive")) { removePushKey((String) entry.get("id")); } } NotificationService
  • 19. logger.error("Error response code from push server: " + c); return null; } @Async public void sendPushNotification(String deviceId, String messageBody, int type) { try { String json = sendPushImpl(deviceId, messageBody, type); if(json != null) { JsonParser parser = JsonParserFactory.getJsonParser(); if(json.startsWith("[")) { List<Object> lst = parser.parseList(json); for(Object o : lst) { if(o instanceof Map) { Map entry = (Map)o; String status = (String)entry.get("status"); if(status != null) { if(status.equals("error") || status.equals("inactive")) { removePushKey((String) entry.get("id")); } } NotificationService
  • 20. @Async public void sendPushNotification(String deviceId, String messageBody, int type) { try { String json = sendPushImpl(deviceId, messageBody, type); if(json != null) { JsonParser parser = JsonParserFactory.getJsonParser(); if(json.startsWith("[")) { List<Object> lst = parser.parseList(json); for(Object o : lst) { if(o instanceof Map) { Map entry = (Map)o; String status = (String)entry.get("status"); if(status != null) { if(status.equals("error") || status.equals("inactive")) { removePushKey((String) entry.get("id")); } } } } } else { NotificationService
  • 21. @Async public void sendPushNotification(String deviceId, String messageBody, int type) { try { String json = sendPushImpl(deviceId, messageBody, type); if(json != null) { JsonParser parser = JsonParserFactory.getJsonParser(); if(json.startsWith("[")) { List<Object> lst = parser.parseList(json); for(Object o : lst) { if(o instanceof Map) { Map entry = (Map)o; String status = (String)entry.get("status"); if(status != null) { if(status.equals("error") || status.equals("inactive")) { removePushKey((String) entry.get("id")); } } } } } else { NotificationService
  • 22. Map entry = (Map)o; String status = (String)entry.get("status"); if(status != null) { if(status.equals("error") || status.equals("inactive")) { removePushKey((String) entry.get("id")); } } } } } else { Map<String, Object> m = parser.parseMap(json); if(m.containsKey("error")) { logger.error("Error message from server: " + m.get("error")); } } } } catch(IOException err) { logger.error("Error during connection to push server", err); } } NotificationService
  • 23. } } catch(IOException err) { logger.error("Error during connection to push server", err); } } private static byte[] readInputStream(InputStream i) throws IOException { try(ByteArrayOutputStream b = new ByteArrayOutputStream()) { copy(i, b); return b.toByteArray(); } } private static void copy(InputStream i, OutputStream o) throws IOException { byte[] buffer = new byte[8192]; int size = i.read(buffer); while(size > -1) { o.write(buffer, 0, size); size = i.read(buffer); } } } NotificationService
  • 24. @RequestMapping(method=RequestMethod.GET, value="/notifications") public List<NotificationDAO> listNotifications( @RequestHeader(name="auth", required=true) String auth, @RequestParam(name="page") int page, @RequestParam(name="size") int size) { return notifications.listNotifications(auth, page, size); } @RequestMapping(method=RequestMethod.POST, value="/contacts") public String uploadContacts( @RequestHeader(name="auth", required=true) String auth, @RequestBody List<ShadowUserDAO> contacts) { users.uploadContacts(auth, contacts); return "OK"; } @RequestMapping(method=RequestMethod.GET, value="/updatePush") public String updatePushKey( @RequestHeader(name="auth", required=true) String auth, String key) { notifications.updatePushKey(auth, key); return "OK"; } } UserWebService
  • 25. @SpringBootApplication @EnableAsync public class FacebookCloneServerApplication { public static void main(String[] args) { SpringApplication.run(FacebookCloneServerApplication.class, args); } @Bean public Executor asyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(2); executor.setMaxPoolSize(2); executor.setQueueCapacity(5000); executor.setThreadNamePrefix("PushThread-"); executor.initialize(); return executor; } } FacebookCloneServerApplication
  • 26. @SpringBootApplication @EnableAsync public class FacebookCloneServerApplication { public static void main(String[] args) { SpringApplication.run(FacebookCloneServerApplication.class, args); } @Bean public Executor asyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(2); executor.setMaxPoolSize(2); executor.setQueueCapacity(5000); executor.setThreadNamePrefix("PushThread-"); executor.initialize(); return executor; } } FacebookCloneServerApplication