This repository has been archived by the owner on Nov 27, 2023. It is now read-only.
/
JsonModel.java
409 lines (339 loc) · 11.7 KB
/
JsonModel.java
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
/*******************************************************************************
* Copyright (c) 2022-2023 The University of York.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* Contributors:
* Sina Madani - initial API and implementation
* Antonio Garcia-Dominguez - complete implementation
******************************************************************************/
package org.eclipse.epsilon.emc.json;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Predicate;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.eclipse.epsilon.common.util.FileUtil;
import org.eclipse.epsilon.common.util.StringProperties;
import org.eclipse.epsilon.eol.exceptions.EolRuntimeException;
import org.eclipse.epsilon.eol.exceptions.models.EolEnumerationValueNotFoundException;
import org.eclipse.epsilon.eol.exceptions.models.EolModelElementTypeNotFoundException;
import org.eclipse.epsilon.eol.exceptions.models.EolModelLoadingException;
import org.eclipse.epsilon.eol.exceptions.models.EolNotInstantiableModelElementTypeException;
import org.eclipse.epsilon.eol.models.CachedModel;
import org.eclipse.epsilon.eol.models.IRelativePathResolver;
import org.json.simple.JSONValue;
public class JsonModel extends CachedModel<Object> {
public static final String JSON_ARRAY_TYPE = "JSONArray";
public static final String JSON_OBJECT_TYPE = "JSONObject";
public static final String PROPERTY_FILE = "file";
public static final String PROPERTY_URI = "uri";
public static final String PROPERTY_USERNAME = "username";
public static final String PROPERTY_PASSWORD = "password";
/* We use header0, header1, and so on to represent the values of various HTTP headers. */
public static final String PROPERTY_HEADER_PREFIX = "header";
/* Separator used between the name and the value of the header. */
public static final char PROPERTY_HEADER_SEPARATOR = ':';
protected File file;
protected String uri;
protected String username;
protected String password;
protected Map<String, String> headers = new HashMap<>();
/* ONLY SET THIS FIELD VIA setRoot() */
protected Object _root;
public JsonModel() {
propertyGetter = new JsonPropertyGetter();
propertySetter = new JsonPropertySetter();
}
public Object getRoot() {
return _root;
}
public void setRoot(Object root) {
if (this._root == root) {
// This is already the root: do nothing
return;
}
if (this._root instanceof Contained) {
((Contained) this._root).removeContainer(this);
}
this._root = root;
if (this._root instanceof Contained) {
((Contained) this._root).addContainer(this);
}
}
public void setFile(File file) {
this.file = file;
}
public File getFile() {
return file;
}
public void setUri(String uri) {
this.uri = uri;
}
public String getUri() {
return uri;
}
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
@Override
public Object getEnumerationValue(String enumeration, String label) throws EolEnumerationValueNotFoundException {
throw new UnsupportedOperationException();
}
@Override
public String getTypeNameOf(Object instance) {
if (instance instanceof JsonModelObject) {
return JSON_OBJECT_TYPE;
} else if (instance instanceof JsonModelArray) {
return JSON_ARRAY_TYPE;
} else {
return instance.getClass().getSimpleName();
}
}
@Override
public Object getElementById(String id) {
throw new UnsupportedOperationException();
}
@Override
public String getElementId(Object instance) {
throw new UnsupportedOperationException();
}
@Override
public void setElementId(Object instance, String newId) {
throw new UnsupportedOperationException();
}
@Override
public boolean owns(Object instance) {
return instance instanceof Contained && ((Contained) instance).isContainedBy(this);
}
@Override
public boolean isInstantiable(String type) {
return hasType(type);
}
@Override
public boolean hasType(String type) {
return JSON_OBJECT_TYPE.equals(type) || JSON_ARRAY_TYPE.equals(type);
}
@Override
public boolean store(String location) {
try {
FileUtil.setFileContents(JSONValue.toJSONString(getRoot()), new File(location));
return true;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public boolean store() {
if (file != null) {
return store(file.getAbsolutePath());
} else {
throw new UnsupportedOperationException();
}
}
@Override
protected Collection<Object> allContentsFromModel() {
List<Object> objects = new ArrayList<>();
addAllContents(getRoot(), objects,
(o) -> o instanceof JsonModelObject || o instanceof JsonModelArray);
return objects;
}
private void addAllContents(Object current, List<Object> objects, Predicate<Object> addFilter) {
if (addFilter.test(current)) {
objects.add(current);
}
if (current instanceof JsonModelObject) {
for (Object value : ((JsonModelObject) current).values()) {
addAllContents(value, objects, addFilter);
}
} else if (current instanceof JsonModelArray) {
for (Object child : (JsonModelArray) current) {
addAllContents(child, objects, addFilter);
}
}
}
@Override
protected Collection<Object> getAllOfTypeFromModel(String type) throws EolModelElementTypeNotFoundException {
List<Object> objects = new ArrayList<>();
if (JSON_OBJECT_TYPE.equals(type)) {
addAllContents(getRoot(), objects, o -> o instanceof JsonModelObject);
} else if (JSON_ARRAY_TYPE.equals(type)) {
addAllContents(getRoot(), objects, o -> o instanceof JsonModelArray);
} else {
throw new EolModelElementTypeNotFoundException(getName(), type);
}
return objects;
}
@Override
protected Collection<Object> getAllOfKindFromModel(String kind) throws EolModelElementTypeNotFoundException {
return getAllOfTypeFromModel(kind);
}
@Override
protected Object createInstanceInModel(String type) throws EolModelElementTypeNotFoundException, EolNotInstantiableModelElementTypeException {
if (JSON_OBJECT_TYPE.equals(type)) {
return new JsonModelObject();
} else if (JSON_ARRAY_TYPE.equals(type)) {
return new JsonModelArray();
}
throw new EolModelElementTypeNotFoundException(this.getName(), type);
}
@Override
public void load(StringProperties properties, IRelativePathResolver resolver) throws EolModelLoadingException {
super.load(properties, resolver);
String filePath = properties.getProperty(JsonModel.PROPERTY_FILE);
if (filePath != null && filePath.trim().length() > 0) {
file = new File(resolver.resolve(filePath));
} else {
uri = properties.getProperty(JsonModel.PROPERTY_URI);
username = properties.getProperty(JsonModel.PROPERTY_USERNAME);
password = properties.getProperty(JsonModel.PROPERTY_PASSWORD);
for (Entry<Object, Object> e : properties.entrySet()) {
if (e.getKey().toString().startsWith(PROPERTY_HEADER_PREFIX)) {
String nameValue = e.getValue().toString();
int firstColon = nameValue.indexOf(PROPERTY_HEADER_SEPARATOR);
if (firstColon == -1) {
throw new IllegalArgumentException(String.format(
"Could not find separator '%s' in value of header property %s",
PROPERTY_HEADER_SEPARATOR, e.getKey()));
}
String name = nameValue.substring(0, firstColon).trim();
String value = nameValue.substring(firstColon + 1).trim();
headers.put(name, value);
}
}
}
load();
}
@Override
protected void loadModel() throws EolModelLoadingException {
if (!readOnLoad)
return;
try {
if (uri != null) {
URI parsedUri = new URI(uri);
if ("http".equals(parsedUri.getScheme()) || "https".equals(parsedUri.getScheme())) {
loadModelViaHTTP(parsedUri);
} else {
// Not HTTP, so we'll rely on Java's standard URL handling (covers file:// and jar:// by default)
try (InputStream is = parsedUri.toURL().openStream(); Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8)) {
setRoot(deepClone(JSONValue.parse(reader)));
}
}
} else if (file != null) {
try (Reader reader = new FileReader(file, StandardCharsets.UTF_8)) {
setRoot(deepClone(JSONValue.parse(reader)));
}
} else {
throw new IllegalStateException("Neither URI nor file path have been set");
}
} catch (Exception ex) {
throw new EolModelLoadingException(ex, this);
}
}
private void loadModelViaHTTP(URI parsedUri) throws IOException, ClientProtocolException, EolRuntimeException {
HttpClientBuilder builder = HttpClients.custom();
if (username != null) {
BasicCredentialsProvider creds = new BasicCredentialsProvider();
creds.setCredentials(new AuthScope(parsedUri.getHost(), parsedUri.getPort()),
new UsernamePasswordCredentials(username, password));
builder.setDefaultCredentialsProvider(creds);
}
try (CloseableHttpClient httpClient = builder.build()) {
HttpGet httpGet = new HttpGet(uri);
for (Entry<String, String> e : headers.entrySet()) {
httpGet.setHeader(e.getKey(), e.getValue());
}
HttpResponse httpResponse = httpClient.execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
throw new EolRuntimeException(
String.format("HTTP request to %s returned a non-200 status code: %d",
uri,
httpResponse.getStatusLine().getStatusCode()));
}
HttpEntity responseEntity = httpResponse.getEntity();
Reader reader = new InputStreamReader(responseEntity.getContent(), StandardCharsets.UTF_8);
setRoot(deepClone(JSONValue.parse(reader)));
}
}
@SuppressWarnings("unchecked")
public static Object deepClone(Object parsed) {
if (parsed instanceof Map) {
JsonModelObject obj = new JsonModelObject();
for (Entry<String, Object> e : ((Map<String, Object>) parsed).entrySet()) {
obj.put(e.getKey(), deepClone(e.getValue()));
}
return obj;
} else if (parsed instanceof Iterable) {
JsonModelArray arr = new JsonModelArray();
for (Object e : (Iterable<Object>) parsed) {
arr.add(deepClone(e));
}
return arr;
} else {
return parsed;
}
}
@Override
protected void disposeModel() {
setRoot(null);
}
@Override
protected boolean deleteElementInModel(Object instance) throws EolRuntimeException {
throw new UnsupportedOperationException();
}
@Override
protected Object getCacheKeyForType(String type) throws EolModelElementTypeNotFoundException {
return type;
}
@Override
protected Collection<String> getAllTypeNamesOf(Object instance) {
return Collections.singleton(getTypeNameOf(instance));
}
@Override
public boolean isLoaded() {
return getRoot() != null;
}
public String getHeader(String name) {
return headers.get(name);
}
public void setHeader(String name, String value) {
headers.put(name, value);
}
@Override
public String toString() {
return "JsonModel [name=" + name + "]";
}
}