private void validateUser(User user) throws Exception { StringBuilder errorList = new StringBuilder(); if (user.getUsername() == null || user.getUsername().equals("") || user.getPassword() == null || user.getPassword().equals("")) { errorList.append("Please, provide username and password"); errorList.append(System.getProperty("line.separator")); } else if (userService.find(user.getUsername(), Encryptor.MD5(user.getPassword())) == null) { errorList.append("Please, provide valid username and password"); errorList.append(System.getProperty("line.separator")); } if (errorList.length() > 0) { throw new Exception(errorList.toString()); } }
@RequestMapping(value = "/login", method = RequestMethod.POST, consumes = "application/json") public ResponseEntity<Response> login(@RequestBody User user) { Response response = new Response(); try { validateUser(user); user.setLoggedIn(true); userService.update(user); response.setSuccess(true); response.addMessage("Logged in successfuly"); return new ResponseEntity<Response>(response, HttpStatus.OK); } catch (Exception ex) { response.setSuccess(false); for (String message : ex.getMessage().split(System.getProperty("line.separator"))) { response.addMessage(message); } return new ResponseEntity<Response>(response, HttpStatus.BAD_REQUEST); } }