Skip to content

Commit d6222a0

Browse files
committed
buffer: move actual send to the buffer implementation
- remove the need of creating a buffer iterator for jetty and undertow - ref #3604
1 parent d54f332 commit d6222a0

File tree

8 files changed

+44
-15
lines changed

8 files changed

+44
-15
lines changed

jooby/src/main/java/io/jooby/buffer/DataBuffer.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.function.IntPredicate;
1717

1818
import edu.umd.cs.findbugs.annotations.NonNull;
19+
import io.jooby.Context;
1920

2021
/**
2122
* Basic abstraction over byte buffers.
@@ -447,6 +448,14 @@ default String toString(Charset charset) {
447448
*/
448449
DataBuffer clear();
449450

451+
/**
452+
* Send the buffer data to the client.
453+
*
454+
* @param ctx HTTP context.
455+
* @return HTTP context.
456+
*/
457+
Context send(Context ctx);
458+
450459
/**
451460
* A dedicated iterator type that ensures the lifecycle of iterated {@link ByteBuffer} elements.
452461
* This iterator must be used in a try-with-resources clause or explicitly {@linkplain #close()

jooby/src/main/java/io/jooby/buffer/DataBufferWrapper.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import java.nio.charset.Charset;
1212
import java.util.function.IntPredicate;
1313

14+
import io.jooby.Context;
15+
1416
/**
1517
* Provides a convenient implementation of the {@link DataBuffer} interface that can be overridden
1618
* to adapt the delegate.
@@ -208,10 +210,16 @@ public String toString(Charset charset) {
208210

209211
@Override
210212
public DataBuffer clear() {
211-
this.clear();
213+
delegate.clear();
212214
return this;
213215
}
214216

217+
@Override
218+
public Context send(Context ctx) {
219+
this.delegate.send(ctx);
220+
return ctx;
221+
}
222+
215223
@Override
216224
public String toString(int index, int length, Charset charset) {
217225
return this.delegate.toString(index, length, charset);

jooby/src/main/java/io/jooby/buffer/DefaultDataBuffer.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import java.util.NoSuchElementException;
1212
import java.util.function.IntPredicate;
1313

14+
import io.jooby.Context;
15+
1416
/**
1517
* Default implementation of the {@link DataBuffer} interface that uses a {@link ByteBuffer}
1618
* internally. with separate read and write positions. Constructed using the {@link
@@ -474,6 +476,12 @@ public DataBuffer clear() {
474476
return this;
475477
}
476478

479+
@Override
480+
public Context send(Context ctx) {
481+
ctx.send(this.byteBuffer.slice(this.readPosition, readableByteCount()));
482+
return ctx;
483+
}
484+
477485
private static final class ByteBufferIterator implements DataBuffer.ByteBufferIterator {
478486

479487
private final ByteBuffer buffer;

modules/jooby-jetty/src/main/java/io/jooby/internal/jetty/JettyContext.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package io.jooby.internal.jetty;
77

88
import static io.jooby.internal.jetty.JettyCallbacks.fromByteBufferArray;
9-
import static io.jooby.internal.jetty.JettyCallbacks.fromDataBuffer;
109
import static org.eclipse.jetty.http.HttpHeader.*;
1110
import static org.eclipse.jetty.http.HttpHeader.CONTENT_TYPE;
1211
import static org.eclipse.jetty.http.HttpHeader.SET_COOKIE;
@@ -520,12 +519,7 @@ public Context send(@NonNull String data, @NonNull Charset charset) {
520519

521520
@NonNull @Override
522521
public Context send(@NonNull DataBuffer data) {
523-
var length = response.getHeaders().getLongField(CONTENT_LENGTH);
524-
if (length <= 0) {
525-
setResponseLength(data.readableByteCount());
526-
}
527-
responseStarted = true;
528-
fromDataBuffer(response, this, data).send(true);
522+
data.send(this);
529523
return this;
530524
}
531525

modules/jooby-netty/src/main/java/io/jooby/internal/netty/NettyContext.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
import io.jooby.ValueNode;
7272
import io.jooby.WebSocket;
7373
import io.jooby.buffer.DataBuffer;
74-
import io.jooby.netty.buffer.NettyDataBuffer;
7574
import io.netty.buffer.ByteBuf;
7675
import io.netty.buffer.Unpooled;
7776
import io.netty.channel.ChannelFuture;
@@ -597,10 +596,11 @@ public final Context send(ByteBuffer data) {
597596

598597
@NonNull @Override
599598
public Context send(@NonNull DataBuffer data) {
600-
return send(((NettyDataBuffer) data).getNativeBuffer());
599+
data.send(this);
600+
return this;
601601
}
602602

603-
private Context send(@NonNull ByteBuf data) {
603+
public Context send(@NonNull ByteBuf data) {
604604
try {
605605
responseStarted = true;
606606
setHeaders.set(CONTENT_LENGTH, Integer.toString(data.readableBytes()));

modules/jooby-netty/src/main/java/io/jooby/netty/buffer/NettyDataBuffer.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
import java.util.Objects;
1313
import java.util.function.IntPredicate;
1414

15+
import io.jooby.Context;
1516
import io.jooby.buffer.DataBuffer;
1617
import io.jooby.buffer.PooledDataBuffer;
18+
import io.jooby.internal.netty.NettyContext;
1719
import io.netty.buffer.ByteBuf;
1820
import io.netty.buffer.ByteBufUtil;
1921

@@ -287,6 +289,12 @@ public DataBuffer clear() {
287289
return this;
288290
}
289291

292+
@Override
293+
public Context send(Context ctx) {
294+
((NettyContext) ctx).send(this.byteBuf);
295+
return ctx;
296+
}
297+
290298
@Override
291299
public boolean isAllocated() {
292300
return this.byteBuf.refCnt() > 0;

modules/jooby-undertow/src/main/java/io/jooby/internal/undertow/UndertowContext.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -500,10 +500,7 @@ public Context send(@NonNull ByteBuffer data) {
500500

501501
@NonNull @Override
502502
public Context send(@NonNull DataBuffer data) {
503-
exchange
504-
.getResponseHeaders()
505-
.put(Headers.CONTENT_LENGTH, Long.toString(data.readableByteCount()));
506-
new UndertowDataBufferCallback(data, this).send(exchange);
503+
data.send(this);
507504
return this;
508505
}
509506

tests/src/test/java/io/jooby/junit/ServerTestRunner.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
import io.jooby.SneakyThrows;
2929
import io.jooby.StartupSummary;
3030
import io.jooby.StatusCode;
31+
import io.jooby.buffer.DefaultDataBufferFactory;
3132
import io.jooby.internal.MutedServer;
33+
import io.jooby.netty.NettyServer;
3234
import io.jooby.test.WebClient;
3335

3436
public class ServerTestRunner {
@@ -89,6 +91,9 @@ public void ready(SneakyThrows.Consumer2<WebClient, WebClient> onReady) {
8991
System.setProperty("___app_name__", testName);
9092
System.setProperty("___server_name__", server.getName());
9193
Jooby app = provider.get();
94+
if (!(server instanceof NettyServer)) {
95+
app.setBufferFactory(new DefaultDataBufferFactory());
96+
}
9297
Optional.ofNullable(executionMode).ifPresent(app::setExecutionMode);
9398
// Reduce log from maven build:
9499
var mavenBuild = System.getProperty("surefire.real.class.path", "").length() > 0;

0 commit comments

Comments
 (0)