Boost Fertility New Invention Ups Success Rates.pdf
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
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.