Search in sources :

Example 1 with JsonRosterServiceFactory

use of jmri.server.json.roster.JsonRosterServiceFactory in project JMRI by JMRI.

the class RosterServlet method doRoster.

/**
     * Generate the JSON, XML, or HTML output specified by {@link #doList(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)},
     * {@link #doGroup(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.String)},
     * or
     * {@link #doEntry(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)}.
     *
     * @param request  servlet request with format and locale for response
     * @param response servlet response
     * @param filter   a JSON object with name-value pairs of parameters for
     *                 {@link jmri.jmrit.roster.Roster#getEntriesMatchingCriteria(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)}.
     * @throws java.io.IOException if communications is cut with client
     */
protected void doRoster(HttpServletRequest request, HttpServletResponse response, JsonNode filter) throws IOException {
    ServletUtil.getInstance().setNonCachingHeaders(response);
    log.debug("Getting roster with filter {}", filter);
    String group = (!filter.path(GROUP).isMissingNode()) ? filter.path(GROUP).asText() : null;
    log.debug("Group {} was in filter", group);
    String format = request.getParameter(FORMAT);
    if (format == null) {
        format = "";
    }
    switch(format) {
        case JSON.JSON:
            response.setContentType(UTF8_APPLICATION_JSON);
            JsonRosterServiceFactory factory = InstanceManager.getOptionalDefault(JsonRosterServiceFactory.class).orElseGet(() -> {
                return InstanceManager.setDefault(JsonRosterServiceFactory.class, new JsonRosterServiceFactory());
            });
            response.getWriter().print(factory.getHttpService(mapper).getRoster(request.getLocale(), filter));
            break;
        case JSON.XML:
            response.setContentType(UTF8_APPLICATION_XML);
            File roster = new File(Roster.getDefault().getRosterIndexPath());
            if (roster.exists()) {
                response.getWriter().print(FileUtil.readFile(roster));
            }
            break;
        case "html":
            String row;
            if ("simple".equals(request.getParameter("view"))) {
                row = FileUtil.readURL(FileUtil.findURL(Bundle.getMessage(request.getLocale(), "SimpleTableRow.html")));
            } else {
                row = FileUtil.readURL(FileUtil.findURL(Bundle.getMessage(request.getLocale(), "TableRow.html")));
            }
            StringBuilder builder = new StringBuilder();
            // NOI18N
            response.setContentType(UTF8_TEXT_HTML);
            if (Roster.AllEntries(request.getLocale()).equals(group)) {
                group = null;
            }
            List<RosterEntry> entries = Roster.getDefault().getEntriesMatchingCriteria((!filter.path(ROAD).isMissingNode()) ? filter.path(ROAD).asText() : null, (!filter.path(NUMBER).isMissingNode()) ? filter.path(NUMBER).asText() : null, (!filter.path(ADDRESS).isMissingNode()) ? filter.path(ADDRESS).asText() : null, (!filter.path(MFG).isMissingNode()) ? filter.path(MFG).asText() : null, (!filter.path(DECODER_MODEL).isMissingNode()) ? filter.path(DECODER_MODEL).asText() : null, (!filter.path(DECODER_FAMILY).isMissingNode()) ? filter.path(DECODER_FAMILY).asText() : null, (!filter.path(NAME).isMissingNode()) ? filter.path(NAME).asText() : null, group);
            entries.stream().forEach((entry) -> {
                // NOTE: changing the following order will break JavaScript and HTML code
                builder.append(String.format(request.getLocale(), row, entry.getId(), entry.getRoadName(), entry.getRoadNumber(), entry.getMfg(), entry.getModel(), entry.getOwner(), entry.getDccAddress(), entry.getDecoderModel(), entry.getDecoderFamily(), entry.getDecoderComment(), entry.getComment(), entry.getURL(), entry.getMaxSpeedPCT(), entry.getFileName(), StringUtil.escapeString(entry.getId())));
            });
            response.getWriter().print(builder.toString());
            break;
        default:
            if (group == null) {
                group = Roster.AllEntries(request.getLocale());
            }
            // NOI18N
            response.setContentType(UTF8_TEXT_HTML);
            response.getWriter().print(String.format(request.getLocale(), FileUtil.readURL(FileUtil.findURL(Bundle.getMessage(request.getLocale(), "Roster.html"))), String.format(request.getLocale(), Bundle.getMessage(request.getLocale(), "HtmlTitle"), ServletUtil.getInstance().getRailroadName(false), Bundle.getMessage(request.getLocale(), "RosterTitle")), ServletUtil.getInstance().getNavBar(request.getLocale(), request.getContextPath()), ServletUtil.getInstance().getRailroadName(false), ServletUtil.getInstance().getFooter(request.getLocale(), request.getContextPath()), group));
            break;
    }
}
Also used : JsonRosterServiceFactory(jmri.server.json.roster.JsonRosterServiceFactory) RosterEntry(jmri.jmrit.roster.RosterEntry) File(java.io.File)

Aggregations

File (java.io.File)1 RosterEntry (jmri.jmrit.roster.RosterEntry)1 JsonRosterServiceFactory (jmri.server.json.roster.JsonRosterServiceFactory)1