-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring EntityDescriptorController - moving as much logic for permission checking and error handling out of the controller to the service layer and ExceptionHandler
- Loading branch information
Showing
9 changed files
with
224 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...rnet2/tier/shibboleth/admin/ui/controller/EntityDescriptorControllerExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package edu.internet2.tier.shibboleth.admin.ui.controller; | ||
|
|
||
| import java.util.ConcurrentModificationException; | ||
|
|
||
| import org.springframework.http.HttpHeaders; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.ControllerAdvice; | ||
| import org.springframework.web.bind.annotation.ExceptionHandler; | ||
| import org.springframework.web.context.request.WebRequest; | ||
| import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
|
||
| import edu.internet2.tier.shibboleth.admin.ui.exception.EntityIdExistsException; | ||
| import edu.internet2.tier.shibboleth.admin.ui.exception.EntityNotFoundException; | ||
| import edu.internet2.tier.shibboleth.admin.ui.exception.ForbiddenException; | ||
|
|
||
| @ControllerAdvice(assignableTypes = {EntityDescriptorController.class}) | ||
| public class EntityDescriptorControllerExceptionHandler extends ResponseEntityExceptionHandler { | ||
|
|
||
| @ExceptionHandler({ ForbiddenException.class }) | ||
| public ResponseEntity<?> handleForbiddenAccess(ForbiddenException e, WebRequest request) { | ||
| return ResponseEntity.status(HttpStatus.FORBIDDEN).body(new ErrorResponse(HttpStatus.FORBIDDEN, e.getMessage())); | ||
| } | ||
|
|
||
| @ExceptionHandler({ EntityIdExistsException.class }) | ||
| public ResponseEntity<?> handleEntityExistsException(EntityIdExistsException e, WebRequest request) { | ||
| HttpHeaders headers = new HttpHeaders(); | ||
| headers.setLocation(EntityDescriptorController.getResourceUriFor(e.getMessage())); | ||
| return ResponseEntity.status(HttpStatus.CONFLICT).headers(headers).body(new ErrorResponse( | ||
| String.valueOf(HttpStatus.CONFLICT.value()), | ||
| String.format("The entity descriptor with entity id [%s] already exists.", e.getMessage()))); | ||
|
|
||
| } | ||
|
|
||
| @ExceptionHandler({ EntityNotFoundException.class }) | ||
| public ResponseEntity<?> handleEntityNotFoundException(EntityNotFoundException e, WebRequest request) { | ||
| return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse(HttpStatus.NOT_FOUND, e.getMessage())); | ||
| } | ||
|
|
||
| @ExceptionHandler({ ConcurrentModificationException.class }) | ||
| public ResponseEntity<?> handleConcurrentModificationException(ConcurrentModificationException e, WebRequest request) { | ||
| return ResponseEntity.status(HttpStatus.CONFLICT).body(new ErrorResponse(HttpStatus.CONFLICT, e.getMessage())); | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
...c/main/java/edu/internet2/tier/shibboleth/admin/ui/exception/EntityIdExistsException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package edu.internet2.tier.shibboleth.admin.ui.exception; | ||
|
|
||
| public class EntityIdExistsException extends Exception { | ||
| public EntityIdExistsException(String entityId) { | ||
| super(entityId); | ||
| } | ||
|
|
||
| } |
7 changes: 7 additions & 0 deletions
7
...c/main/java/edu/internet2/tier/shibboleth/admin/ui/exception/EntityNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package edu.internet2.tier.shibboleth.admin.ui.exception; | ||
|
|
||
| public class EntityNotFoundException extends Exception { | ||
| public EntityNotFoundException(String message) { | ||
| super(message); | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
...nd/src/main/java/edu/internet2/tier/shibboleth/admin/ui/exception/ForbiddenException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package edu.internet2.tier.shibboleth.admin.ui.exception; | ||
|
|
||
| public class ForbiddenException extends Exception { | ||
| public ForbiddenException() { | ||
| super("You are not authorized to perform the requested operation."); | ||
| } | ||
|
|
||
| public ForbiddenException(String message) { | ||
| super(message); | ||
| } | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.