View Javadoc
1   package fr.ifremer.quadrige2.synchro.server.rest;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 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.quadrige2.core.dao.technical.gson.Gsons;
27  import fr.ifremer.quadrige2.core.dao.technical.http.HttpHelper;
28  import fr.ifremer.quadrige2.core.exception.BadUpdateDtException;
29  import fr.ifremer.quadrige2.core.exception.DataLockedException;
30  import fr.ifremer.quadrige2.core.exception.DeleteForbiddenException;
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.springframework.http.HttpHeaders;
34  import org.springframework.http.HttpStatus;
35  import org.springframework.http.ResponseEntity;
36  import org.springframework.web.bind.annotation.ControllerAdvice;
37  import org.springframework.web.bind.annotation.ExceptionHandler;
38  import org.springframework.web.context.request.WebRequest;
39  import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
40  
41  @ControllerAdvice
42  public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
43      /* Logger */
44      private static final Log log = LogFactory.getLog(RestResponseEntityExceptionHandler.class);
45  
46      public RestResponseEntityExceptionHandler() {
47          super();
48      }
49  
50      /**
51       * Transform an exception on bad update_dt, into a HTTP error 500 + a body response with the exact error code.
52       */
53      @ExceptionHandler(value = { BadUpdateDtException.class })
54      protected ResponseEntity<Object> handleBadUpdateDt(RuntimeException ex, WebRequest request) {
55          String message = HttpHelper.getInternalServerErrorMessage(fr.ifremer.quadrige2.core.dao.technical.http.HttpStatus.SC_BAD_UPDATE_DT, ex.getMessage());
56          if (log.isDebugEnabled()) {
57              log.debug(message);
58          }
59          return handleExceptionInternal(ex, message, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request);
60      }
61  
62      /**
63       * Transform an exception on locked data, into a HTTP error 500 + a body response with the exact error code.
64       */
65      @ExceptionHandler(value = {DataLockedException.class })
66      protected ResponseEntity<Object> handleLock(RuntimeException ex, WebRequest request) {
67          String message = HttpHelper.getInternalServerErrorMessage(fr.ifremer.quadrige2.core.dao.technical.http.HttpStatus.SC_DATA_LOCKED, ex.getMessage());
68          if (log.isDebugEnabled()) {
69              log.debug(message);
70          }
71          return handleExceptionInternal(ex, message, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request);
72      }
73  
74      /**
75       * Transform an exception on delete data, into a HTTP error 500 + a body response with the exact error code.
76       */
77      @ExceptionHandler(value = {DeleteForbiddenException.class })
78      protected ResponseEntity<Object> handleDeleteForbidden(RuntimeException ex, WebRequest request) {
79          String message = HttpHelper.getInternalServerErrorMessage(fr.ifremer.quadrige2.core.dao.technical.http.HttpStatus.SC_DELETE_FORBIDDEN, ex.getMessage());
80          if (log.isDebugEnabled()) {
81              log.debug(message);
82          }
83          HttpHeaders httpHeaders = new HttpHeaders();
84          // Add objects in specific header in json format
85          httpHeaders.add(
86                  fr.ifremer.quadrige2.core.dao.technical.http.HttpHeaders.HH_DELETE_FORBIDDEN_OBJECT_IDS,
87                  Gsons.newBuilder().create().toJson(((DeleteForbiddenException)ex).getObjectIds()));
88          return handleExceptionInternal(ex, message, httpHeaders, HttpStatus.INTERNAL_SERVER_ERROR, request);
89      }
90  
91  }