Skip to content

Instantly share code, notes, and snippets.

@krmahadevan
Created August 9, 2012 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save krmahadevan/3302497 to your computer and use it in GitHub Desktop.
Save krmahadevan/3302497 to your computer and use it in GitHub Desktop.
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
Copy link

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