package geniusweb.runserver; import java.io.IOException; import java.util.logging.Level; import java.util.stream.Collectors; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import geniusweb.protocol.NegoSettings; import tudelft.utilities.logging.ReportToLogger; import tudelft.utilities.logging.Reporter; /** * Contains incoming connection to run a Nego. runserver/runsession with the * JSON {@link NegoSettings} in the request header. After the session is * started, the new Nego ID is returned. This can be used to find back the log * files.. */ @SuppressWarnings("serial") public class RunNego extends HttpServlet { private final Reporter log; public RunNego() { this(new ReportToLogger("runserver")); } public RunNego(Reporter logger) { super(); this.log = logger; } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response). * * @return the ID of the new started run. Or SC_BAD_REQUEST if the request * can not be processed. */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String reqstring = request.getReader().lines() .collect(Collectors.joining()); NegoSettings settings; try { settings = Jackson.instance().readValue(reqstring, NegoSettings.class); } catch (IOException e) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage()); System.err.println("Run request failed " + reqstring); e.printStackTrace(); // helpful for debugging run requests return; } log.log(Level.INFO, "request to run Nego: " + settings); RunningNego running = new RunningNego(settings, RunningNegotiationsRepo.instance(), log); running.start(); // set content type to prevent some browsers // trying to parse this (eg as XML) response.setContentType("application/json"); response.getWriter().append(running.getID()); } /** * @see #doGet(HttpServletRequest, HttpServletResponse) */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }