Skip to content

Commit

Permalink
[grid] More migrations to our own json abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Feb 22, 2018
1 parent 5734158 commit 910549e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 81 deletions.
88 changes: 42 additions & 46 deletions java/server/src/org/openqa/grid/web/servlet/HubStatusServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,27 @@

package org.openqa.grid.web.servlet;

import com.google.common.io.CharStreams;
import com.google.gson.Gson;
import static org.openqa.selenium.json.Json.MAP_TYPE;

import com.google.common.collect.ImmutableSortedMap;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;

import org.openqa.grid.common.exception.GridException;
import org.openqa.grid.internal.GridRegistry;
import org.openqa.grid.internal.RemoteProxy;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.json.JsonException;
import org.openqa.selenium.json.JsonInput;
import org.openqa.selenium.json.JsonOutput;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.io.Writer;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

Expand All @@ -62,6 +63,8 @@
*/
public class HubStatusServlet extends RegistryBasedServlet {

private final Json json = new Json();

public HubStatusServlet() {
super(null);
}
Expand All @@ -72,13 +75,12 @@ public HubStatusServlet(GridRegistry registry) {

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
throws IOException {
process(request, response);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
process(req, resp);
}

Expand All @@ -87,80 +89,74 @@ protected void process(HttpServletRequest request, HttpServletResponse response)
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.setStatus(200);
JsonObject res;
try {
res = getResponse(request);
response.getWriter().print(res);
response.getWriter().close();
} catch (JsonSyntaxException e) {
throw new GridException(e.getMessage());
try (Writer writer = response.getWriter();
JsonOutput out = json.newOutput(writer)) {
Map<String, Object> res = getResponse(request);
out.write(res, MAP_TYPE);
}

}

private JsonObject getResponse(HttpServletRequest request) throws IOException {
JsonObject res = new JsonObject();
res.addProperty("success", true);
private Map<String, Object> getResponse(HttpServletRequest request) {
Map<String, Object> res = new TreeMap<>();
res.put("success", true);

try {
if (request.getInputStream() != null) {
JsonObject requestJSON = getRequestJSON(request);
Map<String, Object> requestJSON = getRequestJSON(request);
List<String> keysToReturn = null;

if (request.getParameter("configuration") != null && !"".equals(request.getParameter("configuration"))) {
keysToReturn = Arrays.asList(request.getParameter("configuration").split(","));
} else if (requestJSON != null && requestJSON.has("configuration")) {
keysToReturn = new Gson().fromJson(requestJSON.getAsJsonArray("configuration"), ArrayList.class);
} else if (requestJSON != null && requestJSON.containsKey("configuration")) {
//noinspection unchecked
keysToReturn = (List<String>) requestJSON.get("configuration");
}

GridRegistry registry = getRegistry();
JsonElement config = registry.getConfiguration().toJson();
for (Map.Entry<String, JsonElement> entry : config.getAsJsonObject().entrySet()) {
if (keysToReturn == null || keysToReturn.isEmpty() || keysToReturn.contains(entry.getKey())) {
res.add(entry.getKey(), entry.getValue());
res.put(entry.getKey(), entry.getValue());
}
}
if (keysToReturn == null || keysToReturn.isEmpty() || keysToReturn.contains("newSessionRequestCount")) {
res.addProperty("newSessionRequestCount", registry.getNewSessionRequestCount());
res.put("newSessionRequestCount", registry.getNewSessionRequestCount());
}

if (keysToReturn == null || keysToReturn.isEmpty() || keysToReturn.contains("slotCounts")) {
res.add("slotCounts", getSlotCounts());
res.put("slotCounts", getSlotCounts());
}
}
} catch (Exception e) {
res.remove("success");
res.addProperty("success", false);
res.addProperty("msg", e.getMessage());
res.put("success", false);
res.put("msg", e.getMessage());
}
return res;

}

private JsonObject getSlotCounts() {
private Map<String, Object> getSlotCounts() {
int totalSlots = 0;
int usedSlots = 0;

for (RemoteProxy proxy : getRegistry().getAllProxies()) {
totalSlots += Math.min(proxy.getMaxNumberOfConcurrentTestSessions(), proxy.getTestSlots().size());
usedSlots += proxy.getTotalUsed();
}
JsonObject result = new JsonObject();
result.addProperty("free", totalSlots - usedSlots);
result.addProperty("total", totalSlots);
return result;
}

private JsonObject getRequestJSON(HttpServletRequest request) throws IOException {
JsonObject requestJSON = new JsonObject();
return ImmutableSortedMap.of(
"free", totalSlots - usedSlots,
"total", totalSlots);
}

try (BufferedReader rd = new BufferedReader(new InputStreamReader(request.getInputStream()))) {
StringBuilder s = new StringBuilder();
CharStreams.copy(rd, s);
String json = s.toString();
if (!"".equals(json)) {
requestJSON = new JsonParser().parse(json).getAsJsonObject();
}
private Map<String, Object> getRequestJSON(HttpServletRequest request) throws IOException {
Json json = new Json();
try (BufferedReader rd = new BufferedReader(new InputStreamReader(request.getInputStream()));
JsonInput jin = json.newInput(rd)) {
return jin.read(MAP_TYPE);
} catch (JsonException e) {
throw new IOException(e);
}
return requestJSON;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,32 @@

package org.openqa.grid.web.servlet;

import com.google.common.io.CharStreams;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import static org.openqa.selenium.json.Json.MAP_TYPE;

import org.openqa.grid.common.exception.GridException;
import org.openqa.grid.internal.ExternalSessionKey;
import org.openqa.grid.internal.GridRegistry;
import org.openqa.grid.internal.RemoteProxy;
import org.openqa.grid.internal.TestSession;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.json.JsonException;
import org.openqa.selenium.json.JsonInput;
import org.openqa.selenium.json.JsonOutput;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Writer;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestSessionStatusServlet extends RegistryBasedServlet {

private static final long serialVersionUID = 4325112892618707612L;
private final Json json = new Json();

public TestSessionStatusServlet() {
super(null);
Expand All @@ -51,13 +54,13 @@ public TestSessionStatusServlet(GridRegistry registry) {

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
throws IOException {
process(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
throws IOException {
process(request, response);
}

Expand All @@ -66,59 +69,53 @@ protected void process(HttpServletRequest request, HttpServletResponse response)
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.setStatus(200);
JsonObject res;
try {
res = getResponse(request);
response.getWriter().print(res);
response.getWriter().close();
} catch (JsonSyntaxException e) {
try (Writer writer = response.getWriter();
JsonOutput out = json.newOutput(writer)){
out.write(getResponse(request), MAP_TYPE);
} catch (JsonException e) {
throw new GridException(e.getMessage());
}
}

private JsonObject getResponse(HttpServletRequest request) throws IOException {
JsonObject requestJSON = null;
private Map<String, Object> getResponse(HttpServletRequest request) throws IOException {
Map<String, Object> requestJSON = null;
if (request.getInputStream() != null) {
String json;
try (Reader rd = new BufferedReader(new InputStreamReader(request.getInputStream()))) {
json = CharStreams.toString(rd);
}
if (!"".equals(json)) {
requestJSON = new JsonParser().parse(json).getAsJsonObject();
try (Reader rd = new BufferedReader(new InputStreamReader(request.getInputStream()));
JsonInput jin = json.newInput(rd)) {
requestJSON = jin.read(MAP_TYPE);
}
}

JsonObject res = new JsonObject();
res.addProperty("success", false);
Map<String, Object> res = new TreeMap<>();
res.put("success", false);

// the id can be specified via a param, or in the json request.
String session;
if (requestJSON == null) {
session = request.getParameter("session");
} else {
if (!requestJSON.has("session")) {
res.addProperty("msg",
if (!requestJSON.containsKey("session")) {
res.put("msg",
"you need to specify at least a session or internalKey when call the test slot status service.");
return res;
}
session = requestJSON.get("session").getAsString();
session = String.valueOf(requestJSON.get("session"));
}

TestSession testSession = getRegistry().getSession(ExternalSessionKey.fromString(session));

if (testSession == null) {
res.addProperty("msg", "Cannot find test slot running session " + session + " in the registry.");
res.put("msg", "Cannot find test slot running session " + session + " in the registry.");
return res;
}
res.addProperty("msg", "slot found !");
res.put("msg", "slot found !");
res.remove("success");
res.addProperty("success", true);
res.addProperty("session", testSession.getExternalKey().getKey());
res.addProperty("internalKey", testSession.getInternalKey());
res.addProperty("inactivityTime", testSession.getInactivityTime());
res.put("success", true);
res.put("session", testSession.getExternalKey().getKey());
res.put("internalKey", testSession.getInternalKey());
res.put("inactivityTime", testSession.getInactivityTime());
RemoteProxy p = testSession.getSlot().getProxy();
res.addProperty("proxyId", p.getId());
res.put("proxyId", p.getId());
return res;
}

}

0 comments on commit 910549e

Please sign in to comment.