1 | package perfectfit;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.io.InputStream;
|
---|
5 | import java.util.Map;
|
---|
6 | import java.util.Map.Entry;
|
---|
7 | import java.util.stream.Collectors;
|
---|
8 |
|
---|
9 | import javax.servlet.ServletException;
|
---|
10 | import javax.servlet.http.HttpServlet;
|
---|
11 | import javax.servlet.http.HttpServletRequest;
|
---|
12 | import javax.servlet.http.HttpServletResponse;
|
---|
13 | import javax.servlet.http.HttpSession;
|
---|
14 |
|
---|
15 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
16 | import com.fasterxml.jackson.core.exc.StreamReadException;
|
---|
17 | import com.fasterxml.jackson.databind.DatabindException;
|
---|
18 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
19 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
20 |
|
---|
21 | import tudelft.dialogmanager.DialogPhase;
|
---|
22 | import tudelft.dialogmanager.DialogState;
|
---|
23 | import tudelft.dialogmanager.answertypes.InvalidAnswerException;
|
---|
24 | import tudelft.dialogmanager.parameters.DoubleValue;
|
---|
25 | import tudelft.dialogmanager.parameters.Parameters;
|
---|
26 | import tudelft.dialogmanager.stimuli.Stimulus;
|
---|
27 | import tudelft.dialogmanager.stimuli.Textual;
|
---|
28 | import tudelft.mentalhealth.perfectfit.updatefuncs.CheckDeadline;
|
---|
29 | import tudelft.mentalhealth.perfectfit.updatefuncs.DateString;
|
---|
30 | import tudelft.mentalhealth.perfectfit.updatefuncs.Example;
|
---|
31 | import tudelft.mentalhealth.perfectfit.updatefuncs.StringLength;
|
---|
32 | import tudelft.mentalhealth.perfectfit.updatefuncs.Time;
|
---|
33 | import tudelft.mentalhealth.perfectfit.updatefuncs.TimeDifference;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Servlet to handle user actions and reply with bot response.
|
---|
37 | */
|
---|
38 | @SuppressWarnings("serial")
|
---|
39 | public class ChatServlet extends HttpServlet {
|
---|
40 | private final static ObjectMapper jackson = new ObjectMapper();
|
---|
41 | static {
|
---|
42 | jackson.registerSubtypes(Example.class, Time.class, CheckDeadline.class,
|
---|
43 | TimeDifference.class, StringLength.class, DateString.class);
|
---|
44 | }
|
---|
45 | InputStream stream = getClass().getClassLoader()
|
---|
46 | .getResourceAsStream("dialog.json");
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * This service gets the current stimulation serialized as json. replies
|
---|
50 | * NOT_ACCEPTABLE if chat was finished already. Replies BAD_REQUEST if the
|
---|
51 | * given text answer is not acceptable in current state. Replies OK if text
|
---|
52 | * answer was processed succesfully.
|
---|
53 | */
|
---|
54 | @Override
|
---|
55 | protected void doPost(HttpServletRequest request,
|
---|
56 | HttpServletResponse response) throws ServletException, IOException {
|
---|
57 | DialogState state = getState(request.getSession());
|
---|
58 | if (state.isFinal()) {
|
---|
59 | response.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE,
|
---|
60 | "Chat is finished. Try starting a new session");
|
---|
61 | return;
|
---|
62 | }
|
---|
63 |
|
---|
64 | String usertext = request.getReader().lines()
|
---|
65 | .collect(Collectors.joining(System.lineSeparator()));
|
---|
66 | try {
|
---|
67 | state = state.with(usertext);
|
---|
68 | } catch (InvalidAnswerException e) {
|
---|
69 | e.printStackTrace();
|
---|
70 | response.sendError(HttpServletResponse.SC_BAD_REQUEST,
|
---|
71 | e.getMessage());
|
---|
72 |
|
---|
73 | }
|
---|
74 | // run evaluation and then prepare the next state immediately.
|
---|
75 | state = state.withUpdate(false);
|
---|
76 | if (!state.isFinal()) {
|
---|
77 | state = state.withUpdate(true);
|
---|
78 | }
|
---|
79 | request.getSession().setAttribute("state", state);
|
---|
80 |
|
---|
81 | response.setStatus(HttpServletResponse.SC_OK);
|
---|
82 | }
|
---|
83 |
|
---|
84 | @Override
|
---|
85 | protected void doGet(HttpServletRequest request,
|
---|
86 | HttpServletResponse response) throws ServletException, IOException {
|
---|
87 | DialogState state = getState(request.getSession());
|
---|
88 | if (state == null) {
|
---|
89 | response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED,
|
---|
90 | "Please enter the chat through the form");
|
---|
91 | return;
|
---|
92 | }
|
---|
93 | if (state.isFinal()) {
|
---|
94 | response.sendError(400,
|
---|
95 | "Chat is finished. Try starting a new session");
|
---|
96 | return;
|
---|
97 | }
|
---|
98 |
|
---|
99 | DialogPhase phase = state.getCurrentPhase();
|
---|
100 | Stimulus stimulus = phase.getStimulation()
|
---|
101 | .substitute(state.getParameters());
|
---|
102 |
|
---|
103 | StimAnswer reply = new StimAnswer(stimulus, phase.getAnswer());
|
---|
104 | if (!(stimulus instanceof Textual)) {
|
---|
105 | throw new IllegalStateException(
|
---|
106 | "The Demo App only supports Textual.");
|
---|
107 | }
|
---|
108 |
|
---|
109 | response.setContentType("text/json;charset=UTF-8");
|
---|
110 | response.getWriter().append(jackson.writeValueAsString(reply));
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * A OPTIONS command resets the state, using the query string in request.
|
---|
115 | * This can be used to restart a session, and must be used to load query
|
---|
116 | * arguments into the state. CONNECT seems not available , otherwise that
|
---|
117 | * would have been better
|
---|
118 | */
|
---|
119 | @Override
|
---|
120 | protected void doOptions(HttpServletRequest request,
|
---|
121 | HttpServletResponse response)
|
---|
122 | throws StreamReadException, DatabindException, IOException {
|
---|
123 | System.out.println("Initializing session");
|
---|
124 | InputStream stream = getClass().getClassLoader()
|
---|
125 | .getResourceAsStream("dialog.json");
|
---|
126 |
|
---|
127 | DialogState state = jackson.readValue(stream, DialogState.class);
|
---|
128 |
|
---|
129 | state = addVariables(state, request.getParameterMap());
|
---|
130 |
|
---|
131 | // initialize the preparation so that we have the text ready.
|
---|
132 | state = state.withUpdate(true);
|
---|
133 | request.getSession().setAttribute("state", state);
|
---|
134 |
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * @param state the current state.
|
---|
139 | * @param map a querystring eg 'age=25&weight=96'
|
---|
140 | * @return DialogState with the parameters added.
|
---|
141 | *
|
---|
142 | */
|
---|
143 | private DialogState addVariables(DialogState state,
|
---|
144 | Map<String, String[]> map) {
|
---|
145 | Parameters params = state.getParameters();
|
---|
146 | for (Entry<String, String[]> entry : map.entrySet()) {
|
---|
147 | params = params.with(entry.getKey(),
|
---|
148 | new DoubleValue(Double.valueOf(entry.getValue()[0])));
|
---|
149 | }
|
---|
150 | return state.with(params);
|
---|
151 |
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | *
|
---|
156 | * @param httpSession the current session
|
---|
157 | * @return current {@link DialogState} for this session
|
---|
158 | * @throws Exception if error occurs reading the initial state. Should not
|
---|
159 | * happen
|
---|
160 | */
|
---|
161 | private DialogState getState(HttpSession httpSession)
|
---|
162 | throws JsonParseException, JsonMappingException, IOException {
|
---|
163 | return (DialogState) httpSession.getAttribute("state");
|
---|
164 | }
|
---|
165 | } |
---|