Search in sources :

Example 46 with JsonException

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
        ""));
    }
}
Also used : JsonException(jmri.server.json.JsonException) Train(jmri.jmrit.operations.trains.Train) JsonUtil(jmri.server.json.operations.JsonUtil) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 47 with JsonException

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));
    }
}
Also used : JsonException(jmri.server.json.JsonException) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonHttpService(jmri.server.json.JsonHttpService)

Example 48 with JsonException

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());
    }
}
Also used : JsonException(jmri.server.json.JsonException) JsonNode(com.fasterxml.jackson.databind.JsonNode) MemoryManager(jmri.MemoryManager) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 49 with JsonException

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());
    }
}
Also used : JsonException(jmri.server.json.JsonException) Memory(jmri.Memory) JsonNode(com.fasterxml.jackson.databind.JsonNode) MemoryManager(jmri.MemoryManager) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 50 with JsonException

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());
    }
}
Also used : JsonException(jmri.server.json.JsonException) JsonMockConnection(jmri.server.json.JsonMockConnection) Memory(jmri.Memory) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) MemoryManager(jmri.MemoryManager)

Aggregations

JsonException (jmri.server.json.JsonException)112 JsonNode (com.fasterxml.jackson.databind.JsonNode)65 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)42 Test (org.junit.Test)31 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)25 JmriException (jmri.JmriException)22 IOException (java.io.IOException)21 JsonMockConnection (jmri.server.json.JsonMockConnection)18 Sensor (jmri.Sensor)13 SensorManager (jmri.SensorManager)13 Locale (java.util.Locale)10 Route (jmri.Route)9 RouteManager (jmri.RouteManager)9 Turnout (jmri.Turnout)9 SignalMast (jmri.SignalMast)8 TurnoutManager (jmri.TurnoutManager)8 Light (jmri.Light)7 ReporterManager (jmri.ReporterManager)7 SignalHead (jmri.SignalHead)7 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)6