[java] Small refactoring of debug server
import static com.google.common.net.MediaType.JSON_UTF_8;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Collections.singletonMap;
-import static org.openqa.selenium.net.PortProber.findFreePort;
import static org.openqa.selenium.testing.InProject.locate;
import com.google.common.collect.ImmutableList;
import org.openqa.selenium.io.TemporaryFilesystem;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.net.NetworkUtils;
+import org.openqa.selenium.net.PortProber;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpMethod;
import org.openqa.selenium.remote.http.HttpRequest;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.util.Optional;
import javax.servlet.Servlet;
@@ -118,8 +119,7 @@ public JettyAppServer(String hostName, int httpPort, int httpsPort) {
TemporaryFilesystem tempFs = TemporaryFilesystem.getDefaultTmpFS();
tempPageDir = tempFs.createTempDir("pages", "test");
- ServletContextHandler tempContext = addResourceHandler(
- TEMP_SRC_CONTEXT_PATH, tempPageDir.toPath());
+ addResourceHandler(TEMP_SRC_CONTEXT_PATH, tempPageDir.toPath());
defaultContext.setInitParameter("tempPageDir", tempPageDir.getAbsolutePath());
defaultContext.setInitParameter("hostname", hostName);
defaultContext.setInitParameter("port", ""+port);
@@ -144,14 +144,16 @@ public JettyAppServer(String hostName, int httpPort, int httpsPort) {
addServlet(defaultContext, "/createPage", CreatePageServlet.class);
}
+ private static Optional<Integer> getEnvValue(String key) {
+ return Optional.ofNullable(System.getenv(key)).map(Integer::parseInt);
+ }
+
private static int getHttpPort() {
- String port = System.getenv(FIXED_HTTP_PORT_ENV_NAME);
- return port == null ? findFreePort() : Integer.parseInt(port);
+ return getEnvValue(FIXED_HTTP_PORT_ENV_NAME).orElseGet(PortProber::findFreePort);
}
private static int getHttpsPort() {
- String port = System.getenv(FIXED_HTTPS_PORT_ENV_NAME);
- return port == null ? findFreePort() : Integer.parseInt(port);
+ return getEnvValue(FIXED_HTTPS_PORT_ENV_NAME).orElseGet(PortProber::findFreePort);
}
@Override
@@ -308,13 +310,11 @@ protected ServletContextHandler addResourceHandler(String contextPath, Path reso
}
protected static int getHttpPortFromEnv() {
- String port = System.getenv(FIXED_HTTP_PORT_ENV_NAME);
- return port == null ? DEFAULT_HTTP_PORT : Integer.parseInt(port);
+ return getEnvValue(FIXED_HTTP_PORT_ENV_NAME).orElse(DEFAULT_HTTP_PORT);
}
protected static int getHttpsPortFromEnv() {
- String port = System.getenv(FIXED_HTTPS_PORT_ENV_NAME);
- return port == null ? DEFAULT_HTTPS_PORT : Integer.parseInt(port);
+ return getEnvValue(FIXED_HTTPS_PORT_ENV_NAME).orElse(DEFAULT_HTTPS_PORT);
}
public static void main(String[] args) {
