Merge "Logging improvements"
[vid.git] / vid-app-common / src / main / java / org / onap / vid / controllers / VidRestrictedBaseController.java
1 package org.onap.vid.controllers;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.apache.commons.lang.exception.ExceptionUtils;
5 import org.onap.vid.model.ExceptionResponse;
6 import org.onap.portalsdk.core.controller.RestrictedBaseController;
7 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
8 import org.springframework.http.HttpStatus;
9 import org.springframework.http.ResponseEntity;
10 import org.springframework.web.bind.annotation.ExceptionHandler;
11 import org.springframework.web.bind.annotation.ResponseBody;
12 import org.springframework.web.bind.annotation.ResponseStatus;
13 import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
14
15 import static org.onap.vid.utils.Logging.getMethodCallerName;
16 import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
17
18 public abstract class VidRestrictedBaseController extends RestrictedBaseController {
19
20     protected final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(this.getClass().getName());
21
22     @ExceptionHandler(MethodArgumentTypeMismatchException.class)
23     @ResponseBody
24     public ResponseEntity handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
25         LOGGER.error(EELFLoggerDelegate.errorLogger, "{}: {}", getMethodCallerName(), ExceptionUtils.getMessage(e), e);
26         Class<?> type = e.getRequiredType();
27         String message;
28         if (type.isEnum()) {
29             message = "The parameter " + e.getName() + " must have a value among : " + StringUtils.join(type.getEnumConstants(), ", ");
30         }
31         else {
32             message = "The parameter " + e.getName() + " must be of type " + type.getTypeName();
33         }
34         ResponseEntity  response = new ResponseEntity<String>(message, HttpStatus.BAD_REQUEST);
35         return response;
36     }
37
38     @ExceptionHandler(Exception.class)
39     @ResponseStatus(value=INTERNAL_SERVER_ERROR)
40     public ExceptionResponse exceptionHandler(Exception e) {
41         return ControllersUtils.handleException(e, LOGGER);
42     }
43 }