Skip to content

Commit

Permalink
[java] Java HTTP client : Allow reading the response more than once
Browse files Browse the repository at this point in the history
  • Loading branch information
pujagani committed Sep 16, 2022
1 parent 0b85339 commit cd8b551
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.openqa.selenium.remote.http.TextMessage;
import org.openqa.selenium.remote.http.WebSocket;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
Expand Down Expand Up @@ -230,9 +231,9 @@ private URI getWebSocketUri(HttpRequest request) {
@Override
public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
Objects.requireNonNull(req, "Request");
BodyHandler<InputStream> streamHandler = BodyHandlers.ofInputStream();
BodyHandler<byte[]> byteHandler = BodyHandlers.ofByteArray();
try {
return messages.createResponse(client.send(messages.createRequest(req), streamHandler));
return messages.createResponse(client.send(messages.createRequest(req), byteHandler));
} catch (HttpTimeoutException e) {
throw new TimeoutException(e);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URI;
import java.net.URLEncoder;
Expand Down Expand Up @@ -118,12 +119,12 @@ public URI getRawUri(HttpRequest req) {
return URI.create(rawUrl);
}

public HttpResponse createResponse(java.net.http.HttpResponse<InputStream> response) {
public HttpResponse createResponse(java.net.http.HttpResponse<byte[]> response) {
HttpResponse res = new HttpResponse();
res.setStatus(response.statusCode());
response.headers().map()
.forEach((name, values) -> values.stream().filter(Objects::nonNull).forEach(value -> res.addHeader(name, value)));
res.setContent(response::body);
res.setContent(() -> new ByteArrayInputStream(response.body()));

return res;
}
Expand Down

0 comments on commit cd8b551

Please sign in to comment.