aboutsummaryrefslogtreecommitdiffstats
path: root/ncomp-utils-java/src/main/java/org/openecomp/ncomp/webservice/utils/JsonUtils.java
blob: ee899ed27bf612483ec3c7b8da6b12e557d3cb9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*-
 * ============LICENSE_START==========================================
 * OPENECOMP - DCAE
 * ===================================================================
 * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
 * ===================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END============================================
 */
	
package org.openecomp.ncomp.webservice.utils;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Vector;

import org.apache.log4j.Logger;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import org.openecomp.ncomp.utils.PropertyUtil;
import org.openecomp.ncomp.utils.StringUtil;

public class JsonUtils {
	public static final Logger logger = Logger.getLogger(JsonUtils.class);
	HashMap<String, List<String>> features = new HashMap<String, List<String>>();
	public static JsonUtils util = new JsonUtils();
	boolean useProxy = true;

	public JsonUtils() {
	}

	public boolean isUseProxy() {
		return useProxy;
	}

	public void setUseProxy(boolean useProxy) {
		this.useProxy = useProxy;
	}

	public void addFeatures(String pfeature, String features1) {
		List<String> l = new Vector<String>();
		for (String s : features1.split(":", -1)) {
			l.add(s);
		}
		features.put(pfeature, l);
	}

	@SuppressWarnings("unchecked")
	private JSONObject ecore2json(EObject ecore, HashMap<EObject, String> map, String context) throws JSONException {
		if (ecore == null)
			return null;
		JSONObject res = new JSONObject();
		if (useProxy && map.get(ecore) != null) {
			res.put("__proxy", map.get(ecore));
			return res;
		}
		map.put(ecore, context);
		if (context.length() > 0) {
			context = context + ".";
		}
		;
		String cfeature = ecore.eContainingFeature() == null ? "none" : ecore.eContainingFeature().getName();
		res.put("_cfeature", cfeature);
		List<String> show = features.get(cfeature);
		for (EAttribute attr : ecore.eClass().getEAllAttributes()) {
			if (show != null && show.size() > 0 && !show.contains(attr.getName()))
				continue;
			if (attr.getUpperBound() == -1 || attr.getUpperBound() > 1) {
				JSONArray a = new JSONArray();
				List<Object> l = (List<Object>) ecore.eGet(attr);
				for (Object o : l) {
					if (attr.getEType().getInstanceTypeName().equals("java.lang.String")) {
						a.put(o);
					} else if (attr.getEType().getInstanceTypeName().equals("java.util.Date")) {
						a.put(o);
					} else if (attr.getEType().getInstanceTypeName().equals("int")) {
						a.put(o);
					} else if (attr.getEType().getInstanceTypeName().equals("long")) {
						a.put(o);
					} else if (attr.getEType().getInstanceTypeName().equals("boolean")) {
						a.put(o);
					} else if (attr.getEType().getInstanceTypeName().equals("double")) {
						a.put(o);
					} else {
						a.put(o.toString());
					}
				}
				res.put(attr.getName(), a);
			} else {
				Object o = ecore.eGet(attr);
				if (attr.getEType().getInstanceTypeName().equals("java.lang.String")) {
					res.put(attr.getName(), o);
				} else if (attr.getEType().getInstanceTypeName().equals("java.util.Date")) {
					res.put(attr.getName(), o);
				} else if (attr.getEType().getInstanceTypeName().equals("int")) {
					res.put(attr.getName(), ecore.eGet(attr));
				} else if (attr.getEType().getInstanceTypeName().equals("long")) {
					res.put(attr.getName(), ecore.eGet(attr));
				} else if (attr.getEType().getInstanceTypeName().equals("boolean")) {
					res.put(attr.getName(), ecore.eGet(attr));
				} else if (attr.getEType().getInstanceTypeName().equals("double")) {
					res.put(attr.getName(), o);
				} else if (attr.getEType() instanceof org.eclipse.emf.ecore.EEnum) {
					res.put(attr.getName(), o);
				} else {
					res.put(attr.getName(), "TODO: " + attr.getEType().getInstanceTypeName());
				}
			}
		}
		for (EReference ref : ecore.eClass().getEAllReferences()) {
			if (show != null && show.size() > 0 && !show.contains(ref.getName()))
				continue;
			if (ref.getUpperBound() == -1 || ref.getUpperBound() > 1) {
				JSONArray a = new JSONArray();
				EList<EObject> l = (EList<EObject>) ecore.eGet(ref);
				int i = 0;
				for (EObject o : l) {
					JSONObject json = ecore2json(o, map, context + ref.getName() + i);
					a.put(json);
					json.put("__index", i);
					i++;
				}
				res.put(ref.getName(), a);
			} else {
				Object ee = ecore.eGet(ref);
				res.put(ref.getName(), ecore2json((EObject) ee, map, context + ref.getName()));
			}
		}
		return res;
	}

