eclipse-ee4j / eclipse-ee4j/jersey

Success after exception thrown while writing StreamingOutput

Open
#3,850 4 comments 5 reactions 0 assignees View on GitHub
Type: Bug
Dominant language
Java
Stars
730
Forks
382
PR merge metrics
No merged PRs in 30d

Description

Expected: some output has been written to the output stream and received by the client. An exception then occurs and the client throws an exception about not receiving a successful final end chunk.

Actual: the exception is caught and the output stream is closed at which point the client believes it has received the entire stream.

Jetty correctly uses `Transfer-Encoding: chunked` when a `StreamingOutput` is used, but the connection is not aborted which would otherwise allow the client to determine whether the stream was successfully written or failed part way through.

Test case that should fail:
```
package org.glassfish.jersey.tests.e2e.entity;

import static org.junit.Assert.assertEquals;

import com.google.common.base.Strings;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;

@Path("/")
public class StreamingOutputTest extends JerseyTest {
@GET
@Path("ise")
public StreamingOutput test3() {
return new StreamingOutput() {
public void write(OutputStream output) throws IOException {
output.write(Strings.repeat("a", 10_000).getBytes(StandardCharsets.UTF_8));
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
}
};
}

@Override
protected Application configure() {
return new ResourceConfig(StreamingOutputTest.class);
}

@Test
public void testInternalServerError() {
Response r = target("ise").request().get();
assertEquals(r.readEntity(String.class), Strings.repeat("a", 10_000));
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.