Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.openqa.selenium.bidi.network;

import static java.util.Collections.unmodifiableMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -57,11 +59,11 @@ public AddInterceptParameters urlStringPatterns(List<String> patterns) {
}

public Map<String, Object> toMap() {
Map<String, Object> map = new HashMap<>();
map.put("phases", phases);
Map<String, Object> map = new HashMap<>(2);
map.put("phases", List.copyOf(phases));
if (!urlPatterns.isEmpty()) {
map.put("urlPatterns", urlPatterns);
map.put("urlPatterns", List.copyOf(urlPatterns));
}
return Map.copyOf(map);
return unmodifiableMap(map);
}
}
3 changes: 2 additions & 1 deletion java/src/org/openqa/selenium/bidi/network/AuthChallenge.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.openqa.selenium.bidi.network;

import org.openqa.selenium.internal.Require;
import org.openqa.selenium.json.JsonInput;

public class AuthChallenge {
Expand Down Expand Up @@ -49,7 +50,7 @@ public static AuthChallenge fromJson(JsonInput input) {

input.endObject();

return new AuthChallenge(scheme, realm);
return new AuthChallenge(Require.nonNull("scheme", scheme), Require.nonNull("realm", realm));
}

public String getScheme() {
Expand Down
1 change: 1 addition & 0 deletions java/src/org/openqa/selenium/bidi/network/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ java_library(
"//java/src/org/openqa/selenium/json",
"//java/src/org/openqa/selenium/remote/http",
artifact("com.google.auto.service:auto-service-annotations"),
"@maven//:org_jspecify_jspecify",
],
)
39 changes: 23 additions & 16 deletions java/src/org/openqa/selenium/bidi/network/BaseParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,21 @@

import java.util.ArrayList;
import java.util.List;
import org.jspecify.annotations.Nullable;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.json.JsonInput;
import org.openqa.selenium.json.TypeToken;

/**
* @see <a href="https://www.w3.org/TR/webdriver-bidi/#type-network-BaseParameters">BiDi spec</a>
*/
public class BaseParameters {

private final String browsingContextId;
@Nullable private final String browsingContextId;

private final boolean isBlocked;

private final String navigationId;
@Nullable private final String navigationId;

private final long redirectCount;

Expand All @@ -39,35 +44,29 @@ public class BaseParameters {
private final List<String> intercepts;

BaseParameters(
String browsingContextId,
@Nullable String browsingContextId,
boolean isBlocked,
String navigation,
@Nullable String navigation,
long redirectCount,
RequestData request,
long timestamp,
List<String> intercepts) {
this.browsingContextId = browsingContextId;
this.isBlocked = isBlocked;
this.navigationId = navigation;
this.redirectCount = redirectCount;
this.redirectCount = Require.nonNegative("Redirect count", redirectCount);
this.request = request;
this.timestamp = timestamp;
this.timestamp = Require.nonNegative("Timestamp", timestamp);
this.intercepts = intercepts;
}

public static BaseParameters fromJson(JsonInput input) {
String browsingContextId = null;

boolean isBlocked = false;

Boolean isBlocked = null;
String navigationId = null;

long redirectCount = 0;

Long redirectCount = null;
RequestData request = null;

long timestamp = 0;

Long timestamp = null;
List<String> intercepts = new ArrayList<>();

input.beginObject();
Expand Down Expand Up @@ -102,9 +101,16 @@ public static BaseParameters fromJson(JsonInput input) {
input.endObject();

return new BaseParameters(
browsingContextId, isBlocked, navigationId, redirectCount, request, timestamp, intercepts);
browsingContextId,
Require.nonNull("isBlocked", isBlocked),
navigationId,
Require.nonNull("Redirect count", redirectCount),
Require.nonNull("request", request),
Require.nonNull("timestamp", timestamp),
Require.nonNull("intercepts", intercepts));
}

@Nullable
public String getBrowsingContextId() {
return browsingContextId;
}
Expand All @@ -113,6 +119,7 @@ public boolean isBlocked() {
return isBlocked;
}

@Nullable
public String getNavigationId() {
return navigationId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
import org.openqa.selenium.json.Json;
import org.openqa.selenium.json.JsonInput;

/**
* @see <a href="https://www.w3.org/TR/webdriver-bidi/#event-network-beforeSendRequest">BiDi
* spec</a>
*/
public class BeforeRequestSent extends BaseParameters {
private static final Json JSON = new Json();

Expand Down
16 changes: 7 additions & 9 deletions java/src/org/openqa/selenium/bidi/network/BytesValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

package org.openqa.selenium.bidi.network;

import java.util.HashMap;
import java.util.Map;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.json.JsonInput;

/**
* @see <a href="https://www.w3.org/TR/webdriver-bidi/#type-network-BytesValue">BiDi spec</a>
*/
public class BytesValue {

public enum Type {
Expand All @@ -40,7 +43,6 @@ public String toString() {
}

private final Type type;

private final String value;

public BytesValue(Type type, String value) {
Expand All @@ -56,7 +58,7 @@ public static BytesValue fromJson(JsonInput input) {
while (input.hasNext()) {
switch (input.nextName()) {
case "type":
String bytesValue = input.read(String.class);
String bytesValue = input.readNonNull(String.class);
type = bytesValue.equals(Type.BASE64.toString()) ? Type.BASE64 : Type.STRING;
break;
case "value":
Expand All @@ -69,7 +71,7 @@ public static BytesValue fromJson(JsonInput input) {

input.endObject();

return new BytesValue(type, value);
return new BytesValue(Require.nonNull("type", type), Require.nonNull("value", value));
}

public Type getType() {
Expand All @@ -81,10 +83,6 @@ public String getValue() {
}

public Map<String, String> toMap() {
Map<String, String> map = new HashMap<>();
map.put("type", type.toString());
map.put("value", value);

return Map.copyOf(map);
return Map.of("type", type.toString(), "value", value);
}
}
67 changes: 40 additions & 27 deletions java/src/org/openqa/selenium/bidi/network/Cookie.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
// under the License.
package org.openqa.selenium.bidi.network;

import static java.util.Collections.unmodifiableMap;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.jspecify.annotations.Nullable;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.json.JsonInput;

public class Cookie {
Expand All @@ -40,36 +44,34 @@ public String toString() {
}

public static SameSite findByName(String name) {
SameSite result = null;
for (SameSite type : values()) {
if (type.toString().equalsIgnoreCase(name)) {
result = type;
break;
return type;
}
}
return result;
throw new IllegalArgumentException("Unsupported \"SameSite\" attribute: " + name);
}
}

private final String name;
private final BytesValue value;
private final String domain;
private final String path;
private final long size;
private final @Nullable String domain;
private final @Nullable String path;
private final @Nullable Long size;
private final boolean isSecure;
private final boolean isHttpOnly;
private final SameSite sameSite;
private final @Nullable SameSite sameSite;
private final Optional<Long> expiry;

public Cookie(
String name,
BytesValue value,
String domain,
String path,
long size,
@Nullable String domain,
@Nullable String path,
@Nullable Long size,
boolean isSecure,
boolean httpOnly,
SameSite sameSite,
@Nullable SameSite sameSite,
Optional<Long> expiry) {
this.name = name;
this.value = value;
Expand All @@ -87,7 +89,7 @@ public static Cookie fromJson(JsonInput input) {
BytesValue value = null;
String domain = null;
String path = null;
long size = 0;
Long size = null;
boolean isSecure = false;
boolean isHttpOnly = false;
SameSite sameSite = null;
Expand All @@ -112,17 +114,17 @@ public static Cookie fromJson(JsonInput input) {
size = input.read(Long.class);
break;
case "secure":
isSecure = input.read(Boolean.class);
isSecure = input.readNonNull(Boolean.class);
break;
case "httpOnly":
isHttpOnly = input.read(Boolean.class);
isHttpOnly = input.readNonNull(Boolean.class);
break;
case "sameSite":
String sameSiteValue = input.read(String.class);
String sameSiteValue = input.readNonNull(String.class);
sameSite = SameSite.findByName(sameSiteValue);
break;
case "expiry":
expiry = Optional.of(input.read(Long.class));
expiry = Optional.of(input.readNonNull(Long.class));
break;
default:
input.skipValue();
Expand All @@ -131,7 +133,16 @@ public static Cookie fromJson(JsonInput input) {

input.endObject();

return new Cookie(name, value, domain, path, size, isSecure, isHttpOnly, sameSite, expiry);
return new Cookie(
Require.nonNull("name", name),
Require.nonNull("value", value),
domain,
path,
size,
isSecure,
isHttpOnly,
sameSite,
expiry);
}

public String getName() {
Expand All @@ -142,15 +153,18 @@ public BytesValue getValue() {
return value;
}

@Nullable
public String getDomain() {
return domain;
}

@Nullable
public String getPath() {
return path;
}

public long getSize() {
@Nullable
public Long getSize() {
return size;
}

Expand All @@ -162,6 +176,7 @@ public boolean isHttpOnly() {
return isHttpOnly;
}

@Nullable
public SameSite getSameSite() {
return sameSite;
}
Expand All @@ -174,15 +189,13 @@ public Map<String, Object> toMap() {
Map<String, Object> map = new HashMap<>();
map.put("name", getName());
map.put("value", getValue().toMap());
map.put("domain", getDomain());
map.put("path", getPath());
map.put("size", getSize());
if (domain != null) map.put("domain", domain);
if (path != null) map.put("path", path);
if (size != null) map.put("size", size);
map.put("secure", isSecure());
map.put("httpOnly", isHttpOnly());
map.put("sameSite", getSameSite().toString());

getExpiry().ifPresent(expiryValue -> map.put("expiry", expiryValue));

return Map.copyOf(map);
if (sameSite != null) map.put("sameSite", sameSite.toString());
expiry.ifPresent(expiry -> map.put("expiry", expiry));
return unmodifiableMap(map);
}
}
Loading