Skip to content

Commit a4a9535

Browse files
committed
[java] Refactoring code to reduce duplication
1 parent 8b79835 commit a4a9535

1 file changed

Lines changed: 16 additions & 22 deletions

File tree

java/client/src/org/openqa/selenium/remote/NewSessionPayload.java

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -213,22 +213,13 @@ public void writeTo(Appendable appendable) throws IOException {
213213

214214
Map<String, Object> first = getOss();
215215
if (first == null) {
216-
//noinspection unchecked
217216
first = stream().findFirst()
218-
.orElse(new ImmutableCapabilities())
219-
.asMap();
217+
.orElse(new ImmutableCapabilities())
218+
.asMap();
220219
}
221220
Map<String, Object> ossFirst = new HashMap<>(first);
222221
if (first.containsKey(CapabilityType.PROXY)) {
223-
Map<String, Object> proxyMap;
224-
Object rawProxy = first.get(CapabilityType.PROXY);
225-
if (rawProxy instanceof Proxy) {
226-
proxyMap = ((Proxy) rawProxy).toJson();
227-
} else if (rawProxy instanceof Map) {
228-
proxyMap = (Map<String, Object>) rawProxy;
229-
} else {
230-
proxyMap = new HashMap<>();
231-
}
222+
Map<String, Object> proxyMap = getProxyFromCapabilities(first);
232223
if (proxyMap.containsKey("noProxy")) {
233224
Map<String, Object> ossProxyMap = new HashMap<>(proxyMap);
234225
Object rawData = proxyMap.get("noProxy");
@@ -252,7 +243,6 @@ public void writeTo(Appendable appendable) throws IOException {
252243
// "alwaysMatch" field, so we do this.
253244
json.name("firstMatch");
254245
json.beginArray();
255-
//noinspection unchecked
256246
getW3C().forEach(json::write);
257247
json.endArray();
258248

@@ -264,6 +254,18 @@ public void writeTo(Appendable appendable) throws IOException {
264254
}
265255
}
266256

257+
private Map<String, Object> getProxyFromCapabilities(Map<String, Object> capabilities) {
258+
Object rawProxy = capabilities.get(CapabilityType.PROXY);
259+
if (rawProxy instanceof Proxy) {
260+
return ((Proxy) rawProxy).toJson();
261+
} else if (rawProxy instanceof Map) {
262+
//noinspection unchecked
263+
return (Map<String, Object>) rawProxy;
264+
} else {
265+
return new HashMap<>();
266+
}
267+
}
268+
267269
private void writeMetaData(JsonOutput out) throws IOException {
268270
CharSource charSource = backingStore.asByteSource().asCharSource(UTF_8);
269271
try (Reader reader = charSource.openBufferedStream();
@@ -426,15 +428,7 @@ private Map<String, Object> convertOssToW3C(Map<String, Object> capabilities) {
426428
}
427429

428430
if (capabilities.containsKey(PROXY)) {
429-
Map<String, Object> proxyMap;
430-
Object rawProxy = capabilities.get(CapabilityType.PROXY);
431-
if (rawProxy instanceof Proxy) {
432-
proxyMap = ((Proxy) rawProxy).toJson();
433-
} else if (rawProxy instanceof Map) {
434-
proxyMap = (Map<String, Object>) rawProxy;
435-
} else {
436-
proxyMap = new HashMap<>();
437-
}
431+
Map<String, Object> proxyMap = getProxyFromCapabilities(capabilities);
438432
if (proxyMap.containsKey("noProxy")) {
439433
Map<String, Object> w3cProxyMap = new HashMap<>(proxyMap);
440434
Object rawData = proxyMap.get("noProxy");

0 commit comments

Comments
 (0)