82a21ae25e07b48b04f42366c066257637147367
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2020 Nordix Foundation. All rights reserved.
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 package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.annotations.SerializedName;
26
27 import io.swagger.v3.oas.annotations.media.Schema;
28
29 import java.lang.invoke.MethodHandles;
30
31 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.http.HttpHeaders;
35 import org.springframework.http.HttpStatus;
36 import org.springframework.http.MediaType;
37 import org.springframework.http.ResponseEntity;
38 import reactor.core.publisher.Mono;
39
40 public class ErrorResponse {
41     private static Gson gson = new GsonBuilder() //
42             .create(); //
43     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
44
45     // Returned as body for all failed REST calls
46     @Schema(name = "error_information", description = "Problem as defined in https://tools.ietf.org/html/rfc7807")
47     public static class ErrorInfo {
48         @SerializedName("type")
49         private String type = "about:blank";
50
51         @SerializedName("title")
52         private String title = null;
53
54         @SerializedName("status")
55         private final Integer status;
56
57         @SerializedName("detail")
58         private String detail = null;
59
60         @SerializedName("instance")
61         private String instance = null;
62
63         public ErrorInfo(String detail, Integer status) {
64             this.detail = detail;
65             this.status = status;
66         }
67
68         @Schema(example = "404",
69                 description = "The HTTP status code generated by the origin server for this occurrence of the problem. ")
70         public Integer getStatus() {
71             return status;
72         }
73
74         @Schema(example = "Policy type not found",
75                 description = " A human-readable explanation specific to this occurrence of the problem.")
76         public String getDetail() {
77             return this.detail;
78         }
79
80     }
81
82     @Schema(name = "message", description = "message")
83     public final String message;
84
85     ErrorResponse(String message) {
86         this.message = message;
87     }
88
89     static Mono<ResponseEntity<Object>> createMono(String text, HttpStatus code) {
90         return Mono.just(create(text, code));
91     }
92
93     static Mono<ResponseEntity<Object>> createMono(Exception e, HttpStatus code) {
94         return createMono(e.toString(), code);
95     }
96
97     static ResponseEntity<Object> create(String text, HttpStatus code) {
98         logger.debug("Error response: {}, {}", code, text);
99         ErrorInfo p = new ErrorInfo(text, code.value());
100         String json = gson.toJson(p);
101         HttpHeaders headers = new HttpHeaders();
102         headers.setContentType(MediaType.APPLICATION_PROBLEM_JSON);
103         return new ResponseEntity<>(json, headers, code);
104     }
105
106     public static ResponseEntity<Object> create(Throwable e, HttpStatus code) {
107         if (e instanceof RuntimeException) {
108             code = HttpStatus.INTERNAL_SERVER_ERROR;
109         } else if (e instanceof ServiceException) {
110             ServiceException se = (ServiceException) e;
111             if (se.getHttpStatus() != null) {
112                 code = se.getHttpStatus();
113             }
114         }
115         return create(e.toString(), code);
116     }
117
118 }