Skip to content

Commit

Permalink
Make http client deal with suppliers of inputstreams
Browse files Browse the repository at this point in the history
This will make migration to the java 9 httpclient as well
as using Netty a bit simpler.
  • Loading branch information
shs96c committed May 2, 2019
1 parent 5ad95b4 commit 1b253f9
Show file tree
Hide file tree
Showing 84 changed files with 586 additions and 360 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import static java.net.HttpURLConnection.HTTP_OK;
import static org.openqa.selenium.json.Json.MAP_TYPE;
import static org.openqa.selenium.remote.http.Contents.string;
import static org.openqa.selenium.remote.http.HttpMethod.GET;

import org.openqa.selenium.Capabilities;
Expand Down Expand Up @@ -69,7 +70,7 @@ public static Optional<Connection> getChromeConnector(
return Optional.empty();
}

Map<String, Object> versionData = JSON.toType(res.getContentString(), MAP_TYPE);
Map<String, Object> versionData = JSON.toType(string(res), MAP_TYPE);
raw = versionData.get("webSocketDebuggerUrl");

if (!(raw instanceof String)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static com.google.common.net.MediaType.JSON_UTF_8;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.openqa.selenium.remote.CapabilityType.PROXY;
import static org.openqa.selenium.remote.http.Contents.string;

import com.google.common.base.Preconditions;
import com.google.common.io.CountingOutputStream;
Expand Down Expand Up @@ -100,7 +101,7 @@ private Optional<Result> createSession(HttpClient client, InputStream newSession

request.setHeader(CONTENT_LENGTH, String.valueOf(size));
request.setHeader(CONTENT_TYPE, JSON_UTF_8.toString());
request.setContent(newSessionBlob);
request.setContent(() -> newSessionBlob);

response = client.execute(request);
long time = System.currentTimeMillis() - start;
Expand All @@ -109,10 +110,10 @@ private Optional<Result> createSession(HttpClient client, InputStream newSession
// W3C spec properly. Oh well.
Map<?, ?> blob;
try {
blob = new Json().toType(response.getContentString(), Map.class);
blob = new Json().toType(string(response), Map.class);
} catch (JsonException e) {
throw new WebDriverException(
"Unable to parse remote response: " + response.getContentString(), e);
"Unable to parse remote response: " + string(response), e);
}

InitialHandshakeResponse initialResponse = new InitialHandshakeResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
package org.openqa.selenium.remote;

import static com.google.common.net.MediaType.JSON_UTF_8;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.openqa.selenium.remote.HttpSessionId.getSessionId;
import static org.openqa.selenium.remote.http.Contents.utf8String;
import static org.openqa.selenium.remote.http.HttpMethod.POST;

import com.google.common.annotations.VisibleForTesting;
Expand Down Expand Up @@ -401,7 +401,7 @@ public Response execute(Command command) throws IOException {
try (JsonOutput jsonOutput = new Json().newOutput(payload)) {
writePayload.accept(jsonOutput);
}
request.setContent(payload.toString().getBytes(UTF_8));
request.setContent(utf8String(payload.toString()));
} else {
request = commandCodec.encode(command);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@
import static org.openqa.selenium.remote.DriverCommand.SWITCH_TO_PARENT_FRAME;
import static org.openqa.selenium.remote.DriverCommand.SWITCH_TO_WINDOW;
import static org.openqa.selenium.remote.DriverCommand.UPLOAD_FILE;
import static org.openqa.selenium.remote.http.Contents.bytes;
import static org.openqa.selenium.remote.http.Contents.string;

import com.google.common.base.Objects;
import com.google.common.base.Splitter;
Expand Down Expand Up @@ -233,7 +235,7 @@ public HttpRequest encode(Command command) {

request.setHeader(CONTENT_LENGTH, String.valueOf(data.length));
request.setHeader(CONTENT_TYPE, JSON_UTF_8.toString());
request.setContent(data);
request.setContent(bytes(data));
}

if (HttpMethod.GET == spec.method) {
Expand Down Expand Up @@ -267,7 +269,7 @@ public Command decode(final HttpRequest encodedCommand) {
Map<String, Object> parameters = new HashMap<>();
spec.parsePathParameters(parts, parameters);

String content = encodedCommand.getContentString();
String content = string(encodedCommand);
if (!content.isEmpty()) {
Map<String, Object> tmp = json.toType(content, MAP_TYPE);
parameters.putAll(tmp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
import static java.net.HttpURLConnection.HTTP_OK;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.openqa.selenium.remote.http.Contents.bytes;
import static org.openqa.selenium.remote.http.Contents.string;

import org.openqa.selenium.json.Json;
import org.openqa.selenium.json.JsonException;
Expand Down Expand Up @@ -66,7 +68,7 @@ public HttpResponse encode(Supplier<HttpResponse> factory, Response response) {
httpResponse.setHeader(EXPIRES, "Thu, 01 Jan 1970 00:00:00 GMT");
httpResponse.setHeader(CONTENT_LENGTH, String.valueOf(data.length));
httpResponse.setHeader(CONTENT_TYPE, JSON_UTF_8.toString());
httpResponse.setContent(data);
httpResponse.setContent(bytes(data));

return httpResponse;
}
Expand All @@ -76,7 +78,7 @@ public HttpResponse encode(Supplier<HttpResponse> factory, Response response) {
@Override
public Response decode(HttpResponse encodedResponse) {
String contentType = nullToEmpty(encodedResponse.getHeader(CONTENT_TYPE));
String content = encodedResponse.getContentString().trim();
String content = string(encodedResponse).trim();
try {
return reconstructValue(json.toType(content, Response.class));
} catch (JsonException e) {
Expand Down Expand Up @@ -109,7 +111,7 @@ public Response decode(HttpResponse encodedResponse) {
}
}

if (encodedResponse.getContent().length > 0) {
if (!content.isEmpty()) {
response.setValue(content);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static java.net.HttpURLConnection.HTTP_OK;
import static org.openqa.selenium.json.Json.MAP_TYPE;
import static org.openqa.selenium.json.Json.OBJECT_TYPE;
import static org.openqa.selenium.remote.http.Contents.string;

import com.google.common.base.Strings;
import com.google.common.base.Throwables;
Expand Down Expand Up @@ -73,7 +74,7 @@ public class W3CHttpResponseCodec extends AbstractHttpResponseCodec {

@Override
public Response decode(HttpResponse encodedResponse) {
String content = encodedResponse.getContentString().trim();
String content = string(encodedResponse).trim();
log.fine(String.format(
"Decoding response. Response code was: %d and content: %s",
encodedResponse.getStatus(),
Expand Down Expand Up @@ -128,7 +129,7 @@ public Response decode(HttpResponse encodedResponse) {

response.setState("success");
response.setStatus(ErrorCodes.SUCCESS);
if (encodedResponse.getContent().length > 0) {
if (!content.isEmpty()) {
if (contentType.startsWith("application/json") || Strings.isNullOrEmpty("")) {
Map<String, Object> parsed = json.toType(content, MAP_TYPE);
if (parsed.containsKey("value")) {
Expand Down
152 changes: 152 additions & 0 deletions java/client/src/org/openqa/selenium/remote/http/Contents.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.remote.http;

import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.io.ByteStreams;
import com.google.common.io.FileBackedOutputStream;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.nio.charset.Charset;
import java.util.Objects;
import java.util.function.Supplier;

public class Contents {

private Contents() {
// Utility class
}

public static Supplier<InputStream> empty() {
return bytes(new byte[0]);
}

public static Supplier<InputStream> utf8String(CharSequence value) {
Objects.requireNonNull(value, "Value to return must be set.");

return string(value, UTF_8);
}

public static Supplier<InputStream> string(CharSequence value, Charset charset) {
Objects.requireNonNull(value, "Value to return must be set.");
Objects.requireNonNull(charset, "Character set to use must be set.");

return bytes(value.toString().getBytes(charset));
}

public static Supplier<InputStream> bytes(byte[] bytes) {
Objects.requireNonNull(bytes, "Bytes to return must be set but may be empty.");

return () -> new ByteArrayInputStream(bytes);
}

public static byte[] bytes(Supplier<InputStream> supplier) {
Objects.requireNonNull(supplier, "Supplier of input must be set.");

try (InputStream is = supplier.get();
ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
ByteStreams.copy(is, bos);
return bos.toByteArray();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

public static String utf8String(Supplier<InputStream> supplier) {
return string(supplier, UTF_8);
}

public static String string(Supplier<InputStream> supplier, Charset charset) {
Objects.requireNonNull(supplier, "Supplier of input must be set.");
Objects.requireNonNull(charset, "Character set to use must be set.");

return new String(bytes(supplier), charset);
}

public static String string(HttpMessage message) {
return string(message.getContent(), message.getContentEncoding());
}

public static Reader utf8Reader(Supplier<InputStream> supplier) {
Objects.requireNonNull(supplier, "Supplier of input must be set.");

return reader(supplier, UTF_8);
}

public static Reader reader(Supplier<InputStream> supplier, Charset charset) {
Objects.requireNonNull(supplier, "Supplier of input must be set.");
Objects.requireNonNull(charset, "Character set to use must be set.");

return new InputStreamReader(supplier.get(), charset);
}

public static Reader reader(HttpMessage message) {
return reader(message.getContent(), message.getContentEncoding());
}

public static Supplier<InputStream> memoize(Supplier<InputStream> delegate) {
return new MemoizedSupplier(delegate);
}

private static final class MemoizedSupplier implements Supplier<InputStream> {

private volatile boolean initialized;
private volatile FileBackedOutputStream fos;
private Supplier<InputStream> delegate;

private MemoizedSupplier(Supplier<InputStream> delegate) {
this.delegate = delegate;
}

@Override
public InputStream get() {
if (!initialized) {
synchronized (this) {
if (!initialized) {
try (InputStream is = delegate.get()) {
this.fos = new FileBackedOutputStream(3 * 1024 * 1024, true);
ByteStreams.copy(is, fos);
initialized = true;
} catch (IOException e) {
throw new UncheckedIOException(e);
} finally {
try {
this.fos.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
}
}

try {
return Objects.requireNonNull(fos.asByteSource()).openBufferedStream();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
}
Loading

0 comments on commit 1b253f9

Please sign in to comment.