Revision 60107
Added by Konstantina Galouni over 3 years ago
ExceptionsHandler.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.uoamonitorservice.handlers; |
2 | 2 |
|
3 |
import eu.dnetlib.uoamonitorservice.responses.ExceptionResponse;
|
|
3 |
import eu.dnetlib.uoaadmintoolslibrary.responses.ExceptionResponse;
|
|
4 | 4 |
import org.apache.log4j.Logger; |
5 |
import org.springframework.data.crossstore.ChangeSetPersister; |
|
6 | 5 |
import org.springframework.http.HttpStatus; |
7 | 6 |
import org.springframework.http.ResponseEntity; |
8 |
import org.springframework.security.access.AccessDeniedException; |
|
9 |
import org.springframework.web.bind.MissingServletRequestParameterException; |
|
10 | 7 |
import org.springframework.web.bind.annotation.ControllerAdvice; |
11 | 8 |
import org.springframework.web.bind.annotation.ExceptionHandler; |
12 | 9 |
import org.springframework.web.bind.annotation.RestController; |
13 |
import org.springframework.web.multipart.support.MissingServletRequestPartException; |
|
14 | 10 |
|
15 | 11 |
@ControllerAdvice |
16 | 12 |
@RestController |
17 | 13 |
public class ExceptionsHandler { |
18 | 14 |
private final Logger log = Logger.getLogger(this.getClass()); |
19 | 15 |
|
20 |
@ExceptionHandler(MissingServletRequestParameterException.class) |
|
21 |
public ResponseEntity<ExceptionResponse> invalidInput(Exception ex) { |
|
22 |
ExceptionResponse response = new ExceptionResponse(); |
|
23 |
response.setErrorCode("Validation Error"); |
|
24 |
response.setErrorMessage("Invalid inputs"); |
|
25 |
response.setErrors(ex.getMessage()); |
|
26 |
response.setStatus(HttpStatus.BAD_REQUEST); |
|
27 |
log.error("invalidInput exception : "+ ex.getMessage()); |
|
28 |
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.BAD_REQUEST); |
|
29 |
} |
|
30 |
|
|
31 |
@ExceptionHandler(NullPointerException.class) |
|
32 |
public ResponseEntity<ExceptionResponse> nullPointerException(Exception ex) { |
|
33 |
ExceptionResponse response = new ExceptionResponse(); |
|
34 |
response.setErrorCode("Null pointer Exception"); |
|
35 |
response.setErrorMessage("Null pointer Exception"); |
|
36 |
response.setErrors(ex.getMessage()); |
|
37 |
response.setStatus(HttpStatus.BAD_REQUEST); |
|
38 |
log.error("nullPointerException exception : "+ ex.getMessage()); |
|
39 |
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.BAD_REQUEST); |
|
40 |
} |
|
41 |
|
|
42 |
@ExceptionHandler(ChangeSetPersister.NotFoundException.class) |
|
43 |
public ResponseEntity<ExceptionResponse> notFoundException(Exception ex) { |
|
44 |
ExceptionResponse response = new ExceptionResponse(); |
|
45 |
response.setErrorCode("Not found Exception"); |
|
46 |
response.setErrorMessage("Not found Exception"); |
|
47 |
response.setErrors(ex.getMessage()); |
|
48 |
response.setStatus(HttpStatus.NOT_FOUND); |
|
49 |
log.error("notFoundException exception : "+ ex.getMessage()); |
|
50 |
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.NOT_FOUND); |
|
51 |
} |
|
52 |
|
|
53 | 16 |
@ExceptionHandler(EntityNotFoundException.class) |
54 | 17 |
public ResponseEntity<ExceptionResponse> entityNotFoundException(Exception ex) { |
55 | 18 |
ExceptionResponse response = new ExceptionResponse(); |
... | ... | |
72 | 35 |
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.NOT_FOUND); |
73 | 36 |
} |
74 | 37 |
|
75 |
@ExceptionHandler(AccessDeniedException.class) |
|
76 |
public ResponseEntity<ExceptionResponse> accessDeniedException(Exception ex) { |
|
77 |
ExceptionResponse response = new ExceptionResponse(); |
|
78 |
response.setErrorCode("Forbidden Exception"); |
|
79 |
response.setErrorMessage("Access Denied Exception"); |
|
80 |
response.setErrors(ex.getMessage()); |
|
81 |
response.setStatus(HttpStatus.FORBIDDEN); |
|
82 |
log.error("accessDeniedException exception : "+ ex.getMessage()); |
|
83 |
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.FORBIDDEN); |
|
84 |
} |
|
85 | 38 |
} |
Also available in: Unified diff
[Trunk | Monitor Service]:
1. StakeholderController.java & TopicController.java & CategoryController.java & SubCategoryController.java && SectionController.java && IndicatorController.java:
a. Comment logs for get requests.
b. Use "ForbiddenException" instead of "AccessDeniedException"
c. On /save, if full entity has id (already in DB), if not found in DB throw EntityNotFoundException.
d. Get children (e.g. when saving a Topic, get its categories) from DB.
2. TopicController.java & CategoryController.java & SubCategoryController.java & SectionController.java:
In /reorder, if there are in DB, ids that are missing from reordered list, do reordering and add in the end of list the missing ids.
3. ReorderEvent.java: [NEW] Added class ReorderEvent with fields "action" (String), "target" (String), "ids" (List<String>) (used in IndicatorController.java).
4. IndicatorController.java:
a. In /reorder, @RequestBody changed from List<String> indicators to ReorderEvent reorderEvent.
b. If there are in DB, ids that are missing from reordered list AND missing id is not moved to other section (action = removed and target = missing id), do reordering and add in the end of list the missing ids.
5. ExceptionsHandler.java: exception handler methods "invalidInput()", "nullPointerException()", "notFoundException()" moved to "Admin Tools Library" - "accessDeniedException()" is removed.
6. responses/ExceptionResponse.java: File and folder deleted (moved to "Admin Tools Library").
7. RolesUtils.java: Added method "isLoggedIn()" (checks if no roles for user, or user has role "ROLE_ANONYMOUS").