[grid] Move the Grid DriverServlet to use our JSON abstraction
import com.google.common.base.Throwables;
import com.google.common.io.ByteStreams;
-import com.google.gson.Gson;
-import com.google.gson.JsonArray;
-import com.google.gson.JsonObject;
import org.openqa.grid.internal.ExternalSessionKey;
import org.openqa.grid.internal.GridRegistry;
import org.openqa.grid.web.servlet.handler.RequestHandler;
import org.openqa.grid.web.servlet.handler.SeleniumBasedRequest;
import org.openqa.grid.web.servlet.handler.WebDriverRequest;
+import org.openqa.selenium.json.Json;
import org.openqa.selenium.remote.ErrorCodes;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
@@ -92,37 +94,37 @@ protected void process(HttpServletRequest request, HttpServletResponse response)
response.setCharacterEncoding("UTF-8");
response.setStatus(500);
- JsonObject resp = new JsonObject();
+ Map<String, Object> resp = new TreeMap<>();
// https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol#error-handling
final ExternalSessionKey serverSession = req.getServerSession();
- resp.addProperty("sessionId", serverSession != null ? serverSession.getKey() : null);
- resp.addProperty("status", ErrorCodes.UNHANDLED_ERROR);
- JsonObject value = new JsonObject();
- value.addProperty("message", e.getMessage());
- value.addProperty("class", e.getClass().getCanonicalName());
+ resp.put("sessionId", serverSession != null ? serverSession.getKey() : null);
+ resp.put("status", ErrorCodes.UNHANDLED_ERROR);
+ Map<String, Object> value = new TreeMap<>();
+ value.put("message", e.getMessage());
+ value.put("class", e.getClass().getCanonicalName());
- JsonArray stacktrace = new JsonArray();
+ List<Object> stacktrace = new LinkedList<>();
for (StackTraceElement ste : e.getStackTrace()) {
- JsonObject st = new JsonObject();
- st.addProperty("fileName", ste.getFileName());
- st.addProperty("className", ste.getClassName());
- st.addProperty("methodName", ste.getMethodName());
- st.addProperty("lineNumber", ste.getLineNumber());
+ Map<String, Object> st = new TreeMap<>();
+ st.put("fileName", ste.getFileName());
+ st.put("className", ste.getClassName());
+ st.put("methodName", ste.getMethodName());
+ st.put("lineNumber", ste.getLineNumber());
stacktrace.add(st);
}
- value.add("stackTrace", stacktrace);
+ value.put("stackTrace", stacktrace);
// https://w3c.github.io/webdriver/webdriver-spec.html#dfn-send-an-error
- value.addProperty("error", "unknown error");
+ value.put("error", "unknown error");
// Already done above, but kept here for when we retire the original protocol
- value.addProperty("message", e.getMessage());
+ value.put("message", e.getMessage());
// Let's hope nothing ever looks at these strings case insensitively.
- value.addProperty("stacktrace", Throwables.getStackTraceAsString(e));
+ value.put("stacktrace", Throwables.getStackTraceAsString(e));
- resp.add("value", value);
+ resp.put("value", value);
- String json = new Gson().toJson(resp);
+ String json = new Json().toJson(resp);
byte[] bytes = json.getBytes("UTF-8");
try (InputStream in = new ByteArrayInputStream(bytes)) {
