Search in sources :

Example 1 with JsonUtilHttpService

use of jmri.server.json.util.JsonUtilHttpService in project JMRI by JMRI.

the class AbstractPanelServlet method listPanels.

protected void listPanels(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if (JSON.JSON.equals(request.getParameter("format"))) {
        response.setContentType(UTF8_APPLICATION_JSON);
        ServletUtil.getInstance().setNonCachingHeaders(response);
        JsonUtilHttpService service = new JsonUtilHttpService(new ObjectMapper());
        response.getWriter().print(service.getPanels(request.getLocale(), JSON.XML));
    } else {
        response.setContentType(UTF8_TEXT_HTML);
        response.getWriter().print(String.format(request.getLocale(), FileUtil.readURL(FileUtil.findURL(Bundle.getMessage(request.getLocale(), "Panel.html"))), String.format(request.getLocale(), Bundle.getMessage(request.getLocale(), "HtmlTitle"), ServletUtil.getInstance().getRailroadName(false), Bundle.getMessage(request.getLocale(), "PanelsTitle")), ServletUtil.getInstance().getNavBar(request.getLocale(), "/panel"), ServletUtil.getInstance().getRailroadName(false), ServletUtil.getInstance().getFooter(request.getLocale(), "/panel")));
    }
}
Also used : JsonUtilHttpService(jmri.server.json.util.JsonUtilHttpService) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with JsonUtilHttpService

use of jmri.server.json.util.JsonUtilHttpService in project JMRI by JMRI.

the class JmriJFrameServlet method doList.

private void doList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    List<String> disallowedFrames = Arrays.asList(WebServerPreferences.getDefault().getDisallowedFrames());
    // NOI18N
    String format = request.getParameter("format");
    ObjectMapper mapper = new ObjectMapper();
    Date now = new Date();
    boolean usePanels = Boolean.parseBoolean(request.getParameter(JSON.PANELS));
    response.setStatus(HttpServletResponse.SC_OK);
    if ("json".equals(format)) {
        // NOI18N
        // NOI18N
        response.setContentType("application/json");
    } else {
        // NOI18N
        response.setContentType("text/html");
    }
    // NOI18N
    response.setHeader("Connection", "Keep-Alive");
    // NOI18N
    response.setDateHeader("Date", now.getTime());
    // NOI18N
    response.setDateHeader("Last-Modified", now.getTime());
    // NOI18N
    response.setDateHeader("Expires", now.getTime());
    if ("json".equals(format)) {
        // NOI18N
        ArrayNode root = mapper.createArrayNode();
        HashSet<JFrame> frames = new HashSet<>();
        JsonUtilHttpService service = new JsonUtilHttpService(new ObjectMapper());
        JmriJFrame.getFrameList().stream().forEach((frame) -> {
            if (usePanels && frame instanceof Editor) {
                ObjectNode node = service.getPanel(request.getLocale(), (Editor) frame, JSON.XML);
                if (node != null) {
                    root.add(node);
                    frames.add(((Editor) frame).getTargetFrame());
                }
            } else {
                String title = frame.getTitle();
                if (!title.isEmpty() && frame.getAllowInFrameServlet() && !disallowedFrames.contains(title) && !frames.contains(frame) && frame.isVisible()) {
                    ObjectNode node = mapper.createObjectNode();
                    node.put(NAME, title);
                    // NOI18N
                    node.put(URL, "/frame/" + StringUtil.escapeString(title) + ".html");
                    // NOI18N
                    node.put("png", "/frame/" + StringUtil.escapeString(title) + ".png");
                    root.add(node);
                    frames.add(frame);
                }
            }
        });
        response.getWriter().write(mapper.writeValueAsString(root));
    } else {
        // NOI18N
        response.getWriter().append(Bundle.getMessage(request.getLocale(), "FrameDocType"));
        // NOI18N
        response.getWriter().append(Bundle.getMessage(request.getLocale(), "ListFront"));
        // NOI18N
        response.getWriter().write(Bundle.getMessage(request.getLocale(), "TableHeader"));
        // list frames, (open JMRI windows)
        for (JmriJFrame frame : JmriJFrame.getFrameList()) {
            String title = frame.getTitle();
            //don't add to list if blank or disallowed
            if (!title.isEmpty() && frame.getAllowInFrameServlet() && !disallowedFrames.contains(title) && frame.isVisible()) {
                // NOI18N
                String link = "/frame/" + StringUtil.escapeString(title) + ".html";
                //format a table row for each valid window (frame)
                // NOI18N
                response.getWriter().append("<tr><td><a href='" + link + "'>");
                response.getWriter().append(title);
                // NOI18N
                response.getWriter().append("</a></td>");
                response.getWriter().append("<td><a href='");
                response.getWriter().append(link);
                // NOI18N
                response.getWriter().append("'><img src='");
                // NOI18N
                response.getWriter().append("/frame/" + StringUtil.escapeString(title) + ".png");
                // NOI18N
                response.getWriter().append("'></a></td></tr>\n");
            }
        }
        // NOI18N
        response.getWriter().append("</table>");
        // NOI18N
        response.getWriter().append(Bundle.getMessage(request.getLocale(), "ListFooter"));
    }
}
Also used : JsonUtilHttpService(jmri.server.json.util.JsonUtilHttpService) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Date(java.util.Date) JmriJFrame(jmri.util.JmriJFrame) JFrame(javax.swing.JFrame) JmriJFrame(jmri.util.JmriJFrame) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Editor(jmri.jmrit.display.Editor) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashSet(java.util.HashSet)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 JsonUtilHttpService (jmri.server.json.util.JsonUtilHttpService)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 JFrame (javax.swing.JFrame)1 Editor (jmri.jmrit.display.Editor)1 JmriJFrame (jmri.util.JmriJFrame)1