Skip to content

Commit 340ddc8

Browse files
committed
#16612 extract anonymous implementations of Contents.Supplier to separate classes
It makes debugging easier. You can easily see what instances they are and where they come from.
1 parent f5d0a60 commit 340ddc8

File tree

5 files changed

+227
-136
lines changed

5 files changed

+227
-136
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.netty.server;
19+
20+
import com.google.common.io.FileBackedOutputStream;
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.IOException;
23+
import java.io.InputStream;
24+
import java.io.UncheckedIOException;
25+
import java.nio.charset.Charset;
26+
import org.openqa.selenium.remote.http.Contents;
27+
28+
class FileBackedOutputStreamContentSupplier implements Contents.Supplier {
29+
private final String description;
30+
private final FileBackedOutputStream buffer;
31+
private final long length;
32+
33+
FileBackedOutputStreamContentSupplier(
34+
String description, FileBackedOutputStream buffer, long length) {
35+
this.description = description;
36+
this.buffer = buffer;
37+
this.length = length;
38+
}
39+
40+
@Override
41+
public InputStream get() {
42+
try {
43+
return buffer.asByteSource().openBufferedStream();
44+
} catch (IOException e) {
45+
throw new UncheckedIOException(e);
46+
}
47+
}
48+
49+
@Override
50+
public long length() {
51+
return length;
52+
}
53+
54+
@Override
55+
public void close() throws IOException {
56+
buffer.reset();
57+
}
58+
59+
@Override
60+
public String toString() {
61+
return String.format("Content for %s (%s bytes)", description, length);
62+
}
63+
64+
@Override
65+
public String contentAsString(Charset charset) {
66+
ByteArrayOutputStream out = new ByteArrayOutputStream();
67+
try {
68+
buffer.asByteSource().copyTo(out);
69+
} catch (IOException e) {
70+
throw new RuntimeException(e);
71+
}
72+
return out.toString(charset);
73+
}
74+
}

java/src/org/openqa/selenium/netty/server/RequestConverter.java

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import static io.netty.handler.codec.http.HttpMethod.OPTIONS;
2424
import static io.netty.handler.codec.http.HttpMethod.POST;
2525

26-
import com.google.common.io.ByteSource;
2726
import com.google.common.io.FileBackedOutputStream;
2827
import io.netty.buffer.ByteBuf;
2928
import io.netty.channel.ChannelHandlerContext;
@@ -37,14 +36,9 @@
3736
import io.netty.handler.codec.http.LastHttpContent;
3837
import io.netty.handler.codec.http.QueryStringDecoder;
3938
import io.netty.util.ReferenceCountUtil;
40-
41-
import java.io.ByteArrayOutputStream;
42-
import java.io.IOException;
43-
import java.io.InputStream;
44-
import java.io.UncheckedIOException;
45-
import java.nio.charset.Charset;
4639
import java.util.Arrays;
4740
import java.util.List;
41+
import java.util.concurrent.atomic.AtomicLong;
4842
import java.util.logging.Logger;
4943
import org.openqa.selenium.internal.Debug;
5044
import org.openqa.selenium.remote.http.Contents;
@@ -59,7 +53,7 @@ class RequestConverter extends SimpleChannelInboundHandler<HttpObject> {
5953
private static final List<io.netty.handler.codec.http.HttpMethod> SUPPORTED_METHODS =
6054
Arrays.asList(DELETE, GET, POST, OPTIONS);
6155
private volatile FileBackedOutputStream buffer;
62-
private volatile long length;
56+
private final AtomicLong length = new AtomicLong();
6357
private volatile HttpRequest request;
6458

6559
@Override
@@ -96,7 +90,7 @@ protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Ex
9690
AttributeKey.HTTP_FLAVOR.getKey(), nettyRequest.protocolVersion().majorVersion());
9791

9892
buffer = null;
99-
length = -1;
93+
length.set(-1);
10094
}
10195

