diff --git a/java/server/src/org/openqa/grid/web/servlet/HubStatusServlet.java b/java/server/src/org/openqa/grid/web/servlet/HubStatusServlet.java index c674675564527..b32205db7e1de 100644 --- a/java/server/src/org/openqa/grid/web/servlet/HubStatusServlet.java +++ b/java/server/src/org/openqa/grid/web/servlet/HubStatusServlet.java @@ -17,26 +17,27 @@ package org.openqa.grid.web.servlet; -import com.google.common.io.CharStreams; -import com.google.gson.Gson; +import static org.openqa.selenium.json.Json.MAP_TYPE; + +import com.google.common.collect.ImmutableSortedMap; import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import com.google.gson.JsonSyntaxException; -import org.openqa.grid.common.exception.GridException; import org.openqa.grid.internal.GridRegistry; import org.openqa.grid.internal.RemoteProxy; +import org.openqa.selenium.json.Json; +import org.openqa.selenium.json.JsonException; +import org.openqa.selenium.json.JsonInput; +import org.openqa.selenium.json.JsonOutput; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; -import java.util.ArrayList; +import java.io.Writer; import java.util.Arrays; import java.util.List; import java.util.Map; +import java.util.TreeMap; -import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -62,6 +63,8 @@ */ public class HubStatusServlet extends RegistryBasedServlet { + private final Json json = new Json(); + public HubStatusServlet() { super(null); } @@ -72,13 +75,12 @@ public HubStatusServlet(GridRegistry registry) { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { + throws IOException { process(request, response); } @Override - protected void doPost(HttpServletRequest req, HttpServletResponse resp) - throws ServletException, IOException { + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { process(req, resp); } @@ -87,56 +89,54 @@ protected void process(HttpServletRequest request, HttpServletResponse response) response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.setStatus(200); - JsonObject res; - try { - res = getResponse(request); - response.getWriter().print(res); - response.getWriter().close(); - } catch (JsonSyntaxException e) { - throw new GridException(e.getMessage()); + try (Writer writer = response.getWriter(); + JsonOutput out = json.newOutput(writer)) { + Map res = getResponse(request); + out.write(res, MAP_TYPE); } - } - private JsonObject getResponse(HttpServletRequest request) throws IOException { - JsonObject res = new JsonObject(); - res.addProperty("success", true); + private Map getResponse(HttpServletRequest request) { + Map res = new TreeMap<>(); + res.put("success", true); + try { if (request.getInputStream() != null) { - JsonObject requestJSON = getRequestJSON(request); + Map requestJSON = getRequestJSON(request); List keysToReturn = null; if (request.getParameter("configuration") != null && !"".equals(request.getParameter("configuration"))) { keysToReturn = Arrays.asList(request.getParameter("configuration").split(",")); - } else if (requestJSON != null && requestJSON.has("configuration")) { - keysToReturn = new Gson().fromJson(requestJSON.getAsJsonArray("configuration"), ArrayList.class); + } else if (requestJSON != null && requestJSON.containsKey("configuration")) { + //noinspection unchecked + keysToReturn = (List) requestJSON.get("configuration"); } GridRegistry registry = getRegistry(); JsonElement config = registry.getConfiguration().toJson(); for (Map.Entry entry : config.getAsJsonObject().entrySet()) { if (keysToReturn == null || keysToReturn.isEmpty() || keysToReturn.contains(entry.getKey())) { - res.add(entry.getKey(), entry.getValue()); + res.put(entry.getKey(), entry.getValue()); } } if (keysToReturn == null || keysToReturn.isEmpty() || keysToReturn.contains("newSessionRequestCount")) { - res.addProperty("newSessionRequestCount", registry.getNewSessionRequestCount()); + res.put("newSessionRequestCount", registry.getNewSessionRequestCount()); } if (keysToReturn == null || keysToReturn.isEmpty() || keysToReturn.contains("slotCounts")) { - res.add("slotCounts", getSlotCounts()); + res.put("slotCounts", getSlotCounts()); } } } catch (Exception e) { res.remove("success"); - res.addProperty("success", false); - res.addProperty("msg", e.getMessage()); + res.put("success", false); + res.put("msg", e.getMessage()); } return res; } - private JsonObject getSlotCounts() { + private Map getSlotCounts() { int totalSlots = 0; int usedSlots = 0; @@ -144,23 +144,19 @@ private JsonObject getSlotCounts() { totalSlots += Math.min(proxy.getMaxNumberOfConcurrentTestSessions(), proxy.getTestSlots().size()); usedSlots += proxy.getTotalUsed(); } - JsonObject result = new JsonObject(); - result.addProperty("free", totalSlots - usedSlots); - result.addProperty("total", totalSlots); - return result; - } - private JsonObject getRequestJSON(HttpServletRequest request) throws IOException { - JsonObject requestJSON = new JsonObject(); + return ImmutableSortedMap.of( + "free", totalSlots - usedSlots, + "total", totalSlots); + } - try (BufferedReader rd = new BufferedReader(new InputStreamReader(request.getInputStream()))) { - StringBuilder s = new StringBuilder(); - CharStreams.copy(rd, s); - String json = s.toString(); - if (!"".equals(json)) { - requestJSON = new JsonParser().parse(json).getAsJsonObject(); - } + private Map getRequestJSON(HttpServletRequest request) throws IOException { + Json json = new Json(); + try (BufferedReader rd = new BufferedReader(new InputStreamReader(request.getInputStream())); + JsonInput jin = json.newInput(rd)) { + return jin.read(MAP_TYPE); + } catch (JsonException e) { + throw new IOException(e); } - return requestJSON; } } diff --git a/java/server/src/org/openqa/grid/web/servlet/TestSessionStatusServlet.java b/java/server/src/org/openqa/grid/web/servlet/TestSessionStatusServlet.java index 101861d5ee287..316fefc35d6f5 100644 --- a/java/server/src/org/openqa/grid/web/servlet/TestSessionStatusServlet.java +++ b/java/server/src/org/openqa/grid/web/servlet/TestSessionStatusServlet.java @@ -17,29 +17,32 @@ package org.openqa.grid.web.servlet; -import com.google.common.io.CharStreams; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import com.google.gson.JsonSyntaxException; +import static org.openqa.selenium.json.Json.MAP_TYPE; import org.openqa.grid.common.exception.GridException; import org.openqa.grid.internal.ExternalSessionKey; import org.openqa.grid.internal.GridRegistry; import org.openqa.grid.internal.RemoteProxy; import org.openqa.grid.internal.TestSession; +import org.openqa.selenium.json.Json; +import org.openqa.selenium.json.JsonException; +import org.openqa.selenium.json.JsonInput; +import org.openqa.selenium.json.JsonOutput; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; +import java.io.Writer; +import java.util.Map; +import java.util.TreeMap; -import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class TestSessionStatusServlet extends RegistryBasedServlet { - private static final long serialVersionUID = 4325112892618707612L; + private final Json json = new Json(); public TestSessionStatusServlet() { super(null); @@ -51,13 +54,13 @@ public TestSessionStatusServlet(GridRegistry registry) { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { + throws IOException { process(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { + throws IOException { process(request, response); } @@ -66,59 +69,53 @@ protected void process(HttpServletRequest request, HttpServletResponse response) response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.setStatus(200); - JsonObject res; - try { - res = getResponse(request); - response.getWriter().print(res); - response.getWriter().close(); - } catch (JsonSyntaxException e) { + try (Writer writer = response.getWriter(); + JsonOutput out = json.newOutput(writer)){ + out.write(getResponse(request), MAP_TYPE); + } catch (JsonException e) { throw new GridException(e.getMessage()); } } - private JsonObject getResponse(HttpServletRequest request) throws IOException { - JsonObject requestJSON = null; + private Map getResponse(HttpServletRequest request) throws IOException { + Map requestJSON = null; if (request.getInputStream() != null) { - String json; - try (Reader rd = new BufferedReader(new InputStreamReader(request.getInputStream()))) { - json = CharStreams.toString(rd); - } - if (!"".equals(json)) { - requestJSON = new JsonParser().parse(json).getAsJsonObject(); + try (Reader rd = new BufferedReader(new InputStreamReader(request.getInputStream())); + JsonInput jin = json.newInput(rd)) { + requestJSON = jin.read(MAP_TYPE); } } - JsonObject res = new JsonObject(); - res.addProperty("success", false); + Map res = new TreeMap<>(); + res.put("success", false); // the id can be specified via a param, or in the json request. String session; if (requestJSON == null) { session = request.getParameter("session"); } else { - if (!requestJSON.has("session")) { - res.addProperty("msg", + if (!requestJSON.containsKey("session")) { + res.put("msg", "you need to specify at least a session or internalKey when call the test slot status service."); return res; } - session = requestJSON.get("session").getAsString(); + session = String.valueOf(requestJSON.get("session")); } TestSession testSession = getRegistry().getSession(ExternalSessionKey.fromString(session)); if (testSession == null) { - res.addProperty("msg", "Cannot find test slot running session " + session + " in the registry."); + res.put("msg", "Cannot find test slot running session " + session + " in the registry."); return res; } - res.addProperty("msg", "slot found !"); + res.put("msg", "slot found !"); res.remove("success"); - res.addProperty("success", true); - res.addProperty("session", testSession.getExternalKey().getKey()); - res.addProperty("internalKey", testSession.getInternalKey()); - res.addProperty("inactivityTime", testSession.getInactivityTime()); + res.put("success", true); + res.put("session", testSession.getExternalKey().getKey()); + res.put("internalKey", testSession.getInternalKey()); + res.put("inactivityTime", testSession.getInactivityTime()); RemoteProxy p = testSession.getSlot().getProxy(); - res.addProperty("proxyId", p.getId()); + res.put("proxyId", p.getId()); return res; } - }