Inventory TreeView Fixed
[ccsdk/features.git] / sdnr / wt / oauth-provider / provider-jar / src / main / java / org / onap / ccsdk / features / sdnr / wt / oauthprovider / http / client / MappedBaseHttpResponse.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.oauthprovider.http.client;
23
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.JsonMappingException;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPResponse;
28 import org.onap.ccsdk.features.sdnr.wt.oauthprovider.data.CustomObjectMapper;
29
30 public class MappedBaseHttpResponse<T> {
31
32
33     public static final int CODE404 = 404;
34     public static final int CODE200 = 200;
35     public static final MappedBaseHttpResponse<String> UNKNOWN = new MappedBaseHttpResponse<>(-1, null);
36     private static final ObjectMapper mapper = new CustomObjectMapper();
37     public final int code;
38     public final T body;
39
40     public MappedBaseHttpResponse(int code, String body, Class<T> clazz)
41             throws JsonMappingException, JsonProcessingException {
42         this(code, body != null ? mapper.readValue(body, clazz) : null);
43     }
44
45     private MappedBaseHttpResponse(int code, T body) {
46         this.code = code;
47         this.body = body;
48     }
49
50     public MappedBaseHttpResponse(BaseHTTPResponse response, Class<T> clazz)
51             throws JsonMappingException, JsonProcessingException {
52         this(response.code, response.body, clazz);
53     }
54
55     @Override
56     public String toString() {
57         return "BaseHTTPResponse [code=" + code + ", body=" + body + "]";
58     }
59
60     public boolean isSuccess() {
61         return this.code == CODE200;
62     }
63 }