diff options
Diffstat (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java')
-rw-r--r-- | ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java | 38 |
1 files changed, 24 insertions, 14 deletions
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java index b0dbd1d..cd340ef 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java @@ -23,6 +23,8 @@ import com.ufund.api.ufundapi.model.User; import com.ufund.api.ufundapi.service.AuthService; import com.ufund.api.ufundapi.service.UserService; +import static java.util.List.of; + @RestController @RequestMapping("users") public class UserController { @@ -43,6 +45,7 @@ public class UserController { */ @PostMapping("") public ResponseEntity<User> createUser(@RequestBody Map<String, String> params) { + LOG.log(Level.INFO, "POST /users body: {0}", params); String username = params.get("username"); String password = params.get("password"); @@ -54,8 +57,10 @@ public class UserController { return new ResponseEntity<>(HttpStatus.CONFLICT); } } catch (DuplicateKeyException ex) { + LOG.log(Level.WARNING, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.CONFLICT); } catch (IOException ex) { + LOG.log(Level.SEVERE, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -72,7 +77,7 @@ public class UserController { */ @GetMapping("/{username}") public ResponseEntity<User> getUser(@PathVariable String username, @RequestHeader("jelly-api-key") String key) { - LOG.log(Level.INFO, "GET /user/{0}", username); + LOG.log(Level.INFO, "GET /user/{0} key: {1}", of(username, key)); try { authService.authenticate(username, key); @@ -83,9 +88,10 @@ public class UserController { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } } catch (IllegalAccessException ex) { + LOG.log(Level.WARNING, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); - } catch (IOException e) { - LOG.log(Level.SEVERE, e.getLocalizedMessage()); + } catch (IOException ex) { + LOG.log(Level.SEVERE, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } @@ -99,11 +105,10 @@ public class UserController { * @param key The authentication key of the user * @return OK response and the user if it was successful, or * INTERNAL_SERVER_ERROR if there was an issue - * @throws IllegalAccessException */ @PutMapping("/{username}") - public ResponseEntity<User> updateUser(@RequestHeader("jelly-api-key") String key, @RequestBody User user, @PathVariable String username) throws IllegalAccessException { - LOG.log(Level.INFO,"PUT: " + user + " " + username + " " + key); + public ResponseEntity<User> updateUser(@RequestBody User user, @PathVariable String username, @RequestHeader("jelly-api-key") String key) { + LOG.log(Level.INFO,"PUT /users/{0} body: {1} key: {2}", of(user, username, key)); try { authService.authenticate(username, key); user = userService.updateUser(user, username); @@ -113,13 +118,15 @@ public class UserController { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } } catch (InvalidParameterException ex) { + LOG.log(Level.WARNING, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.BAD_REQUEST); - } catch (IOException e) { + } catch (IllegalAccessException ex) { + LOG.log(Level.WARNING, ex.getLocalizedMessage()); + return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); + } catch (IOException ex) { + LOG.log(Level.SEVERE, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); - } - // catch (IllegalAccessException e) { - // return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); - // } + } } /** @@ -132,6 +139,7 @@ public class UserController { */ @DeleteMapping("/{username}") public ResponseEntity<Boolean> deleteUser(@PathVariable String username, @RequestHeader("jelly-api-key") String key) { + LOG.log(Level.INFO, "DELETE /users/{0} id: {1}", of(username, key)); try { authService.authenticate(username, key); @@ -140,10 +148,12 @@ public class UserController { } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } - } catch (IOException e) { - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); - } catch (IllegalAccessException e) { + } catch (IllegalAccessException ex) { + LOG.log(Level.WARNING, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); + } catch (IOException ex) { + LOG.log(Level.SEVERE, ex.getLocalizedMessage()); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } |