Port to java 17
[ccsdk/apps.git] / services / src / main / java / org / onap / ccsdk / apps / services / RestExceptionHandler.java
1 package org.onap.ccsdk.apps.services;
2
3 import org.jvnet.hk2.annotations.Service;
4 import org.springframework.beans.ConversionNotSupportedException;
5 import org.springframework.beans.TypeMismatchException;
6 import org.springframework.core.Ordered;
7 import org.springframework.core.annotation.Order;
8 import org.springframework.http.HttpHeaders;
9 import org.springframework.http.HttpStatus;
10 import org.springframework.http.HttpStatusCode;
11 import org.springframework.http.ResponseEntity;
12 import org.springframework.http.converter.HttpMessageNotReadableException;
13 import org.springframework.http.converter.HttpMessageNotWritableException;
14 import org.springframework.validation.BindException;
15 import org.springframework.web.HttpMediaTypeNotAcceptableException;
16 import org.springframework.web.HttpMediaTypeNotSupportedException;
17 import org.springframework.web.HttpRequestMethodNotSupportedException;
18 import org.springframework.web.bind.MethodArgumentNotValidException;
19 import org.springframework.web.bind.MissingPathVariableException;
20 import org.springframework.web.bind.MissingServletRequestParameterException;
21 import org.springframework.web.bind.ServletRequestBindingException;
22 import org.springframework.web.bind.annotation.ControllerAdvice;
23 import org.springframework.web.bind.annotation.ExceptionHandler;
24 import org.springframework.web.bind.annotation.RestController;
25 import org.springframework.web.bind.annotation.RestControllerAdvice;
26 import org.springframework.web.context.request.WebRequest;
27 import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
28 import org.springframework.web.multipart.support.MissingServletRequestPartException;
29 import org.springframework.web.servlet.NoHandlerFoundException;
30 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
31 import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
32
33 import jakarta.servlet.http.HttpServletRequest;
34
35 @Order(Ordered.HIGHEST_PRECEDENCE)
36 @RestControllerAdvice
37 public class RestExceptionHandler extends ResponseEntityExceptionHandler {
38
39
40     @Override
41     protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
42       return createResponseEntity(new RestProtocolError("bad-method", "Method not supported", ex), status);
43     }
44
45     @Override
46     protected ResponseEntity<Object> handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
47         return createResponseEntity(new RestProtocolError("bad-media-type", "Media type not supported", ex), status);
48     }
49
50     @Override
51     protected ResponseEntity<Object> handleHttpMediaTypeNotAcceptable(HttpMediaTypeNotAcceptableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
52         return createResponseEntity(new RestProtocolError("bad-media-type", "Media type not acceptable", ex), status);
53     }
54
55     @Override
56     protected ResponseEntity<Object> handleMissingPathVariable(MissingPathVariableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
57         return createResponseEntity(new RestApplicationError("missing-path", "Missing path variable", ex), status);
58     }
59
60     @Override
61     protected ResponseEntity<Object> handleMissingServletRequestParameter(MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
62         return createResponseEntity(new RestProtocolError("missing-param", "Missing servlet request parameter", ex), status);
63     }
64
65     @Override
66     protected ResponseEntity<Object> handleServletRequestBindingException(ServletRequestBindingException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
67         return createResponseEntity(new RestApplicationError("request-binding", "Servlet binding exception", ex), status);
68     }
69
70     @Override
71     protected ResponseEntity<Object> handleConversionNotSupported(ConversionNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
72         return createResponseEntity(new RestApplicationError("conversion", "Conversion not supported", ex), status);
73     }
74
75     @Override
76     protected ResponseEntity<Object> handleTypeMismatch(TypeMismatchException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
77         return createResponseEntity(new RestProtocolError("type-mismatch", "Type mismatch", ex), status);
78     }
79
80     @Override
81     protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
82         return createResponseEntity(new RestProtocolError("bad-message", "HTTP message not readable", ex), status);
83     }
84
85     @Override
86     protected ResponseEntity<Object> handleHttpMessageNotWritable(HttpMessageNotWritableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
87         return createResponseEntity(new RestProtocolError("bad-message", "HTTP message not writable", ex), status);
88     }
89
90     @Override
91     protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
92         return createResponseEntity(new RestProtocolError("bad-message", "Method argument not valid", ex), status);
93     }
94
95     @Override
96     protected ResponseEntity<Object> handleMissingServletRequestPart(MissingServletRequestPartException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
97         return createResponseEntity(new RestProtocolError("bad-message", "Missing servlet request part", ex), status);
98     }
99
100     @Override
101     protected ResponseEntity<Object> handleBindException(BindException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
102         return createResponseEntity(new RestApplicationError("binding-error", "Missing servlet request part", ex), status);
103     }
104
105     @Override
106     protected ResponseEntity<Object> handleNoHandlerFoundException(NoHandlerFoundException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
107         return createResponseEntity(new RestApplicationError("binding-error", "No handler found", ex), status);
108     }
109
110     @Override
111     protected ResponseEntity<Object> handleAsyncRequestTimeoutException(AsyncRequestTimeoutException ex, HttpHeaders headers, HttpStatusCode status, WebRequest webRequest) {
112         return createResponseEntity(new RestApplicationError("timeout", "Async request timeout", ex), status);
113     }
114
115     @Override
116     protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
117         return createResponseEntity(new RestApplicationError("internal-error", "Internal error", ex), status);
118     }
119
120     @ExceptionHandler({RestException.class})
121     protected ResponseEntity<Object> handleRestException(RestException ex, HttpServletRequest request) {
122         RestError error = ex.getRestError();
123         if (request != null) {
124             error.setErrorPath(request.getServletPath());
125         }
126         return createResponseEntity(error,HttpStatusCode.valueOf(ex.getStatus()));
127     }
128
129     private ResponseEntity<Object> createResponseEntity(RestError restError, HttpStatusCode status) {
130
131         return new ResponseEntity<>(new RestErrors(restError), status);
132     }
133 }