Aharoni, Pavel (pa0916) | ca3cb01 | 2018-10-22 15:29:57 +0300 | [diff] [blame] | 1 | /*-
|
| 2 | * ============LICENSE_START=======================================================
|
| 3 | * OSAM
|
| 4 | * ================================================================================
|
| 5 | * Copyright (C) 2018 AT&T
|
| 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 |
|
| 22 | package org.onap.osam.mso;
|
| 23 |
|
| 24 | import com.fasterxml.jackson.annotation.JsonIgnore;
|
| 25 | import com.fasterxml.jackson.annotation.JsonProperty;
|
| 26 | import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
| 27 | import com.fasterxml.jackson.core.JsonProcessingException;
|
| 28 | import com.fasterxml.jackson.databind.ObjectMapper;
|
| 29 | import io.joshworks.restclient.http.HttpResponse;
|
| 30 |
|
| 31 | @JsonPropertyOrder({
|
| 32 | "status",
|
| 33 | "entity"
|
| 34 | })
|
| 35 |
|
| 36 | public class MsoResponseWrapper2<T> implements MsoResponseWrapperInterface {
|
| 37 |
|
| 38 | final static ObjectMapper objectMapper = new ObjectMapper();
|
| 39 |
|
| 40 | private final int status;
|
| 41 | private final T entity;
|
| 42 | private final String raw;
|
| 43 |
|
| 44 | public MsoResponseWrapper2(RestObject<T> msoResponse) {
|
| 45 | this.status = msoResponse.getStatusCode();
|
| 46 | this.entity = msoResponse.get();
|
| 47 | this.raw = msoResponse.getRaw();
|
| 48 | }
|
| 49 |
|
| 50 | public MsoResponseWrapper2(HttpResponse<T> msoResponse) {
|
| 51 | this.status = msoResponse.getStatus();
|
| 52 | this.entity = msoResponse.getBody();
|
| 53 | this.raw = msoResponse.getBody().toString();
|
| 54 | }
|
| 55 |
|
| 56 | public MsoResponseWrapper2(
|
| 57 | @JsonProperty(value = "status", required = true) int status,
|
| 58 | @JsonProperty(value = "entity", required = true) T entity) {
|
| 59 | this.status = status;
|
| 60 | this.entity = entity;
|
| 61 | this.raw = null;
|
| 62 | }
|
| 63 |
|
| 64 | public int getStatus() {
|
| 65 | return status;
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | @JsonIgnore
|
| 70 | public String getResponse() {
|
| 71 | try {
|
| 72 | return objectMapper.writeValueAsString(this);
|
| 73 | } catch (JsonProcessingException e) {
|
| 74 | return getEntity() != null ? getEntity().toString() : null;
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | @JsonProperty
|
| 79 | public Object getEntity() {
|
| 80 | return entity != null ? entity : raw;
|
| 81 | }
|
| 82 |
|
| 83 | }
|