Search in sources :

Example 1 with JsonUtil

use of jmri.server.json.operations.JsonUtil 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)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Train (jmri.jmrit.operations.trains.Train)1 JsonException (jmri.server.json.JsonException)1 JsonUtil (jmri.server.json.operations.JsonUtil)1