2 * Copyright © 2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.sdc.workflow.api.exceptionshandlers;
19 import static org.springframework.http.HttpStatus.BAD_REQUEST;
20 import static org.springframework.http.HttpStatus.FORBIDDEN;
21 import static org.springframework.http.HttpStatus.NOT_FOUND;
22 import static org.springframework.http.HttpStatus.UNPROCESSABLE_ENTITY;
24 import org.onap.sdc.workflow.services.exceptions.EntityNotFoundException;
25 import org.onap.sdc.workflow.services.exceptions.InvalidArtifactException;
26 import org.onap.sdc.workflow.services.exceptions.InvalidPaginationParameterException;
27 import org.onap.sdc.workflow.services.exceptions.UniqueValueViolationException;
28 import org.onap.sdc.workflow.services.exceptions.VersionCreationException;
29 import org.onap.sdc.workflow.services.exceptions.VersionModificationException;
30 import org.onap.sdc.workflow.services.exceptions.VersionStateModificationException;
31 import org.springframework.context.support.DefaultMessageSourceResolvable;
32 import org.springframework.http.HttpHeaders;
33 import org.springframework.http.HttpStatus;
34 import org.springframework.http.ResponseEntity;
35 import org.springframework.validation.FieldError;
36 import org.springframework.web.bind.MethodArgumentNotValidException;
37 import org.springframework.web.bind.ServletRequestBindingException;
38 import org.springframework.web.bind.annotation.ControllerAdvice;
39 import org.springframework.web.bind.annotation.ExceptionHandler;
40 import org.springframework.web.bind.annotation.ResponseStatus;
41 import org.springframework.web.bind.annotation.RestController;
42 import org.springframework.web.context.request.WebRequest;
43 import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
47 public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
49 @ExceptionHandler(UniqueValueViolationException.class)
50 public final ResponseEntity<String> handleUniqueValueViolationException(
51 UniqueValueViolationException exception) {
52 return new ResponseEntity<>(exception.getMessage(), UNPROCESSABLE_ENTITY);
55 @ExceptionHandler(EntityNotFoundException.class)
56 public final ResponseEntity<String> handleWorkflowNotFoundException(
57 Exception exception) {
58 return new ResponseEntity<>(exception.getMessage(), NOT_FOUND);
61 @ExceptionHandler({InvalidPaginationParameterException.class})
62 public final ResponseEntity<String> handlePaginationException(InvalidPaginationParameterException exception) {
63 return new ResponseEntity<>(exception.getMessage(), BAD_REQUEST);
67 protected final ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgumentNotValidException exception,
68 final HttpHeaders headers,
69 final HttpStatus status,
70 final WebRequest request) {
72 String errorMsg = exception.getBindingResult().getFieldErrors().stream()
73 .map(DefaultMessageSourceResolvable::getDefaultMessage)
75 .orElse(exception.getMessage());
77 return new ResponseEntity<>(errorMsg, BAD_REQUEST);
80 //For missing header exceptions
82 public ResponseEntity<Object> handleServletRequestBindingException(ServletRequestBindingException ex,
83 HttpHeaders headers, HttpStatus status,
85 return new ResponseEntity<>(ex.getMessage(), BAD_REQUEST);
89 @ExceptionHandler({InvalidArtifactException.class, VersionModificationException.class,
90 VersionStateModificationException.class})
91 public final ResponseEntity<String> handleInvalidArtifactException(
92 Exception exception) {
93 return new ResponseEntity<>(exception.getMessage(), UNPROCESSABLE_ENTITY);
97 @ExceptionHandler(VersionCreationException.class)
98 public final ResponseEntity<String> handleVersioningErrorException(
99 VersionCreationException exception) {
100 return new ResponseEntity<>(exception.getMessage(), FORBIDDEN);