public void beforeSession(TestSession session) {
   Map<String, Object> cap = session.getRequestedCapabilities();
   if ("firefox".equals(cap.get(CapabilityType.BROWSER_NAME))) {
     if (session.getSlot().getCapabilities().get(FirefoxDriver.BINARY) != null
         && cap.get(FirefoxDriver.BINARY) == null) {
       session
           .getRequestedCapabilities()
           .put(
               FirefoxDriver.BINARY,
               session.getSlot().getCapabilities().get(FirefoxDriver.BINARY));
     }
   }
 }
  private JsonObject getResponse(HttpServletRequest request) throws IOException {
    JsonObject requestJSON = null;
    if (request.getInputStream() != null) {
      BufferedReader rd = new BufferedReader(new InputStreamReader(request.getInputStream()));
      StringBuilder s = new StringBuilder();
      String line;
      while ((line = rd.readLine()) != null) {
        s.append(line);
      }
      rd.close();
      String json = s.toString();
      if (!"".equals(json)) {
        requestJSON = new JsonParser().parse(json).getAsJsonObject();
      }
    }

    JsonObject res = new JsonObject();
    res.addProperty("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",
            "you need to specify at least a session or internalKey when call the test slot status service.");
        return res;
      }
      session = requestJSON.get("session").getAsString();
    }

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

    if (testSession == null) {
      res.addProperty(
          "msg", "Cannot find test slot running session " + session + " in the registry.");
      return res;
    }
    res.addProperty("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());
    RemoteProxy p = testSession.getSlot().getProxy();
    res.addProperty("proxyId", p.getId());
    return res;
  }
  /**
   * The client shouldn't have to care where firefox is installed as long as the correct version is
   * launched, however with webdriver the binary location is specified in the desiredCapability,
   * making it the responsibility of the person running the test.
   *
   * <p>With this implementation of beforeSession, that problem disappears . If the webdriver slot
   * is registered with a firefox using a custom binary location, the hub will handle it.
   *
   * <p>For instance if a node registers:
   * {"browserName":"firefox","version":"7.0","firefox_binary":"/home/ff7"}
   *
   * <p>and later on a client requests {"browserName":"firefox","version":"7.0"} , the hub will
   * automatically append the correct binary path to the desiredCapability before it's forwarded to
   * the server. That way the version / install location mapping is done only once at the node
   * level.
   */
  public void beforeSession(TestSession session) {
    if (session.getSlot().getProtocol() == SeleniumProtocol.WebDriver) {
      Map<String, Object> cap = session.getRequestedCapabilities();

      if (BrowserType.FIREFOX.equals(cap.get(CapabilityType.BROWSER_NAME))) {
        if (session.getSlot().getCapabilities().get(FirefoxDriver.BINARY) != null
            && cap.get(FirefoxDriver.BINARY) == null) {
          session
              .getRequestedCapabilities()
              .put(
                  FirefoxDriver.BINARY,
                  session.getSlot().getCapabilities().get(FirefoxDriver.BINARY));
        }
      }

      if (BrowserType.CHROME.equals(cap.get(CapabilityType.BROWSER_NAME))) {
        if (session.getSlot().getCapabilities().get("chrome_binary") != null) {
          JSONObject options = (JSONObject) cap.get(ChromeOptions.CAPABILITY);
          if (options == null) {
            options = new JSONObject();
          }
          try {
            options.put("binary", session.getSlot().getCapabilities().get("chrome_binary"));
          } catch (JSONException e) {
            e.printStackTrace();
          }
          cap.put(ChromeOptions.CAPABILITY, options);
        }
      }

      if (BrowserType.OPERA.equals(cap.get(CapabilityType.BROWSER_NAME))) {
        if (session.getSlot().getCapabilities().get("opera_binary") != null
            && cap.get("opera.binary") == null) {
          session
              .getRequestedCapabilities()
              .put("opera.binary", session.getSlot().getCapabilities().get("opera_binary"));
        }
      }
    }
  }