/
JsonMemoryHttpService.java
103 lines (91 loc) · 3.73 KB
/
JsonMemoryHttpService.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
package jmri.server.json.memory;
import static jmri.server.json.JSON.DATA;
import static jmri.server.json.JSON.VALUE;
import static jmri.server.json.memory.JsonMemory.MEMORIES;
import static jmri.server.json.memory.JsonMemory.MEMORY;
import static jmri.server.json.idtag.JsonIdTag.IDTAG;
import static jmri.server.json.reporter.JsonReporter.REPORTER;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.Locale;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jmri.InstanceManager;
import jmri.Memory;
import jmri.MemoryManager;
import jmri.ProvidingManager;
import jmri.Reportable;
import jmri.server.json.JsonException;
import jmri.server.json.JsonNamedBeanHttpService;
import jmri.server.json.idtag.JsonIdTagHttpService;
import jmri.server.json.reporter.JsonReporterHttpService;
/**
*
* @author Randall Wood
*/
public class JsonMemoryHttpService extends JsonNamedBeanHttpService<Memory> {
private static final Logger log = LoggerFactory.getLogger(JsonMemoryHttpService.class);
private JsonIdTagHttpService idTagService = new JsonIdTagHttpService(mapper);
private JsonReporterHttpService reporterService = new JsonReporterHttpService(mapper);
public JsonMemoryHttpService(ObjectMapper mapper) {
super(mapper);
}
@Override
public ObjectNode doGet(Memory memory, String name, String type, Locale locale, int id) throws JsonException {
ObjectNode root = this.getNamedBean(memory, name, type, locale, id);
ObjectNode data = root.with(DATA);
if (memory != null) {
Object val = memory.getValue();
if (val == null) {
data.putNull(VALUE);
} else {
//set memory value based on type
if (val instanceof jmri.IdTag){
ObjectNode idTagValue = idTagService.doGet((jmri.IdTag)val, name, IDTAG, locale, id);
data.set(VALUE, idTagValue);
} else if (val instanceof Reportable) {
ObjectNode reporterValue = reporterService.doGet((jmri.Reporter)val, name, REPORTER, locale, id);
data.set(VALUE, reporterValue);
} else {
data.put(VALUE, val.toString()); //send string for types not explicitly handled
}
}
}
return root;
}
@Override
public ObjectNode doPost(Memory memory, String name, String type, JsonNode data, Locale locale, int id) throws JsonException {
if (!data.path(VALUE).isMissingNode()) {
if (data.path(VALUE).isNull()) {
memory.setValue(null);
} else {
memory.setValue(data.path(VALUE).asText());
}
}
return this.doGet(memory, name, type, locale, id);
}
@Override
public JsonNode doSchema(String type, boolean server, Locale locale, int id) throws JsonException {
switch (type) {
case MEMORY:
case MEMORIES:
return doSchema(type,
server,
"jmri/server/json/memory/memory-server.json",
"jmri/server/json/memory/memory-client.json",
id);
default:
throw new JsonException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, Bundle.getMessage(locale, JsonException.ERROR_UNKNOWN_TYPE, type), id);
}
}
@Override
protected String getType() {
return MEMORY;
}
@Override
protected ProvidingManager<Memory> getManager() {
return InstanceManager.getDefault(MemoryManager.class);
}
}