Creating PM-producer
[nonrtric/plt/ranpm.git] / pmproducer / src / main / java / org / oran / pmproducer / controllers / ErrorResponse.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2023 Nordix Foundation
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.oran.pmproducer.controllers;
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.oran.pmproducer.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             .disableHtmlEscaping() //
43             .create(); //
44
45     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
46
47     // Returned as body for all failed REST calls
48     @Schema(name = "error_information", description = "Problem as defined in https://tools.ietf.org/html/rfc7807")
49     public static class ErrorInfo {
50         @SerializedName("type")
51         private String type = "about:blank";
52
53         @SerializedName("title")
54         private String title = null;
55
56         @SerializedName("status")
57         private final Integer status;
58
59         @SerializedName("detail")
60         private String detail = null;
61
62         @SerializedName("instance")
63         private String instance = null;
64
65         public ErrorInfo(String detail, Integer status) {
66             this.detail = detail;
67             this.status = status;
68         }
69
70         @Schema(example = "503",
71                 description = "The HTTP status code generated by the origin server for this occurrence of the problem. ")
72         public Integer getStatus() {
73             return status;
74         }
75
76         @Schema(example = "Policy type not found",
77                 description = " A human-readable explanation specific to this occurrence of the problem.")
78         public String getDetail() {
79             return this.detail;
80         }
81
82     }
83
84     @Schema(name = "message", description = "message")
85     public final String message;
86
87     ErrorResponse(String message) {
88         this.message = message;
89     }
90
91     static Mono<ResponseEntity<Object>> createMono(String text, HttpStatus code) {
92         return Mono.just(create(text, code));
93     }
94
95     static Mono<ResponseEntity<Object>> createMono(Exception e, HttpStatus code) {
96         return createMono(e.toString(), code);
97     }
98
99     public static ResponseEntity<Object> create(String text, HttpStatus code) {
100         logger.debug("Error response: {}, {}", code, text);
101         ErrorInfo p = new ErrorInfo(text, code.value());
102         String json = gson.toJson(p);
103         HttpHeaders headers = new HttpHeaders();
104         headers.setContentType(MediaType.APPLICATION_PROBLEM_JSON);
105         return new ResponseEntity<>(json, headers, code);
106     }
107
108     public static ResponseEntity<Object> create(Throwable e, HttpStatus code) {
109         if (e instanceof RuntimeException) {
110             code = HttpStatus.INTERNAL_SERVER_ERROR;
111         } else if (e instanceof ServiceException) {
112             ServiceException se = (ServiceException) e;
113             if (se.getHttpStatus() != null) {
114                 code = se.getHttpStatus();
115             }
116         }
117         return create(e.toString(), code);
118     }
119
120 }