Skip to content

Commit 279c3cd

Browse files
author
Chan, Kingston a | BANKB
committed
Converted JSON lib from org.json to com.google.gson to match the latest version of selenium-server-standalone
1 parent d6d9867 commit 279c3cd

File tree

6 files changed

+61
-57
lines changed

6 files changed

+61
-57
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
*.jar
66
*.war
77
*.ear
8+
target

pom.xml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
<groupId>selenium-grid2-api</groupId>
88
<artifactId>selenium-grid2-api</artifactId>
9-
<version>1.0-SNAPSHOT</version>
9+
<version>1.0.1</version>
1010

1111
<dependencies>
1212
<dependency>
1313
<groupId>org.seleniumhq.selenium</groupId>
1414
<artifactId>selenium-server</artifactId>
15-
<version>2.32.0</version>
15+
<version>2.53.0</version>
1616
</dependency>
1717
<dependency>
1818
<groupId>junit</groupId>
@@ -36,5 +36,18 @@
3636
</dependency>
3737
</dependencies>
3838

39+
<build>
40+
<plugins>
41+
<plugin>
42+
<groupId>org.apache.maven.plugins</groupId>
43+
<artifactId>maven-compiler-plugin</artifactId>
44+
<version>3.5.1</version>
45+
<configuration>
46+
<source>1.7</source>
47+
<target>1.7</target>
48+
</configuration>
49+
</plugin>
50+
</plugins>
51+
</build>
3952

4053
</project>

src/main/java/com/seleniumgrid2api/api/GridInfo.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.seleniumgrid2api.api;
22

3-
import org.json.JSONException;
4-
import org.json.JSONObject;
3+
import com.google.gson.JsonObject;
54

65
import java.net.MalformedURLException;
76
import java.net.URL;
@@ -50,20 +49,20 @@ public String getSuccess() {
5049
return success;
5150
}
5251

53-
public GridInfo(JSONObject object) {
52+
public GridInfo(JsonObject object) {
5453
try {
55-
proxyId = new URL(object.getString("proxyId"));
54+
proxyId = new URL(object.get("proxyId").getAsString());
5655
if ((proxyId.getHost() != null) && (proxyId.getPort() != -1)) {
5756
host = proxyId.getHost();
5857
port = proxyId.getPort();
5958
}
60-
internalKey = object.getString("internalKey");
61-
session = object.getString("session");
62-
inactivityTime = object.getString("inactivityTime");
63-
msg = object.getString("msg");
64-
success = object.getString("success");
59+
internalKey = object.get("internalKey").getAsString();
60+
session = object.get("session").getAsString();
61+
inactivityTime = object.get("inactivityTime").getAsString();
62+
msg = object.get("msg").getAsString();
63+
success = object.get("success").getAsString();
6564

66-
} catch (MalformedURLException | JSONException e) {
65+
} catch (MalformedURLException e) {
6766
System.out.println("Error Parsing Grid Info.");
6867
}
6968

src/main/java/com/seleniumgrid2api/api/GridInfoExtractor.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import org.apache.http.HttpStatus;
66
import org.apache.http.impl.client.DefaultHttpClient;
77
import org.apache.http.message.BasicHttpEntityEnclosingRequest;
8-
import org.json.JSONException;
9-
import org.json.JSONObject;
8+
import com.google.gson.JsonObject;
9+
import com.google.gson.JsonParser;
1010
import org.openqa.selenium.remote.SessionId;
1111

1212
import java.io.BufferedReader;
@@ -30,26 +30,27 @@ public static GridInfo getHostNameAndPort(String hubHost, int hubPort, SessionId
3030
BasicHttpEntityEnclosingRequest basicHttpEntityEnclosingRequest = new BasicHttpEntityEnclosingRequest("POST", sessionURL.toExternalForm());
3131
HttpResponse response = client.execute(host, basicHttpEntityEnclosingRequest);
3232
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
33-
JSONObject object = extractObject(response);
33+
JsonObject object = extractObject(response);
3434
retVal = new GridInfo(object);
3535
} else {
3636
System.out.println("Problem connecting to Grid Server");
3737
}
38-
} catch (JSONException | IOException e) {
38+
} catch (IOException e) {
3939
throw new RuntimeException("Failed to acquire remote webdriver node and port info", e);
4040
}
4141
return retVal;
4242
}
4343

44-
private static JSONObject extractObject(HttpResponse resp) throws IOException, JSONException {
44+
private static JsonObject extractObject(HttpResponse resp) throws IOException {
4545
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()))) {
4646
StringBuilder stringBuilder = new StringBuilder();
4747
String line;
4848
while ((line = bufferedReader.readLine()) != null) {
4949
stringBuilder.append(line);
5050
}
5151
bufferedReader.close();
52-
return new JSONObject(stringBuilder.toString());
52+
JsonParser parser = new JsonParser();
53+
return (JsonObject) parser.parse(stringBuilder.toString());
5354
} catch (Exception e) {
5455
System.out.println("error" + e.toString());
5556
}

