SlideShare a Scribd company logo
Creating a WhatsApp Clone - Part XIV
Next we’ll jump to the webservice package
@Controller
@RequestMapping("/user")
@RestController
public class UserWebService {
@Autowired
private UserService users;
@ExceptionHandler(LoginException.class)
@ResponseStatus(value=HttpStatus.FORBIDDEN)
public @ResponseBody
ErrorDAO handleLoginException(LoginException e) {
return new ErrorDAO(e.getMessage(), 0);
}
@ExceptionHandler(SignupException.class)
@ResponseStatus(value=HttpStatus.FORBIDDEN)
public @ResponseBody
ErrorDAO handleSignupException(SignupException e) {
return new ErrorDAO(e.getMessage(), 0);
}
UserWebService
This is pretty much identical to the facebook clone app

We create a webservice mapping for user services. Technically we could have broken this down to more webservices but there is no real reason as we don't have that
much functionality here.
@Controller
@RequestMapping("/user")
@RestController
public class UserWebService {
@Autowired
private UserService users;
@ExceptionHandler(LoginException.class)
@ResponseStatus(value=HttpStatus.FORBIDDEN)
public @ResponseBody
ErrorDAO handleLoginException(LoginException e) {
return new ErrorDAO(e.getMessage(), 0);
}
@ExceptionHandler(SignupException.class)
@ResponseStatus(value=HttpStatus.FORBIDDEN)
public @ResponseBody
ErrorDAO handleSignupException(SignupException e) {
return new ErrorDAO(e.getMessage(), 0);
}
@RequestMapping(method=RequestMethod.POST, value="/login")
public @ResponseBody
UserWebService
This is a thin wrapper around UserService that contains no actual functionality. It only translates the logic in that class to web calls
@Controller
@RequestMapping("/user")
@RestController
public class UserWebService {
@Autowired
private UserService users;
@ExceptionHandler(LoginException.class)
@ResponseStatus(value=HttpStatus.FORBIDDEN)
public @ResponseBody
ErrorDAO handleLoginException(LoginException e) {
return new ErrorDAO(e.getMessage(), 0);
}
@ExceptionHandler(SignupException.class)
@ResponseStatus(value=HttpStatus.FORBIDDEN)
public @ResponseBody
ErrorDAO handleSignupException(SignupException e) {
return new ErrorDAO(e.getMessage(), 0);
}
@RequestMapping(method=RequestMethod.POST, value="/login")
public @ResponseBody
UserWebService
If an exception is thrown in this class it’s implicitly translated to an ErrorDAO which is translated to an error JSON
@ExceptionHandler(SignupException.class)
@ResponseStatus(value=HttpStatus.FORBIDDEN)
public @ResponseBody
ErrorDAO handleSignupException(SignupException e) {
return new ErrorDAO(e.getMessage(), 0);
}
@RequestMapping(method=RequestMethod.POST, value="/login")
public @ResponseBody
UserDAO login(@RequestHeader String auth, @RequestBody UserDAO u)
throws LoginException {
return users.login(u.getPhone(), auth);
}
@RequestMapping(method=RequestMethod.POST, value="/signup")
public @ResponseBody
UserDAO signup(@RequestBody UserDAO user)
throws SignupException {
return users.signup(user);
}
@RequestMapping(method=RequestMethod.GET, value="/verify")
public @ResponseBody
UserWebService
Login and signup are almost identical with the small exception that login expects an auth header value. Both are simple post methods that return the DAO object as
JSON body to the client
UserDAO signup(@RequestBody UserDAO user)
throws SignupException {
return users.signup(user);
}
@RequestMapping(method=RequestMethod.GET, value="/verify")
public @ResponseBody
String verifyPhone(
@RequestParam String userId,
@RequestParam String code) {
if(users.verifyPhone(userId, code)) {
return "OK";
}
return "ERROR";
}
@RequestMapping(method=RequestMethod.POST, value="/update")
public String update(@RequestHeader String auth,
@RequestBody UserDAO user) {
users.update(auth, user);
return "OK";
}
@RequestMapping(value="/avatar/{id:.+}", method=RequestMethod.GET)
UserWebService
Verify and update return string values to indicate that they succeeded
users.update(auth, user);
return "OK";
}
@RequestMapping(value="/avatar/{id:.+}", method=RequestMethod.GET)
public ResponseEntity<byte[]> getAvatar(
@PathVariable("id") String id) {
byte[] av = users.getAvatar(id);
if(av != null) {
return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).
body(av);
}
return ResponseEntity.notFound().build();
}
@RequestMapping(method=RequestMethod.GET, value="/set-avatar")
public String setAvatar(
@RequestHeader String auth,
@RequestParam String userId,
@RequestParam String mediaId) {
users.setAvatar(auth, userId, mediaId);
return "OK";
}
UserWebService
I added the implementation to set/get avatar via URL but this isn’t mapped in the client side. This can probably be implemented in the same way as the facebook clone
users.setAvatar(auth, userId, mediaId);
return "OK";
}
@RequestMapping(method=RequestMethod.GET, value="/findRegisteredUser")
public List<UserDAO> findRegisteredUser(String phone) {
UserDAO d = users.findRegisteredUser(phone);
if(d == null) {
return new ArrayList<>();
}
return Arrays.asList(d);
}
@RequestMapping(method=RequestMethod.GET,
value="/findRegisteredUserById")
public List<UserDAO> findRegisteredUserById(String id) {
UserDAO d = users.findRegisteredUserById(id);
if(d == null) {
return new ArrayList<>();
}
return Arrays.asList(d);
}
@RequestMapping(method=RequestMethod.POST, value="/sendMessage")
UserWebService
These methods return their result as an array of one element or as a zero length array. Since there is no way in JSON to return null like the business logic method does.
So we return a blank array list or a list with one element.
return new ArrayList<>();
}
return Arrays.asList(d);
}
@RequestMapping(method=RequestMethod.POST, value="/sendMessage")
public MessageDAO sendMessage(@RequestHeader String auth,
@RequestBody MessageDAO m) {
return users.sendMessage(m);
}
@RequestMapping(method=RequestMethod.POST, value="/ackMessage")
public void ackMessage(@RequestHeader String auth,
@RequestBody String id) {
users.ackMessage(id);
}
@RequestMapping(method=RequestMethod.GET, value="/updatePushKey")
public void updatePushKey(@RequestHeader String auth, String id,
String key) {
users.updatePushKey(auth, id, key);
}
}
UserWebService
And that’s the end of the class, the rest of the methods delegate directly to the user service bean.

