Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Grid console servlet, that will provide you with all free and busy proxies along with their registration info as a JSON object
package com.test.MyServlets;
import java.io.IOException;
import java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.openqa.grid.common.exception.GridException;
import org.openqa.grid.internal.ProxySet;
import org.openqa.grid.internal.Registry;
import org.openqa.grid.internal.RemoteProxy;
import org.openqa.grid.web.servlet.RegistryBasedServlet;
public class MyConsoleServlet extends RegistryBasedServlet {
/**
*
*/
private static final long serialVersionUID = -1975392591408983229L;
public MyConsoleServlet() {
this(null);
}
public MyConsoleServlet(Registry registry) {
super(registry);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
process(req, resp);
}
protected void process(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/json");
response.setCharacterEncoding("UTF-8");
response.setStatus(200);
JSONObject res;
try {
res = getResponse();
response.getWriter().print(res);
response.getWriter().close();
} catch (JSONException e) {
throw new GridException(e.getMessage());
}
}
private JSONObject getResponse() throws IOException, JSONException {
JSONObject requestJSON = new JSONObject();
ProxySet proxies = this.getRegistry().getAllProxies();
Iterator<RemoteProxy> iterator = proxies.iterator();
JSONArray busyProxies = new JSONArray();
JSONArray freeProxies = new JSONArray();
while (iterator.hasNext()) {
RemoteProxy eachProxy = iterator.next();
if (eachProxy.isBusy()) {
busyProxies.put(eachProxy.getOriginalRegistrationRequest().getAssociatedJSON());
} else {
freeProxies.put(eachProxy.getOriginalRegistrationRequest().getAssociatedJSON());
}
}
requestJSON.put("BusyProxies", busyProxies);
requestJSON.put("FreeProxies", freeProxies);
return requestJSON;
}
}
@praveen133t

This comment has been minimized.

Copy link

praveen133t commented Nov 2, 2017

Just in case if any one is wondering about getAssociatedJSON() method... it's removed. You need to use eachProxy.getOriginalRegistrationRequest().toJson()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.