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

Throwing NoContentException when InputStream is empty #4275

Merged
merged 5 commits into from
Oct 2, 2019
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
Expand Up @@ -29,6 +29,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 All @@ -39,6 +40,7 @@

import org.glassfish.jersey.jsonb.LocalizationMessages;
import org.glassfish.jersey.message.internal.AbstractMessageReaderWriterProvider;
import org.glassfish.jersey.message.internal.EntityInputStream;

/**
* Entity provider (reader and writer) for JSONB.
Expand Down Expand Up @@ -70,7 +72,14 @@ public Object readFrom(Class<Object> type, Type genericType,
MediaType mediaType,
MultivaluedMap<String, String> httpHeaders,
InputStream entityStream) throws IOException, WebApplicationException {
final EntityInputStream entityInputStream = new EntityInputStream(entityStream);
entityStream = entityInputStream;
if (entityInputStream.isEmpty()) {
throw new NoContentException(LocalizationMessages.ERROR_JSONB_EMPTYSTREAM());
}

Jsonb jsonb = getJsonb(type);

try {
return jsonb.fromJson(entityStream, genericType);
} catch (JsonbException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2017, 2019 Oracle and/or its affiliates. All rights reserved.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -16,3 +16,4 @@

error.jsonb.serialization=Error writing JSON-B serialized object.
error.jsonb.deserialization=Error deserializing object from entity stream.
error.jsonb.emptystream=JSON-B cannot parse empty input stream.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright (c) 2020 Markus KARG
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.jsonb.internal;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.NoContentException;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Providers;

import org.junit.Test;

/**
* Unit Test for {@link JsonBindingProvider}.
*
* @author Markus KARG ([email protected])
*/
public final class JsonBindingProviderTest {

@Test(expected = NoContentException.class)
public final void shouldThrowNoContentException() throws IOException {
// given
final Providers providers = new EmptyProviders();
final MessageBodyReader<Foo> mbr = (MessageBodyReader) new JsonBindingProvider(providers);

// when
mbr.readFrom(Foo.class, Foo.class, new Annotation[0], APPLICATION_JSON_TYPE,
new MultivaluedHashMap<>(), new ByteArrayInputStream(new byte[0]));

// then
// should throw NoContentException
}

private static final class Foo {
// no members
}

private static final class EmptyProviders implements Providers {

@Override
public final <T> MessageBodyReader<T> getMessageBodyReader(final Class<T> type, final Type genericType,
final Annotation[] annotations, final MediaType mediaType) {
return null;
}

@Override
public final <T> MessageBodyWriter<T> getMessageBodyWriter(final Class<T> type, final Type genericType,
final Annotation[] annotations, final MediaType mediaType) {
return null;
}

@Override
public final <T extends Throwable> ExceptionMapper<T> getExceptionMapper(final Class<T> type) {
return null;
}

@Override
public final <T> ContextResolver<T> getContextResolver(final Class<T> contextType, final MediaType mediaType) {
return null;
}

}

}