Skip to content

Commit

Permalink
[java] Refactoring code to reduce duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Nov 20, 2020
1 parent 8b79835 commit a4a9535
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions java/client/src/org/openqa/selenium/remote/NewSessionPayload.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,22 +213,13 @@ public void writeTo(Appendable appendable) throws IOException {

Map<String, Object> first = getOss();
if (first == null) {
//noinspection unchecked
first = stream().findFirst()
.orElse(new ImmutableCapabilities())
.asMap();
.orElse(new ImmutableCapabilities())
.asMap();
}
Map<String, Object> ossFirst = new HashMap<>(first);
if (first.containsKey(CapabilityType.PROXY)) {
Map<String, Object> proxyMap;
Object rawProxy = first.get(CapabilityType.PROXY);
if (rawProxy instanceof Proxy) {
proxyMap = ((Proxy) rawProxy).toJson();
} else if (rawProxy instanceof Map) {
proxyMap = (Map<String, Object>) rawProxy;
} else {
proxyMap = new HashMap<>();
}
Map<String, Object> proxyMap = getProxyFromCapabilities(first);
if (proxyMap.containsKey("noProxy")) {
Map<String, Object> ossProxyMap = new HashMap<>(proxyMap);
Object rawData = proxyMap.get("noProxy");
Expand All @@ -252,7 +243,6 @@ public void writeTo(Appendable appendable) throws IOException {
// "alwaysMatch" field, so we do this.
json.name("firstMatch");
json.beginArray();
//noinspection unchecked
getW3C().forEach(json::write);
json.endArray();

Expand All @@ -264,6 +254,18 @@ public void writeTo(Appendable appendable) throws IOException {
}
}

private Map<String, Object> getProxyFromCapabilities(Map<String, Object> capabilities) {
Object rawProxy = capabilities.get(CapabilityType.PROXY);
if (rawProxy instanceof Proxy) {
return ((Proxy) rawProxy).toJson();
} else if (rawProxy instanceof Map) {
//noinspection unchecked
return (Map<String, Object>) rawProxy;
} else {
return new HashMap<>();
}
}

private void writeMetaData(JsonOutput out) throws IOException {
CharSource charSource = backingStore.asByteSource().asCharSource(UTF_8);
try (Reader reader = charSource.openBufferedStream();
Expand Down Expand Up @@ -426,15 +428,7 @@ private Map<String, Object> convertOssToW3C(Map<String, Object> capabilities) {
}

if (capabilities.containsKey(PROXY)) {
Map<String, Object> proxyMap;
Object rawProxy = capabilities.get(CapabilityType.PROXY);
if (rawProxy instanceof Proxy) {
proxyMap = ((Proxy) rawProxy).toJson();
} else if (rawProxy instanceof Map) {
proxyMap = (Map<String, Object>) rawProxy;
} else {
proxyMap = new HashMap<>();
}
Map<String, Object> proxyMap = getProxyFromCapabilities(capabilities);
if (proxyMap.containsKey("noProxy")) {
Map<String, Object> w3cProxyMap = new HashMap<>(proxyMap);
Object rawData = proxyMap.get("noProxy");
Expand Down

0 comments on commit a4a9535

Please sign in to comment.