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). Returns
|
---|
41 | * <ul>
|
---|
42 | * <li>URL with websocket address to contact the now running party.
|
---|
43 | * <li>SC_NOT_FOUND (404) if attempt is made to access non-running
|
---|
44 | * party.
|
---|
45 | * <li>SC_SERVICE_UNAVAILABLE (503) if there are no free slots to run
|
---|
46 | * the party.
|
---|
47 | * <li>SC_EXPECTATION_FAILED (417) if something else goes wrong during
|
---|
48 | * instantiating the party (then see logs for more details)
|
---|
49 | * </ul>
|
---|
50 | */
|
---|
51 | @Override
|
---|
52 | protected void doGet(HttpServletRequest request,
|
---|
53 | HttpServletResponse response) throws ServletException, IOException {
|
---|
54 | String reqstring = request.getReader().lines()
|
---|
55 | .collect(Collectors.joining());
|
---|
56 | NegoSettings settings;
|
---|
57 | try {
|
---|
58 | settings = jackson.readValue(reqstring, NegoSettings.class);
|
---|
59 | } catch (IOException e) {
|
---|
60 | response.sendError(HttpServletResponse.SC_BAD_REQUEST,
|
---|
61 | e.getMessage());
|
---|
62 | return;
|
---|
63 | }
|
---|
64 | log.log(Level.INFO, "request to run Nego: " + settings);
|
---|
65 | RunningNego session = new RunningNego(settings,
|
---|
66 | RunningNegotiationsRepo.instance(), log);
|
---|
67 | RunningNegotiationsRepo.instance().put(session);
|
---|
68 | // set content type to prevent some browsers
|
---|
69 | // trying to parse this (eg as XML)
|
---|
70 | response.setContentType("application/json");
|
---|
71 | response.getWriter().append(session.getID());
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * @see #doGet(HttpServletRequest, HttpServletResponse)
|
---|
76 | */
|
---|
77 | @Override
|
---|
78 | protected void doPost(HttpServletRequest request,
|
---|
79 | HttpServletResponse response) throws ServletException, IOException {
|
---|
80 | doGet(request, response);
|
---|
81 | }
|
---|
82 |
|
---|
83 | }
|
---|