More Related Content

PDF
Creating a Facebook Clone - Part XXV.pdf
PDF
Creating a Facebook Clone - Part XXV - Transcript.pdf
PDF
Creating a Whatsapp Clone - Part XIV.pdf
PDF
Creating a Facebook Clone - Part XXVI.pdf
PDF
Creating a Facebook Clone - Part XXVI - Transcript.pdf
ODP
Codemotion appengine
PDF
Creating a Facebook Clone - Part XXVII - Transcript.pdf
PDF
Creating a Whatsapp Clone - Part XIII.pdf
Creating a Facebook Clone - Part XXV.pdf
Creating a Facebook Clone - Part XXV - Transcript.pdf
Creating a Whatsapp Clone - Part XIV.pdf
Creating a Facebook Clone - Part XXVI.pdf
Creating a Facebook Clone - Part XXVI - Transcript.pdf
Codemotion appengine
Creating a Facebook Clone - Part XXVII - Transcript.pdf
Creating a Whatsapp Clone - Part XIII.pdf

Similar to Creating a Whatsapp Clone - Part XIV - Transcript.pdf (20)

PDF
Creating a Whatsapp Clone - Part II - Transcript.pdf
PDF
Creating a Whatsapp Clone - Part XIII - Transcript.pdf
PDF
Creating a Facebook Clone - Part XLV - Transcript.pdf
PPT
Easy rest service using PHP reflection api
PPTX
Angular Tutorial Freshers and Experienced
PPTX
Crafting beautiful software
PDF
Creating a Facebook Clone - Part XXVIII - Transcript.pdf
PDF
Mashing up JavaScript – Advanced Techniques for modern Web Apps
PDF
Mashing up JavaScript
PPT
Struts 2 + Spring
PPTX
Testando API's de forma unitária mocando as dependências
PDF
Android Testing
PDF
May 2010 - RestEasy
PDF
JavaOne Brasil 2016: JavaEE e HTML5: da web/desktop ao mobile
PDF
Creating an Uber Clone - Part XXXIV - Transcript.pdf
PDF
WordPress REST API hacking
PDF
Doctrine For Beginners
PDF
Lesson_07_Spring_Security_Register_NEW.pdf
PPTX
Orchestrating things in Angular application
PDF
What's Coming in Spring 3.0
Creating a Whatsapp Clone - Part II - Transcript.pdf
Creating a Whatsapp Clone - Part XIII - Transcript.pdf
Creating a Facebook Clone - Part XLV - Transcript.pdf
Easy rest service using PHP reflection api
Angular Tutorial Freshers and Experienced
Crafting beautiful software
Creating a Facebook Clone - Part XXVIII - Transcript.pdf
Mashing up JavaScript – Advanced Techniques for modern Web Apps
Mashing up JavaScript
Struts 2 + Spring
Testando API's de forma unitária mocando as dependências
Android Testing
May 2010 - RestEasy
JavaOne Brasil 2016: JavaEE e HTML5: da web/desktop ao mobile
Creating an Uber Clone - Part XXXIV - Transcript.pdf
WordPress REST API hacking
Doctrine For Beginners
Lesson_07_Spring_Security_Register_NEW.pdf
Orchestrating things in Angular application
What's Coming in Spring 3.0

