Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e697c42
refactor(artifact): follow artifact.ts
yury-s Nov 18, 2026
c855dce
refactor(stream): follow stream.ts
yury-s Nov 18, 2026
bc4983c
refactor(writable-stream): follow writableStream.ts
yury-s Nov 18, 2026
99daa14
refactor(download): follow download.ts
yury-s Nov 18, 2026
da1f665
refactor(video): follow video.ts
yury-s Nov 18, 2026
307a24f
refactor(screencast): follow screencast.ts
yury-s Nov 18, 2026
eb87193
refactor(consoleMessage): follow consoleMessage.ts
yury-s Nov 18, 2026
51118c6
refactor(dialog): follow dialog.ts
yury-s Nov 18, 2026
950381a
refactor(fileChooser): follow fileChooser.ts
yury-s Nov 18, 2026
5d8d70a
refactor(webError): follow webError.ts
yury-s Nov 18, 2026
95787e3
refactor(worker): follow worker.ts
yury-s Nov 18, 2026
84ce8e0
refactor(cdpSession): follow cdpSession.ts
yury-s Nov 18, 2026
1b0b325
refactor(debugger): follow debugger.ts
yury-s Nov 18, 2026
3b7e270
refactor(clock): follow clock.ts
yury-s Nov 18, 2026
d530c68
refactor(webStorage): follow webStorage.ts
yury-s Nov 18, 2026
4136d39
refactor(credentials): follow credentials.ts
yury-s Nov 18, 2026
da05f2d
refactor(webmcp): follow webmcp.ts
yury-s Nov 18, 2026
5aca50f
refactor(input): follow input.ts
yury-s Nov 18, 2026
70cbd2c
refactor(localUtils): follow localUtils.ts
yury-s Nov 18, 2026
e77c633
refactor(jsonPipe): follow jsonPipe.ts
yury-s Nov 18, 2026
60e0422
refactor(selectors): follow selectors.ts
yury-s Nov 18, 2026
78681d4
refactor(clientHelper): follow clientHelper.ts and fileUtils.ts
yury-s Nov 18, 2026
05516de
chore: drop the notes on the remote path message, it is the upstream …
yury-s Nov 20, 2026
1026a1b
chore: remove the classes that are translated anew from the Node.js c…
yury-s Nov 20, 2026
35f8583
chore: translate page, frame, context, locator, network, handles, fet…
yury-s Nov 20, 2026
debac0a
fix: subscribe before pumping in waitForURL, let the condition win in…
yury-s Nov 20, 2026
58c9da6
chore: review the translated classes line by line against the Node.js…
yury-s Nov 20, 2026
9596990
chore: port the helper files of the Node.js client and remove dead code
yury-s Nov 20, 2026
6e6c0e5
chore: move the ports of the isomorphic helpers into impl.isomorphic
yury-s Nov 20, 2026
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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -14,96 +14,68 @@
* limitations under the License.
*/

// Port of fetch.ts.
package com.microsoft.playwright.impl;

import com.microsoft.playwright.impl.protocol.*;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import com.microsoft.playwright.impl.isomorphic.Headers;
import com.microsoft.playwright.APIRequest;
import com.microsoft.playwright.PlaywrightException;
import com.microsoft.playwright.impl.protocol.PlaywrightNewRequestParams;
import com.microsoft.playwright.options.ClientCertificate;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.microsoft.playwright.impl.ChannelOwner.NO_TIMEOUT;
import static com.microsoft.playwright.impl.Serialization.gson;
import static com.microsoft.playwright.impl.Utils.toProtocol;

