[30] | 1 | package geniusweb.runserver;
|
---|
| 2 |
|
---|
| 3 | import java.io.IOException;
|
---|
| 4 | import java.util.logging.Level;
|
---|
| 5 | import java.util.stream.Collectors;
|
---|
| 6 |
|
---|
| 7 | import javax.servlet.ServletException;
|
---|
| 8 | import javax.servlet.http.HttpServlet;
|
---|
| 9 | import javax.servlet.http.HttpServletRequest;
|
---|
| 10 | import javax.servlet.http.HttpServletResponse;
|
---|
| 11 |
|
---|
| 12 | import geniusweb.protocol.NegoSettings;
|
---|
| 13 | import tudelft.utilities.logging.ReportToLogger;
|
---|
| 14 | import tudelft.utilities.logging.Reporter;
|
---|
| 15 |
|
---|
| 16 | /**
|
---|
| 17 | * Contains incoming connection to run a Nego. runserver/runsession with the
|
---|
| 18 | * JSON {@link NegoSettings} in the request header. After the session is
|
---|
| 19 | * started, the new Nego ID is returned. This can be used to find back the log
|
---|
| 20 | * files..
|
---|
| 21 | */
|
---|
| 22 | @SuppressWarnings("serial")
|
---|
| 23 | public class RunNego extends HttpServlet {
|
---|
| 24 | private final Reporter log;
|
---|
| 25 |
|
---|
| 26 | public RunNego() {
|
---|
| 27 | this(new ReportToLogger("runserver"));
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | public RunNego(Reporter logger) {
|
---|
| 31 | super();
|
---|
| 32 | this.log = logger;
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | /**
|
---|
| 36 | * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
|
---|
| 37 | * response).
|
---|
| 38 | *
|
---|
| 39 | * @return the ID of the new started run. Or SC_BAD_REQUEST if the request
|
---|
| 40 | * can not be processed.
|
---|
| 41 | */
|
---|
| 42 | @Override
|
---|
| 43 | protected void doGet(HttpServletRequest request,
|
---|
| 44 | HttpServletResponse response) throws ServletException, IOException {
|
---|
| 45 | String reqstring = request.getReader().lines()
|
---|
| 46 | .collect(Collectors.joining());
|
---|
| 47 | NegoSettings settings;
|
---|
| 48 | try {
|
---|
| 49 | settings = Jackson.instance().readValue(reqstring,
|
---|
| 50 | NegoSettings.class);
|
---|
| 51 | } catch (IOException e) {
|
---|
| 52 | response.sendError(HttpServletResponse.SC_BAD_REQUEST,
|
---|
| 53 | e.getMessage());
|
---|
| 54 | System.err.println("Run request failed " + reqstring);
|
---|
| 55 | e.printStackTrace(); // helpful for debugging run requests
|
---|
| 56 | return;
|
---|
| 57 | }
|
---|
| 58 | log.log(Level.INFO, "request to run Nego: " + settings);
|
---|
| 59 | RunningNego running = new RunningNego(settings,
|
---|
| 60 | RunningNegotiationsRepo.instance(), log);
|
---|
| 61 | running.start();
|
---|
| 62 | // set content type to prevent some browsers
|
---|
| 63 | // trying to parse this (eg as XML)
|
---|
| 64 | response.setContentType("application/json");
|
---|
| 65 | response.getWriter().append(running.getID());
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | /**
|
---|
| 69 | * @see #doGet(HttpServletRequest, HttpServletResponse)
|
---|
| 70 | */
|
---|
| 71 | @Override
|
---|
| 72 | protected void doPost(HttpServletRequest request,
|
---|
| 73 | HttpServletResponse response) throws ServletException, IOException {
|
---|
| 74 | doGet(request, response);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | }
|
---|