Merge "ICS tests with istio and JWTs"
[nonrtric.git] / dmaap-adaptor-java / src / main / java / org / oran / dmaapadapter / controllers / ErrorResponse.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2021 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.dmaapadapter.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 org.oran.dmaapadapter.exceptions.ServiceException;
30 import org.springframework.http.HttpHeaders;
31 import org.springframework.http.HttpStatus;
32 import org.springframework.http.MediaType;
33 import org.springframework.http.ResponseEntity;
34 import reactor.core.publisher.Mono;
35
36 public class ErrorResponse {
37     private static Gson gson = new GsonBuilder() //
38             .create(); //
39
40     // Returned as body for all failed REST calls
41     @Schema(name = "error_information", description = "Problem as defined in https://tools.ietf.org/html/rfc7807")
42     public static class ErrorInfo {
43         @SerializedName("type")
44         private String type = "about:blank";
45
46         @SerializedName("title")
47         private String title = null;
48
49         @SerializedName("status")
50         private final Integer status;
51
52         @SerializedName("detail")
53         private String detail = null;
54
55         @SerializedName("instance")
56         private String instance = null;
57
58         public ErrorInfo(String detail, Integer status) {
59             this.detail = detail;
60             this.status = status;
61         }
62
63         @Schema(example = "503",
64                 description = "The HTTP status code generated by the origin server for this occurrence of the problem. ")
65         public Integer getStatus() {
66             return status;
67         }
68
69         @Schema(example = "Policy type not found",
70                 description = " A human-readable explanation specific to this occurrence of the problem.")
71         public String getDetail() {
72             return this.detail;
73         }
74
75     }
76
77     @Schema(name = "message", description = "message")
78     public final String message;
79
80     ErrorResponse(String message) {
81         this.message = message;
82     }
83
84     static Mono<ResponseEntity<Object>> createMono(String text, HttpStatus code) {
85         return Mono.just(create(text, code));
86     }
87
88     static Mono<ResponseEntity<Object>> createMono(Exception e, HttpStatus code) {
89         return createMono(e.toString(), code);
90     }
91
92     public static ResponseEntity<Object> create(String text, HttpStatus code) {
93         ErrorInfo p = new ErrorInfo(text, code.value());
94         String json = gson.toJson(p);
95         HttpHeaders headers = new HttpHeaders();
96         headers.setContentType(MediaType.APPLICATION_PROBLEM_JSON);
97         return new ResponseEntity<>(json, headers, code);
98     }
99
100     public static ResponseEntity<Object> create(Throwable e, HttpStatus code) {
101         if (e instanceof RuntimeException) {
102             code = HttpStatus.INTERNAL_SERVER_ERROR;
103         } else if (e instanceof ServiceException) {
104             ServiceException se = (ServiceException) e;
105             if (se.getHttpStatus() != null) {
106                 code = se.getHttpStatus();
107             }
108         }
109         return create(e.toString(), code);
110     }
111
112 }