create the flag FLAG_2006_VFMODULE_TAKES_TENANT_AND_REGION_FROM_VNF
[vid.git] / vid-app-common / src / main / java / org / onap / vid / client / UnirestPatch.kt
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. 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.vid.client
22
23 import io.joshworks.restclient.http.Headers
24 import io.joshworks.restclient.http.HttpResponse
25 import org.apache.commons.io.IOUtils
26 import org.apache.http.HttpVersion
27 import org.apache.http.message.BasicHttpResponse
28 import java.io.InputStream
29 import java.nio.charset.StandardCharsets
30
31 /// Patch NPE in joshworks's Unirest HttpResponse::getBody when getRawBody is null
32 fun <T> patched(httpResponse: HttpResponse<T>) =
33         if (willGetBodyTriggerNPE(httpResponse)) HttpResponsePatch(httpResponse) else httpResponse
34
35 private fun <T> willGetBodyTriggerNPE(httpResponse: HttpResponse<T>) =
36         httpResponse.rawBody == null || httpResponse.rawBody.available() == 0
37
38 private val dummyHttpResponse = BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "ok")
39
40 fun extractRawAsString(response: HttpResponse<*>?): String {
41     try {
42         if (response == null || response.rawBody==null) return ""
43         response.rawBody.reset()
44         return IOUtils.toString(response.rawBody, StandardCharsets.UTF_8.name())
45     } catch (e: Exception) {
46         //Nothing to do here
47     }
48
49     return ""
50 }
51 /**
52  * This class inherits HttpResponse to have compatible interface,
53  * but implementation is done through delegation to another
54  * instance.
55  * For that, it's enough to pass dummy values to HttpResponse's
56  * constructor, as parent HttpResponse methods won't be used,
57  * only overridden.
58  */
59 private class HttpResponsePatch<T>(private val delegatee: HttpResponse<T>) : HttpResponse<T>(
60         dummyHttpResponse, null, null
61 ) {
62     override fun getBody(): T? = if (willGetBodyTriggerNPE(delegatee)) null else delegatee.body
63     override fun getHeaders(): Headers? = delegatee.headers
64     override fun getStatus() = delegatee.status
65     override fun isSuccessful() = delegatee.isSuccessful
66     override fun getStatusText(): String? = delegatee.statusText
67     override fun getRawBody(): InputStream? = delegatee.rawBody
68 }