001package jmri.server.json.memory; 002 003import static jmri.server.json.JSON.DATA; 004import static jmri.server.json.JSON.VALUE; 005import static jmri.server.json.memory.JsonMemory.MEMORIES; 006import static jmri.server.json.memory.JsonMemory.MEMORY; 007import static jmri.server.json.idtag.JsonIdTag.IDTAG; 008import static jmri.server.json.reporter.JsonReporter.REPORTER; 009 010import com.fasterxml.jackson.databind.JsonNode; 011import com.fasterxml.jackson.databind.ObjectMapper; 012import com.fasterxml.jackson.databind.node.ObjectNode; 013import javax.servlet.http.HttpServletResponse; 014 015import jmri.BasicRosterEntry; 016import jmri.InstanceManager; 017import jmri.Memory; 018import jmri.MemoryManager; 019import jmri.ProvidingManager; 020import jmri.server.json.JsonException; 021import jmri.server.json.JsonNamedBeanHttpService; 022import jmri.server.json.JsonRequest; 023import jmri.server.json.idtag.JsonIdTagHttpService; 024import jmri.server.json.reporter.JsonReporterHttpService; 025import jmri.server.json.roster.JsonRosterHttpService; 026 027/** 028 * @author Randall Wood 029 */ 030public class JsonMemoryHttpService extends JsonNamedBeanHttpService<Memory> { 031 032 private JsonIdTagHttpService idTagService = new JsonIdTagHttpService(mapper); 033 private JsonReporterHttpService reporterService = new JsonReporterHttpService(mapper); 034 private JsonRosterHttpService rosterService = new JsonRosterHttpService(mapper); 035 036 public JsonMemoryHttpService(ObjectMapper mapper) { 037 super(mapper); 038 } 039 040 @Override 041 public ObjectNode doGet(Memory memory, String name, String type, JsonRequest request) throws JsonException { 042 ObjectNode root = this.getNamedBean(memory, name, getType(), request); 043 ObjectNode data = root.with(DATA); 044 if (memory != null) { 045 Object val = memory.getValue(); 046 if (val == null) { 047 data.putNull(VALUE); 048 } else { 049 // set memory value based on type 050 if (val instanceof jmri.IdTag) { 051 ObjectNode idTagValue = idTagService.doGet((jmri.IdTag) val, name, IDTAG, request); 052 data.set(VALUE, idTagValue); 053 } else if (val instanceof jmri.Reporter) { 054 ObjectNode reporterValue = reporterService.doGet((jmri.Reporter) val, name, REPORTER, request); 055 data.set(VALUE, reporterValue); 056 } else if (val instanceof jmri.BasicRosterEntry) { 057 ObjectNode rosterValue = (ObjectNode) rosterService.getRosterEntry(request.locale, ((BasicRosterEntry) val).getId(), request.id); 058 data.set(VALUE, rosterValue); 059 } else { 060 // send string for types not explicitly handled 061 data.put(VALUE, val.toString()); 062 } 063 } 064 } 065 return root; 066 } 067 068 @Override 069 public ObjectNode doPost(Memory memory, String name, String type, JsonNode data, JsonRequest request) 070 throws JsonException { 071 if (!data.path(VALUE).isMissingNode()) { 072 if (data.path(VALUE).isNull()) { 073 memory.setValue(null); 074 } else { 075 memory.setValue(data.path(VALUE).asText()); 076 } 077 } 078 return this.doGet(memory, name, type, request); 079 } 080 081 @Override 082 public JsonNode doSchema(String type, boolean server, JsonRequest request) throws JsonException { 083 switch (type) { 084 case MEMORY: 085 case MEMORIES: 086 return doSchema(type, 087 server, 088 "jmri/server/json/memory/memory-server.json", 089 "jmri/server/json/memory/memory-client.json", 090 request.id); 091 default: 092 throw new JsonException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 093 Bundle.getMessage(request.locale, JsonException.ERROR_UNKNOWN_TYPE, type), request.id); 094 } 095 } 096 097 @Override 098 protected String getType() { 099 return MEMORY; 100 } 101 102 @Override 103 protected ProvidingManager<Memory> getProvidingManager() { 104 return InstanceManager.getDefault(MemoryManager.class); 105 } 106}