2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.datarouter.util;
25 import com.fasterxml.jackson.databind.JsonNode;
27 import org.apache.camel.Exchange;
28 import org.apache.camel.component.cxf.common.message.CxfConstants;
29 import org.apache.cxf.message.Message;
30 import org.json.JSONObject;
31 import org.openecomp.cl.mdc.MdcContext;
32 import org.onap.aai.restclient.client.Headers;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.Collection;
37 import java.util.HashMap;
38 import java.util.Iterator;
39 import java.util.List;
41 import java.util.UUID;
43 import javax.servlet.ServletRequest;
45 public class RouterServiceUtil {
47 public static void setMdcContext(Exchange exchange){
48 String txnID = exchange.getIn().getHeader(Headers.TRANSACTION_ID,
49 Arrays.asList(UUID.randomUUID())).toString();
50 String remote = exchange.getIn().getHeader(Headers.FROM_APP_ID, "").toString();
51 String remoteAddress = "";
52 Message cxfMessage = exchange.getIn().getHeader(CxfConstants.CAMEL_CXF_MESSAGE, Message.class);
53 if (cxfMessage != null) {
54 ServletRequest request = (ServletRequest) cxfMessage.get("HTTP.REQUEST");
56 remoteAddress = request.getRemoteAddr();
59 MdcContext.initialize(txnID, "Synapse", "", remote, remoteAddress);
62 public static Map<String, String> parseJsonPayloadIntoMap(String jsonPayload) {
64 JSONObject jsonObject = new JSONObject(jsonPayload);
65 Map<String, String> map = new HashMap<String, String>();
66 Iterator iter = jsonObject.keys();
67 while (iter.hasNext()) {
68 String key = (String) iter.next();
69 String value = jsonObject.getString(key);
75 public static String getNodeFieldAsText(JsonNode node, String fieldName) {
77 String fieldValue = null;
79 JsonNode valueNode = node.get(fieldName);
81 if (valueNode != null) {
82 fieldValue = valueNode.asText();
88 public static String concatArray(List<String> list) {
89 return concatArray(list, " ");
92 public static String concatArray(List<String> list, String delimiter) {
94 if (list == null || list.size() == 0) {
98 StringBuilder result = new StringBuilder(64);
100 boolean firstValue = true;
102 for (String item : list) {
108 result.append(delimiter).append(item);
112 return result.toString();
116 public static String concatArray(String[] values) {
118 if (values == null || values.length == 0) {
122 StringBuilder result = new StringBuilder(64);
124 boolean firstValue = true;
126 for (String item : values) {
132 result.append(".").append(item);
137 return result.toString();
141 public static String recursivelyLookupJsonPayload(JsonNode node, String key) {
143 if (node.isObject()) {
144 Iterator<Map.Entry<String, JsonNode>> nodeIterator = node.fields();
146 while (nodeIterator.hasNext()) {
147 Map.Entry<String, JsonNode> entry = (Map.Entry<String, JsonNode>) nodeIterator.next();
148 if (!entry.getValue().isValueNode()) {
149 value = recursivelyLookupJsonPayload(entry.getValue(), key);
155 String name = entry.getKey();
156 if (name.equalsIgnoreCase(key)) {
157 return entry.getValue().asText();
160 } else if (node.isArray()) {
161 Iterator<JsonNode> arrayItemsIterator = node.elements();
162 while (arrayItemsIterator.hasNext()) {
163 value = recursivelyLookupJsonPayload(arrayItemsIterator.next(), key);
172 public static void extractObjectsByKey(JsonNode node, String searchKey,
173 Collection<JsonNode> foundObjects) {
175 if (node.isObject()) {
176 Iterator<Map.Entry<String, JsonNode>> nodeIterator = node.fields();
178 while (nodeIterator.hasNext()) {
179 Map.Entry<String, JsonNode> entry = (Map.Entry<String, JsonNode>) nodeIterator.next();
180 if (!entry.getValue().isValueNode()) {
181 extractObjectsByKey(entry.getValue(), searchKey, foundObjects);
184 String name = entry.getKey();
185 if (name.equalsIgnoreCase(searchKey)) {
187 JsonNode entryValue = entry.getValue();
189 if (entryValue.isArray()) {
191 Iterator<JsonNode> arrayItemsIterator = entryValue.elements();
192 while (arrayItemsIterator.hasNext()) {
193 foundObjects.add(arrayItemsIterator.next());
197 foundObjects.add(entry.getValue());
201 } else if (node.isArray()) {
202 Iterator<JsonNode> arrayItemsIterator = node.elements();
203 while (arrayItemsIterator.hasNext()) {
204 extractObjectsByKey(arrayItemsIterator.next(), searchKey, foundObjects);
209 public static void convertArrayIntoList(JsonNode node, Collection<JsonNode> instances) {
211 if (node.isArray()) {
212 Iterator<JsonNode> arrayItemsIterator = node.elements();
213 while (arrayItemsIterator.hasNext()) {
214 instances.add(arrayItemsIterator.next());
221 public static void extractFieldValuesFromObject(JsonNode node,
222 Collection<String> attributesToExtract, Collection<String> fieldValues) {
224 if (node.isObject()) {
226 JsonNode valueNode = null;
228 for (String attrToExtract : attributesToExtract) {
230 valueNode = node.get(attrToExtract);
232 if (valueNode != null) {
234 if (valueNode.isValueNode()) {
235 fieldValues.add(valueNode.asText());
243 public static String objToJson(Object obj) {
244 JSONObject jsonObject = new JSONObject(obj);
245 String json = jsonObject.toString();
250 * Helper utility to concatenate substrings of a URI together to form a proper URI.
252 * @param suburis the list of substrings to concatenate together
253 * @return the concatenated list of substrings
255 public static String concatSubUri(String... suburis) {
256 String finalUri = "";
258 for (String suburi : suburis) {
260 if (suburi != null) {
261 // Remove any leading / since we only want to append /
262 suburi = suburi.replaceFirst("^/*", "");
264 // Add a trailing / if one isn't already there
265 finalUri += suburi.endsWith("/") ? suburi : suburi + "/";