From 01a056529df92ebbda43be50c03b0cc3126e5863 Mon Sep 17 00:00:00 2001 From: Kingston Chan Date: Fri, 27 May 2016 12:16:24 +0900 Subject: [PATCH 1/2] Converted JSON lib from org.json to com.google.gson to match the latest version of selenium-server-standalone --- .gitignore | 1 + pom.xml | 17 ++++++++- .../com/seleniumgrid2api/api/GridInfo.java | 19 +++++----- .../api/GridInfoExtractor.java | 13 ++++--- .../servlet/AllProxiesJsonServlet.java | 31 +++++++--------- .../servlet/ProxyStatusJsonServlet.java | 37 ++++++++----------- 6 files changed, 61 insertions(+), 57 deletions(-) diff --git a/.gitignore b/.gitignore index 2c23055..65b06c9 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ *.jar *.war *.ear +target diff --git a/pom.xml b/pom.xml index 83108e9..f6591f4 100644 --- a/pom.xml +++ b/pom.xml @@ -6,13 +6,13 @@ selenium-grid2-api selenium-grid2-api - 1.0-SNAPSHOT + 1.0.1 org.seleniumhq.selenium selenium-server - 2.32.0 + 2.53.0 junit @@ -36,5 +36,18 @@ + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 1.7 + 1.7 + + + + \ No newline at end of file diff --git a/src/main/java/com/seleniumgrid2api/api/GridInfo.java b/src/main/java/com/seleniumgrid2api/api/GridInfo.java index e368dde..00f011e 100644 --- a/src/main/java/com/seleniumgrid2api/api/GridInfo.java +++ b/src/main/java/com/seleniumgrid2api/api/GridInfo.java @@ -1,7 +1,6 @@ package com.seleniumgrid2api.api; -import org.json.JSONException; -import org.json.JSONObject; +import com.google.gson.JsonObject; import java.net.MalformedURLException; import java.net.URL; @@ -50,20 +49,20 @@ public String getSuccess() { return success; } - public GridInfo(JSONObject object) { + public GridInfo(JsonObject object) { try { - proxyId = new URL(object.getString("proxyId")); + proxyId = new URL(object.get("proxyId").getAsString()); if ((proxyId.getHost() != null) && (proxyId.getPort() != -1)) { host = proxyId.getHost(); port = proxyId.getPort(); } - internalKey = object.getString("internalKey"); - session = object.getString("session"); - inactivityTime = object.getString("inactivityTime"); - msg = object.getString("msg"); - success = object.getString("success"); + internalKey = object.get("internalKey").getAsString(); + session = object.get("session").getAsString(); + inactivityTime = object.get("inactivityTime").getAsString(); + msg = object.get("msg").getAsString(); + success = object.get("success").getAsString(); - } catch (MalformedURLException | JSONException e) { + } catch (MalformedURLException e) { System.out.println("Error Parsing Grid Info."); } diff --git a/src/main/java/com/seleniumgrid2api/api/GridInfoExtractor.java b/src/main/java/com/seleniumgrid2api/api/GridInfoExtractor.java index 76a75d4..b388dab 100644 --- a/src/main/java/com/seleniumgrid2api/api/GridInfoExtractor.java +++ b/src/main/java/com/seleniumgrid2api/api/GridInfoExtractor.java @@ -5,8 +5,8 @@ import org.apache.http.HttpStatus; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicHttpEntityEnclosingRequest; -import org.json.JSONException; -import org.json.JSONObject; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import org.openqa.selenium.remote.SessionId; import java.io.BufferedReader; @@ -30,18 +30,18 @@ public static GridInfo getHostNameAndPort(String hubHost, int hubPort, SessionId BasicHttpEntityEnclosingRequest basicHttpEntityEnclosingRequest = new BasicHttpEntityEnclosingRequest("POST", sessionURL.toExternalForm()); HttpResponse response = client.execute(host, basicHttpEntityEnclosingRequest); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { - JSONObject object = extractObject(response); + JsonObject object = extractObject(response); retVal = new GridInfo(object); } else { System.out.println("Problem connecting to Grid Server"); } - } catch (JSONException | IOException e) { + } catch (IOException e) { throw new RuntimeException("Failed to acquire remote webdriver node and port info", e); } return retVal; } - private static JSONObject extractObject(HttpResponse resp) throws IOException, JSONException { + private static JsonObject extractObject(HttpResponse resp) throws IOException { try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()))) { StringBuilder stringBuilder = new StringBuilder(); String line; @@ -49,7 +49,8 @@ private static JSONObject extractObject(HttpResponse resp) throws IOException, J stringBuilder.append(line); } bufferedReader.close(); - return new JSONObject(stringBuilder.toString()); + JsonParser parser = new JsonParser(); + return (JsonObject) parser.parse(stringBuilder.toString()); } catch (Exception e) { System.out.println("error" + e.toString()); } diff --git a/src/main/java/com/seleniumgrid2api/servlet/AllProxiesJsonServlet.java b/src/main/java/com/seleniumgrid2api/servlet/AllProxiesJsonServlet.java index 52b6d09..36e1735 100644 --- a/src/main/java/com/seleniumgrid2api/servlet/AllProxiesJsonServlet.java +++ b/src/main/java/com/seleniumgrid2api/servlet/AllProxiesJsonServlet.java @@ -1,9 +1,10 @@ package com.seleniumgrid2api.servlet; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import org.openqa.grid.common.exception.GridException; import org.openqa.grid.internal.ProxySet; import org.openqa.grid.internal.Registry; @@ -43,29 +44,23 @@ protected void process(HttpServletRequest request, HttpServletResponse response) response.setContentType("text/json"); response.setCharacterEncoding("UTF-8"); response.setStatus(200); - JSONObject res; - try { - res = getResponse(); - response.getWriter().print(res.toString(4)); - response.getWriter().close(); - } catch (JSONException e) { - throw new GridException(e.getMessage()); - } - + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + response.getWriter().print(gson.toJson(getResponse())); + response.getWriter().close(); } - private JSONObject getResponse() throws IOException, JSONException { - JSONObject requestJSON = new JSONObject(); + private JsonObject getResponse() throws IOException { + JsonObject requestJSON = new JsonObject(); ProxySet proxies = this.getRegistry().getAllProxies(); Iterator iterator = proxies.iterator(); - JSONArray p = new JSONArray(); + JsonArray p = new JsonArray(); while (iterator.hasNext()) { RemoteProxy eachProxy = iterator.next(); - p.put(eachProxy.getOriginalRegistrationRequest().getAssociatedJSON()); + JsonObject proxyInfo = eachProxy.getOriginalRegistrationRequest().getAssociatedJSON(); + p.add(proxyInfo); } - requestJSON.put("Proxies", p); + requestJSON.add("Proxies", p); return requestJSON; } - } \ No newline at end of file diff --git a/src/main/java/com/seleniumgrid2api/servlet/ProxyStatusJsonServlet.java b/src/main/java/com/seleniumgrid2api/servlet/ProxyStatusJsonServlet.java index 1025ca6..be48452 100644 --- a/src/main/java/com/seleniumgrid2api/servlet/ProxyStatusJsonServlet.java +++ b/src/main/java/com/seleniumgrid2api/servlet/ProxyStatusJsonServlet.java @@ -1,8 +1,9 @@ package com.seleniumgrid2api.servlet; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import org.openqa.grid.common.exception.GridException; import org.openqa.grid.internal.ProxySet; import org.openqa.grid.internal.Registry; @@ -42,35 +43,29 @@ protected void process(HttpServletRequest request, HttpServletResponse response) response.setContentType("text/json"); response.setCharacterEncoding("UTF-8"); response.setStatus(200); - JSONObject res; - try { - res = getResponse(); - response.getWriter().print(res.toString(4)); - response.getWriter().close(); - } catch (JSONException e) { - throw new GridException(e.getMessage()); - } - + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + response.getWriter().print(gson.toJson(getResponse())); + response.getWriter().close(); } - private JSONObject getResponse() throws IOException, JSONException { - JSONObject requestJSON = new JSONObject(); + private JsonObject getResponse() throws IOException { + JsonObject requestJSON = new JsonObject(); ProxySet proxies = this.getRegistry().getAllProxies(); Iterator iterator = proxies.iterator(); - JSONArray busyProxies = new JSONArray(); - JSONArray freeProxies = new JSONArray(); + JsonArray busyProxies = new JsonArray(); + JsonArray freeProxies = new JsonArray(); while (iterator.hasNext()) { RemoteProxy eachProxy = iterator.next(); + JsonObject proxyInfo = eachProxy.getOriginalRegistrationRequest().getAssociatedJSON(); if (eachProxy.isBusy()) { - busyProxies.put(eachProxy.getOriginalRegistrationRequest().getAssociatedJSON()); + busyProxies.add(proxyInfo); } else { - freeProxies.put(eachProxy.getOriginalRegistrationRequest().getAssociatedJSON()); + freeProxies.add(proxyInfo); } } - requestJSON.put("BusyProxies", busyProxies); - requestJSON.put("FreeProxies", freeProxies); + requestJSON.add("BusyProxies", busyProxies); + requestJSON.add("FreeProxies", freeProxies); return requestJSON; } - } From a5d4b027625f72bfe93680672932540a48717cbe Mon Sep 17 00:00:00 2001 From: Kingston Chan Date: Tue, 19 Jul 2016 15:12:17 +0900 Subject: [PATCH 2/2] Added extra info to readme --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/README.md b/README.md index f1ea880..221c586 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,23 @@ +# kgston/selenium-grid2-api + +This is a fork of [nicegraham/selenium-grid2-api](https://github.com/nicegraham/selenium-grid2-api) + +This extension provides Selenium Grid 2 with additional APIs to retrieve all connected nodes and their information. + +## Modifications + +1. Fixed the JSON library incompatability between the latest version of Selenium Grid 2.53.0 and this extension + +## Build + +1. Install Maven + 1. Set up and required proxy settings if required +1. Run from the project root `mvn clean install` in the command line + +## Usage + +1. When running the Selenium Grid 2 Hub, append to the command `-servlets "com.seleniumgrid2api.servlet.AllProxiesJsonServlet,com.seleniumgrid2api.servlet.ProxyStatusJsonServlet"` + selenium-grid2-api ==================