Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converted JSON lib from org.json to com.google.gson #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
*.jar
*.war
*.ear
target
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
==================

Expand Down
17 changes: 15 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

<groupId>selenium-grid2-api</groupId>
<artifactId>selenium-grid2-api</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.0.1</version>

<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>2.32.0</version>
<version>2.53.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand All @@ -36,5 +36,18 @@
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

</project>
19 changes: 9 additions & 10 deletions src/main/java/com/seleniumgrid2api/api/GridInfo.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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.");
}

Expand Down
13 changes: 7 additions & 6 deletions src/main/java/com/seleniumgrid2api/api/GridInfoExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,26 +30,27 @@ 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;
while ((line = bufferedReader.readLine()) != null) {
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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<RemoteProxy> 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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<RemoteProxy> 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;
}

}