log.log(Level.WARNING,"error processing request",e);
}
}
private void process(HttpServletRequest request, HttpServletResponse response) throws Exception {
WebDriverLikeRequest req = new WebDriverLikeRequest(request);
response.setContentType("application/json;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
try {
response.setStatus(200);
Response resp = getResponse(req);
// TODO implement the json protocol properly.
if (req.getGenericCommand() == WebDriverLikeCommand.NEW_SESSION && resp.getStatus() == 0) {
response.setStatus(301);
String session = resp.getSessionId();
if (session == null || session.isEmpty()) {
response.setStatus(500);
}
String scheme = request.getScheme(); // http
String serverName = request.getServerName(); // hostname.com
int serverPort = request.getServerPort(); // 80
String contextPath = request.getContextPath(); // /mywebapp
// Reconstruct original requesting URL
String url = scheme + "://" + serverName + ":" + serverPort + contextPath;
response.setHeader("location", url + "/session/" + session);
}
BeanToJsonConverter converter = new BeanToJsonConverter();
String s = converter.convert(resp);
// status is also used for debugging, it's worth formatting it nice.
if (req.getGenericCommand() == WebDriverLikeCommand.STATUS) {
JSONObject o = new JSONObject(s);
response.getWriter().print(o.toString(2));
} else {
response.getWriter().print(s);
}