10296
if (request != null && msg instanceof HttpContent) {
@@ -106,12 +100,12 @@ protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Ex
106100
if (nBytes > 0) {
107101
if (buffer == null) {
108102
buffer = new FileBackedOutputStream(3 * 1024 * 1024, true);
109-
length = 0;
103+
length.set(0);
110104
}
111105

112106
try {
113107
buf.readBytes(buffer, nBytes);
114-
length += nBytes;
108+
length.addAndGet(nBytes);
115109
} finally {
116110
buf.release();
117111
}
@@ -121,46 +115,8 @@ protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Ex
121115
LOG.log(Debug.getDebugLogLevel(), "End of http request: {0}", msg);
122116

123117
if (buffer != null) {
124-
ByteSource source = buffer.asByteSource();
125-
long len = length;
126-
127118
request.setContent(
128-
new Contents.Supplier() {
129-
@Override
130-
public InputStream get() {
131-
try {
132-
return source.openBufferedStream();
133-
} catch (IOException e) {
134-
throw new UncheckedIOException(e);
135-
}
136-
}
137-
138-
@Override
139-
public long length() {
140-
return len;
141-
}
142-
143-
@Override
144-
public void close() throws IOException {
145-
buffer.reset();
146-
}
147-
148-
@Override
149-
public String toString() {
150-
return String.format("Last content for %s (%s bytes)", request.toString(), len);
151-
}
152-
153-
@Override
154-
public String contentAsString(Charset charset) {
155-
ByteArrayOutputStream out = new ByteArrayOutputStream();
156-
try {
157-
source.copyTo(out);
158-
} catch (IOException e) {
159-
throw new RuntimeException(e);
160-
}
161-
return out.toString(charset);
162-
}
163-
});
119+
new FileBackedOutputStreamContentSupplier(request.toString(), buffer, length.get()));
164120
} else {
165121
request.setContent(Contents.empty());
166122
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.remote.http;
19+
20+
import static java.nio.charset.StandardCharsets.UTF_8;
21+
22+
import java.io.ByteArrayInputStream;
23+
import java.io.InputStream;
24+
import java.nio.charset.Charset;
25+
import org.openqa.selenium.internal.Require;
26+
27+
public class BytesContentSupplier implements Contents.Supplier {
28+
private final byte[] bytes;
29+
30+
public BytesContentSupplier(byte[] bytes) {
31+
this.bytes = Require.nonNull("Bytes to return", bytes, "may be empty");
32+
}
33+
34+
private boolean closed;
35+
36+
@Override
37+
public InputStream get() {
38+
if (closed) throw new IllegalStateException("Contents.Supplier has been closed before");
39+
40+
return new ByteArrayInputStream(bytes);
41+
}
42+
43+
@Override
44+
public long length() {
45+
if (closed) throw new IllegalStateException("Contents.Supplier has been closed before");
46+
47+
return bytes.length;
48+
}
49+
50+
public void close() {
51+
closed = true;
52+
}
53+
54+
@Override
55+
public String toString() {
56+
return bytes.length < 256
57+
? new String(bytes, UTF_8)
58+
: String.format("%s bytes: \"%s\"...", bytes.length, new String(bytes, 0, 256, UTF_8));
59+
}
60+
61+
@Override
62+
public String contentAsString(Charset charset) {
63+
return new String(bytes, charset);
64+
}
65+
}

java/src/org/openqa/selenium/remote/http/Contents.java

Lines changed: 2 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
package org.openqa.selenium.remote.http;
1919

2020
import static java.nio.charset.StandardCharsets.UTF_8;
21-
import static java.nio.file.Files.readAttributes;
2221

23-
import java.io.ByteArrayInputStream;
2422
import java.io.ByteArrayOutputStream;
2523
import java.io.File;
2624
import java.io.IOException;
@@ -32,7 +30,6 @@
3230
import java.lang.reflect.Type;
3331
import java.nio.charset.Charset;
3432
import java.nio.file.Files;
35-
import java.nio.file.attribute.BasicFileAttributes;
3633
import java.util.Base64;
3734
import org.openqa.selenium.internal.Require;
3835
import org.openqa.selenium.json.Json;
@@ -89,92 +86,11 @@ public static Supplier string(CharSequence value, Charset charset) {
8986
}
9087

9188
public static Supplier file(final File file) {
92-
Require.nonNull("File to download", file);
93-
94-
return new Supplier() {
95-
private volatile InputStream inputStream;
96-
97-
@Override
98-
public synchronized InputStream get() {
99-
if (inputStream != null) {
100-
throw new IllegalStateException("File input stream has been opened before");
101-
}
102-
try {
103-
inputStream = Files.newInputStream(file.toPath());
104-
} catch (IOException e) {
105-
throw new IllegalStateException("File not readable: " + file.getAbsolutePath(), e);
106-
}
107-
108-
return inputStream;
109-
}
110-
111-
@Override
112-
public long length() {
113-
try {
114-
BasicFileAttributes attributes = readAttributes(file.toPath(), BasicFileAttributes.class);
115-
return attributes.size();
116-
} catch (IOException e) {
117-
throw new IllegalStateException("File not readable: " + file.getAbsolutePath(), e);
118-
}
119-
}
120-
121-
public void close() {
122-
if (inputStream != null) {
123-
try {
124-
inputStream.close();
125-
} catch (IOException ignore) {
126-
}
127-
}
128-
}
129-
130-
@Override
131-
public String toString() {
132-
return String.format("Contents.file(%s)", file);
133-
}
134-
135-
@Override
136-
public String contentAsString(Charset charset) {
137-
throw new UnsupportedOperationException("File content may be too large");
138-
}
139-
};
89+
return new FileContentSupplier(file);
14090
}
14191

14292
public static Supplier bytes(byte[] bytes) {
143-
Require.nonNull("Bytes to return", bytes, "may be empty");
144-
145-
return new Supplier() {
146-
private boolean closed;
147-
148-
@Override
149-
public InputStream get() {
150-
if (closed) throw new IllegalStateException("Contents.Supplier has been closed before");
151-
152-
return new ByteArrayInputStream(bytes);
153-
}
154-
155-
@Override
156-
public long length() {
157-
if (closed) throw new IllegalStateException("Contents.Supplier has been closed before");
158-
159-
return bytes.length;
160-
}
161-
162-
public void close() {
163-
closed = true;
164-
}
165-
166-
@Override
167-
public String toString() {
168-
return bytes.length < 256 ?
169-
new String(bytes, UTF_8) :
170-
String.format("%s bytes: \"%s\"...", bytes.length, new String(bytes, 0, 256, UTF_8));
171-
}
172-
173-
@Override
174-
public String contentAsString(Charset charset) {
175-
return new String(bytes, charset);
176-
}
177-
};
93+
return new BytesContentSupplier(bytes);
17894
}
17995

18096
public static byte[] bytes(Supplier supplier) {

0 commit comments

Comments
 (0)