ca6111df56590321f25a74e7ad43dbfe9a674c42
[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.springframework.http.HttpHeaders;
32 import org.springframework.http.HttpStatus;
33 import org.springframework.http.ResponseEntity;
34 import org.springframework.validation.FieldError;
35 import org.springframework.web.bind.MethodArgumentNotValidException;
36 import org.springframework.web.bind.ServletRequestBindingException;
37 import org.springframework.web.bind.annotation.ControllerAdvice;
38 import org.springframework.web.bind.annotation.ExceptionHandler;
39 import org.springframework.web.bind.annotation.RestController;
40 import org.springframework.web.context.request.WebRequest;
41 import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
42
43 @ControllerAdvice
44 @RestController
45 public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
46
47     @ExceptionHandler(UniqueValueViolationException.class)
48     public final ResponseEntity<String> handleUniqueValueViolationException(
49             UniqueValueViolationException exception) {
50         return new ResponseEntity<>(exception.getMessage(), UNPROCESSABLE_ENTITY);
51     }
52
53     @ExceptionHandler(EntityNotFoundException.class)
54     public final ResponseEntity<String> handleWorkflowNotFoundException(
55             Exception exception) {
56         return new ResponseEntity<>(exception.getMessage(), NOT_FOUND);
57     }
58
59     @ExceptionHandler({InvalidPaginationParameterException.class})
60     public final ResponseEntity<String> handlePaginationException(InvalidPaginationParameterException exception) {
61         return new ResponseEntity<>(exception.getMessage(), BAD_REQUEST);
62     }
63
64     //For workflowVersionValidator exception
65     @Override
66     protected final ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgumentNotValidException e,
67             final HttpHeaders headers,
68             final HttpStatus status,
69             final WebRequest request) {
70
71         FieldError result = e.getBindingResult().getFieldError();
72        return new ResponseEntity<>(result.getDefaultMessage(), BAD_REQUEST);
73     }
74
75     //For missing header exceptions
76     @Override
77     public ResponseEntity<Object> handleServletRequestBindingException(ServletRequestBindingException ex,
78                                                                        HttpHeaders headers, HttpStatus status,
79                                                                        WebRequest request) {
80         return new ResponseEntity<>(ex.getMessage(), BAD_REQUEST);
81     }
82
83
84     @ExceptionHandler({InvalidArtifactException.class, VersionModificationException.class,
85             VersionStateModificationException.class})
86     public final ResponseEntity<String> handleInvalidArtifactException(
87             Exception exception) {
88         return new ResponseEntity<>(exception.getMessage(), UNPROCESSABLE_ENTITY);
89     }
90
91
92     @ExceptionHandler(VersionCreationException.class)
93     public final ResponseEntity<String> handleVersioningErrorException(
94             VersionCreationException exception) {
95         return new ResponseEntity<>(exception.getMessage(), FORBIDDEN);
96     }
97 }