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