...23import org.openqa.selenium.remote.Response;24import org.openqa.selenium.remote.ResponseCodec;25import org.openqa.selenium.remote.codec.jwp.JsonHttpCommandCodec;26import org.openqa.selenium.remote.codec.jwp.JsonHttpResponseCodec;27import org.openqa.selenium.remote.codec.w3c.W3CHttpCommandCodec;28import org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec;29import org.openqa.selenium.remote.http.HttpClient;30import org.openqa.selenium.remote.http.HttpRequest;31import org.openqa.selenium.remote.http.HttpResponse;32import org.openqa.selenium.remote.internal.JsonToWebElementConverter;33import java.io.IOException;34import java.util.Map;35import java.util.Objects;36public class ProtocolConverter implements CommandHandler {37 private final static ImmutableSet<String> IGNORED_REQ_HEADERS = ImmutableSet.<String>builder()38 .add("connection")39 .add("keep-alive")40 .add("proxy-authorization")41 .add("proxy-authenticate")42 .add("proxy-connection")43 .add("te")44 .add("trailer")45 .add("transfer-encoding")46 .add("upgrade")47 .build();48 private final HttpClient client;49 private final CommandCodec<HttpRequest> downstream;50 private final CommandCodec<HttpRequest> upstream;51 private final ResponseCodec<HttpResponse> downstreamResponse;52 private final ResponseCodec<HttpResponse> upstreamResponse;53 private final JsonToWebElementConverter converter;54 public ProtocolConverter(55 HttpClient client,56 Dialect downstream,57 Dialect upstream) {58 this.client = Objects.requireNonNull(client);59 Objects.requireNonNull(downstream);60 this.downstream = getCommandCodec(downstream);61 this.downstreamResponse = getResponseCodec(downstream);62 Objects.requireNonNull(upstream);63 this.upstream = getCommandCodec(upstream);64 this.upstreamResponse = getResponseCodec(upstream);65 converter = new JsonToWebElementConverter(null);66 }67 @Override68 public void execute(HttpRequest req, HttpResponse resp) throws IOException {69 Command command = downstream.decode(req);70 // Massage the webelements71 @SuppressWarnings("unchecked")72 Map<String, ?> parameters = (Map<String, ?>) converter.apply(command.getParameters());73 command = new Command(74 command.getSessionId(),75 command.getName(),76 parameters);77 HttpRequest request = upstream.encode(command);78 HttpResponse res = makeRequest(request);79 Response decoded = upstreamResponse.decode(res);80 HttpResponse response = downstreamResponse.encode(HttpResponse::new, decoded);81 copyToServletResponse(response, resp);82 }83 @VisibleForTesting84 HttpResponse makeRequest(HttpRequest request) throws IOException {85 return client.execute(request);86 }87 private void copyToServletResponse(HttpResponse response, HttpResponse resp) {88 resp.setStatus(response.getStatus());89 for (String name : response.getHeaderNames()) {90 if (IGNORED_REQ_HEADERS.contains(name.toLowerCase())) {91 continue;92 }93 for (String value : response.getHeaders(name)) {94 resp.addHeader(name, value);95 }96 }97 resp.setContent(response.getContent());98 }99 private CommandCodec<HttpRequest> getCommandCodec(Dialect dialect) {100 switch (dialect) {101 case OSS:102 return new JsonHttpCommandCodec();103 case W3C:104 return new W3CHttpCommandCodec();105 default:106 throw new IllegalStateException("Unknown dialect: " + dialect);107 }108 }109 private ResponseCodec<HttpResponse> getResponseCodec(Dialect dialect) {110 switch (dialect) {111 case OSS:112 return new JsonHttpResponseCodec();113 case W3C:114 return new W3CHttpResponseCodec();115 default:116 throw new IllegalStateException("Unknown dialect: " + dialect);117 }118 }...