Skip to content

Commit 7ff25ba

Browse files
committed
adding some sample servlets
1 parent c867e34 commit 7ff25ba

File tree

3 files changed

+150
-2
lines changed

3 files changed

+150
-2
lines changed

src/main/java/com/seleniumgrid2api/StartGrid.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
public class StartGrid {
66

77
public static void main(String[] args) throws Exception {
8-
String[] a = {"-port", "4455",
8+
String[] a = {"-port", "4444",
99
"-host", "localhost",
10-
"-role", "hub"};
10+
"-role", "hub",
11+
"-servlets", "com.seleniumgrid2api.servlet.AllProxiesJsonServlet,com.seleniumgrid2api.servlet.ProxyStatusJsonServlet" };
1112
GridLauncher.main(a);
1213
}
1314
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.seleniumgrid2api.servlet;
2+
3+
4+
import org.json.JSONArray;
5+
import org.json.JSONException;
6+
import org.json.JSONObject;
7+
import org.openqa.grid.common.exception.GridException;
8+
import org.openqa.grid.internal.ProxySet;
9+
import org.openqa.grid.internal.Registry;
10+
import org.openqa.grid.internal.RemoteProxy;
11+
import org.openqa.grid.web.servlet.RegistryBasedServlet;
12+
13+
import javax.servlet.ServletException;
14+
import javax.servlet.http.HttpServletRequest;
15+
import javax.servlet.http.HttpServletResponse;
16+
import java.io.IOException;
17+
import java.util.Iterator;
18+
19+
public class AllProxiesJsonServlet extends RegistryBasedServlet {
20+
21+
private static final long serialVersionUID = -1975392591408983229L;
22+
23+
public AllProxiesJsonServlet() {
24+
this(null);
25+
}
26+
27+
public AllProxiesJsonServlet(Registry registry) {
28+
super(registry);
29+
}
30+
31+
@Override
32+
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
33+
doPost(req, resp);
34+
}
35+
36+
@Override
37+
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
38+
process(req, resp);
39+
40+
}
41+
42+
protected void process(HttpServletRequest request, HttpServletResponse response) throws IOException {
43+
response.setContentType("text/json");
44+
response.setCharacterEncoding("UTF-8");
45+
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+
55+
}
56+
57+
private JSONObject getResponse() throws IOException, JSONException {
58+
JSONObject requestJSON = new JSONObject();
59+
ProxySet proxies = this.getRegistry().getAllProxies();
60+
Iterator<RemoteProxy> iterator = proxies.iterator();
61+
JSONArray p = new JSONArray();
62+
while (iterator.hasNext()) {
63+
RemoteProxy eachProxy = iterator.next();
64+
p.put(eachProxy.getOriginalRegistrationRequest().getAssociatedJSON());
65+
}
66+
requestJSON.put("Proxies", p);
67+
68+
return requestJSON;
69+
}
70+
71+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.seleniumgrid2api.servlet;
2+
3+
import org.json.JSONArray;
4+
import org.json.JSONException;
5+
import org.json.JSONObject;
6+
import org.openqa.grid.common.exception.GridException;
7+
import org.openqa.grid.internal.ProxySet;
8+
import org.openqa.grid.internal.Registry;
9+
import org.openqa.grid.internal.RemoteProxy;
10+
import org.openqa.grid.web.servlet.RegistryBasedServlet;
11+
12+
import javax.servlet.ServletException;
13+
import javax.servlet.http.HttpServletRequest;
14+
import javax.servlet.http.HttpServletResponse;
15+
import java.io.IOException;
16+
import java.util.Iterator;
17+
18+
public class ProxyStatusJsonServlet extends RegistryBasedServlet {
19+
20+
private static final long serialVersionUID = -1975392591408983229L;
21+
22+
public ProxyStatusJsonServlet() {
23+
this(null);
24+
}
25+
26+
public ProxyStatusJsonServlet(Registry registry) {
27+
super(registry);
28+
}
29+
30+
@Override
31+
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
32+
doPost(req, resp);
33+
}
34+
35+
@Override
36+
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
37+
process(req, resp);
38+
39+
}
40+
41+
protected void process(HttpServletRequest request, HttpServletResponse response) throws IOException {
42+
response.setContentType("text/json");
43+
response.setCharacterEncoding("UTF-8");
44+
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+
54+
}
55+
56+
private JSONObject getResponse() throws IOException, JSONException {
57+
JSONObject requestJSON = new JSONObject();
58+
ProxySet proxies = this.getRegistry().getAllProxies();
59+
Iterator<RemoteProxy> iterator = proxies.iterator();
60+
JSONArray busyProxies = new JSONArray();
61+
JSONArray freeProxies = new JSONArray();
62+
while (iterator.hasNext()) {
63+
RemoteProxy eachProxy = iterator.next();
64+
if (eachProxy.isBusy()) {
65+
busyProxies.put(eachProxy.getOriginalRegistrationRequest().getAssociatedJSON());
66+
} else {
67+
freeProxies.put(eachProxy.getOriginalRegistrationRequest().getAssociatedJSON());
68+
}
69+
}
70+
requestJSON.put("BusyProxies", busyProxies);
71+
requestJSON.put("FreeProxies", freeProxies);
72+
73+
return requestJSON;
74+
}
75+
76+
}

0 commit comments

Comments
 (0)