diff options
author | benal01 <bja4245@rit.edu> | 2025-04-01 09:34:36 -0400 |
---|---|---|
committer | benal01 <bja4245@rit.edu> | 2025-04-01 09:34:36 -0400 |
commit | 7ed26c5ee7171a502f6f8527fc55de2bb77eab3b (patch) | |
tree | 2046e58c146097aac21c9e352771420c31df6589 /ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java | |
parent | ef46ddd082bb91d0262363536d46fe3eb4da47be (diff) | |
parent | d8330f1ac85b26d08ca4df5ce3875078d7b4f47f (diff) | |
download | JellySolutions-7ed26c5ee7171a502f6f8527fc55de2bb77eab3b.tar.gz JellySolutions-7ed26c5ee7171a502f6f8527fc55de2bb77eab3b.tar.bz2 JellySolutions-7ed26c5ee7171a502f6f8527fc55de2bb77eab3b.zip |
Merge branch 'main' of https://github.com/RIT-SWEN-261-02/team-project-2245-swen-261-02-2b-jellysolutions
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 | 68 |
1 files changed, 40 insertions, 28 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 dfaad3a..a34e891 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 @@ -1,7 +1,6 @@ package com.ufund.api.ufundapi.controller; import java.io.IOException; -import java.security.InvalidParameterException; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -42,7 +41,8 @@ public class UserController { * otherwise */ @PostMapping("") - public ResponseEntity<User> createUser(@RequestBody Map<String, String> params) { + public ResponseEntity<Object> 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,9 +54,11 @@ public class UserController { return new ResponseEntity<>(HttpStatus.CONFLICT); } } catch (DuplicateKeyException ex) { - return new ResponseEntity<>(HttpStatus.CONFLICT); + LOG.log(Level.WARNING, ex.getLocalizedMessage()); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.CONFLICT); } catch (IOException ex) { - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + LOG.log(Level.SEVERE, ex.getLocalizedMessage()); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -71,11 +73,11 @@ public class UserController { * ResponseEntity with HTTP status of INTERNAL_SERVER_ERROR otherwise */ @GetMapping("/{username}") - public ResponseEntity<User> getUser(@PathVariable String username, @RequestHeader("jelly-api-key") String key) { - LOG.log(Level.INFO, "GET /user/{0}", username); + public ResponseEntity<Object> getUser(@PathVariable String username, @RequestHeader("jelly-api-key") String key) { + LOG.log(Level.INFO, "GET /user/{0} key={1}", of(username, key)); try { - authService.authenticate(username, key); + authService.keyHasAccessToUser(username, key); User user = userService.getUser(username); if (user != null) { return new ResponseEntity<>(user.withoutPasswordHash(), HttpStatus.OK); @@ -83,10 +85,11 @@ public class UserController { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } } catch (IllegalAccessException ex) { - return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); - } catch (IOException e) { - LOG.log(Level.SEVERE, e.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + LOG.log(Level.WARNING, ex.getLocalizedMessage()); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.UNAUTHORIZED); + } catch (IOException ex) { + LOG.log(Level.SEVERE, ex.getLocalizedMessage()); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -101,24 +104,26 @@ public class UserController { * INTERNAL_SERVER_ERROR if there was an issue */ @PutMapping("/{username}") - public ResponseEntity<User> updateUser(@RequestBody User user, @PathVariable String username, @RequestHeader("jelly-api-key") String key) { - LOG.log(Level.INFO,"PUT: " + user + " " + username + " " + key.toString()); + public ResponseEntity<Object> 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(username, user, key)); try { - //authService.authenticate(username, key); + authService.keyHasAccessToUser(username, key); user = userService.updateUser(user, username); if (user != null) { return new ResponseEntity<>(user, HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } - } catch (InvalidParameterException ex) { - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); - } catch (IOException e) { - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); - } - // catch (IllegalAccessException e) { - // return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); - // } + } catch (IllegalArgumentException ex) { + LOG.log(Level.WARNING, ex.getLocalizedMessage()); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.BAD_REQUEST); + } catch (IllegalAccessException ex) { + LOG.log(Level.WARNING, ex.getLocalizedMessage()); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.UNAUTHORIZED); + } catch (IOException ex) { + LOG.log(Level.SEVERE, ex.getLocalizedMessage()); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); + } } /** @@ -130,20 +135,27 @@ public class UserController { * INTERNAL_SERVER_ERROR if an error occurred */ @DeleteMapping("/{username}") - public ResponseEntity<Boolean> deleteUser(@PathVariable String username, @RequestHeader("jelly-api-key") String key) { + public ResponseEntity<Object> 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); + authService.keyHasAccessToUser(username, key); if (userService.deleteUser(username)) { return new ResponseEntity<>(HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } - } catch (IOException e) { - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); - } catch (IllegalAccessException e) { - return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); + } catch (IllegalAccessException ex) { + LOG.log(Level.WARNING, ex.getLocalizedMessage()); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.UNAUTHORIZED); + } catch (IOException ex) { + LOG.log(Level.SEVERE, ex.getLocalizedMessage()); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } + private Object[] of(Object ...params) { + return params; + } + } |