class APIRequestImpl implements APIRequest {
private final PlaywrightImpl playwright;
final Set<APIRequestContextImpl> contexts = new LinkedHashSet<>();

APIRequestImpl(PlaywrightImpl playwright) {
this.playwright = playwright;
}

@Override
public APIRequestContextImpl newContext(NewContextOptions options) {
if (options == null) {
options = new NewContextOptions();
} else {
options = Utils.clone(options);
}
options = options == null ? new NewContextOptions() : Utils.clone(options);
// java: storageState is the JSON text and storageStatePath the file, upstream takes a path string or an object.
String storageStateJson = options.storageState;
if (options.storageStatePath != null) {
try {
byte[] bytes = Files.readAllBytes(options.storageStatePath);
options.storageState = new String(bytes, StandardCharsets.UTF_8);
options.storageStatePath = null;
storageStateJson = new String(Files.readAllBytes(options.storageStatePath), StandardCharsets.UTF_8);
} catch (IOException e) {
throw new PlaywrightException("Failed to read storage state from file", e);
}
}
PlaywrightNewRequestParams.PlaywrightNewRequestParamsStorageState storageState = null;
if (options.storageState != null) {
storageState = gson().fromJson(options.storageState, PlaywrightNewRequestParams.PlaywrightNewRequestParamsStorageState.class);
options.storageState = null;
}
List<ClientCertificate> clientCertificateList = options.clientCertificates;
options.clientCertificates = null;
// The protocol always takes a list (mirrors toHttpCredentialsProtocol() in the JS client).
PlaywrightNewRequestParams.PlaywrightNewRequestParamsStorageState storageState = storageStateJson == null ? null :
gson().fromJson(storageStateJson, PlaywrightNewRequestParams.PlaywrightNewRequestParamsStorageState.class);
Map<String, String> extraHTTPHeaders = options.extraHTTPHeaders;
List<ClientCertificate> clientCertificates = options.clientCertificates;
Object httpCredentials = options.httpCredentials;
// java: the fields that the spread of upstream replaces are cleared before the options are copied into the params.
options.storageState = null;
options.storageStatePath = null;
options.extraHTTPHeaders = null;
options.clientCertificates = null;
options.httpCredentials = null;
Double timeout = options.timeout;
// Timeout is handled on the client.
options.timeout = null;
PlaywrightNewRequestParams params = toProtocol(options, PlaywrightNewRequestParams.class);
if (storageState != null) {
params.storageState = storageState;
}
if (clientCertificateList != null) {
params.clientCertificates = new ArrayList<>();
for (ClientCertificate cert : clientCertificateList) {
PlaywrightNewRequestParams.PlaywrightNewRequestParamsClientCertificates c = new PlaywrightNewRequestParams.PlaywrightNewRequestParamsClientCertificates();
c.origin = cert.origin;
try {
c.cert = cert.certPath != null ? Files.readAllBytes(cert.certPath) : cert.cert;
c.key = cert.keyPath != null ? Files.readAllBytes(cert.keyPath) : cert.key;
c.pfx = cert.pfxPath != null ? Files.readAllBytes(cert.pfxPath) : cert.pfx;
} catch (IOException e) {
throw new PlaywrightException("Failed to read from file", e);
}
c.passphrase = cert.passphrase;
params.clientCertificates.add(c);
}
}
if (httpCredentials != null) {
JsonElement credentials = gson().toJsonTree(httpCredentials);
JsonArray list = new JsonArray();
if (credentials.isJsonArray()) {
list.addAll(credentials.getAsJsonArray());
} else {
list.add(credentials);
}
if (list.size() > 0) {
params.httpCredentials = gson().fromJson(list, new TypeToken<List<HttpCredentials>>() {}.getType());
}
}
PlaywrightNewRequestParams params = Utils.toProtocol(options, PlaywrightNewRequestParams.class);
params.extraHTTPHeaders = extraHTTPHeaders != null ? Headers.headersObjectToArray(extraHTTPHeaders) : null;
params.storageState = storageState;
// java: no _defaultLaunchOptions in the Java API, so no tracesDir here and none for context.tracing below.
params.clientCertificates = BrowserContextImpl.toClientCertificatesProtocol(clientCertificates);
params.httpCredentials = BrowserContextImpl.toHttpCredentialsProtocol(httpCredentials);
APIRequestContextImpl context = playwright.channel.newRequest(params, NO_TIMEOUT).request;
context.timeoutSettings.setDefaultTimeout(timeout);
contexts.add(context);
context.request = this;
// java: no _defaultContextTimeout in the Java API.
context.timeoutSettings.setDefaultTimeout(options.timeout);
return context;
}
}
Loading
Loading