src/main/java/com/seleniumgrid2api/servlet/AllProxiesJsonServlet.java

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.seleniumgrid2api.servlet;
22

33

4-
import org.json.JSONArray;
5-
import org.json.JSONException;
6-
import org.json.JSONObject;
4+
import com.google.gson.JsonArray;
5+
import com.google.gson.JsonObject;
6+
import com.google.gson.Gson;
7+
import com.google.gson.GsonBuilder;
78
import org.openqa.grid.common.exception.GridException;
89
import org.openqa.grid.internal.ProxySet;
910
import org.openqa.grid.internal.Registry;
@@ -43,29 +44,23 @@ protected void process(HttpServletRequest request, HttpServletResponse response)
4344
response.setContentType("text/json");
4445
response.setCharacterEncoding("UTF-8");
4546
response.setStatus(200);
46-
JSONObject res;
47-
try {
48-
res = getResponse();
49-
response.getWriter().print(res.toString(4));
50-
response.getWriter().close();
51-
} catch (JSONException e) {
52-
throw new GridException(e.getMessage());
53-
}
54-
47+
Gson gson = new GsonBuilder().setPrettyPrinting().create();
48+
response.getWriter().print(gson.toJson(getResponse()));
49+
response.getWriter().close();
5550
}
5651

57-
private JSONObject getResponse() throws IOException, JSONException {
58-
JSONObject requestJSON = new JSONObject();
52+
private JsonObject getResponse() throws IOException {
53+
JsonObject requestJSON = new JsonObject();
5954
ProxySet proxies = this.getRegistry().getAllProxies();
6055
Iterator<RemoteProxy> iterator = proxies.iterator();
61-
JSONArray p = new JSONArray();
56+
JsonArray p = new JsonArray();
6257
while (iterator.hasNext()) {
6358
RemoteProxy eachProxy = iterator.next();
64-
p.put(eachProxy.getOriginalRegistrationRequest().getAssociatedJSON());
59+
JsonObject proxyInfo = eachProxy.getOriginalRegistrationRequest().getAssociatedJSON();
60+
p.add(proxyInfo);
6561
}
66-
requestJSON.put("Proxies", p);
62+
requestJSON.add("Proxies", p);
6763

6864
return requestJSON;
6965
}
70-
7166
}

src/main/java/com/seleniumgrid2api/servlet/ProxyStatusJsonServlet.java

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package com.seleniumgrid2api.servlet;
22

3-
import org.json.JSONArray;
4-
import org.json.JSONException;
5-
import org.json.JSONObject;
3+
import com.google.gson.JsonArray;
4+
import com.google.gson.JsonObject;
5+
import com.google.gson.Gson;
6+
import com.google.gson.GsonBuilder;
67
import org.openqa.grid.common.exception.GridException;
78
import org.openqa.grid.internal.ProxySet;
89
import org.openqa.grid.internal.Registry;
@@ -42,35 +43,29 @@ protected void process(HttpServletRequest request, HttpServletResponse response)
4243
response.setContentType("text/json");
4344
response.setCharacterEncoding("UTF-8");
4445
response.setStatus(200);
45-
JSONObject res;
46-
try {
47-
res = getResponse();
48-
response.getWriter().print(res.toString(4));
49-
response.getWriter().close();
50-
} catch (JSONException e) {
51-
throw new GridException(e.getMessage());
52-
}
53-
46+
Gson gson = new GsonBuilder().setPrettyPrinting().create();
47+
response.getWriter().print(gson.toJson(getResponse()));
48+
response.getWriter().close();
5449
}
5550

56-
private JSONObject getResponse() throws IOException, JSONException {
57-
JSONObject requestJSON = new JSONObject();
51+
private JsonObject getResponse() throws IOException {
52+
JsonObject requestJSON = new JsonObject();
5853
ProxySet proxies = this.getRegistry().getAllProxies();
5954
Iterator<RemoteProxy> iterator = proxies.iterator();
60-
JSONArray busyProxies = new JSONArray();
61-
JSONArray freeProxies = new JSONArray();
55+
JsonArray busyProxies = new JsonArray();
56+
JsonArray freeProxies = new JsonArray();
6257
while (iterator.hasNext()) {
6358
RemoteProxy eachProxy = iterator.next();
59+
JsonObject proxyInfo = eachProxy.getOriginalRegistrationRequest().getAssociatedJSON();
6460
if (eachProxy.isBusy()) {
65-
busyProxies.put(eachProxy.getOriginalRegistrationRequest().getAssociatedJSON());
61+
busyProxies.add(proxyInfo);
6662
} else {
67-
freeProxies.put(eachProxy.getOriginalRegistrationRequest().getAssociatedJSON());
63+
freeProxies.add(proxyInfo);
6864
}
6965
}
70-
requestJSON.put("BusyProxies", busyProxies);
71-
requestJSON.put("FreeProxies", freeProxies);
66+
requestJSON.add("BusyProxies", busyProxies);
67+
requestJSON.add("FreeProxies", freeProxies);
7268

7369
return requestJSON;
7470
}
75-
7671
}

0 commit comments

Comments
 (0)