Skip to content

Commit

Permalink
Move element converter so it's only used in RemoteWebDriver
Browse files Browse the repository at this point in the history
Furrfu.
  • Loading branch information
shs96c committed Sep 1, 2017
1 parent d80f86d commit c8f6bf9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
Expand Down Expand Up @@ -558,7 +559,7 @@ public Options manage() {
}

protected void setElementConverter(JsonToWebElementConverter converter) {
this.converter = converter;
this.converter = Objects.requireNonNull(converter, "Element converter must not be null");
}

protected JsonToWebElementConverter getElementConverter() {
Expand Down Expand Up @@ -593,7 +594,7 @@ protected Response execute(String driverCommand, Map<String, ?> parameters) {

// Unwrap the response value by converting any JSON objects of the form
// {"ELEMENT": id} to RemoteWebElements.
Object value = converter.apply(response.getValue());
Object value = getElementConverter().apply(response.getValue());
response.setValue(value);
} catch (WebDriverException e) {
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@
import org.openqa.selenium.remote.CommandCodec;
import org.openqa.selenium.remote.JsonToBeanConverter;
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.remote.internal.JsonToWebElementConverter;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -124,7 +123,6 @@ public abstract class AbstractHttpCommandCodec implements CommandCodec<HttpReque
private final Map<String, String> aliases = new HashMap<>();
private final BeanToJsonConverter beanToJsonConverter = new BeanToJsonConverter();
private final JsonToBeanConverter jsonToBeanConverter = new JsonToBeanConverter();
private final JsonToWebElementConverter elementConverter = new JsonToWebElementConverter(null);

public AbstractHttpCommandCodec() {
defineCommand(STATUS, get("/status"));
Expand Down Expand Up @@ -271,8 +269,6 @@ public Command decode(final HttpRequest encodedCommand) {
if (!content.isEmpty()) {
@SuppressWarnings("unchecked")
Map<String, ?> tmp = jsonToBeanConverter.convert(HashMap.class, content);
//noinspection unchecked
tmp = (Map<String, ?>) elementConverter.apply(tmp);
parameters.putAll(tmp);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;

class HttpMessage {

Expand Down Expand Up @@ -75,8 +76,12 @@ public Iterable<String> getHeaders(String name) {
}

public String getHeader(String name) {
Collection<String> values = headers.get(name);
return values.isEmpty() ? null : values.iterator().next();
return headers.entries().stream()
.filter(e -> Objects.nonNull(e.getKey()))
.filter(e -> e.getKey().toLowerCase().equals(name.toLowerCase()))
.map(Map.Entry::getValue)
.findFirst()
.orElse(null);
}

public void setHeader(String name, String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;
import org.openqa.selenium.remote.internal.ApacheHttpClient;
import org.openqa.selenium.remote.internal.JsonToWebElementConverter;

import java.io.IOException;
import java.net.URL;
import java.util.Map;

class ProtocolConverter implements SessionCodec {

Expand All @@ -51,6 +53,7 @@ class ProtocolConverter implements SessionCodec {
private final CommandCodec<HttpRequest> upstream;
private final ResponseCodec<HttpResponse> downstreamResponse;
private final ResponseCodec<HttpResponse> upstreamResponse;
private final JsonToWebElementConverter converter;

public ProtocolConverter(
URL upstreamUrl,
Expand All @@ -64,11 +67,20 @@ public ProtocolConverter(
this.upstreamResponse = upstreamResponse;

client = new ApacheHttpClient.Factory().createClient(upstreamUrl);
converter = new JsonToWebElementConverter(null);
}

@Override
public void handle(HttpRequest req, HttpResponse resp) throws IOException {
Command command = downstream.decode(req);
// Massage the webelements
@SuppressWarnings("unchecked")
Map<String, ?> parameters = (Map<String, ?>) converter.apply(command.getParameters());
command = new Command(
command.getSessionId(),
command.getName(),
parameters);

HttpRequest request = upstream.encode(command);

HttpResponse res = makeRequest(request);
Expand Down

0 comments on commit c8f6bf9

Please sign in to comment.