source: src/main/java/geniusweb/runserver/RunNego.java@ 8

Last change on this file since 8 was 8, checked in by bart, 5 years ago

Update 28 jan 2020

File size: 2.7 KB
Line 
1package geniusweb.runserver;
2
3import java.io.IOException;
4import java.util.logging.Level;
5import java.util.stream.Collectors;
6
7import javax.servlet.ServletException;
8import javax.servlet.http.HttpServlet;
9import javax.servlet.http.HttpServletRequest;
10import javax.servlet.http.HttpServletResponse;
11
12import com.fasterxml.jackson.databind.ObjectMapper;
13
14import geniusweb.protocol.NegoSettings;
15import tudelft.utilities.logging.ReportToLogger;
16import 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")
25public 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 System.err.println("Run request failed " + reqstring);
63 e.printStackTrace(); // helpful for debugging run requests
64 return;
65 }
66 log.log(Level.INFO, "request to run Nego: " + settings);
67 RunningNego session = new RunningNego(settings,
68 RunningNegotiationsRepo.instance(), log);
69 session.start();
70 // set content type to prevent some browsers
71 // trying to parse this (eg as XML)
72 response.setContentType("application/json");
73 response.getWriter().append(session.getID());
74 }
75
76 /**
77 * @see #doGet(HttpServletRequest, HttpServletResponse)
78 */
79 @Override
80 protected void doPost(HttpServletRequest request,
81 HttpServletResponse response) throws ServletException, IOException {
82 doGet(request, response);
83 }
84
85}
Note: See TracBrowser for help on using the repository browser.