Aharoni, Pavel (pa0916) | ca3cb01 | 2018-10-22 15:29:57 +0300 | [diff] [blame] | 1 | /*-
|
| 2 | * ============LICENSE_START=======================================================
|
| 3 | * OSAM
|
| 4 | * ================================================================================
|
| 5 | * Copyright (C) 2018 AT&T
|
| 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 |
|
| 22 |
|
| 23 | package org.onap.osam.controllers;
|
| 24 |
|
| 25 | import org.apache.commons.lang.StringUtils;
|
| 26 | import org.apache.commons.lang.exception.ExceptionUtils;
|
| 27 | import org.onap.osam.model.ExceptionResponse;
|
| 28 | import org.onap.portalsdk.core.controller.RestrictedBaseController;
|
| 29 | import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
|
| 30 | import org.springframework.http.HttpStatus;
|
| 31 | import org.springframework.http.ResponseEntity;
|
| 32 | import org.springframework.web.bind.annotation.ExceptionHandler;
|
| 33 | import org.springframework.web.bind.annotation.ResponseBody;
|
| 34 | import org.springframework.web.bind.annotation.ResponseStatus;
|
| 35 | import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
| 36 |
|
| 37 | import static org.onap.osam.utils.Logging.getMethodCallerName;
|
| 38 | import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
|
| 39 |
|
| 40 | public abstract class OsamCoreRestrictedBaseController extends RestrictedBaseController {
|
| 41 |
|
| 42 | protected final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(this.getClass().getName());
|
| 43 |
|
| 44 | @ExceptionHandler(MethodArgumentTypeMismatchException.class)
|
| 45 | @ResponseBody
|
| 46 | public ResponseEntity handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
|
| 47 | LOGGER.error(EELFLoggerDelegate.errorLogger, "{}: {}", getMethodCallerName(), ExceptionUtils.getMessage(e), e);
|
| 48 | Class<?> type = e.getRequiredType();
|
| 49 | String message;
|
| 50 | if (type.isEnum()) {
|
| 51 | message = "The parameter " + e.getName() + " must have a value among : " + StringUtils.join(type.getEnumConstants(), ", ");
|
| 52 | }
|
| 53 | else {
|
| 54 | message = "The parameter " + e.getName() + " must be of type " + type.getTypeName();
|
| 55 | }
|
| 56 | ResponseEntity response = new ResponseEntity<String>(message, HttpStatus.BAD_REQUEST);
|
| 57 | return response;
|
| 58 | }
|
| 59 |
|
| 60 | @ExceptionHandler(Exception.class)
|
| 61 | @ResponseStatus(value=INTERNAL_SERVER_ERROR)
|
| 62 | public ExceptionResponse exceptionHandler(Exception e) {
|
| 63 | return ControllersUtils.handleException(e, LOGGER);
|
| 64 | }
|
| 65 | }
|