001package jmri.web.servlet.about;
002
003import static jmri.web.servlet.ServletUtil.UTF8_TEXT_HTML;
004
005import java.io.IOException;
006import java.util.Locale;
007import javax.servlet.ServletException;
008import javax.servlet.annotation.WebServlet;
009import javax.servlet.http.HttpServlet;
010import javax.servlet.http.HttpServletRequest;
011import javax.servlet.http.HttpServletResponse;
012import jmri.Application;
013import jmri.InstanceManager;
014import jmri.jmrix.ConnectionConfig;
015import jmri.jmrix.ConnectionConfigManager;
016import jmri.profile.Profile;
017import jmri.profile.ProfileManager;
018import jmri.util.FileUtil;
019import jmri.web.servlet.ServletUtil;
020import org.openide.util.lookup.ServiceProvider;
021
022/**
023 *
024 * @author Randall Wood (C) 2014, 2016
025 * @author mstevetodd (C) 2017
026 */
027@WebServlet(name = "AboutServlet",
028        urlPatterns = {"/about"})
029@ServiceProvider(service = HttpServlet.class)
030public class AboutServlet extends HttpServlet {
031
032    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
033
034        //retrieve the list of JMRI connections as a string
035        StringBuilder connList = new StringBuilder("");
036        String comma = "";
037        for (ConnectionConfig conn : InstanceManager.getDefault(ConnectionConfigManager.class)) {
038            if (!conn.getDisabled()) {
039                connList.append(comma).append(Bundle.getMessage(request.getLocale(), "ConnectionSucceeded", conn.getConnectionName(), conn.name(), conn.getInfo()));
040                comma = ", ";
041            }
042        }
043
044        //print the html, using the replacement values listed to fill in the calculated stuff
045        response.setHeader("Connection", "Keep-Alive"); // NOI18N
046        response.setContentType(UTF8_TEXT_HTML);
047        Profile profile = ProfileManager.getDefault().getActiveProfile();
048        String profileName = profile != null ? profile.getName() : "";
049        response.getWriter().print(String.format(request.getLocale(),
050                FileUtil.readURL(FileUtil.findURL(Bundle.getMessage(request.getLocale(), "About.html"))),
051                Bundle.getMessage(request.getLocale(), "AboutTitle"),                                   // page title is parm 1
052                InstanceManager.getDefault(ServletUtil.class).getNavBar(request.getLocale(), "/about"), // navbar is parm 2
053                InstanceManager.getDefault(ServletUtil.class).getRailroadName(false),                   // railroad name is parm 3
054                InstanceManager.getDefault(ServletUtil.class).getFooter(request.getLocale(), "/about"), // footer is parm 4
055                connList,                                                                               // system connection list is parm 5
056                Application.getApplicationName() + " " + jmri.Version.name(),                           // JMRI version is parm 6                                         //JMRI version is parm 6
057                jmri.Version.getCopyright(),                                                            // Copyright is parm 7
058                System.getProperty("java.version", "<unknown>"),                                        // Java version is parm 8
059                Locale.getDefault().toString(),                                                         // locale is parm 9
060                profileName                                                                             // active profile name is 10
061        ));
062    }
063
064// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
065    /**
066     * Handles the HTTP <code>GET</code> method.
067     *
068     * @param request  servlet request
069     * @param response servlet response
070     * @throws ServletException if a servlet-specific error occurs
071     * @throws IOException      if an I/O error occurs
072     */
073    @Override
074    protected void doGet(HttpServletRequest request, HttpServletResponse response)
075            throws ServletException, IOException {
076        processRequest(request, response);
077    }
078
079    /**
080     * Handles the HTTP <code>POST</code> method.
081     *
082     * @param request  servlet request
083     * @param response servlet response
084     * @throws ServletException if a servlet-specific error occurs
085     * @throws IOException      if an I/O error occurs
086     */
087    @Override
088    protected void doPost(HttpServletRequest request, HttpServletResponse response)
089            throws ServletException, IOException {
090        processRequest(request, response);
091    }
092
093    /**
094     * Returns a short description of the servlet.
095     *
096     * @return a String containing servlet description
097     */
098    @Override
099    public String getServletInfo() {
100        return "About Servlet";
101    }// </editor-fold>
102
103}