1 package fr.ifremer.quadrige3.synchro.server.rest;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 import fr.ifremer.quadrige3.core.dao.technical.gson.Gsons;
27 import fr.ifremer.quadrige3.core.dao.technical.http.CustomHttpHeaders;
28 import fr.ifremer.quadrige3.core.dao.technical.http.CustomHttpStatus;
29 import fr.ifremer.quadrige3.core.exception.BadUpdateDtException;
30 import fr.ifremer.quadrige3.core.exception.DataLockedException;
31 import fr.ifremer.quadrige3.core.exception.DeleteForbiddenException;
32 import fr.ifremer.quadrige3.core.exception.SaveForbiddenException;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.springframework.http.HttpHeaders;
36 import org.springframework.http.HttpStatus;
37 import org.springframework.http.ResponseEntity;
38 import org.springframework.web.bind.annotation.ControllerAdvice;
39 import org.springframework.web.bind.annotation.ExceptionHandler;
40 import org.springframework.web.context.request.WebRequest;
41 import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
42
43 @ControllerAdvice
44 public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
45
46 private static final Log log = LogFactory.getLog(RestResponseEntityExceptionHandler.class);
47 private static final String MESSAGE_FORMAT = "[%s] %s";
48
49 public RestResponseEntityExceptionHandler() {
50 super();
51 }
52
53
54
55
56 @ExceptionHandler(value = {BadUpdateDtException.class})
57 protected ResponseEntity<Object> handleBadUpdateDt(RuntimeException ex, WebRequest request) {
58 String message = String.format(MESSAGE_FORMAT, CustomHttpStatus.SC_BAD_UPDATE_DT, ex.getMessage());
59 if (log.isDebugEnabled()) {
60 log.debug(message);
61 }
62 return handleExceptionInternal(ex, message, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request);
63 }
64
65
66
67
68 @ExceptionHandler(value = {DataLockedException.class})
69 protected ResponseEntity<Object> handleLock(RuntimeException ex, WebRequest request) {
70 String message = String.format(MESSAGE_FORMAT, CustomHttpStatus.SC_DATA_LOCKED, ex.getMessage());
71 if (log.isDebugEnabled()) {
72 log.debug(message);
73 }
74 return handleExceptionInternal(ex, message, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request);
75 }
76
77
78
79
80 @ExceptionHandler(value = {DeleteForbiddenException.class})
81 protected ResponseEntity<Object> handleDeleteForbidden(RuntimeException ex, WebRequest request) {
82 String message = String.format(MESSAGE_FORMAT, CustomHttpStatus.SC_DELETE_FORBIDDEN, ex.getMessage());
83 if (log.isDebugEnabled()) {
84 log.debug(message);
85 }
86 HttpHeaders httpHeaders = new HttpHeaders();
87
88 httpHeaders.add(
89 CustomHttpHeaders.HH_DELETE_FORBIDDEN_OBJECT_IDS,
90 Gsons.newBuilder().create().toJson(((DeleteForbiddenException) ex).getObjectIds()));
91 return handleExceptionInternal(ex, message, httpHeaders, HttpStatus.INTERNAL_SERVER_ERROR, request);
92 }
93
94
95
96
97 @ExceptionHandler(value = {SaveForbiddenException.class})
98 protected ResponseEntity<Object> handleSaveForbidden(RuntimeException ex, WebRequest request) {
99 String message = String.format(MESSAGE_FORMAT, CustomHttpStatus.SC_SAVE_FORBIDDEN, ex.getMessage());
100 if (log.isDebugEnabled()) {
101 log.debug(message);
102 }
103 HttpHeaders httpHeaders = new HttpHeaders();
104
105 httpHeaders.add(
106 CustomHttpHeaders.HH_SAVE_FORBIDDEN_TYPE,
107 ((SaveForbiddenException)ex).getType().name().toUpperCase()
108 );
109 httpHeaders.add(
110 CustomHttpHeaders.HH_SAVE_FORBIDDEN_OBJECT_IDS,
111 Gsons.newBuilder().create().toJson(((SaveForbiddenException) ex).getObjectIds())
112 );
113 return handleExceptionInternal(ex, message, httpHeaders, HttpStatus.INTERNAL_SERVER_ERROR, request);
114 }
115
116 }