001package jmri.web.servlet.config;
002
003import java.io.IOException;
004import javax.servlet.ServletException;
005import javax.servlet.http.HttpServlet;
006import javax.servlet.http.HttpServletRequest;
007import javax.servlet.http.HttpServletResponse;
008import jmri.InstanceManager;
009import jmri.web.server.WebServerPreferences;
010
011/**
012 *
013 * @author Randall Wood (C) 2016
014 */
015public class ConfigServlet extends HttpServlet {
016
017    /**
018     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
019     * methods.
020     *
021     * Always returns a 501 Not Implemented response. This is currently a
022     * placeholder for a future capability to move (most) preferences controls
023     * into a web service, so that headless systems can be configured without
024     * having to copy a configuration from another system.
025     *
026     * @param request  servlet request
027     * @param response servlet response
028     * @throws ServletException if a servlet-specific error occurs
029     * @throws IOException      if an I/O error occurs
030     */
031    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
032            throws ServletException, IOException {
033        if (!InstanceManager.getDefault(WebServerPreferences.class).allowRemoteConfig()) {
034            response.sendError(HttpServletResponse.SC_FORBIDDEN);
035            return;
036        }
037        response.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, Bundle.getMessage("501NotImplemented")); // NOI18N
038    }
039
040    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
041    /**
042     * Handles the HTTP <code>GET</code> method.
043     *
044     * @param request  servlet request
045     * @param response servlet response
046     * @throws ServletException if a servlet-specific error occurs
047     * @throws IOException      if an I/O error occurs
048     */
049    @Override
050    protected void doGet(HttpServletRequest request, HttpServletResponse response)
051            throws ServletException, IOException {
052        processRequest(request, response);
053    }
054
055    /**
056     * Handles the HTTP <code>POST</code> method.
057     *
058     * @param request  servlet request
059     * @param response servlet response
060     * @throws ServletException if a servlet-specific error occurs
061     * @throws IOException      if an I/O error occurs
062     */
063    @Override
064    protected void doPost(HttpServletRequest request, HttpServletResponse response)
065            throws ServletException, IOException {
066        processRequest(request, response);
067    }
068
069    /**
070     * Returns a short description of the servlet.
071     *
072     * @return a String containing servlet description
073     */
074    @Override
075    public String getServletInfo() {
076        return "Short description";
077    }// </editor-fold>
078
079}