More from ShaiAlmog1 (20)

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

Recently uploaded (20)

PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Empathic Computing: Creating Shared Understanding
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Cloud computing and distributed systems.
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Big Data Technologies - Introduction.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
“AI and Expert System Decision Support & Business Intelligence Systems”
MIND Revenue Release Quarter 2 2025 Press Release
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Empathic Computing: Creating Shared Understanding
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Cloud computing and distributed systems.
NewMind AI Weekly Chronicles - August'25 Week I
MYSQL Presentation for SQL database connectivity
20250228 LYD VKU AI Blended-Learning.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Spectroscopy.pptx food analysis technology
Big Data Technologies - Introduction.pptx
Encapsulation_ Review paper, used for researhc scholars
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Dropbox Q2 2025 Financial Results & Investor Presentation
Review of recent advances in non-invasive hemoglobin estimation
Building Integrated photovoltaic BIPV_UPV.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing

Creating a Whatsapp Clone - Part XIV - Transcript.pdf

  • 1. Creating a WhatsApp Clone - Part XIV Next we’ll jump to the webservice package
  • 2. @Controller @RequestMapping("/user") @RestController public class UserWebService { @Autowired private UserService users; @ExceptionHandler(LoginException.class) @ResponseStatus(value=HttpStatus.FORBIDDEN) public @ResponseBody ErrorDAO handleLoginException(LoginException e) { return new ErrorDAO(e.getMessage(), 0); } @ExceptionHandler(SignupException.class) @ResponseStatus(value=HttpStatus.FORBIDDEN) public @ResponseBody ErrorDAO handleSignupException(SignupException e) { return new ErrorDAO(e.getMessage(), 0); } UserWebService This is pretty much identical to the facebook clone app We create a webservice mapping for user services. Technically we could have broken this down to more webservices but there is no real reason as we don't have that much functionality here.
  • 3. @Controller @RequestMapping("/user") @RestController public class UserWebService { @Autowired private UserService users; @ExceptionHandler(LoginException.class) @ResponseStatus(value=HttpStatus.FORBIDDEN) public @ResponseBody ErrorDAO handleLoginException(LoginException e) { return new ErrorDAO(e.getMessage(), 0); } @ExceptionHandler(SignupException.class) @ResponseStatus(value=HttpStatus.FORBIDDEN) public @ResponseBody ErrorDAO handleSignupException(SignupException e) { return new ErrorDAO(e.getMessage(), 0); } @RequestMapping(method=RequestMethod.POST, value="/login") public @ResponseBody UserWebService This is a thin wrapper around UserService that contains no actual functionality. It only translates the logic in that class to web calls
  • 4. @Controller @RequestMapping("/user") @RestController public class UserWebService { @Autowired private UserService users; @ExceptionHandler(LoginException.class) @ResponseStatus(value=HttpStatus.FORBIDDEN) public @ResponseBody ErrorDAO handleLoginException(LoginException e) { return new ErrorDAO(e.getMessage(), 0); } @ExceptionHandler(SignupException.class) @ResponseStatus(value=HttpStatus.FORBIDDEN) public @ResponseBody ErrorDAO handleSignupException(SignupException e) { return new ErrorDAO(e.getMessage(), 0); } @RequestMapping(method=RequestMethod.POST, value="/login") public @ResponseBody UserWebService If an exception is thrown in this class it’s implicitly translated to an ErrorDAO which is translated to an error JSON
  • 5. @ExceptionHandler(SignupException.class) @ResponseStatus(value=HttpStatus.FORBIDDEN) public @ResponseBody ErrorDAO handleSignupException(SignupException e) { return new ErrorDAO(e.getMessage(), 0); } @RequestMapping(method=RequestMethod.POST, value="/login") public @ResponseBody UserDAO login(@RequestHeader String auth, @RequestBody UserDAO u) throws LoginException { return users.login(u.getPhone(), auth); } @RequestMapping(method=RequestMethod.POST, value="/signup") public @ResponseBody UserDAO signup(@RequestBody UserDAO user) throws SignupException { return users.signup(user); } @RequestMapping(method=RequestMethod.GET, value="/verify") public @ResponseBody UserWebService Login and signup are almost identical with the small exception that login expects an auth header value. Both are simple post methods that return the DAO object as JSON body to the client
  • 6. UserDAO signup(@RequestBody UserDAO user) throws SignupException { return users.signup(user); } @RequestMapping(method=RequestMethod.GET, value="/verify") public @ResponseBody String verifyPhone( @RequestParam String userId, @RequestParam String code) { if(users.verifyPhone(userId, code)) { return "OK"; } return "ERROR"; } @RequestMapping(method=RequestMethod.POST, value="/update") public String update(@RequestHeader String auth, @RequestBody UserDAO user) { users.update(auth, user); return "OK"; } @RequestMapping(value="/avatar/{id:.+}", method=RequestMethod.GET) UserWebService Verify and update return string values to indicate that they succeeded
  • 7. users.update(auth, user); return "OK"; } @RequestMapping(value="/avatar/{id:.+}", method=RequestMethod.GET) public ResponseEntity<byte[]> getAvatar( @PathVariable("id") String id) { byte[] av = users.getAvatar(id); if(av != null) { return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG). body(av); } return ResponseEntity.notFound().build(); } @RequestMapping(method=RequestMethod.GET, value="/set-avatar") public String setAvatar( @RequestHeader String auth, @RequestParam String userId, @RequestParam String mediaId) { users.setAvatar(auth, userId, mediaId); return "OK"; } UserWebService I added the implementation to set/get avatar via URL but this isn’t mapped in the client side. This can probably be implemented in the same way as the facebook clone
  • 8. users.setAvatar(auth, userId, mediaId); return "OK"; } @RequestMapping(method=RequestMethod.GET, value="/findRegisteredUser") public List<UserDAO> findRegisteredUser(String phone) { UserDAO d = users.findRegisteredUser(phone); if(d == null) { return new ArrayList<>(); } return Arrays.asList(d); } @RequestMapping(method=RequestMethod.GET, value="/findRegisteredUserById") public List<UserDAO> findRegisteredUserById(String id) { UserDAO d = users.findRegisteredUserById(id); if(d == null) { return new ArrayList<>(); } return Arrays.asList(d); } @RequestMapping(method=RequestMethod.POST, value="/sendMessage") UserWebService These methods return their result as an array of one element or as a zero length array. Since there is no way in JSON to return null like the business logic method does. So we return a blank array list or a list with one element.
  • 9. return new ArrayList<>(); } return Arrays.asList(d); } @RequestMapping(method=RequestMethod.POST, value="/sendMessage") public MessageDAO sendMessage(@RequestHeader String auth, @RequestBody MessageDAO m) { return users.sendMessage(m); } @RequestMapping(method=RequestMethod.POST, value="/ackMessage") public void ackMessage(@RequestHeader String auth, @RequestBody String id) { users.ackMessage(id); } @RequestMapping(method=RequestMethod.GET, value="/updatePushKey") public void updatePushKey(@RequestHeader String auth, String id, String key) { users.updatePushKey(auth, id, key); } } UserWebService And that’s the end of the class, the rest of the methods delegate directly to the user service bean.