e8622905bd6c8e7ca466b7662ad7ef7dd052658c
[sdc/sdc-workflow-designer.git] /
1 /*
2  * Copyright © 2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.sdc.workflow.api.exceptionshandlers;
18
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;
23
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.onap.sdc.workflow.services.exceptions.VersionValidationException;
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.RestController;
41 import org.springframework.web.context.request.WebRequest;
42 import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
43
44 @ControllerAdvice
45 @RestController
46 public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
47
48     @ExceptionHandler(UniqueValueViolationException.class)
49     public final ResponseEntity<String> handleUniqueValueViolationException(
50             UniqueValueViolationException exception) {
51         return new ResponseEntity<>(exception.getMessage(), UNPROCESSABLE_ENTITY);
52     }
53
54     @ExceptionHandler(EntityNotFoundException.class)
55     public final ResponseEntity<String> handleWorkflowNotFoundException(
56             Exception exception) {
57         return new ResponseEntity<>(exception.getMessage(), NOT_FOUND);
58     }
59
60     @ExceptionHandler({InvalidPaginationParameterException.class})
61     public final ResponseEntity<String> handlePaginationException(InvalidPaginationParameterException exception) {
62         return new ResponseEntity<>(exception.getMessage(), BAD_REQUEST);
63     }
64
65     //For workflowVersionValidator exception
66     @Override
67     protected final ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgumentNotValidException e,
68             final HttpHeaders headers,
69             final HttpStatus status,
70             final WebRequest request) {
71
72         FieldError result = e.getBindingResult().getFieldError();
73        return new ResponseEntity<>(result.getDefaultMessage(), BAD_REQUEST);
74     }
75
76     //For missing header exceptions
77     @Override
78     public ResponseEntity<Object> handleServletRequestBindingException(ServletRequestBindingException ex,
79                                                                        HttpHeaders headers, HttpStatus status,
80                                                                        WebRequest request) {
81         return new ResponseEntity<>(ex.getMessage(), BAD_REQUEST);
82     }
83
84
85     @ExceptionHandler({InvalidArtifactException.class, VersionModificationException.class,
86             VersionStateModificationException.class, VersionValidationException.class})
87     public final ResponseEntity<String> handleInvalidArtifactException(
88             Exception exception) {
89         return new ResponseEntity<>(exception.getMessage(), UNPROCESSABLE_ENTITY);
90     }
91
92
93     @ExceptionHandler(VersionCreationException.class)
94     public final ResponseEntity<String> handleVersioningErrorException(
95             VersionCreationException exception) {
96         return new ResponseEntity<>(exception.getMessage(), FORBIDDEN);
97     }
98 }