001package jmri.web.servlet.home;
002
003import static jmri.web.servlet.ServletUtil.UTF8_TEXT_HTML;
004
005import java.io.IOException;
006import javax.servlet.ServletException;
007import javax.servlet.annotation.WebServlet;
008import javax.servlet.http.HttpServlet;
009import javax.servlet.http.HttpServletRequest;
010import javax.servlet.http.HttpServletResponse;
011import jmri.InstanceManager;
012import jmri.util.FileUtil;
013import jmri.web.servlet.ServletUtil;
014import org.openide.util.lookup.ServiceProvider;
015
016/**
017 *
018 * @author Randall Wood (C) 2014, 2016
019 */
020@WebServlet(name = "HomeServlet",
021        urlPatterns = {
022            "/", // default
023            "/index.html", // redirect to default since ~ 1 FEB 2014
024            "/prefs/index.html" // some WiThrottle clients require this URL to show web services
025        })
026@ServiceProvider(service = HttpServlet.class)
027public class HomeServlet extends HttpServlet {
028
029    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
030        if (request.getRequestURI().startsWith("/index.html")
031                || request.getRequestURI().startsWith("/prefs/index.html")) {
032            response.sendRedirect("/");
033            return;
034        }
035        if (!request.getRequestURI().equals("/")) {
036            response.sendError(HttpServletResponse.SC_NOT_FOUND);
037            return;
038        }
039        response.setHeader("Connection", "Keep-Alive"); // NOI18N
040        response.setContentType(UTF8_TEXT_HTML);
041        response.getWriter().print(String.format(request.getLocale(),
042                FileUtil.readURL(FileUtil.findURL(Bundle.getMessage(request.getLocale(), "Home.html"))),
043                InstanceManager.getDefault(ServletUtil.class).getRailroadName(false),
044                InstanceManager.getDefault(ServletUtil.class).getNavBar(request.getLocale(), "/home"),
045                InstanceManager.getDefault(ServletUtil.class).getRailroadName(false),
046                InstanceManager.getDefault(ServletUtil.class).getFooter(request.getLocale(), "/home")
047        ));
048    }
049
050// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
051    /**
052     * Handles the HTTP <code>GET</code> method.
053     *
054     * @param request  servlet request
055     * @param response servlet response
056     * @throws ServletException if a servlet-specific error occurs
057     * @throws IOException      if an I/O error occurs
058     */
059    @Override
060    protected void doGet(HttpServletRequest request, HttpServletResponse response)
061            throws ServletException, IOException {
062        processRequest(request, response);
063    }
064
065    /**
066     * Handles the HTTP <code>POST</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 doPost(HttpServletRequest request, HttpServletResponse response)
075            throws ServletException, IOException {
076        processRequest(request, response);
077    }
078
079    /**
080     * Returns a short description of the servlet.
081     *
082     * @return a String containing servlet description
083     */
084    @Override
085    public String getServletInfo() {
086        return "Home Servlet";
087    }// </editor-fold>
088
089}