Skip to content

Commit fc2b146

Browse files
mkargquipsy-karg
authored andcommitted
Throwing NoContentException when InputStream is empty
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>
1 parent 7c67bae commit fc2b146

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

media/json-binding/src/main/java/org/glassfish/jersey/jsonb/internal/JsonBindingProvider.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.io.InputStream;
2121
import java.io.OutputStream;
22+
import java.io.PushbackInputStream;
2223
import java.lang.annotation.Annotation;
2324
import java.lang.reflect.Type;
2425

@@ -29,6 +30,7 @@
2930
import javax.ws.rs.core.Context;
3031
import javax.ws.rs.core.MediaType;
3132
import javax.ws.rs.core.MultivaluedMap;
33+
import javax.ws.rs.core.NoContentException;
3234
import javax.ws.rs.ext.ContextResolver;
3335
import javax.ws.rs.ext.Provider;
3436
import javax.ws.rs.ext.Providers;
@@ -70,7 +72,24 @@ public Object readFrom(Class<Object> type, Type genericType,
7072
MediaType mediaType,
7173
MultivaluedMap<String, String> httpHeaders,
7274
InputStream entityStream) throws IOException, WebApplicationException {
75+
if (entityStream.markSupported()) {
76+
entityStream.mark(1);
77+
if (entityStream.read() == -1) {
78+
throw new NoContentException("JSON-B cannot process empty input stream");
79+
}
80+
entityStream.reset();
81+
} else {
82+
final PushbackInputStream buffer = new PushbackInputStream(entityStream);
83+
final int firstByte = buffer.read();
84+
if (firstByte == -1) {
85+
throw new NoContentException("JSON-B cannot process empty input stream");
86+
}
87+
buffer.unread(firstByte);
88+
entityStream = buffer;
89+
}
90+
7391
Jsonb jsonb = getJsonb(type);
92+
7493
try {
7594
return jsonb.fromJson(entityStream, genericType);
7695
} catch (JsonbException e) {

0 commit comments

Comments
 (0)