/
JsonServlet.java
563 lines (535 loc) · 25.9 KB
/
JsonServlet.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
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
package jmri.web.servlet.json;
import static jmri.server.json.JSON.DATA;
import static jmri.server.json.JSON.ID;
import static jmri.server.json.JSON.NAME;
import static jmri.server.json.JSON.STATE;
import static jmri.server.json.JSON.V5;
import static jmri.server.json.JSON.VALUE;
import static jmri.server.json.JSON.VERSIONS;
import static jmri.server.json.JsonException.CODE;
import static jmri.server.json.operations.JsonOperations.LOCATION;
import static jmri.server.json.power.JsonPowerServiceFactory.POWER;
import static jmri.web.servlet.ServletUtil.APPLICATION_JSON;
import static jmri.web.servlet.ServletUtil.UTF8;
import static jmri.web.servlet.ServletUtil.UTF8_APPLICATION_JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.ServiceLoader;
import javax.annotation.Nonnull;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jmri.InstanceManager;
import jmri.server.json.JsonServerPreferences;
import jmri.server.json.JsonException;
import jmri.server.json.JsonHttpService;
import jmri.server.json.JsonRequest;
import jmri.server.json.JsonWebSocket;
import jmri.server.json.schema.JsonSchemaServiceCache;
import jmri.spi.JsonServiceFactory;
import jmri.util.FileUtil;
import jmri.web.servlet.ServletUtil;
import org.eclipse.jetty.websocket.servlet.WebSocketServlet;
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
import org.openide.util.lookup.ServiceProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Provide JSON formatted responses to requests for information from the JMRI
* Web Server.
* <p>
* See {@link jmri.server.json} for details on how this Servlet handles JSON
* requests.
*
* @author Randall Wood Copyright (C) 2012, 2013, 2016, 2019
*/
@WebServlet(name = "JsonServlet",
urlPatterns = {"/json"})
@ServiceProvider(service = HttpServlet.class)
public class JsonServlet extends WebSocketServlet {
private final transient ObjectMapper mapper = new ObjectMapper();
private final transient HashMap<String, HashMap<String, HashSet<JsonHttpService>>> services = new HashMap<>();
private final transient JsonServerPreferences preferences = InstanceManager.getDefault(JsonServerPreferences.class);
private static final Logger log = LoggerFactory.getLogger(JsonServlet.class);
@Override
public void init() throws ServletException {
superInit();
ServiceLoader.load(JsonServiceFactory.class).forEach(factory -> VERSIONS.stream().forEach(version -> {
JsonHttpService service = factory.getHttpService(mapper, version);
HashMap<String, HashSet<JsonHttpService>> types = services.computeIfAbsent(version, map -> new HashMap<>());
Arrays.stream(factory.getTypes(version))
.forEach(type -> types.computeIfAbsent(type, set -> new HashSet<>()).add(service));
Arrays.stream(factory.getReceivedTypes(version))
.forEach(type -> types.computeIfAbsent(type, set -> new HashSet<>()).add(service));
}));
}
/**
* Package private method to call
* {@link org.eclipse.jetty.websocket.servlet.WebSocketServlet#init()} so
* this call can be mocked out in unit tests.
*
* @throws ServletException if unable to initialize server
*/
void superInit() throws ServletException {
super.init();
}
@Override
public void configure(WebSocketServletFactory factory) {
factory.register(JsonWebSocket.class);
}
/**
* Handle HTTP get requests for JSON data. Examples:
* <ul>
* <li>/json/v5/sensor/IS22 (return data for sensor with system name
* "IS22")</li>
* <li>/json/v5/sensor (returns a list of all sensors known to JMRI)</li>
* </ul>
* sample responses:
* <ul>
* <li>{"type":"sensor","data":{"name":"IS22","userName":"FarEast","comment":null,"inverted":false,"state":4}}</li>
* <li>[{"type":"sensor","data":{"name":"IS22","userName":"FarEast","comment":null,"inverted":false,"state":4}}]</li>
* </ul>
* Note that data will vary for each type. Note that if an array is returned
* when requesting a single object, the client must resolve the multiple
* objects in the array, since it is possible for plugins to JMRI to provide
* their own response, and JMRI is incapable of judging the correctness of
* the plugin's response.
* <p>
* If the request includes a {@literal result} attribute, the content of the
* response will be solely the contents of that attribute. This is an aid to
* the development and testing of JMRI and clients, but is not considered a
* usable feature in production. This capability may be removed without
* notice if it is deemed too complex to maintain.
*
* @param request an HttpServletRequest object that contains the request
* the client has made of the servlet
* @param response an HttpServletResponse object that contains the response
* the servlet sends to the client
* @throws java.io.IOException if an input or output error is detected when
* the servlet handles the GET request
*/
@Override
protected void doGet(final HttpServletRequest request, HttpServletResponse response) throws IOException {
configureResponse(response);
JsonRequest jsonRequest = createJsonRequest(request);
String[] path = request.getRequestURI().substring(request.getContextPath().length()).split("/"); // NOI18N
String[] rest = path;
if (path.length > 1 && jsonRequest.version.equals(path[1])) {
rest = Arrays.copyOfRange(path, 1, path.length);
}
// echo the contents of result if present and abort further processing
if (request.getAttribute("result") != null) {
JsonNode result = (JsonNode) request.getAttribute("result");
// use HTTP error codes when possible
int code = result.path(DATA).path(CODE).asInt(HttpServletResponse.SC_OK);
sendMessage(response, code, result, jsonRequest);
return;
}
String type = (rest.length > 1) ? URLDecoder.decode(rest[1], UTF8) : null;
if (type != null && !type.isEmpty()) {
response.setContentType(UTF8_APPLICATION_JSON);
InstanceManager.getDefault(ServletUtil.class).setNonCachingHeaders(response);
final String name = (rest.length > 2) ? URLDecoder.decode(rest[2], UTF8) : null;
ObjectNode parameters = mapper.createObjectNode();
for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
String value = URLDecoder.decode(entry.getValue()[0], UTF8);
log.debug("Setting parameter {} to {}", entry.getKey(), value);
try {
parameters
.setAll((ObjectNode) mapper.readTree(String.format("{\"%s\":%s}", entry.getKey(), value)));
} catch (JsonProcessingException ex) {
log.error("Unable to parse JSON {\"{}\":{}}", entry.getKey(), value);
}
}
JsonNode reply = null;
try {
if (name == null) {
if (services.get(jsonRequest.version).get(type) != null) {
ArrayList<JsonNode> lists = new ArrayList<>();
ArrayNode array = mapper.createArrayNode();
JsonException exception = null;
try {
for (JsonHttpService service : services.get(jsonRequest.version).get(type)) {
lists.add(service.doGetList(type, parameters, jsonRequest));
}
} catch (JsonException ex) {
exception = ex;
}
switch (lists.size()) {
case 0:
if (exception != null) {
throw exception;
}
// either empty array or object with empty data
reply = JsonHttpService.message(mapper, array, null, jsonRequest.id);
break;
case 1:
reply = lists.get(0);
break;
default:
for (JsonNode list : lists) {
if (list.isArray()) {
list.forEach(array::add);
} else if (list.path(DATA).isArray()) {
list.path(DATA).forEach(array::add);
}
}
reply = JsonHttpService.message(mapper, array, null, jsonRequest.id);
break;
}
}
if (reply == null) {
log.warn("Requested type '{}' unknown.", type);
throw new JsonException(HttpServletResponse.SC_NOT_FOUND,
JsonBundle.getMessage(request.getLocale(), "ErrorUnknownType", type), jsonRequest.id);
}
} else {
if (services.get(jsonRequest.version).get(type) != null) {
ArrayNode array = mapper.createArrayNode();
JsonException exception = null;
try {
for (JsonHttpService service : services.get(jsonRequest.version).get(type)) {
array.add(service.doGet(type, name, parameters, jsonRequest));
}
} catch (JsonException ex) {
exception = ex;
}
switch (array.size()) {
case 0:
if (exception != null) {
throw exception;
}
reply = array;
break;
case 1:
reply = array.get(0);
break;
default:
reply = array;
break;
}
}
if (reply == null) {
log.warn("Requested type '{}' unknown.", type);
throw new JsonException(HttpServletResponse.SC_NOT_FOUND,
JsonBundle.getMessage(request.getLocale(), "ErrorUnknownType", type), jsonRequest.id);
}
}
} catch (JsonException ex) {
reply = ex.getJsonMessage();
}
// use HTTP error codes when possible
int code = reply.path(DATA).path(CODE).asInt(HttpServletResponse.SC_OK);
sendMessage(response, code, reply, jsonRequest);
} else {
ServletUtil util = InstanceManager.getDefault(ServletUtil.class);
response.setContentType(ServletUtil.UTF8_TEXT_HTML); // NOI18N
response.getWriter().print(String.format(request.getLocale(),
FileUtil.readURL(FileUtil.findURL(Bundle.getMessage(request.getLocale(), "Json.html"))),
util.getTitle(request.getLocale(), Bundle.getMessage(request.getLocale(), "JsonTitle")),
util.getNavBar(request.getLocale(), request.getContextPath()),
util.getRailroadName(false),
util.getFooter(request.getLocale(), request.getContextPath())));
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
configureResponse(response);
InstanceManager.getDefault(ServletUtil.class).setNonCachingHeaders(response);
JsonRequest jsonRequest = createJsonRequest(request);
String[] path = request.getRequestURI().substring(request.getContextPath().length()).split("/"); // NOI18N
String[] rest = path;
if (path.length >= 1 && jsonRequest.version.equals(path[1])) {
rest = Arrays.copyOfRange(path, 1, path.length);
}
String type = (rest.length > 1) ? URLDecoder.decode(rest[1], UTF8) : null;
String name = (rest.length > 2) ? URLDecoder.decode(rest[2], UTF8) : null;
int id = 0;
try {
id = Integer.parseInt(request.getParameter(ID));
} catch (NumberFormatException ex) {
id = 0;
}
JsonNode data;
JsonNode reply = null;
try {
if (request.getContentType().contains(APPLICATION_JSON)) {
data = mapper.readTree(request.getReader());
if (!data.path(DATA).isMissingNode()) {
data = data.path(DATA);
}
} else {
data = mapper.createObjectNode();
if (request.getParameter(STATE) != null) {
((ObjectNode) data).put(STATE, Integer.parseInt(request.getParameter(STATE)));
} else if (request.getParameter(LOCATION) != null) {
((ObjectNode) data).put(LOCATION, request.getParameter(LOCATION));
} else if (request.getParameter(VALUE) != null) {
// values other than Strings should be sent in a JSON object
((ObjectNode) data).put(VALUE, request.getParameter(VALUE));
}
}
if (type != null) {
// for historical reasons, set the name to POWER on a power
// request
if (type.equals(POWER)) {
name = POWER;
} else if (name == null) {
name = data.path(NAME).asText();
}
log.debug("POST operation for {}/{} with {}", type, name, data);
if (name != null) {
if (services.get(jsonRequest.version).get(type) != null) {
log.debug("Using data: {}", data);
ArrayNode array = mapper.createArrayNode();
JsonException exception = null;
try {
for (JsonHttpService service : services.get(jsonRequest.version).get(type)) {
array.add(service.doPost(type, name, data, jsonRequest));
}
} catch (JsonException ex) {
exception = ex;
}
switch (array.size()) {
case 0:
if (exception != null) {
throw exception;
}
reply = array;
break;
case 1:
reply = array.get(0);
break;
default:
reply = array;
break;
}
}
if (reply == null) {
log.warn("Requested type '{}' unknown.", type);
throw new JsonException(HttpServletResponse.SC_NOT_FOUND,
JsonBundle.getMessage(request.getLocale(), "ErrorUnknownType", type), id);
}
} else {
log.error("Name must be defined.");
throw new JsonException(HttpServletResponse.SC_BAD_REQUEST,
JsonBundle.getMessage(request.getLocale(), "ErrorMissingName"), id);
}
} else {
log.warn("Type not specified.");
// TODO: I18N
throw new JsonException(HttpServletResponse.SC_BAD_REQUEST, "Type must be specified.", id);
}
} catch (JsonException ex) {
reply = ex.getJsonMessage();
}
// use HTTP error codes when possible
int code = reply.path(DATA).path(CODE).asInt(HttpServletResponse.SC_OK);
sendMessage(response, code, reply, jsonRequest);
}
@Override
protected void doPut(HttpServletRequest request, HttpServletResponse response) throws IOException {
configureResponse(response);
InstanceManager.getDefault(ServletUtil.class).setNonCachingHeaders(response);
JsonRequest jsonRequest = createJsonRequest(request);
String[] path = request.getRequestURI().substring(request.getContextPath().length()).split("/"); // NOI18N
String[] rest = path;
if (path.length >= 1 && jsonRequest.version.equals(path[1])) {
rest = Arrays.copyOfRange(path, 1, path.length);
}
String type = (rest.length > 1) ? URLDecoder.decode(rest[1], UTF8) : null;
String name = (rest.length > 2) ? URLDecoder.decode(rest[2], UTF8) : null;
JsonNode data;
JsonNode reply = null;
try {
if (request.getContentType().contains(APPLICATION_JSON)) {
data = mapper.readTree(request.getReader());
if (!data.path(DATA).isMissingNode()) {
data = data.path(DATA);
}
} else {
throw new JsonException(HttpServletResponse.SC_BAD_REQUEST, "PUT request must be a JSON object",
jsonRequest.id); // need to I18N
}
if (type != null) {
// for historical reasons, set the name to POWER on a power
// request
if (type.equals(POWER)) {
name = POWER;
} else if (name == null) {
name = data.path(NAME).asText();
}
if (name != null) {
if (services.get(jsonRequest.version).get(type) != null) {
ArrayNode array = mapper.createArrayNode();
JsonException exception = null;
try {
for (JsonHttpService service : services.get(jsonRequest.version).get(type)) {
array.add(service.doPut(type, name, data, jsonRequest));
}
} catch (JsonException ex) {
exception = ex;
}
switch (array.size()) {
case 0:
if (exception != null) {
throw exception;
}
reply = array;
break;
case 1:
reply = array.get(0);
break;
default:
reply = array;
break;
}
}
if (reply == null) {
// item cannot be created
// TODO: I18N
throw new JsonException(HttpServletResponse.SC_BAD_REQUEST, type + " is not a creatable type",
jsonRequest.id);
}
} else {
log.warn("Requested type '{}' unknown.", type);
throw new JsonException(HttpServletResponse.SC_NOT_FOUND,
JsonBundle.getMessage(request.getLocale(), "ErrorUnknownType", type), jsonRequest.id);
}
} else {
log.warn("Type not specified.");
// TODO: I18N
throw new JsonException(HttpServletResponse.SC_BAD_REQUEST, "Type must be specified.", jsonRequest.id);
}
} catch (JsonException ex) {
reply = ex.getJsonMessage();
}
// use HTTP error codes when possible
int code = reply.path(DATA).path(CODE).asInt(HttpServletResponse.SC_OK);
sendMessage(response, code, reply, jsonRequest);
}
@Override
protected void doDelete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
configureResponse(response);
InstanceManager.getDefault(ServletUtil.class).setNonCachingHeaders(response);
JsonRequest jsonRequest = createJsonRequest(request);
String[] path = request.getRequestURI().substring(request.getContextPath().length()).split("/"); // NOI18N
String[] rest = path;
if (path.length >= 1 && jsonRequest.version.equals(path[1])) {
rest = Arrays.copyOfRange(path, 1, path.length);
}
String type = (rest.length > 1) ? URLDecoder.decode(rest[1], UTF8) : null;
String name = (rest.length > 2) ? URLDecoder.decode(rest[2], UTF8) : null;
JsonNode reply = mapper.createObjectNode();
try {
if (type != null) {
if (name == null) {
throw new JsonException(HttpServletResponse.SC_BAD_REQUEST, "name must be specified",
jsonRequest.id); // need to I18N
}
if (services.get(jsonRequest.version).get(type) != null) {
JsonNode data = mapper.createObjectNode();
if (request.getContentType().contains(APPLICATION_JSON)) {
data = mapper.readTree(request.getReader());
if (!data.path(DATA).isMissingNode()) {
data = data.path(DATA);
}
}
for (JsonHttpService service : services.get(jsonRequest.version).get(type)) {
service.doDelete(type, name, data, jsonRequest);
}
} else {
log.warn("Requested type '{}' unknown.", type);
throw new JsonException(HttpServletResponse.SC_NOT_FOUND,
JsonBundle.getMessage(request.getLocale(), "ErrorUnknownType", type), jsonRequest.id);
}
} else {
log.debug("Type not specified.");
// TODO: I18N
throw new JsonException(HttpServletResponse.SC_BAD_REQUEST, "Type must be specified.", jsonRequest.id);
}
} catch (JsonException ex) {
reply = ex.getJsonMessage();
}
// use HTTP error codes when possible
int code = reply.path(DATA).path(CODE).asInt(HttpServletResponse.SC_OK);
// only include a response body if something went wrong
if (code != HttpServletResponse.SC_OK) {
sendMessage(response, code, reply, jsonRequest);
}
}
/**
* Create a JsonRequest from an HttpServletRequest.
*
* @param request the source
* @return a new JsonRequest
*/
private JsonRequest createJsonRequest(HttpServletRequest request) {
int id = 0;
String version = V5;
String idParameter = request.getParameter(ID);
if (idParameter != null) {
try {
id = Integer.parseInt(idParameter);
} catch (NumberFormatException ex) {
id = 0;
}
}
String[] path = request.getRequestURI().substring(request.getContextPath().length()).split("/"); // NOI18N
if (path.length > 1 && VERSIONS.stream().anyMatch(v -> v.equals(path[1]))) {
version = path[1];
}
return new JsonRequest(request.getLocale(), version, request.getMethod().toLowerCase(), id);
}
/**
* Configure common settings for the response.
*
* @param response the response to configure
*/
private void configureResponse(HttpServletResponse response) {
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType(UTF8_APPLICATION_JSON);
response.setHeader("Connection", "Keep-Alive"); // NOI18N
}
/**
* Send a message to the HTTP client in an HTTP response. This closes the
* response to future messages.
* <p>
* If {@link JsonServerPreferences#getValidateServerMessages()} is
* {@code true}, this may send an error message instead of {@code message}
* if the message is not schema valid.
*
* @param response the HTTP response
* @param code the HTTP response code
* @param message the message to send
* @param request the JSON request
* @throws IOException if unable to send
*/
private void sendMessage(@Nonnull HttpServletResponse response, int code, @Nonnull JsonNode message,
@Nonnull JsonRequest request) throws IOException {
if (preferences.getValidateServerMessages()) {
try {
InstanceManager.getDefault(JsonSchemaServiceCache.class).validateMessage(message, true, request);
} catch (JsonException ex) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
response.getWriter().write(mapper.writeValueAsString(ex.getJsonMessage()));
return;
}
}
response.setStatus(code);
response.getWriter().write(mapper.writeValueAsString(message));
}
}