VfModule and VolumeGroup RequestParameters: introduce objects hierarchy
[vid.git] / vid-app-common / src / main / java / org / onap / vid / controller / VidRestrictedBaseController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.vid.controller;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.commons.lang.exception.ExceptionUtils;
25 import org.onap.portalsdk.core.controller.RestrictedBaseController;
26 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
27 import org.onap.vid.exceptions.AccessDeniedException;
28 import org.onap.vid.exceptions.NotFoundException;
29 import org.onap.vid.exceptions.OperationNotAllowedException;
30 import org.onap.vid.model.ExceptionResponse;
31 import org.springframework.http.HttpStatus;
32 import org.springframework.http.ResponseEntity;
33 import org.springframework.web.bind.annotation.ExceptionHandler;
34 import org.springframework.web.bind.annotation.ResponseBody;
35 import org.springframework.web.bind.annotation.ResponseStatus;
36 import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
37
38 import static org.onap.vid.utils.Logging.getMethodCallerName;
39 import static org.springframework.http.HttpStatus.FORBIDDEN;
40 import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
41 import static org.springframework.http.HttpStatus.METHOD_NOT_ALLOWED;
42 import static org.springframework.http.HttpStatus.NOT_FOUND;
43
44 public abstract class VidRestrictedBaseController extends RestrictedBaseController {
45
46     protected final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(this.getClass().getName());
47
48     @ExceptionHandler(MethodArgumentTypeMismatchException.class)
49     @ResponseBody
50     public ResponseEntity handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
51         LOGGER.error(EELFLoggerDelegate.errorLogger, "{}: {}", getMethodCallerName(), ExceptionUtils.getMessage(e), e);
52         Class<?> type = e.getRequiredType();
53         String message;
54         if (type.isEnum()) {
55             message = "The parameter " + e.getName() + " must have a value among : " + StringUtils.join(type.getEnumConstants(), ", ");
56         }
57         else {
58             message = "The parameter " + e.getName() + " must be of type " + type.getTypeName();
59         }
60         return new ResponseEntity<String>(message, HttpStatus.BAD_REQUEST);
61     }
62
63     @ExceptionHandler(Exception.class)
64     @ResponseStatus(value=INTERNAL_SERVER_ERROR)
65     public ExceptionResponse exceptionHandler(Exception e) {
66         return ControllersUtils.handleException(e, LOGGER);
67     }
68
69     @ExceptionHandler(NotFoundException.class)
70     @ResponseStatus(value=NOT_FOUND)
71     public ExceptionResponse notFoundExceptionHandler(Exception e) {
72         return ControllersUtils.handleException(e, LOGGER);
73     }
74
75     @ExceptionHandler(AccessDeniedException.class)
76     @ResponseStatus(value=FORBIDDEN)
77     public ExceptionResponse accessDeniedExceptionHandler(Exception e) {
78         return ControllersUtils.handleException(e, LOGGER);
79     }
80
81     @ExceptionHandler(OperationNotAllowedException.class)
82     @ResponseStatus(value=METHOD_NOT_ALLOWED)
83     public ExceptionResponse illegalStateExceptionHandler(Exception e) {
84         return ControllersUtils.handleException(e, LOGGER);
85     }
86 }