SlideShare a Scribd company logo
Creating a Facebook Clone - Part XXIII
The NotificationService is arguably one of the simplest services we have right now. Its main reason for existence is push notification that we'll integrate into it later on.
@Service
public class NotificationService {
@Autowired
private NotificationRepository notifications;
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);
}
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
As I said the class is trivial and contains just 2 methods.

The only repository is the notification repository as we don’t need access to anything else for this service.
@Service
public class NotificationService {
@Autowired
private NotificationRepository notifications;
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);
}
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
sendNotification is important as this would be a great location to send push notification or trigger a websocket based push in the future
@Service
public class NotificationService {
@Autowired
private NotificationRepository notifications;
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);
}
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
Right now we just save the data to the database which we would do anyway as we want to keep track of notifications
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);
}
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")));
List<NotificationDAO> resp = new ArrayList<>();
for(Notification c : notificationsPage) {
resp.add(c.getDAO());
}
return resp;
}
}
NotificationService
This is an API for the client side so it can list the notifications pending to the user, it accepts a page number so we can go through pages of results
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);
}
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")));
List<NotificationDAO> resp = new ArrayList<>();
for(Notification c : notificationsPage) {
resp.add(c.getDAO());
}
return resp;
}
}
NotificationService
When we go through the notification pages we also pass the sort value to indicate we want notifications arranged by data
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);
}
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")));
List<NotificationDAO> resp = new ArrayList<>();
for(Notification c : notificationsPage) {
resp.add(c.getDAO());
}
return resp;
}
}
NotificationService
The return value is the DAO's for the current page carrying the notification data. The send notification API is an API designed for the server side. Notifications only
originate within the server to prevent abuse by a malicious client.
@Service
public class MediaService {
@Autowired
private MediaRepository media;
@Autowired
private UserRepository user;
public String storeMedia(String authtoken, byte[] data,
String mimeType, String role, String visibility,
String filename) {
User u = user.findByAuthtoken(authtoken).get(0);
Media m = new Media();
m.setData(data);
m.setOwner(u);
m.setDate(System.currentTimeMillis());
m.setFilename(filename);
m.setMimeType(mimeType);
m.setRole(role);
m.setVisibility(visibility);
MediaService
We continue the trend of simple services with the MediaService which is almost as simple as the NotificationService.

The media service works with the media repository only but it needs to authenticate some actions against the user repository
@Service
public class MediaService {
@Autowired
private MediaRepository media;
@Autowired
private UserRepository user;
public String storeMedia(String authtoken, byte[] data,
String mimeType, String role, String visibility,
String filename) {
User u = user.findByAuthtoken(authtoken).get(0);
Media m = new Media();
m.setData(data);
m.setOwner(u);
m.setDate(System.currentTimeMillis());
m.setFilename(filename);
m.setMimeType(mimeType);
m.setRole(role);
m.setVisibility(visibility);
MediaService
When adding a new media we accept almost all the values from the client with the exception of time which we store in the server to prevent abuse
@Service
public class MediaService {
@Autowired
private MediaRepository media;
@Autowired
private UserRepository user;
public String storeMedia(String authtoken, byte[] data,
String mimeType, String role, String visibility,
String filename) {
User u = user.findByAuthtoken(authtoken).get(0);
Media m = new Media();
m.setData(data);
m.setOwner(u);
m.setDate(System.currentTimeMillis());
m.setFilename(filename);
m.setMimeType(mimeType);
m.setRole(role);
m.setVisibility(visibility);
MediaService
We need an auth token to make sure the media is saved under the correct user
@Autowired
private MediaRepository media;
@Autowired
private UserRepository user;
public String storeMedia(String authtoken, byte[] data,
String mimeType, String role, String visibility,
String filename) {
User u = user.findByAuthtoken(authtoken).get(0);
Media m = new Media();
m.setData(data);
m.setOwner(u);
m.setDate(System.currentTimeMillis());
m.setFilename(filename);
m.setMimeType(mimeType);
m.setRole(role);
m.setVisibility(visibility);
media.save(m);
return m.getId();
}
public MediaDAO getPublicMedia(String id) throws PermissionException {
Media m = media.findById(id).get();
if(VisibilityConstants.isPublic(m.getVisibility())) {
return m.getDAO();
MediaService
We return the media ID when we are done so it can be referenced by the user
m.setRole(role);
m.setVisibility(visibility);
media.save(m);
return m.getId();
}
public MediaDAO getPublicMedia(String id) throws PermissionException {
Media m = media.findById(id).get();
if(VisibilityConstants.isPublic(m.getVisibility())) {
return m.getDAO();
}
throw new PermissionException(
"Media item belongs to a user that isn't a friend");
}
public MediaDAO getMedia(String authToken, String id) throws
PermissionException {
Media m = media.findById(id).get();
if(VisibilityConstants.isPublic(m.getVisibility())) {
return m.getDAO();
}
if(!m.getOwner().getAuthtoken().equals(authToken)) {
if(!m.getOwner().isFriendByToken(authToken)) {
throw new PermissionException(
"Media item belongs to a user that isn't a friend");
}
MediaService
Public media can be requested via the id of the media and doesn't require any authentication
media.save(m);
return m.getId();
}
public MediaDAO getPublicMedia(String id) throws PermissionException {
Media m = media.findById(id).get();
if(VisibilityConstants.isPublic(m.getVisibility())) {
return m.getDAO();
}
throw new PermissionException(
"Media item belongs to a user that isn't a friend");
}
public MediaDAO getMedia(String authToken, String id) throws
PermissionException {
Media m = media.findById(id).get();
if(VisibilityConstants.isPublic(m.getVisibility())) {
return m.getDAO();
}
if(!m.getOwner().getAuthtoken().equals(authToken)) {
if(!m.getOwner().isFriendByToken(authToken)) {
throw new PermissionException(
"Media item belongs to a user that isn't a friend");
}
}
return m.getDAO();
MediaService
public enum VisibilityConstants {
PUBLIC("public"), FRIENDS("friends");
private String str;
private VisibilityConstants(String str) {
this.str = str;
}
public String asString() {
return str;
}
public static boolean isPublic(String s) {
return s == null || s.equalsIgnoreCase(PUBLIC.asString());
}
}
We need to check that the user is indeed requesting a public media object and isn't spoofing a request.

This obviously brings up the visibility constants enum which is pretty trivial right now. We just have two visibility types for public & friend. As you can see the isPublic
method tests if the field is public or null.
media.save(m);
return m.getId();
}
public MediaDAO getPublicMedia(String id) throws PermissionException {
Media m = media.findById(id).get();
if(VisibilityConstants.isPublic(m.getVisibility())) {
return m.getDAO();
}
throw new PermissionException(
"Media item belongs to a user that isn't a friend");
}
public MediaDAO getMedia(String authToken, String id) throws
PermissionException {
Media m = media.findById(id).get();
if(VisibilityConstants.isPublic(m.getVisibility())) {
return m.getDAO();
}
if(!m.getOwner().getAuthtoken().equals(authToken)) {
if(!m.getOwner().isFriendByToken(authToken)) {
throw new PermissionException(
"Media item belongs to a user that isn't a friend");
}
}
return m.getDAO();
MediaService
public class PermissionException extends Exception {
public PermissionException(String message) {
super(message);
}
}
The permission exception is thrown otherwise. 

That exception is pretty simple and again just a marker exception for the webservice layer
public MediaDAO getPublicMedia(String id) throws PermissionException {
Media m = media.findById(id).get();
if(VisibilityConstants.isPublic(m.getVisibility())) {
return m.getDAO();
}
throw new PermissionException(
"Media item belongs to a user that isn't a friend");
}
public MediaDAO getMedia(String authToken, String id) throws
PermissionException {
Media m = media.findById(id).get();
if(VisibilityConstants.isPublic(m.getVisibility())) {
return m.getDAO();
}
if(!m.getOwner().getAuthtoken().equals(authToken)) {
if(!m.getOwner().isFriendByToken(authToken)) {
throw new PermissionException(
"Media item belongs to a user that isn't a friend");
}
}
return m.getDAO();
}
}
MediaService
Finally for media that could potentially be private we need this method, it accepts the user token and media id
public MediaDAO getPublicMedia(String id) throws PermissionException {
Media m = media.findById(id).get();
if(VisibilityConstants.isPublic(m.getVisibility())) {
return m.getDAO();
}
throw new PermissionException(
"Media item belongs to a user that isn't a friend");
}
public MediaDAO getMedia(String authToken, String id) throws
PermissionException {
Media m = media.findById(id).get();
if(VisibilityConstants.isPublic(m.getVisibility())) {
return m.getDAO();
}
if(!m.getOwner().getAuthtoken().equals(authToken)) {
if(!m.getOwner().isFriendByToken(authToken)) {
throw new PermissionException(
"Media item belongs to a user that isn't a friend");
}
}
return m.getDAO();
}
}
MediaService
If the media is public then there is no problem we can just return it
public MediaDAO getPublicMedia(String id) throws PermissionException {
Media m = media.findById(id).get();
if(VisibilityConstants.isPublic(m.getVisibility())) {
return m.getDAO();
}
throw new PermissionException(
"Media item belongs to a user that isn't a friend");
}
public MediaDAO getMedia(String authToken, String id) throws
PermissionException {
Media m = media.findById(id).get();
if(VisibilityConstants.isPublic(m.getVisibility())) {
return m.getDAO();
}
if(!m.getOwner().getAuthtoken().equals(authToken)) {
if(!m.getOwner().isFriendByToken(authToken)) {
throw new PermissionException(
"Media item belongs to a user that isn't a friend");
}
}
return m.getDAO();
}
}
MediaService
If the media is for friends only we need to verify that the user requesting the media is indeed a friend

More Related Content

Similar to Creating a Facebook Clone - Part XXIII - Transcript.pdf

Android Event and IntentAndroid Event and Intent
Android Event and IntentAndroid Event and IntentAndroid Event and IntentAndroid Event and Intent
Android Event and IntentAndroid Event and Intent
admin220812
 
Creating a Facebook Clone - Part XLV - Transcript.pdf
Creating a Facebook Clone - Part XLV - Transcript.pdfCreating a Facebook Clone - Part XLV - Transcript.pdf
Creating a Facebook Clone - Part XLV - Transcript.pdf
ShaiAlmog1
 
Creating a Whatsapp Clone - Part XIII - Transcript.pdf
Creating a Whatsapp Clone - Part XIII - Transcript.pdfCreating a Whatsapp Clone - Part XIII - Transcript.pdf
Creating a Whatsapp Clone - Part XIII - Transcript.pdf
ShaiAlmog1
 
Creating a Facebook Clone - Part XXVIII.pdf
Creating a Facebook Clone - Part XXVIII.pdfCreating a Facebook Clone - Part XXVIII.pdf
Creating a Facebook Clone - Part XXVIII.pdf
ShaiAlmog1
 
Android For All The Things
Android For All The ThingsAndroid For All The Things
Android For All The Things
Paul Trebilcox-Ruiz
 
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
ShaiAlmog1
 
UI Design From Scratch - Part 5.pdf
UI Design From Scratch - Part 5.pdfUI Design From Scratch - Part 5.pdf
UI Design From Scratch - Part 5.pdf
ShaiAlmog1
 
Creating a Facebook Clone - Part XXXVIII.pdf
Creating a Facebook Clone - Part XXXVIII.pdfCreating a Facebook Clone - Part XXXVIII.pdf
Creating a Facebook Clone - Part XXXVIII.pdf
ShaiAlmog1
 
Windows 8 metro applications
Windows 8 metro applicationsWindows 8 metro applications
Windows 8 metro applications
Alex Golesh
 
Vaadin 7
Vaadin 7Vaadin 7
Vaadin 7
Joonas Lehtinen
 
Backendless apps
Backendless appsBackendless apps
Backendless apps
Matteo Bonifazi
 
Reactive programming every day
Reactive programming every dayReactive programming every day
Reactive programming every day
Vadym Khondar
 
Build your own entity with Drupal
Build your own entity with DrupalBuild your own entity with Drupal
Build your own entity with Drupal
Marco Vito Moscaritolo
 
Binding business data to vaadin components
Binding business data to vaadin componentsBinding business data to vaadin components
Binding business data to vaadin components
Peter Lehto
 
#36.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_재직자환급교육,실업자교육,국비지원교육, 자바교육,구...
#36.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_재직자환급교육,실업자교육,국비지원교육, 자바교육,구...#36.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_재직자환급교육,실업자교육,국비지원교육, 자바교육,구...
#36.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_재직자환급교육,실업자교육,국비지원교육, 자바교육,구...
탑크리에듀(구로디지털단지역3번출구 2분거리)
 
Notifications
NotificationsNotifications
Notifications
Youssef ELBOUZIANI
 
Android Sample Project By Wael Almadhoun
Android Sample Project By Wael AlmadhounAndroid Sample Project By Wael Almadhoun
Android Sample Project By Wael Almadhoun
Wael Almadhoun, MSc, PMP®
 
Creating an Uber Clone - Part XXXX.pdf
Creating an Uber Clone - Part XXXX.pdfCreating an Uber Clone - Part XXXX.pdf
Creating an Uber Clone - Part XXXX.pdf
ShaiAlmog1
 
Creating a Facebook Clone - Part XXVI - Transcript.pdf
Creating a Facebook Clone - Part XXVI - Transcript.pdfCreating a Facebook Clone - Part XXVI - Transcript.pdf
Creating a Facebook Clone - Part XXVI - Transcript.pdf
ShaiAlmog1
 

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

Android Event and IntentAndroid Event and Intent
Android Event and IntentAndroid Event and IntentAndroid Event and IntentAndroid Event and Intent
Android Event and IntentAndroid Event and Intent
 
Creating a Facebook Clone - Part XLV - Transcript.pdf
Creating a Facebook Clone - Part XLV - Transcript.pdfCreating a Facebook Clone - Part XLV - Transcript.pdf
Creating a Facebook Clone - Part XLV - Transcript.pdf
 
Creating a Whatsapp Clone - Part XIII - Transcript.pdf
Creating a Whatsapp Clone - Part XIII - Transcript.pdfCreating a Whatsapp Clone - Part XIII - Transcript.pdf
Creating a Whatsapp Clone - Part XIII - Transcript.pdf
 
Creating a Facebook Clone - Part XXVIII.pdf
Creating a Facebook Clone - Part XXVIII.pdfCreating a Facebook Clone - Part XXVIII.pdf
Creating a Facebook Clone - Part XXVIII.pdf
 
Android For All The Things
Android For All The ThingsAndroid For All The Things
Android For All The Things
 
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
 
UI Design From Scratch - Part 5.pdf
UI Design From Scratch - Part 5.pdfUI Design From Scratch - Part 5.pdf
UI Design From Scratch - Part 5.pdf
 
Android crashcourse
Android crashcourseAndroid crashcourse
Android crashcourse
 
Creating a Facebook Clone - Part XXXVIII.pdf
Creating a Facebook Clone - Part XXXVIII.pdfCreating a Facebook Clone - Part XXXVIII.pdf
Creating a Facebook Clone - Part XXXVIII.pdf
 
Windows 8 metro applications
Windows 8 metro applicationsWindows 8 metro applications
Windows 8 metro applications
 
Vaadin 7
Vaadin 7Vaadin 7
Vaadin 7
 
Backendless apps
Backendless appsBackendless apps
Backendless apps
 
Reactive programming every day
Reactive programming every dayReactive programming every day
Reactive programming every day
 
Build your own entity with Drupal
Build your own entity with DrupalBuild your own entity with Drupal
Build your own entity with Drupal
 
Binding business data to vaadin components
Binding business data to vaadin componentsBinding business data to vaadin components
Binding business data to vaadin components
 
#36.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_재직자환급교육,실업자교육,국비지원교육, 자바교육,구...
#36.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_재직자환급교육,실업자교육,국비지원교육, 자바교육,구...#36.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_재직자환급교육,실업자교육,국비지원교육, 자바교육,구...
#36.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_재직자환급교육,실업자교육,국비지원교육, 자바교육,구...
 
Notifications
NotificationsNotifications
Notifications
 
Android Sample Project By Wael Almadhoun
Android Sample Project By Wael AlmadhounAndroid Sample Project By Wael Almadhoun
Android Sample Project By Wael Almadhoun
 
Creating an Uber Clone - Part XXXX.pdf
Creating an Uber Clone - Part XXXX.pdfCreating an Uber Clone - Part XXXX.pdf
Creating an Uber Clone - Part XXXX.pdf
 
Creating a Facebook Clone - Part XXVI - Transcript.pdf
Creating a Facebook Clone - Part XXVI - Transcript.pdfCreating a Facebook Clone - Part XXVI - Transcript.pdf
Creating a Facebook Clone - Part XXVI - 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.pdf
ShaiAlmog1
 
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
ShaiAlmog1
 
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
ShaiAlmog1
 
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
ShaiAlmog1
 
create-netflix-clone-01-introduction.pdf
create-netflix-clone-01-introduction.pdfcreate-netflix-clone-01-introduction.pdf
create-netflix-clone-01-introduction.pdf
ShaiAlmog1
 
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
ShaiAlmog1
 
create-netflix-clone-03-server.pdf
create-netflix-clone-03-server.pdfcreate-netflix-clone-03-server.pdf
create-netflix-clone-03-server.pdf
ShaiAlmog1
 
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
ShaiAlmog1
 
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
ShaiAlmog1
 
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
ShaiAlmog1
 
create-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdfcreate-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdf
ShaiAlmog1
 
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
ShaiAlmog1
 
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
ShaiAlmog1
 
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
ShaiAlmog1
 
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
ShaiAlmog1
 
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
ShaiAlmog1
 
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
ShaiAlmog1
 
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
ShaiAlmog1
 
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
ShaiAlmog1
 

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

The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
Vlad Stirbu
 
Ransomware Mallox [EN].pdf
Ransomware         Mallox       [EN].pdfRansomware         Mallox       [EN].pdf
Ransomware Mallox [EN].pdf
Overkill Security
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Peter Udo Diehl
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 

Recently uploaded (20)

The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
 
Ransomware Mallox [EN].pdf
Ransomware         Mallox       [EN].pdfRansomware         Mallox       [EN].pdf
Ransomware Mallox [EN].pdf
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 

Creating a Facebook Clone - Part XXIII - Transcript.pdf

  • 1. Creating a Facebook Clone - Part XXIII The NotificationService is arguably one of the simplest services we have right now. Its main reason for existence is push notification that we'll integrate into it later on.
  • 2. @Service public class NotificationService { @Autowired private NotificationRepository notifications; 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); } 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 As I said the class is trivial and contains just 2 methods. The only repository is the notification repository as we don’t need access to anything else for this service.
  • 3. @Service public class NotificationService { @Autowired private NotificationRepository notifications; 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); } 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 sendNotification is important as this would be a great location to send push notification or trigger a websocket based push in the future
  • 4. @Service public class NotificationService { @Autowired private NotificationRepository notifications; 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); } 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 Right now we just save the data to the database which we would do anyway as we want to keep track of notifications
  • 5. 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); } 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"))); List<NotificationDAO> resp = new ArrayList<>(); for(Notification c : notificationsPage) { resp.add(c.getDAO()); } return resp; } } NotificationService This is an API for the client side so it can list the notifications pending to the user, it accepts a page number so we can go through pages of results
  • 6. 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); } 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"))); List<NotificationDAO> resp = new ArrayList<>(); for(Notification c : notificationsPage) { resp.add(c.getDAO()); } return resp; } } NotificationService When we go through the notification pages we also pass the sort value to indicate we want notifications arranged by data
  • 7. 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); } 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"))); List<NotificationDAO> resp = new ArrayList<>(); for(Notification c : notificationsPage) { resp.add(c.getDAO()); } return resp; } } NotificationService The return value is the DAO's for the current page carrying the notification data. The send notification API is an API designed for the server side. Notifications only originate within the server to prevent abuse by a malicious client.
  • 8. @Service public class MediaService { @Autowired private MediaRepository media; @Autowired private UserRepository user; public String storeMedia(String authtoken, byte[] data, String mimeType, String role, String visibility, String filename) { User u = user.findByAuthtoken(authtoken).get(0); Media m = new Media(); m.setData(data); m.setOwner(u); m.setDate(System.currentTimeMillis()); m.setFilename(filename); m.setMimeType(mimeType); m.setRole(role); m.setVisibility(visibility); MediaService We continue the trend of simple services with the MediaService which is almost as simple as the NotificationService. The media service works with the media repository only but it needs to authenticate some actions against the user repository
  • 9. @Service public class MediaService { @Autowired private MediaRepository media; @Autowired private UserRepository user; public String storeMedia(String authtoken, byte[] data, String mimeType, String role, String visibility, String filename) { User u = user.findByAuthtoken(authtoken).get(0); Media m = new Media(); m.setData(data); m.setOwner(u); m.setDate(System.currentTimeMillis()); m.setFilename(filename); m.setMimeType(mimeType); m.setRole(role); m.setVisibility(visibility); MediaService When adding a new media we accept almost all the values from the client with the exception of time which we store in the server to prevent abuse
  • 10. @Service public class MediaService { @Autowired private MediaRepository media; @Autowired private UserRepository user; public String storeMedia(String authtoken, byte[] data, String mimeType, String role, String visibility, String filename) { User u = user.findByAuthtoken(authtoken).get(0); Media m = new Media(); m.setData(data); m.setOwner(u); m.setDate(System.currentTimeMillis()); m.setFilename(filename); m.setMimeType(mimeType); m.setRole(role); m.setVisibility(visibility); MediaService We need an auth token to make sure the media is saved under the correct user
  • 11. @Autowired private MediaRepository media; @Autowired private UserRepository user; public String storeMedia(String authtoken, byte[] data, String mimeType, String role, String visibility, String filename) { User u = user.findByAuthtoken(authtoken).get(0); Media m = new Media(); m.setData(data); m.setOwner(u); m.setDate(System.currentTimeMillis()); m.setFilename(filename); m.setMimeType(mimeType); m.setRole(role); m.setVisibility(visibility); media.save(m); return m.getId(); } public MediaDAO getPublicMedia(String id) throws PermissionException { Media m = media.findById(id).get(); if(VisibilityConstants.isPublic(m.getVisibility())) { return m.getDAO(); MediaService We return the media ID when we are done so it can be referenced by the user
  • 12. m.setRole(role); m.setVisibility(visibility); media.save(m); return m.getId(); } public MediaDAO getPublicMedia(String id) throws PermissionException { Media m = media.findById(id).get(); if(VisibilityConstants.isPublic(m.getVisibility())) { return m.getDAO(); } throw new PermissionException( "Media item belongs to a user that isn't a friend"); } public MediaDAO getMedia(String authToken, String id) throws PermissionException { Media m = media.findById(id).get(); if(VisibilityConstants.isPublic(m.getVisibility())) { return m.getDAO(); } if(!m.getOwner().getAuthtoken().equals(authToken)) { if(!m.getOwner().isFriendByToken(authToken)) { throw new PermissionException( "Media item belongs to a user that isn't a friend"); } MediaService Public media can be requested via the id of the media and doesn't require any authentication
  • 13. media.save(m); return m.getId(); } public MediaDAO getPublicMedia(String id) throws PermissionException { Media m = media.findById(id).get(); if(VisibilityConstants.isPublic(m.getVisibility())) { return m.getDAO(); } throw new PermissionException( "Media item belongs to a user that isn't a friend"); } public MediaDAO getMedia(String authToken, String id) throws PermissionException { Media m = media.findById(id).get(); if(VisibilityConstants.isPublic(m.getVisibility())) { return m.getDAO(); } if(!m.getOwner().getAuthtoken().equals(authToken)) { if(!m.getOwner().isFriendByToken(authToken)) { throw new PermissionException( "Media item belongs to a user that isn't a friend"); } } return m.getDAO(); MediaService public enum VisibilityConstants { PUBLIC("public"), FRIENDS("friends"); private String str; private VisibilityConstants(String str) { this.str = str; } public String asString() { return str; } public static boolean isPublic(String s) { return s == null || s.equalsIgnoreCase(PUBLIC.asString()); } } We need to check that the user is indeed requesting a public media object and isn't spoofing a request. This obviously brings up the visibility constants enum which is pretty trivial right now. We just have two visibility types for public & friend. As you can see the isPublic method tests if the field is public or null.
  • 14. media.save(m); return m.getId(); } public MediaDAO getPublicMedia(String id) throws PermissionException { Media m = media.findById(id).get(); if(VisibilityConstants.isPublic(m.getVisibility())) { return m.getDAO(); } throw new PermissionException( "Media item belongs to a user that isn't a friend"); } public MediaDAO getMedia(String authToken, String id) throws PermissionException { Media m = media.findById(id).get(); if(VisibilityConstants.isPublic(m.getVisibility())) { return m.getDAO(); } if(!m.getOwner().getAuthtoken().equals(authToken)) { if(!m.getOwner().isFriendByToken(authToken)) { throw new PermissionException( "Media item belongs to a user that isn't a friend"); } } return m.getDAO(); MediaService public class PermissionException extends Exception { public PermissionException(String message) { super(message); } } The permission exception is thrown otherwise. That exception is pretty simple and again just a marker exception for the webservice layer
  • 15. public MediaDAO getPublicMedia(String id) throws PermissionException { Media m = media.findById(id).get(); if(VisibilityConstants.isPublic(m.getVisibility())) { return m.getDAO(); } throw new PermissionException( "Media item belongs to a user that isn't a friend"); } public MediaDAO getMedia(String authToken, String id) throws PermissionException { Media m = media.findById(id).get(); if(VisibilityConstants.isPublic(m.getVisibility())) { return m.getDAO(); } if(!m.getOwner().getAuthtoken().equals(authToken)) { if(!m.getOwner().isFriendByToken(authToken)) { throw new PermissionException( "Media item belongs to a user that isn't a friend"); } } return m.getDAO(); } } MediaService Finally for media that could potentially be private we need this method, it accepts the user token and media id
  • 16. public MediaDAO getPublicMedia(String id) throws PermissionException { Media m = media.findById(id).get(); if(VisibilityConstants.isPublic(m.getVisibility())) { return m.getDAO(); } throw new PermissionException( "Media item belongs to a user that isn't a friend"); } public MediaDAO getMedia(String authToken, String id) throws PermissionException { Media m = media.findById(id).get(); if(VisibilityConstants.isPublic(m.getVisibility())) { return m.getDAO(); } if(!m.getOwner().getAuthtoken().equals(authToken)) { if(!m.getOwner().isFriendByToken(authToken)) { throw new PermissionException( "Media item belongs to a user that isn't a friend"); } } return m.getDAO(); } } MediaService If the media is public then there is no problem we can just return it
  • 17. public MediaDAO getPublicMedia(String id) throws PermissionException { Media m = media.findById(id).get(); if(VisibilityConstants.isPublic(m.getVisibility())) { return m.getDAO(); } throw new PermissionException( "Media item belongs to a user that isn't a friend"); } public MediaDAO getMedia(String authToken, String id) throws PermissionException { Media m = media.findById(id).get(); if(VisibilityConstants.isPublic(m.getVisibility())) { return m.getDAO(); } if(!m.getOwner().getAuthtoken().equals(authToken)) { if(!m.getOwner().isFriendByToken(authToken)) { throw new PermissionException( "Media item belongs to a user that isn't a friend"); } } return m.getDAO(); } } MediaService If the media is for friends only we need to verify that the user requesting the media is indeed a friend