	public JSONObject ecore2json(EObject ecore) {
		try {
			return ecore2json(ecore, new HashMap<EObject, String>(), "");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public String ecore2jsonString(EObject ecore, int indentFactor) {
		try {
			if (ecore == null)
				return null;
			return ecore2json(ecore, new HashMap<EObject, String>(), "").toString(indentFactor);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public JSONArray ecores2json(EList<EObject> l) {
		JSONArray res = new JSONArray();
		for (EObject e : l) {
			res.put(ecore2json(e));
		}
		return res;
	}

	public static JSONObject merge(JSONObject md1, JSONObject md2) {
		JSONObject o = new JSONObject();
		if (md1 == null)
			return md2;
		if (md2 == null)
			return md1;
		Iterator<String> i = md1.keys();
		while (i.hasNext()) {
			String k = i.next();
			o.put(k, md1.get(k));
		}
		i = md2.keys();
		while (i.hasNext()) {
			String k = i.next();
			o.put(k, md2.get(k));
		}
		return o;
	}

	public static String getStringValue(JSONObject json, String name) {
		String[] a = name.split("\\.");
		return getStringValue(json, a, 0);
	}

	private static String getStringValue(JSONObject json, String[] a, int i) {
		if (i >= a.length)
			return null;
		Object o;
		try {
			o = json.get(a[i]);
		} catch (JSONException e) {
			return null;
		}
		if (i == a.length - 1) {
			if (o instanceof String) {
				String s = (String) o;
				return s;
			}
			if (o instanceof Boolean || o instanceof Double || o instanceof Integer || o instanceof Long) {
				return o.toString();
			}
			return null;
		}
		if (o instanceof JSONObject) {
			JSONObject json1 = (JSONObject) o;
			return getStringValue(json1, a, i + 1);
		}
		return null;
	}

	public static Object getValue(JSONObject json, String name) {
		String[] a = name.split("\\.");
		return getValue(json, a, 0);
	}

	private static Object getValue(JSONObject json, String[] a, int i) {
		if (i >= a.length)
			return null;
		Object o;
		try {
			o = json.get(a[i]);
		} catch (JSONException e) {
			return null;
		}
		if (i == a.length - 1) {
			return o;
		}
		if (o instanceof JSONObject) {
			JSONObject json1 = (JSONObject) o;
			return getValue(json1, a, i + 1);
		}
		if (o instanceof JSONArray) {
			JSONArray json1 = (JSONArray) o;
			return getValue(json1, a, i + 1);
		}

		return null;
	}
	
	private static Object getValue(JSONArray json, String[] a, int i) {
		if (i >= a.length)
			return null;
		Object o;
		try {
			o = json.get(Integer.parseInt(a[i]));
		} catch (JSONException e) {
			return null;
		}
		if (i == a.length - 1) {
			return o;
		}
		if (o instanceof JSONObject) {
			JSONObject json1 = (JSONObject) o;
			return getValue(json1, a, i + 1);
		}
		return null;
	}


	public static JSONObject file2json(String file) throws IOException {
		InputStream in = FileUtils.filename2stream(file, null);
		if (in == null)
			throw new RuntimeException("Unable to open: " + file);
		ByteArrayOutputStream buf = new ByteArrayOutputStream();
		try {
			FileUtils.copyStream(in, buf);
		} finally {
			in.close();
			buf.close();
		}
		return new JSONObject(buf.toString());
	}

	public static JSONObject file2json(String file, Properties props, String prefix) throws IOException {
		InputStream in = FileUtils.filename2stream(file, null);
		if (in == null)
			throw new RuntimeException("Unable to open: " + file);
		ByteArrayOutputStream buf = new ByteArrayOutputStream();
		try {
			FileUtils.copyStream(in, buf);
		} finally {
			in.close();
			buf.close();
		}
		String s = buf.toString().replaceAll("##.*", "");
		try {
			s = StringUtil.expandUsingProperties(s, props, prefix);
			return new JSONObject(s);
		} catch (JSONException e) {
			logger.debug("bad JSON String" + s + " " + e);
			throw e;
		}
	}

	public static HashMap<String, String> getStringValueMap(JSONObject v, String string) {
		Object x = getValue(v, string);
		if (!(x instanceof JSONObject)) {
			throw new RuntimeException("Expected Json object value for key: " + string);
		}
		v = (JSONObject) x;
		HashMap<String, String> res = new HashMap<String, String>();
		for (Iterator<String> i = v.keys(); i.hasNext();) {
			String key = (String) i.next();
			Object o = v.get(key);
			if (o instanceof String) {
				String s = (String) o;
				res.put(key, s);
			} else
				throw new RuntimeException("Expected string value for key: " + key);
		}
		return res;
	}

	public static JSONObject stream2json(InputStream in) throws IOException {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		FileUtils.copyStream(in, out);
		if (out.toString().length() == 0) return null;
		String s = out.toString().replaceAll("##.*", "");
		try {
			return new JSONObject(s);
		} catch (JSONException e) {
			logger.debug("bad JSON String" + s + " " + e);
			throw e;
		}
	}

	public static String toStringLazy(Object oo, int indentFactor, int indent) throws JSONException {
		int j;
		if (oo instanceof JSONArray) {
			JSONArray a = (JSONArray) oo;
			return toStringLazy(a, indentFactor, indent);
		}
		if (! (oo instanceof JSONObject)) {
			return JSONObject.valueToString(oo, indentFactor, indent);
		}
		JSONObject json = (JSONObject)oo;
		int n = json.length();
		if (n == 0) {
			return "{}";
		}
		Iterator<String> keys = json.keys();
		StringBuffer sb = new StringBuffer("{");
		int newindent = indent + indentFactor;
		Object o;
		if (n == 1) {
			o = keys.next();
			sb.append(o.toString());
			sb.append(": ");
			sb.append(toStringLazy(json.get((String) o), indentFactor, indent));
		} else {
			while (keys.hasNext()) {
				o = keys.next();
				if (sb.length() > 1) {
					sb.append(",\n");
				} else {
					sb.append("\n");
				}
				for (j = 0; j < newindent; j += 1) {
					sb.append(' ');
				}
				sb.append(o.toString());
				sb.append(": ");
				sb.append(toStringLazy(json.get((String) o), indentFactor, newindent));
			}
			if (sb.length() > 1) {
				sb.append('\n');
				for (j = 0; j < indent; j += 1) {
					sb.append(' ');
				}
			}
		}
		sb.append('}');
		return sb.toString();
	}
    public static String toStringLazy(JSONArray a, int indentFactor, int indent) throws JSONException {
        int len = a.length();
        if (len == 0) {
            return "[]";
        }
        int i;
        StringBuffer sb = new StringBuffer("[");
        int newindent = indent + indentFactor;
        if (len == 1) {
    		Object oo = a.get(0);
			if (oo instanceof JSONArray) {
    			JSONArray aa = (JSONArray) oo;
    			sb.append(toStringLazy(aa, indentFactor, indent));
    		}
			else if (oo instanceof JSONObject) {
				JSONObject ooo = (JSONObject) oo;
				sb.append(toStringLazy(ooo, indentFactor, indent));
    		}
			else sb.append(JSONObject.valueToString(oo,
                    indentFactor, indent));
        } else {
            sb.append('\n');
            for (i = 0; i < len; i += 1) {
                if (i > 0) {
                    sb.append(",\n");
                }
                for (int j = 0; j < newindent; j += 1) {
                    sb.append(' ');
                }
                sb.append(toStringLazy(a.get(i),
                        indentFactor, newindent));
            }
            sb.append('\n');
            for (i = 0; i < indent; i += 1) {
                sb.append(' ');
            }
        }
        sb.append(']');
        return sb.toString();
    }

	public static JSONObject getJsonFromClasspath(String filename) {
		JSONObject res = new JSONObject();
		if (filename == null) return res;
		URL url = PropertyUtil.class.getClassLoader().getResource(filename);
		InputStream inputStream = null;
		try {
			logger.info("Loading JSON" + url);
			inputStream = PropertyUtil.class.getClassLoader().getResourceAsStream(filename);
			if (inputStream == null) {
				throw new FileNotFoundException("JSON file '" + filename + "' not found in the classpath");
			}
			res = stream2json(inputStream);
		} catch (Exception e) {
			logger.warn("Bad JSON in file: " + url + " " + e);
			e.printStackTrace();
		} finally {
			if (inputStream != null)
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
					throw new RuntimeException("unable to close stream",e);
				}
		}
		return res;
	}
}