View Javadoc
1   package net.sumaris.server.http.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 General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (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 General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import com.google.common.base.Joiner;
28  import net.sumaris.core.exception.BadUpdateDateException;
29  import net.sumaris.core.exception.DataLockedException;
30  import net.sumaris.core.exception.DenyDeletionException;
31  import net.sumaris.server.exception.ErrorCodes;
32  import net.sumaris.server.exception.ErrorHelper;
33  import net.sumaris.server.exception.InvalidEmailConfirmationException;
34  import org.apache.commons.collections.CollectionUtils;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  import org.springframework.http.HttpHeaders;
38  import org.springframework.http.HttpStatus;
39  import org.springframework.http.MediaType;
40  import org.springframework.http.ResponseEntity;
41  import org.springframework.web.bind.annotation.ControllerAdvice;
42  import org.springframework.web.bind.annotation.ExceptionHandler;
43  import org.springframework.web.context.request.WebRequest;
44  import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
45  
46  import java.util.List;
47  
48  @ControllerAdvice
49  public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
50  
51      /* Logger */
52      private static final Logger log = LoggerFactory.getLogger(RestResponseEntityExceptionHandler.class);
53  
54      public RestResponseEntityExceptionHandler() {
55          super();
56      }
57  
58      /**
59       * Transform an exception on bad update_dt, into a HTTP error 500 + a body response with the exact error code.
60       */
61      @ExceptionHandler(value = {InvalidEmailConfirmationException.class })
62      protected ResponseEntity<Object> handleAnyException(RuntimeException ex, WebRequest request) {
63          String message = ErrorHelper.toJsonErrorString(ErrorCodes.INVALID_EMAIL_CONFIRMATION, ex.getMessage());
64          if (log.isDebugEnabled()) {
65              log.debug(message);
66          }
67          HttpHeaders headers = new HttpHeaders();
68          headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
69          return handleExceptionInternal(ex, message, headers, HttpStatus.INTERNAL_SERVER_ERROR, request);
70      }
71  
72      /**
73       * Transform an exception on bad update_dt, into a HTTP error 500 + a body response with the exact error code.
74       */
75      @ExceptionHandler(value = {BadUpdateDateException.class })
76      protected ResponseEntity<Object> handleBadUpdateDt(RuntimeException ex, WebRequest request) {
77          String message = ErrorHelper.toJsonErrorString(ErrorCodes.BAD_UPDATE_DATE, ex.getMessage());
78          if (log.isDebugEnabled()) {
79              log.debug(message);
80          }
81          return handleExceptionInternal(ex, message, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request);
82      }
83  
84      /**
85       * Transform an exception on locked data, into a HTTP error 500 + a body response with the exact error code.
86       */
87      @ExceptionHandler(value = {DataLockedException.class })
88      protected ResponseEntity<Object> handleLock(RuntimeException ex, WebRequest request) {
89          String message = ErrorHelper.toJsonErrorString(ErrorCodes.DATA_LOCKED, ex.getMessage());
90          if (log.isDebugEnabled()) {
91              log.debug(message);
92          }
93          return handleExceptionInternal(ex, message, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request);
94      }
95  
96      /**
97       * Transform an exception on delete data, into a HTTP error 500 + a body response with the exact error code.
98       */
99      @ExceptionHandler(value = {DenyDeletionException.class })
100     protected ResponseEntity<Object> handleDeleteForbidden(RuntimeException ex, WebRequest request) {
101         String message = ErrorHelper.toJsonErrorString(ErrorCodes.DENY_DELETION, ex.getMessage());
102         if (log.isDebugEnabled()) {
103             log.debug(message);
104         }
105         HttpHeaders httpHeaders = new HttpHeaders();
106 
107         // Add entities in a specific header
108         List<String> entitiesIds = ((DenyDeletionException)ex).getObjectIds();
109         if (CollectionUtils.isNotEmpty(entitiesIds)) {
110             httpHeaders.add(
111                     net.sumaris.server.http.HttpHeaders.ACCESS_CONTROL_DENY_DELETION_ENTITIES,
112                     Joiner.on(", ").join(entitiesIds));
113         }
114         return handleExceptionInternal(ex, message, httpHeaders, HttpStatus.INTERNAL_SERVER_ERROR, request);
115     }
116 
117 
118 }