use of jmri.server.json.JsonException in project JMRI by JMRI.
the class OperationsServlet method processTrains.
protected void processTrains(HttpServletRequest request, HttpServletResponse response) throws IOException {
if (JSON.JSON.equals(request.getParameter("format"))) {
response.setContentType(UTF8_APPLICATION_JSON);
ServletUtil.getInstance().setNonCachingHeaders(response);
try {
JsonUtil utilities = new JsonUtil(this.mapper);
response.getWriter().print(utilities.getTrains(request.getLocale()));
} catch (JsonException ex) {
int code = ex.getJsonMessage().path(JSON.DATA).path(JsonException.CODE).asInt(200);
response.sendError(code, (new ObjectMapper()).writeValueAsString(ex.getJsonMessage()));
}
} else if ("html".equals(request.getParameter("format"))) {
response.setContentType(UTF8_TEXT_HTML);
ServletUtil.getInstance().setNonCachingHeaders(response);
boolean showAll = ("all".equals(request.getParameter("show")));
StringBuilder html = new StringBuilder();
String format = FileUtil.readURL(FileUtil.findURL(Bundle.getMessage(request.getLocale(), "TrainsSnippet.html")));
for (Train train : TrainManager.instance().getTrainsByNameList()) {
if (showAll || !CarManager.instance().getByTrainDestinationList(train).isEmpty()) {
html.append(String.format(request.getLocale(), format, train.getIconName(), StringEscapeUtils.escapeHtml4(train.getDescription()), train.getLeadEngine() != null ? train.getLeadEngine().toString() : "", StringEscapeUtils.escapeHtml4(train.getTrainDepartsName()), train.getDepartureTime(), train.getStatus(), StringEscapeUtils.escapeHtml4(train.getCurrentLocationName()), StringEscapeUtils.escapeHtml4(train.getTrainTerminatesName()), StringEscapeUtils.escapeHtml4(train.getTrainRouteName()), train.getId()));
}
}
response.getWriter().print(html.toString());
} else {
response.setContentType(UTF8_TEXT_HTML);
response.getWriter().print(String.format(request.getLocale(), FileUtil.readURL(FileUtil.findURL(Bundle.getMessage(request.getLocale(), "Operations.html"))), String.format(request.getLocale(), Bundle.getMessage(request.getLocale(), "HtmlTitle"), ServletUtil.getInstance().getRailroadName(false), Bundle.getMessage(request.getLocale(), "TrainsTitle")), ServletUtil.getInstance().getNavBar(request.getLocale(), request.getContextPath()), ServletUtil.getInstance().getRailroadName(false), ServletUtil.getInstance().getFooter(request.getLocale(), request.getContextPath()), // no train Id
""));
}
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonServlet method doDelete.
@Override
protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType(UTF8_APPLICATION_JSON);
// NOI18N
response.setHeader("Connection", "Keep-Alive");
ServletUtil.getInstance().setNonCachingHeaders(response);
// NOI18N
String[] rest = request.getPathInfo().split("/");
String type = (rest.length > 1) ? rest[1] : null;
String name = (rest.length > 2) ? rest[2] : null;
JsonNode reply = mapper.createObjectNode();
try {
if (type != null) {
if (name == null) {
// need to I18N
throw new JsonException(HttpServletResponse.SC_BAD_REQUEST, "name must be specified");
}
for (JsonHttpService service : this.services.get(type)) {
service.doDelete(type, name, request.getLocale());
}
} else {
log.warn("Type not specified.");
// Need to I18N
throw new JsonException(HttpServletResponse.SC_BAD_REQUEST, "Type must be specified.");
}
} 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) {
this.sendError(response, code, this.mapper.writeValueAsString(reply));
}
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonMemoryHttpServiceTest method testDoPut.
public void testDoPut() {
ObjectMapper mapper = new ObjectMapper();
JsonMemoryHttpService service = new JsonMemoryHttpService(mapper);
MemoryManager manager = InstanceManager.getDefault(MemoryManager.class);
JsonNode message;
try {
// add a memory
Assert.assertNull(manager.getMemory("IM1"));
message = mapper.createObjectNode().put(JSON.NAME, "IM1").put(JSON.VALUE, "close");
service.doPut(JsonMemory.MEMORY, "IM1", message, Locale.ENGLISH);
Assert.assertNotNull(manager.getMemory("IM1"));
} catch (JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonMemoryHttpServiceTest method testDoGet.
public void testDoGet() throws JmriException {
JsonMemoryHttpService service = new JsonMemoryHttpService(new ObjectMapper());
MemoryManager manager = InstanceManager.getDefault(MemoryManager.class);
// no value
Memory memory1 = manager.provideMemory("IM1");
JsonNode result;
try {
result = service.doGet(JsonMemory.MEMORY, "IM1", Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(JsonMemory.MEMORY, result.path(JSON.TYPE).asText());
Assert.assertEquals("IM1", result.path(JSON.DATA).path(JSON.NAME).asText());
// JSON node has the text "null" if memory is null
Assert.assertEquals("null", result.path(JSON.DATA).path(JSON.VALUE).asText());
memory1.setValue("throw");
result = service.doGet(JsonMemory.MEMORY, "IM1", Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals("throw", result.path(JSON.DATA).path(JSON.VALUE).asText());
memory1.setValue("close");
result = service.doGet(JsonMemory.MEMORY, "IM1", Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals("close", result.path(JSON.DATA).path(JSON.VALUE).asText());
} catch (JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonMemorySocketServiceTest method testOnMessageChange.
public void testOnMessageChange() {
try {
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonNode message;
JsonMemorySocketService service = new JsonMemorySocketService(connection);
MemoryManager manager = InstanceManager.getDefault(MemoryManager.class);
Memory memory1 = manager.provideMemory("IM1");
// Memory "close"
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IM1").put(JSON.VALUE, "close");
service.onMessage(JsonMemory.MEMORY, message, Locale.ENGLISH);
Assert.assertEquals("close", memory1.getValue());
// Memory "throw"
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IM1").put(JSON.VALUE, "throw");
service.onMessage(JsonMemory.MEMORY, message, Locale.ENGLISH);
Assert.assertEquals("throw", memory1.getValue());
// Memory UNKNOWN - remains ON
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IM1").putNull(JSON.VALUE);
service.onMessage(JsonMemory.MEMORY, message, Locale.ENGLISH);
Assert.assertEquals(null, memory1.getValue());
memory1.setValue("throw");
// Memory no value
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IM1");
JsonException exception = null;
try {
service.onMessage(JsonMemory.MEMORY, message, Locale.ENGLISH);
} catch (JsonException ex) {
exception = ex;
}
Assert.assertEquals("throw", memory1.getValue());
Assert.assertNull(exception);
} catch (IOException | JmriException | JsonException ex) {
Assert.fail(ex.getMessage());
}
}
Aggregations