Skip to content

Commit

Permalink
Throwing NoContentException when InputStream is empty
Browse files Browse the repository at this point in the history
Since JAX-RS 2.1 an MBR must react upon an empty stream either with an
empty instance or with NoContentException, but Jersey's JSON-B MBR does
neither - it simply throws ProcessingException:

"In case the entity input stream is empty, the reader is expected to either return a
Java representation of a zero-length entity or throw a javax.ws.rs.core.NoContentException
in case no zero-length entity representation is defined for the supported Java type."

Signed-off-by: Markus KARG <markus@headcrashing.eu>
  • Loading branch information
mkarg authored and quipsy-karg committed Sep 26, 2019
1 parent 7c67bae commit fc2b146
Showing 1 changed file with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PushbackInputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

Expand All @@ -29,6 +30,7 @@
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.NoContentException;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import javax.ws.rs.ext.Providers;
Expand Down Expand Up @@ -70,7 +72,24 @@ public Object readFrom(Class<Object> type, Type genericType,
MediaType mediaType,
MultivaluedMap<String, String> httpHeaders,
InputStream entityStream) throws IOException, WebApplicationException {
if (entityStream.markSupported()) {
entityStream.mark(1);
if (entityStream.read() == -1) {
throw new NoContentException("JSON-B cannot process empty input stream");
}
entityStream.reset();
} else {
final PushbackInputStream buffer = new PushbackInputStream(entityStream);
final int firstByte = buffer.read();
if (firstByte == -1) {
throw new NoContentException("JSON-B cannot process empty input stream");
}
buffer.unread(firstByte);
entityStream = buffer;
}

Jsonb jsonb = getJsonb(type);

try {
return jsonb.fromJson(entityStream, genericType);
} catch (JsonbException e) {
Expand Down

0 comments on commit fc2b146

Please sign in to comment.