Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix InputStream handling as Response entity in RESTEasy Classic #21655

Merged
merged 1 commit into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.quarkus.resteasy.test;

import static io.restassured.RestAssured.when;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

import org.hamcrest.Matchers;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

public class InputStreamResponseLargePayloadWithRemainderTest {

@RegisterExtension
static QuarkusUnitTest runner = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(RootResource.class)
.addAsResource(new StringAsset("quarkus.resteasy.vertx.response-buffer-size=11\n"),
"application.properties"));

@Test
public void test() {
when().get("/test").then().body(Matchers.is("Hello World!"));
}

@Path("test")
public static class TestResource {

@Produces("text/plain")
@GET
public Response test() {
return Response.ok(new ByteArrayInputStream("Hello World!".getBytes(StandardCharsets.UTF_8))).build();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.quarkus.resteasy.test;

import static io.restassured.RestAssured.when;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

import org.hamcrest.Matchers;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

public class InputStreamResponseLargePayloadWithoutRemainderTest {

@RegisterExtension
static QuarkusUnitTest runner = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(RootResource.class)
.addAsResource(new StringAsset("quarkus.resteasy.vertx.response-buffer-size=3\n"),
"application.properties"));

@Test
public void test() {
when().get("/test").then().body(Matchers.is("Hello World!"));
}

@Path("test")
public static class TestResource {

@Produces("text/plain")
@GET
public Response test() {
return Response.ok(new ByteArrayInputStream("Hello World!".getBytes(StandardCharsets.UTF_8))).build();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.quarkus.resteasy.test;

import static io.restassured.RestAssured.when;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

public class InputStreamResponseTest {

@RegisterExtension
static QuarkusUnitTest runner = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(RootResource.class));

@Test
public void test() {
when().get("/test").then().body(Matchers.is("Hello World"));
}

@Path("test")
public static class TestResource {

@Produces("text/plain")
@GET
public Response test() {
return Response.ok(new ByteArrayInputStream("Hello World".getBytes(StandardCharsets.UTF_8))).build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,21 @@ public CompletionStage<Void> asyncWrite(final byte[] b, final int off, final int
}

if (bufferCount == 1) {
pooledBuffer = allocator.allocateBuffer();
pooledBuffer.writeBytes(b);
if (pooledBuffer == null) {
pooledBuffer = allocator.allocateBuffer();
}
pooledBuffer.writeBytes(b, 0, len);
} else {
for (int i = 0; i < bufferCount - 1; i++) {
int bufferIndex = i;
ret = ret.thenCompose(v -> {
ByteBuf tmpBuf = allocator.allocateBuffer();
ByteBuf tmpBuf = null;
if ((bufferIndex == 0) && ((pooledBuffer != null))) {
tmpBuf = pooledBuffer;
}
if (tmpBuf == null) {
tmpBuf = allocator.allocateBuffer();
}
tmpBuf.writeBytes(b, bufferIndex * bufferSize, bufferSize);
return response.writeNonBlocking(tmpBuf, false);
});
Expand Down