Fix for Penetration test _ Session and cookie management
[vid.git] / vid-app-common / src / main / java / org / onap / vid / mso / MsoUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2018 - 2019 Nokia. 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.vid.mso;
23
24 import static org.apache.commons.lang3.StringUtils.firstNonBlank;
25 import static org.apache.commons.lang3.StringUtils.isEmpty;
26 import static org.onap.vid.utils.KotlinUtilsKt.JACKSON_OBJECT_MAPPER;
27
28 import com.fasterxml.jackson.core.JsonProcessingException;
29 import com.fasterxml.jackson.databind.JsonNode;
30 import io.joshworks.restclient.http.HttpResponse;
31 import java.io.IOException;
32 import org.apache.commons.lang3.exception.ExceptionUtils;
33
34 public class MsoUtil {
35
36
37     private MsoUtil() {
38     }
39
40     public static MsoResponseWrapper wrapResponse(RestObject<String> restObject) {
41         String response = restObject.get() != null ? restObject.get() : restObject.getRaw();
42         int status = restObject.getStatusCode();
43         return new MsoResponseWrapper(status, response);
44     }
45
46     public static <T> MsoResponseWrapper wrapResponse(HttpResponse<T> httpResponse)  {
47         MsoResponseWrapper msoResponseWrapper = new MsoResponseWrapper();
48         msoResponseWrapper.setStatus(httpResponse.getStatus());
49         if (httpResponse.getRawBody() != null) {
50             try {
51                 T body = httpResponse.getBody();
52                 String entityStr = (body instanceof String || body==null) ? (String) body :
53                     JACKSON_OBJECT_MAPPER.writeValueAsString(httpResponse.getBody());
54                 msoResponseWrapper.setEntity(entityStr);
55             } catch(JsonProcessingException e) {
56                 ExceptionUtils.rethrow(e);
57             }
58         }
59         return msoResponseWrapper;
60     }
61
62     public static <T> MsoResponseWrapper2<T> wrapResponse2(HttpResponse<String> httpResponse, Class<T> clazz) {
63         String raw = httpResponse.getBody(); // As body's T is String, use getBody as a simple way to get raw
64         if (raw != null) {
65             try {
66                 T entity = JACKSON_OBJECT_MAPPER.readValue(raw, clazz);
67                 return new MsoResponseWrapper2<>(httpResponse.getStatus(), entity, raw);
68             } catch (JsonProcessingException exception) {
69                 return new MsoResponseWrapper2<>(httpResponse.getStatus(), null, raw);
70             } catch (IOException exception) {
71                 ExceptionUtils.rethrow(exception);
72             }
73         }
74
75         return new MsoResponseWrapper2<>(httpResponse.getStatus(), null, null);
76     }
77
78     public static String formatExceptionAdditionalInfo(int statusCode, String msoResponse) {
79         final String errorMsg = "Http Code:" + statusCode;
80
81         if (isEmpty(msoResponse)) {
82             return errorMsg;
83         }
84
85         try {
86             JsonNode jsonNode = JACKSON_OBJECT_MAPPER.readTree(msoResponse);
87
88             return errorMsg + ", " + firstNonBlank(
89                 removeBraces(jsonNode.get("serviceException")),
90                 removeBraces(jsonNode.path("requestError").get("serviceException")),
91                 msoResponse
92             );
93
94         } catch (Exception e) {
95             return errorMsg + ", " + msoResponse;
96         }
97     }
98
99     private static String removeBraces(JsonNode jsonNode) {
100         if (jsonNode == null || jsonNode.isMissingNode()) {
101             return null;
102         }
103
104         return jsonNode.toString().replaceAll("[\\{\\}]", "");
105     }
106 }