All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openqa.grid.web.servlet.beta.ConsoleServlet Maven / Gradle / Ivy

Go to download

Selenium automates browsers. That's it! What you do with that power is entirely up to you.

There is a newer version: 4.0.0-alpha-2
Show newest version
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.grid.web.servlet.beta;

import com.google.common.io.ByteStreams;

import org.openqa.grid.internal.GridRegistry;
import org.openqa.grid.internal.RemoteProxy;
import org.openqa.grid.internal.utils.HtmlRenderer;
import org.openqa.grid.internal.utils.configuration.GridHubConfiguration;
import org.openqa.grid.web.servlet.RegistryBasedServlet;
import org.openqa.selenium.internal.BuildInfo;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

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

public class ConsoleServlet extends RegistryBasedServlet {

  private static final long serialVersionUID = 8484071790930378855L;
  private static final Logger log = Logger.getLogger(ConsoleServlet.class.getName());
  private static String coreVersion;

  public static final String CONSOLE_PATH_PARAMETER = "webdriver.server.consoleservlet.path";

  public ConsoleServlet() {
    this(null);
  }

  public ConsoleServlet(GridRegistry registry) {
    super(registry);
    coreVersion = new BuildInfo().getReleaseLabel();
  }

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

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

  protected void process(HttpServletRequest request, HttpServletResponse response)
      throws IOException {

    int refresh = -1;

    if (request.getParameter("refresh") != null) {
      try {
        refresh = Integer.parseInt(request.getParameter("refresh"));
      } catch (NumberFormatException e) {
        // ignore wrong param
      }

    }

    response.setContentType("text/html");
    response.setCharacterEncoding("UTF-8");
    response.setStatus(200);

    StringBuilder builder = new StringBuilder();

    builder.append("");
    builder.append("");
    builder
        .append("");

    builder.append("");

    builder
        .append("");
    builder
        .append("");


    if (refresh != -1) {
      builder.append(String.format("", refresh));
    }
    builder.append("Grid Console");

    builder.append("");
    builder.append("");

    builder.append("");

    builder.append("
"); builder.append(getHeader()); // TODO freynaud : registry to return a copy of proxies ? List nodes = new ArrayList<>(); for (RemoteProxy proxy : getRegistry().getAllProxies()) { HtmlRenderer beta = new WebProxyHtmlRendererBeta(proxy); nodes.add(beta.renderSummary()); } int size = nodes.size(); int rightColumnSize = size / 2; int leftColumnSize = size - rightColumnSize; builder.append("
"); for (int i = 0; i < leftColumnSize; i++) { builder.append(nodes.get(i)); } builder.append("
"); builder.append("
"); for (int i = leftColumnSize; i < nodes.size(); i++) { builder.append(nodes.get(i)); } builder.append("
"); builder.append("
"); builder.append(getRequestQueue()); if (request.getParameter("config") != null) { builder.append(getConfigInfo(request.getParameter("configDebug") != null)); } else { builder.append("view config"); } builder.append("
"); builder.append(""); builder.append(""); InputStream in = new ByteArrayInputStream(builder.toString().getBytes("UTF-8")); try { ByteStreams.copy(in, response.getOutputStream()); } finally { in.close(); response.getOutputStream().close(); } } private Object getRequestQueue() { StringBuilder builder = new StringBuilder(); builder.append("
"); int numUnprocessedRequests = getRegistry().getNewSessionRequestCount(); if (numUnprocessedRequests > 0) { builder.append(String.format("%d requests waiting for a slot to be free.", numUnprocessedRequests)); } builder.append("
    "); for (DesiredCapabilities req : getRegistry().getDesiredCapabilities()) { builder.append("
  • ").append(req).append("
  • "); } builder.append("
"); builder.append("
"); return builder.toString(); } private Object getHeader() { StringBuilder builder = new StringBuilder(); builder.append(""); builder.append(""); return builder.toString(); } /** * retracing how the hub config was built to help debugging. * * @return html representation of the hub config */ private String getConfigInfo(boolean verbose) { StringBuilder builder = new StringBuilder(); GridHubConfiguration config = getRegistry().getConfiguration(); builder.append("
"); builder.append("Config for the hub :
"); builder.append(prettyHtmlPrint(config)); if (verbose) { GridHubConfiguration tmp = new GridHubConfiguration(); builder.append("Config details :
"); builder.append("hub launched with :"); builder.append(config.toString()); builder.append("
the final configuration comes from :
"); builder.append("the default :
"); builder.append(prettyHtmlPrint(tmp)); builder.append("
updated with params :
"); tmp.merge(config); builder.append(prettyHtmlPrint(tmp)); } builder.append("
"); return builder.toString(); } private String prettyHtmlPrint(GridHubConfiguration config) { return config.toString("%1$s : %2$s
"); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy