View Javadoc
1   package fr.ifremer.quadrige3.synchro.server.rest;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 Synchro server
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   *
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   *
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
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      /* Logger */
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       * Transform an exception on bad update_dt, into a HTTP error 500 + a body response with the exact error code.
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       * Transform an exception on locked data, into a HTTP error 500 + a body response with the exact error code.
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       * Transform an exception on delete data, into a HTTP error 500 + a body response with the exact error code.
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          // Add objects in specific header in json format
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       * Transform an exception on save data, into a HTTP error 500 + a body response with the exact error code.
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         // Add objects in specific header in json format
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 }