Skip to content

Commit

Permalink
Ensure that providers in RESTEasy Reactive common module aren't disco…
Browse files Browse the repository at this point in the history
…verable.

These providers are registered manually, but previously because
they were annotated with @Provider, they could be inadvertently
registered as application classes in some test cases
(essentially if an actual application class extended them).

Added note about sorting - this note is needed in order to folks debugging in the future
from going down the wrong path and having the TCK fail.

Fix ChunkedResponseTest to be spec compliant.
  • Loading branch information
geoand authored and manovotn committed Oct 10, 2022
1 parent 300fb32 commit 6c58561
Show file tree
Hide file tree
Showing 15 changed files with 33 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;

import javax.annotation.Priority;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Priorities;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.reactive.server.providers.serialisers.ServerStringMessageBodyHandler;
import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveResourceInfo;
import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyWriter;
import org.jboss.resteasy.reactive.server.spi.ServerRequestContext;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -68,29 +75,39 @@ public String helloSmall() {
}

@Provider
public static class CustomStringMessageBodyWriter extends ServerStringMessageBodyHandler {
public static class CustomStringMessageBodyWriter implements ServerMessageBodyWriter<String> {

@Override
public void writeResponse(Object o, Type genericType, ServerRequestContext context)
throws WebApplicationException {
public boolean isWriteable(Class<?> type, Type genericType, ResteasyReactiveResourceInfo target, MediaType mediaType) {
return true;
}

try (OutputStream stream = context.getOrCreateOutputStream()) {
stream.write(((String) o).getBytes());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
@Override
public void writeResponse(String o, Type genericType, ServerRequestContext context) throws WebApplicationException {
context.serverResponse().end(o);
}

public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return true;
}

public void writeTo(String o, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
throws IOException, WebApplicationException {
entityStream.write(o.getBytes(StandardCharsets.UTF_8));
}
}

@Provider
@Priority(Priorities.USER + 1) // the spec says that when it comes to writers, higher number means higher priority...
public static final class CustomStringMessageBodyWriter2 extends CustomStringMessageBodyWriter {

@Override
public void writeResponse(Object o, Type genericType, ServerRequestContext context)
public void writeResponse(String o, Type genericType, ServerRequestContext context)
throws WebApplicationException {

try (OutputStream stream = context.getOrCreateOutputStream()) {
stream.write(((String) o).getBytes());
stream.write(o.getBytes());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,11 @@ public List<MessageBodyWriter<?>> findBuildTimeWriters(Class<?> entityType, Runt
}

}
return toMessageBodyWriters(findResourceWriters(writers, klass, produces, runtimeType));

var resourceWriters = findResourceWriters(writers, klass, produces, runtimeType);
// we must NOT sort here because the spec mentions that the writers closer to the requested java type are tried first
// and the list has already been built up in this way
return toMessageBodyWriters(resourceWriters);
}

protected List<ResourceWriter> findResourceWriters(QuarkusMultivaluedMap<Class<?>, ResourceWriter> writers, Class<?> klass,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.reactive.common.providers.serialisers.BooleanMessageBodyHandler;
import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveResourceInfo;
import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyReader;
import org.jboss.resteasy.reactive.server.spi.ServerRequestContext;

@Provider
public class ServerBooleanMessageBodyHandler extends BooleanMessageBodyHandler
implements ServerMessageBodyReader<Boolean> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.reactive.common.providers.serialisers.ByteArrayMessageBodyHandler;
import org.jboss.resteasy.reactive.common.providers.serialisers.MessageReaderUtil;
Expand All @@ -14,7 +13,6 @@
import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyWriter;
import org.jboss.resteasy.reactive.server.spi.ServerRequestContext;

@Provider
public class ServerByteArrayMessageBodyHandler extends ByteArrayMessageBodyHandler
implements ServerMessageBodyWriter<byte[]>, ServerMessageBodyReader<byte[]> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.reactive.common.providers.serialisers.CharArrayMessageBodyHandler;
import org.jboss.resteasy.reactive.common.providers.serialisers.MessageReaderUtil;
Expand All @@ -14,7 +13,6 @@
import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyWriter;
import org.jboss.resteasy.reactive.server.spi.ServerRequestContext;

@Provider
public class ServerCharArrayMessageBodyHandler extends CharArrayMessageBodyHandler
implements ServerMessageBodyWriter<char[]>, ServerMessageBodyReader<char[]> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.reactive.common.providers.serialisers.CharacterMessageBodyHandler;
import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveResourceInfo;
import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyReader;
import org.jboss.resteasy.reactive.server.spi.ServerRequestContext;

@Provider
public class ServerCharacterMessageBodyHandler extends CharacterMessageBodyHandler
implements ServerMessageBodyReader<Character> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.reactive.common.providers.serialisers.DefaultTextPlainBodyHandler;
import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveResourceInfo;
import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyReader;
import org.jboss.resteasy.reactive.server.spi.ServerRequestContext;

@Provider
@Consumes("text/plain")
public class ServerDefaultTextPlainBodyHandler extends DefaultTextPlainBodyHandler implements ServerMessageBodyReader<Object> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.reactive.common.providers.serialisers.FileBodyHandler;
import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveResourceInfo;
import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyWriter;
import org.jboss.resteasy.reactive.server.spi.ServerRequestContext;

@Provider
@Produces("*/*")
@Consumes("*/*")
public class ServerFileBodyHandler extends FileBodyHandler implements ServerMessageBodyWriter<File> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.reactive.FilePart;
import org.jboss.resteasy.reactive.common.providers.serialisers.FilePartBodyHandler;
Expand All @@ -18,7 +17,6 @@

// TODO: this is very simplistic at the moment

@Provider
@Produces("*/*")
@Consumes("*/*")
public class ServerFilePartBodyHandler extends FilePartBodyHandler implements ServerMessageBodyWriter<FilePart> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.reactive.common.providers.serialisers.MapAsFormUrlEncodedProvider;
import org.jboss.resteasy.reactive.common.providers.serialisers.MessageReaderUtil;
Expand All @@ -25,7 +24,6 @@
* @version $Revision: 1 $
*/
@SuppressWarnings("rawtypes")
@Provider
@Produces("application/x-www-form-urlencoded")
@Consumes("application/x-www-form-urlencoded")
@ConstrainedTo(RuntimeType.CLIENT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.reactive.common.providers.serialisers.InputStreamMessageBodyHandler;
import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveResourceInfo;
import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyReader;
import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyWriter;
import org.jboss.resteasy.reactive.server.spi.ServerRequestContext;

@Provider
public class ServerInputStreamMessageBodyHandler extends InputStreamMessageBodyHandler
implements ServerMessageBodyReader<InputStream>, ServerMessageBodyWriter<InputStream> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.reactive.common.providers.serialisers.NumberMessageBodyHandler;
import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveResourceInfo;
import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyReader;
import org.jboss.resteasy.reactive.server.spi.ServerRequestContext;

@Provider
public class ServerNumberMessageBodyHandler extends NumberMessageBodyHandler
implements ServerMessageBodyReader<Number> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.reactive.common.providers.serialisers.PathBodyHandler;
import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveResourceInfo;
import org.jboss.resteasy.reactive.server.spi.ServerHttpResponse;
import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyWriter;
import org.jboss.resteasy.reactive.server.spi.ServerRequestContext;

@Provider
@Produces("*/*")
public class ServerPathBodyHandler extends PathBodyHandler implements ServerMessageBodyWriter<java.nio.file.Path> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.reactive.PathPart;
import org.jboss.resteasy.reactive.common.providers.serialisers.PathPartBodyHandler;
Expand All @@ -18,7 +17,6 @@
import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyWriter;
import org.jboss.resteasy.reactive.server.spi.ServerRequestContext;

@Provider
@Produces("*/*")
public class ServerPathPartBodyHandler extends PathPartBodyHandler implements ServerMessageBodyWriter<PathPart> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.reactive.common.providers.serialisers.StringMessageBodyHandler;
import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveResourceInfo;
import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyReader;
import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyWriter;
import org.jboss.resteasy.reactive.server.spi.ServerRequestContext;

@Provider
public class ServerStringMessageBodyHandler extends StringMessageBodyHandler
implements ServerMessageBodyWriter<Object>, ServerMessageBodyReader<String> {

Expand Down

0 comments on commit 6c58561

Please sign in to comment.