This repository has been archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
WebAppServlet.java
56 lines (41 loc) · 1.82 KB
/
WebAppServlet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.twilio.calltracking.servlets;
import com.twilio.calltracking.lib.utilities.Lazy;
import com.twilio.calltracking.lib.web.request.validators.RequestParametersValidator;
import com.twilio.sdk.verbs.TwiMLResponse;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ObjectWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class WebAppServlet extends HttpServlet {
protected Lazy<RequestParametersValidator> requestValidator;
protected WebAppServlet() {
requestValidator = new Lazy<>(RequestParametersValidator::new);
}
protected void respondTwiML(HttpServletResponse response, TwiMLResponse twiMLResponse)
throws IOException {
response.setContentType("text/xml");
response.getWriter().write(twiMLResponse.toXML());
}
protected void respondJson(HttpServletResponse response, Object object) throws IOException {
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json = ow.writeValueAsString(object);
response.setContentType("application/json");
response.getWriter().write(json);
}
protected void respondContent(HttpServletResponse response, String content) throws IOException {
response.getWriter().write(content);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
requestValidator.get().setRequest(request);
}
protected boolean isValidRequest() {
return isValidRequest(requestValidator.get());
}
protected boolean isValidRequest(RequestParametersValidator validator) {
return true;
}
}