2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.datarouter.util;
23 import com.fasterxml.jackson.databind.JsonNode;
25 import org.apache.camel.Exchange;
26 import org.apache.camel.component.cxf.common.message.CxfConstants;
27 import org.apache.cxf.message.Message;
28 import org.json.JSONObject;
29 import org.onap.aai.cl.mdc.MdcContext;
30 import org.onap.aai.restclient.client.Headers;
32 import java.util.Arrays;
33 import java.util.Collection;
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.List;
38 import java.util.UUID;
40 import javax.servlet.ServletRequest;
42 public class RouterServiceUtil {
44 public static void setMdcContext(Exchange exchange){
45 String txnID = exchange.getIn().getHeader(Headers.TRANSACTION_ID,
46 Arrays.asList(UUID.randomUUID())).toString();
47 String remote = exchange.getIn().getHeader(Headers.FROM_APP_ID, "").toString();
48 String remoteAddress = "";
49 Message cxfMessage = exchange.getIn().getHeader(CxfConstants.CAMEL_CXF_MESSAGE, Message.class);
50 if (cxfMessage != null) {
51 ServletRequest request = (ServletRequest) cxfMessage.get("HTTP.REQUEST");
53 remoteAddress = request.getRemoteAddr();
56 MdcContext.initialize(txnID, "Synapse", "", remote, remoteAddress);
59 public static Map<String, String> parseJsonPayloadIntoMap(String jsonPayload) {
61 JSONObject jsonObject = new JSONObject(jsonPayload);
62 Map<String, String> map = new HashMap<>();
63 Iterator iter = jsonObject.keys();
64 while (iter.hasNext()) {
65 String key = (String) iter.next();
66 String value = jsonObject.getString(key);
72 public static String getNodeFieldAsText(JsonNode node, String fieldName) {
74 String fieldValue = null;
76 JsonNode valueNode = node.get(fieldName);
78 if (valueNode != null) {
79 fieldValue = valueNode.asText();
85 public static String concatArray(List<String> list) {
86 return concatArray(list, " ");
89 public static String concatArray(List<String> list, String delimiter) {
91 if (list == null || !list.isEmpty()) {
95 StringBuilder result = new StringBuilder(64);
97 boolean firstValue = true;
99 for (String item : list) {
105 result.append(delimiter).append(item);
109 return result.toString();
113 public static String concatArray(String[] values) {
115 if (values == null || values.length == 0) {
119 StringBuilder result = new StringBuilder(64);
121 boolean firstValue = true;
123 for (String item : values) {
129 result.append(".").append(item);
134 return result.toString();
138 public static String recursivelyLookupJsonPayload(JsonNode node, String key) {
141 if (node.isArray()) {
142 Iterator<JsonNode> arrayItemsIterator = node.elements();
143 while (arrayItemsIterator.hasNext()) {
144 value = recursivelyLookupJsonPayload(arrayItemsIterator.next(), key);
153 if (!node.isObject()) {
157 Iterator<Map.Entry<String, JsonNode>> nodeIterator = node.fields();
159 while (nodeIterator.hasNext()) {
160 Map.Entry<String, JsonNode> entry = nodeIterator.next();
161 if (!entry.getValue().isValueNode()) {
162 value = recursivelyLookupJsonPayload(entry.getValue(), key);
168 String name = entry.getKey();
169 if (name.equalsIgnoreCase(key)) {
170 return entry.getValue().asText();
177 public static void extractObjectsByKey(JsonNode node, String searchKey,
178 Collection<JsonNode> foundObjects) {
180 if (node.isArray()) {
181 Iterator<JsonNode> arrayItemsIterator = node.elements();
182 while (arrayItemsIterator.hasNext()) {
183 extractObjectsByKey(arrayItemsIterator.next(), searchKey, foundObjects);
189 if (!node.isObject()) {
193 Iterator<Map.Entry<String, JsonNode>> nodeIterator = node.fields();
195 while (nodeIterator.hasNext()) {
196 Map.Entry<String, JsonNode> entry = nodeIterator.next();
197 if (!entry.getValue().isValueNode()) {
198 extractObjectsByKey(entry.getValue(), searchKey, foundObjects);
201 String name = entry.getKey();
202 if (!name.equalsIgnoreCase(searchKey)) {
206 JsonNode entryValue = entry.getValue();
208 if (entryValue.isArray()) {
210 Iterator<JsonNode> arrayItemsIterator = entryValue.elements();
211 while (arrayItemsIterator.hasNext()) {
212 foundObjects.add(arrayItemsIterator.next());
216 foundObjects.add(entry.getValue());
221 public static void convertArrayIntoList(JsonNode node, Collection<JsonNode> instances) {
223 if (node.isArray()) {
224 Iterator<JsonNode> arrayItemsIterator = node.elements();
225 while (arrayItemsIterator.hasNext()) {
226 instances.add(arrayItemsIterator.next());
233 public static void extractFieldValuesFromObject(JsonNode node,
234 Collection<String> attributesToExtract, Collection<String> fieldValues) {
236 if (node==null || !node.isObject()) {
242 for (String attrToExtract : attributesToExtract) {
244 valueNode = node.get(attrToExtract);
246 if (valueNode != null && valueNode.isValueNode()) {
247 fieldValues.add(valueNode.asText());
252 public static String objToJson(Object obj) {
253 JSONObject jsonObject = new JSONObject(obj);
254 return jsonObject.toString();
258 * Helper utility to concatenate substrings of a URI together to form a proper URI.
260 * @param suburis the list of substrings to concatenate together
261 * @return the concatenated list of substrings
263 public static String concatSubUri(String... suburis) {
264 String finalUri = "";
266 for (String suburi : suburis) {
268 if (suburi != null) {
269 // Remove any leading / since we only want to append /
270 suburi = suburi.replaceFirst("^/*", "");
272 // Add a trailing / if one isn't already there
273 finalUri += suburi.endsWith("/") ? suburi : suburi + "/";