2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.openecomp.datarouter.util;
27 import com.fasterxml.jackson.databind.JsonNode;
29 import org.apache.camel.Exchange;
30 import org.apache.camel.component.cxf.common.message.CxfConstants;
31 import org.apache.cxf.message.Message;
32 import org.json.JSONObject;
33 import org.openecomp.cl.mdc.MdcContext;
34 import org.openecomp.restclient.client.Headers;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.Collection;
39 import java.util.HashMap;
40 import java.util.Iterator;
41 import java.util.List;
43 import java.util.UUID;
45 import javax.servlet.ServletRequest;
47 public class RouterServiceUtil {
49 public static void setMdcContext(Exchange exchange){
50 String txnID = exchange.getIn().getHeader(Headers.TRANSACTION_ID,
51 Arrays.asList(UUID.randomUUID())).toString();
52 String remote = exchange.getIn().getHeader(Headers.FROM_APP_ID, "").toString();
53 String remoteAddress = "";
54 Message cxfMessage = exchange.getIn().getHeader(CxfConstants.CAMEL_CXF_MESSAGE, Message.class);
55 if (cxfMessage != null) {
56 ServletRequest request = (ServletRequest) cxfMessage.get("HTTP.REQUEST");
58 remoteAddress = request.getRemoteAddr();
61 MdcContext.initialize(txnID, "Synapse", "", remote, remoteAddress);
64 public static Map<String, String> parseJsonPayloadIntoMap(String jsonPayload) {
66 JSONObject jsonObject = new JSONObject(jsonPayload);
67 Map<String, String> map = new HashMap<String, String>();
68 Iterator iter = jsonObject.keys();
69 while (iter.hasNext()) {
70 String key = (String) iter.next();
71 String value = jsonObject.getString(key);
77 public static String getNodeFieldAsText(JsonNode node, String fieldName) {
79 String fieldValue = null;
81 JsonNode valueNode = node.get(fieldName);
83 if (valueNode != null) {
84 fieldValue = valueNode.asText();
90 public static String concatArray(List<String> list) {
91 return concatArray(list, " ");
94 public static String concatArray(List<String> list, String delimiter) {
96 if (list == null || list.size() == 0) {
100 StringBuilder result = new StringBuilder(64);
102 boolean firstValue = true;
104 for (String item : list) {
110 result.append(delimiter).append(item);
114 return result.toString();
118 public static String concatArray(String[] values) {
120 if (values == null || values.length == 0) {
124 StringBuilder result = new StringBuilder(64);
126 boolean firstValue = true;
128 for (String item : values) {
134 result.append(".").append(item);
139 return result.toString();
143 public static String recursivelyLookupJsonPayload(JsonNode node, String key) {
145 if (node.isObject()) {
146 Iterator<Map.Entry<String, JsonNode>> nodeIterator = node.fields();
148 while (nodeIterator.hasNext()) {
149 Map.Entry<String, JsonNode> entry = (Map.Entry<String, JsonNode>) nodeIterator.next();
150 if (!entry.getValue().isValueNode()) {
151 value = recursivelyLookupJsonPayload(entry.getValue(), key);
157 String name = entry.getKey();
158 if (name.equalsIgnoreCase(key)) {
159 return entry.getValue().asText();
162 } else if (node.isArray()) {
163 Iterator<JsonNode> arrayItemsIterator = node.elements();
164 while (arrayItemsIterator.hasNext()) {
165 value = recursivelyLookupJsonPayload(arrayItemsIterator.next(), key);
174 public static void extractObjectsByKey(JsonNode node, String searchKey,
175 Collection<JsonNode> foundObjects) {
177 if (node.isObject()) {
178 Iterator<Map.Entry<String, JsonNode>> nodeIterator = node.fields();
180 while (nodeIterator.hasNext()) {
181 Map.Entry<String, JsonNode> entry = (Map.Entry<String, JsonNode>) nodeIterator.next();
182 if (!entry.getValue().isValueNode()) {
183 extractObjectsByKey(entry.getValue(), searchKey, foundObjects);
186 String name = entry.getKey();
187 if (name.equalsIgnoreCase(searchKey)) {
189 JsonNode entryValue = entry.getValue();
191 if (entryValue.isArray()) {
193 Iterator<JsonNode> arrayItemsIterator = entryValue.elements();
194 while (arrayItemsIterator.hasNext()) {
195 foundObjects.add(arrayItemsIterator.next());
199 foundObjects.add(entry.getValue());
203 } else if (node.isArray()) {
204 Iterator<JsonNode> arrayItemsIterator = node.elements();
205 while (arrayItemsIterator.hasNext()) {
206 extractObjectsByKey(arrayItemsIterator.next(), searchKey, foundObjects);
211 public static void convertArrayIntoList(JsonNode node, Collection<JsonNode> instances) {
213 if (node.isArray()) {
214 Iterator<JsonNode> arrayItemsIterator = node.elements();
215 while (arrayItemsIterator.hasNext()) {
216 instances.add(arrayItemsIterator.next());
223 public static void extractFieldValuesFromObject(JsonNode node,
224 Collection<String> attributesToExtract, Collection<String> fieldValues) {
226 if (node.isObject()) {
228 JsonNode valueNode = null;
230 for (String attrToExtract : attributesToExtract) {
232 valueNode = node.get(attrToExtract);
234 if (valueNode != null) {
236 if (valueNode.isValueNode()) {
237 fieldValues.add(valueNode.asText());
245 public static String objToJson(Object obj) {
246 JSONObject jsonObject = new JSONObject(obj);
247 String json = jsonObject.toString();
252 * Helper utility to concatenate substrings of a URI together to form a proper URI.
254 * @param suburis the list of substrings to concatenate together
255 * @return the concatenated list of substrings
257 public static String concatSubUri(String... suburis) {
258 String finalUri = "";
260 for (String suburi : suburis) {
262 if (suburi != null) {
263 // Remove any leading / since we only want to append /
264 suburi = suburi.replaceFirst("^/*", "");
266 // Add a trailing / if one isn't already there
267 finalUri += suburi.endsWith("/") ? suburi : suburi + "/";