Skip to content

Commit

Permalink
[java] Remove Json Wire Protocol support (#11823)
Browse files Browse the repository at this point in the history
* [java] Remove support for the original Json Wire Protocol

* cp: remote jwp from new session new session payload

* cp: make the driver command encode capabilities as a set in all cases

* cp: remove `desiredCapabilities` from encoded new session command

* cp: continuing to remove desired capabilities

* cp: start removing usages of `Dialect.OSS`

* cp: making w3c the default protocol

* cp: start ripping out protocol conversion

* cp: removing protocol conversion

* cp: finish removing `Dialect.OSS` from the tree

* [java] Refining proxyType to fix browser tests

* [java] Fixing proxy tests

* [grid] Removing platform before sending payload to driver

---------

Co-authored-by: Diego Molina <[email protected]>
Co-authored-by: Diego Molina <[email protected]>
  • Loading branch information
3 people authored Apr 5, 2023
1 parent ca50360 commit f28f1df
Show file tree
Hide file tree
Showing 40 changed files with 150 additions and 2,240 deletions.
25 changes: 18 additions & 7 deletions java/src/org/openqa/selenium/Proxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,27 @@ public enum ProxyType {
// Keep these in sync with the Firefox preferences numbers:
// http://kb.mozillazine.org/Network.proxy.type

DIRECT, // Direct connection, no proxy (default on Windows)
MANUAL, // Manual proxy settings (e.g. for httpProxy)
PAC, // Proxy auto-configuration from URL
DIRECT("direct"), // Direct connection, no proxy (default on Windows)
MANUAL("manual"), // Manual proxy settings (e.g. for httpProxy)
PAC("pac"), // Proxy auto-configuration from URL

RESERVED_1, // Never used (but reserved in Firefox)
RESERVED_1("reserved_1"), // Never used (but reserved in Firefox)

AUTODETECT, // Proxy auto-detection (presumably with WPAD)
SYSTEM, // Use system settings (default on Linux)
AUTODETECT("autodetect"), // Proxy auto-detection (presumably with WPAD)
SYSTEM("system"), // Use system settings (default on Linux)

UNSPECIFIED
UNSPECIFIED("unspecified");

private final String type;

ProxyType(String type) {
this.type = type;
}

@Override
public String toString() {
return String.valueOf(type);
}
}

private static final String PROXY_TYPE = "proxyType";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.remote.Dialect;
import org.openqa.selenium.remote.ErrorCodes;
import org.openqa.selenium.remote.SessionId;

import java.util.Map;
Expand All @@ -36,8 +35,6 @@
public class CapabilityResponseEncoder {

private static final Json JSON = new Json();
private static final ResponseEncoder<Session, Map<String, Object>, byte[]> JWP_ENCODER =
new Encoder(Dialect.OSS);
private static final ResponseEncoder<Session, Map<String, Object>, byte[]> W3C_ENCODER =
new Encoder(Dialect.W3C);

Expand All @@ -47,9 +44,6 @@ private CapabilityResponseEncoder() {

public static ResponseEncoder<Session, Map<String, Object>, byte[]> getEncoder(Dialect dialect) {
switch (dialect) {
case OSS:
return JWP_ENCODER;

case W3C:
return W3C_ENCODER;

Expand Down Expand Up @@ -108,10 +102,6 @@ private static byte[] encodeAsResponse(
Map<String, Object> toEncode;

switch (dialect) {
case OSS:
toEncode = encodeJsonWireProtocol(id, capabilities, metadata);
break;

case W3C:
toEncode = encodeW3C(id, capabilities, metadata);
break;
Expand All @@ -134,19 +124,5 @@ private static Map<String, Object> encodeW3C(
"capabilities", capabilities))
.build();
}

private static Map<String, Object> encodeJsonWireProtocol(
SessionId id,
Capabilities capabilities,
Map<String, Object> metadata) {
return ImmutableMap.<String, Object>builder()
.putAll(metadata)
.put("status", ErrorCodes.SUCCESS)
.put("sessionId", id)
.put("value", capabilities)
.build();

}

}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
// 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.selenium.grid.node;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.grid.web.ProtocolConverter;
import org.openqa.selenium.grid.web.ReverseProxyHandler;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.remote.Dialect;
Expand All @@ -29,19 +11,20 @@
import org.openqa.selenium.remote.http.HttpResponse;
import org.openqa.selenium.remote.tracing.Tracer;

import java.io.UncheckedIOException;
import java.net.URL;
import java.time.Instant;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.openqa.selenium.remote.http.HttpMethod.DELETE;

public abstract class ProtocolConvertingSession extends BaseActiveSession {
public class DefaultActiveSession extends BaseActiveSession {

private final HttpHandler handler;
private final String killUrl;

protected ProtocolConvertingSession(
protected DefaultActiveSession(
Tracer tracer,
HttpClient client,
SessionId id,
Expand All @@ -55,17 +38,12 @@ protected ProtocolConvertingSession(

Require.nonNull("HTTP client", client);

if (downstream.equals(upstream)) {
this.handler = new ReverseProxyHandler(tracer, client);
} else {
this.handler = new ProtocolConverter(tracer, client, downstream, upstream);
}

this.handler = new ReverseProxyHandler(tracer, client);
this.killUrl = "/session/" + id;
}

@Override
public HttpResponse execute(HttpRequest req) {
public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
String host = "host";
StreamSupport.stream(req.getHeaderNames().spliterator(), true)
.filter(host::equalsIgnoreCase)
Expand All @@ -78,4 +56,9 @@ public HttpResponse execute(HttpRequest req) {
}
return res;
}

@Override
public void stop() {
// no-op
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.PersistentCapabilities;
import org.openqa.selenium.Platform;
import org.openqa.selenium.SessionNotCreatedException;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.devtools.CdpEndpointFinder;
import org.openqa.selenium.grid.data.CreateSessionRequest;
import org.openqa.selenium.grid.node.ActiveSession;
import org.openqa.selenium.grid.node.ProtocolConvertingSession;
import org.openqa.selenium.grid.node.DefaultActiveSession;
import org.openqa.selenium.grid.node.SessionFactory;
import org.openqa.selenium.internal.Either;
import org.openqa.selenium.internal.Require;
Expand Down Expand Up @@ -119,7 +120,7 @@ public Either<WebDriverException, ActiveSession> apply(CreateSessionRequest sess

Optional<Platform> platformName = Optional.ofNullable(capabilities.getPlatformName());
if (platformName.isPresent()) {
capabilities = generalizePlatform(capabilities);
capabilities = removePlatform(capabilities);
}

CAPABILITIES.accept(span, capabilities);
Expand Down Expand Up @@ -171,7 +172,7 @@ public Either<WebDriverException, ActiveSession> apply(CreateSessionRequest sess

span.addEvent("Driver service created session", attributeMap);
return Either.right(
new ProtocolConvertingSession(
new DefaultActiveSession(
tracer,
client,
new SessionId(response.getSessionId()),
Expand Down Expand Up @@ -293,10 +294,12 @@ private Capabilities readVncEndpoint(Capabilities requestedCaps, Capabilities re
return returnedCaps;
}

// We set the platform to ANY before sending the caps to the driver because some drivers will
// We remove the platform before sending the caps to the driver because some drivers will
// reject session requests when they cannot parse the platform.
private Capabilities generalizePlatform(Capabilities caps) {
return new PersistentCapabilities(caps).setCapability("platformName", Platform.ANY);
private Capabilities removePlatform(Capabilities caps) {
MutableCapabilities noPlatformName = new MutableCapabilities(new HashMap<>(caps.asMap()));
noPlatformName.setCapability("platformName", (String) null);
return new PersistentCapabilities(noPlatformName);
}

private Capabilities setInitialPlatform(Capabilities caps, Platform platform) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.docker.Container;
import org.openqa.selenium.grid.node.ProtocolConvertingSession;
import org.openqa.selenium.grid.node.DefaultActiveSession;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.remote.Dialect;
import org.openqa.selenium.remote.SessionId;
Expand All @@ -35,7 +35,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;

public class DockerSession extends ProtocolConvertingSession {
public class DockerSession extends DefaultActiveSession {

private static final Logger LOG = Logger.getLogger(DockerSession.class.getName());
private final Container container;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.grid.data.CreateSessionRequest;
import org.openqa.selenium.grid.node.ActiveSession;
import org.openqa.selenium.grid.node.ProtocolConvertingSession;
import org.openqa.selenium.grid.node.DefaultActiveSession;
import org.openqa.selenium.grid.node.SessionFactory;
import org.openqa.selenium.internal.Debug;
import org.openqa.selenium.internal.Either;
Expand Down Expand Up @@ -172,7 +172,7 @@ public Either<WebDriverException, ActiveSession> apply(CreateSessionRequest sess

span.addEvent("Relay service created session", attributeMap);
LOG.fine(String.format("Created session: %s - %s", response.getSessionId(), capabilities));
return Either.right(new ProtocolConvertingSession(
return Either.right(new DefaultActiveSession(
tracer,
client,
new SessionId(response.getSessionId()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.grid.session.ActiveSession;
import org.openqa.selenium.grid.session.SessionFactory;
import org.openqa.selenium.grid.web.ProtocolConverter;
import org.openqa.selenium.grid.web.ReverseProxyHandler;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.io.TemporaryFilesystem;
Expand Down Expand Up @@ -52,8 +51,6 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import static org.openqa.selenium.remote.Dialect.OSS;

/**
* Abstract class designed to do things like protocol conversion.
*/
Expand Down Expand Up @@ -150,8 +147,11 @@ protected Optional<ActiveSession> performHandshake(
codec = new ReverseProxyHandler(tracer, client);
downstream = upstream;
} else {
downstream = downstreamDialects.isEmpty() ? OSS : downstreamDialects.iterator().next();
codec = new ProtocolConverter(tracer, client, downstream, upstream);
LOG.warning(String.format(
"Unable to match protocol versions. Found %s upstream but can handle %s",
upstream,
downstreamDialects));
return Optional.empty();
}

Response response = result.createResponse();
Expand Down
Loading

0 comments on commit f28f1df

Please sign in to comment.