001package jmri.web.servlet.tables;
002
003import static jmri.web.servlet.ServletUtil.UTF8_TEXT_HTML;
004
005import java.io.IOException;
006import java.net.URLDecoder;
007import java.nio.charset.StandardCharsets;
008import javax.servlet.ServletException;
009import javax.servlet.annotation.WebServlet;
010import javax.servlet.http.HttpServlet;
011import javax.servlet.http.HttpServletRequest;
012import javax.servlet.http.HttpServletResponse;
013import jmri.InstanceManager;
014import jmri.util.FileUtil;
015import jmri.web.servlet.ServletUtil;
016import org.openide.util.lookup.ServiceProvider;
017
018/**
019 * Provide web UI for data, such as turnouts, sensors, etc. displayed in tables
020 *
021 * Each method of this Servlet responds to a unique URL pattern.
022 *
023 * @author mstevetodd
024 */
025
026/*
027 *
028 */
029@WebServlet(name = "TablesServlet",
030        urlPatterns = {
031            "/tables", // default
032        })
033@ServiceProvider(service = HttpServlet.class)
034public class TablesServlet extends HttpServlet {
035
036    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
037
038        String[] path = request.getRequestURI().split("/"); // NOI18N
039        String tableType = URLDecoder.decode(path[path.length - 1], StandardCharsets.UTF_8.name());
040        if (tableType.equals("tables")) tableType = "type"; // NOI18N redirect to list of table types if none passed in
041
042        //print the html, using the replacement values listed to fill in the calculated stuff
043        response.setHeader("Connection", "Keep-Alive"); // NOI18N
044        response.setContentType(UTF8_TEXT_HTML);
045        response.getWriter().print(String.format(request.getLocale(),
046                FileUtil.readURL(FileUtil.findURL(Bundle.getMessage(request.getLocale(), "Tables.html"))),
047                Bundle.getMessage(request.getLocale(), "TablesTitle"),               //page title is parm 1
048                InstanceManager.getDefault(ServletUtil.class).getNavBar(request.getLocale(), "/tables"), //navbar is parm 2
049                InstanceManager.getDefault(ServletUtil.class).getRailroadName(false),                   //railroad name is parm 3
050                InstanceManager.getDefault(ServletUtil.class).getFooter(request.getLocale(), "/tables"), //footer is parm 4
051                tableType //tableType is parm 5
052        ));
053    }
054
055// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
056    /**
057     * Handles the HTTP <code>GET</code> method.
058     *
059     * @param request  servlet request
060     * @param response servlet response
061     * @throws ServletException if a servlet-specific error occurs
062     * @throws IOException      if an I/O error occurs
063     */
064    @Override
065    protected void doGet(HttpServletRequest request, HttpServletResponse response)
066            throws ServletException, IOException {
067        processRequest(request, response);
068    }
069
070    /**
071     * Handles the HTTP <code>POST</code> method.
072     *
073     * @param request  servlet request
074     * @param response servlet response
075     * @throws ServletException if a servlet-specific error occurs
076     * @throws IOException      if an I/O error occurs
077     */
078    @Override
079    protected void doPost(HttpServletRequest request, HttpServletResponse response)
080            throws ServletException, IOException {
081        processRequest(request, response);
082    }
083
084    /**
085     * Returns a short description of the servlet.
086     *
087     * @return a String containing servlet description
088     */
089    @Override
090    public String getServletInfo() {
091        return "Tables Servlet";
092    }// </editor-fold>
093
094}