org.openqa.grid.web.servlet.console.ConsoleServlet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of selenium-server Show documentation
Show all versions of selenium-server Show documentation
Selenium automates browsers. That's it! What you do with that power is entirely up to you.
// 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.console;
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.BuildInfo;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ConsoleServlet extends RegistryBasedServlet {
private static final long serialVersionUID = 8484071790930378855L;
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 IOException {
process(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws 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 = proxy.getHtmlRender();
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());
builder.append(getConfigInfo());
builder.append(" ");
builder.append("");
builder.append("");
try (InputStream in = new ByteArrayInputStream(builder.toString().getBytes("UTF-8"))) {
ByteStreams.copy(in, response.getOutputStream());
} finally {
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("Selenium
");
builder.append("Grid Console v.");
builder.append(coreVersion);
builder.append("
");
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() {
StringBuilder builder = new StringBuilder();
builder.append("");
GridHubConfiguration config = getRegistry().getHub().getConfiguration();
builder.append("");
builder.append("Config for the hub :
");
builder.append(prettyHtmlPrint(config));
builder.append(getVerboseConfig()); // Display verbose configuration details
builder.append(""); // End of Config Content
// Display View/Hide Link at the bottom beneath the details
builder.append("View Config");
builder.append(""); // End of Config Container
return builder.toString();
}
/**
* Displays more detailed configuration
* @return html representation of the verbose hub config
*/
private String getVerboseConfig() {
StringBuilder builder = new StringBuilder();
GridHubConfiguration config = getRegistry().getHub().getConfiguration();
builder.append("");
builder.append("View Verbose");
builder.append("");
GridHubConfiguration tmp = new GridHubConfiguration();
builder.append("
The final configuration comes from:
");
builder.append("the default :
");
builder.append(prettyHtmlPrint(tmp));
if (config.getRawArgs() != null) {
builder.append("updated with command line options:
");
builder.append(String.join(" ", config.getRawArgs()));
if (config.getConfigFile() != null) {
builder.append("
and configuration loaded from ").append(config.getConfigFile()).append(":
");
try {
builder.append(String.join("
", Files.readAllLines(new File(config.getConfigFile()).toPath())));
} catch (IOException e) {
builder.append("").append(e.getMessage()).append("");
}
}
}
builder.append(""); // End of Verbose Content
builder.append(""); // End of Verbose Container
return builder.toString();
}
private String prettyHtmlPrint(GridHubConfiguration config) {
return config.toString("%1$s : %2$s");
}
}