From 3b7b8c4c469c35bc8e9187cb81e3a03ddf982675 Mon Sep 17 00:00:00 2001 From: Alessio Soldano Date: Tue, 8 Sep 2020 01:54:46 +0200 Subject: [PATCH] Do not use ResteasyProviderFactory.getInstance() in SseEventProvider --- .../plugins/providers/sse/SseEventProvider.java | 17 +++++++++++++++-- .../providers/sse/SseEventSinkInterceptor.java | 8 +++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/resteasy-core/src/main/java/org/jboss/resteasy/plugins/providers/sse/SseEventProvider.java b/resteasy-core/src/main/java/org/jboss/resteasy/plugins/providers/sse/SseEventProvider.java index 0bfed4ccbc4..d282e707165 100644 --- a/resteasy-core/src/main/java/org/jboss/resteasy/plugins/providers/sse/SseEventProvider.java +++ b/resteasy-core/src/main/java/org/jboss/resteasy/plugins/providers/sse/SseEventProvider.java @@ -22,6 +22,7 @@ import javax.ws.rs.ext.MessageBodyReader; import javax.ws.rs.ext.MessageBodyWriter; import javax.ws.rs.ext.Provider; +import javax.ws.rs.ext.Providers; import javax.ws.rs.sse.OutboundSseEvent; import org.jboss.resteasy.resteasy_jaxrs.i18n.Messages; @@ -37,6 +38,16 @@ public class SseEventProvider implements AsyncMessageBodyWriter type, Type genericType, Annotation[] annotations, MediaType mediaType) { @@ -108,8 +119,9 @@ public void writeTo(OutboundSseEvent event, Class type, Type genericType, Ann } entityStream.write(SseConstants.DATA_LEAD); + final Providers p = providers != null ? providers : ResteasyProviderFactory.getInstance(); @SuppressWarnings("rawtypes") - MessageBodyWriter writer = ResteasyProviderFactory.getInstance().getMessageBodyWriter(payloadClass, + MessageBodyWriter writer = p.getMessageBodyWriter(payloadClass, payloadType, annotations, event.getMediaType()); if (writer == null) @@ -268,8 +280,9 @@ public CompletionStage asyncWriteTo(OutboundSseEvent event, Class type, Type finalPayloadType = payloadType; ret = ret.thenCompose(v -> entityStream.asyncWrite(SseConstants.DATA_LEAD)); + final Providers p = providers != null ? providers : ResteasyProviderFactory.getInstance(); @SuppressWarnings("rawtypes") - AsyncMessageBodyWriter writer = (AsyncMessageBodyWriter)ResteasyProviderFactory.getInstance().getMessageBodyWriter(payloadClass, + AsyncMessageBodyWriter writer = (AsyncMessageBodyWriter)p.getMessageBodyWriter(payloadClass, payloadType, annotations, event.getMediaType()); if (writer == null) diff --git a/resteasy-core/src/main/java/org/jboss/resteasy/plugins/providers/sse/SseEventSinkInterceptor.java b/resteasy-core/src/main/java/org/jboss/resteasy/plugins/providers/sse/SseEventSinkInterceptor.java index ae84aa6704f..019c62632df 100644 --- a/resteasy-core/src/main/java/org/jboss/resteasy/plugins/providers/sse/SseEventSinkInterceptor.java +++ b/resteasy-core/src/main/java/org/jboss/resteasy/plugins/providers/sse/SseEventSinkInterceptor.java @@ -5,7 +5,9 @@ import javax.annotation.Priority; import javax.ws.rs.container.ContainerRequestContext; import javax.ws.rs.container.ContainerRequestFilter; +import javax.ws.rs.core.Context; import javax.ws.rs.ext.Provider; +import javax.ws.rs.ext.Providers; import javax.ws.rs.sse.SseEventSink; import org.jboss.resteasy.annotations.Stream; @@ -21,16 +23,20 @@ @Priority(Integer.MAX_VALUE) public class SseEventSinkInterceptor implements ContainerRequestFilter { + @Context + protected Providers providers; + @Override public void filter(ContainerRequestContext requestContext) throws IOException { ResourceMethodInvoker rmi = ((PostMatchContainerRequestContext) requestContext).getResourceMethod(); Stream stream = FindAnnotation.findAnnotation(rmi.getMethodAnnotations(), Stream.class); Stream.MODE mode = stream != null ? stream.value() : null; + if ((mode == Stream.MODE.GENERAL && ResteasyProviderFactory.getInstance().getAsyncStreamProvider(rmi.getReturnType()) != null) || requestContext instanceof PostMatchContainerRequestContext && rmi.isSse()) { - SseEventOutputImpl sink = new SseEventOutputImpl(new SseEventProvider()); + SseEventOutputImpl sink = new SseEventOutputImpl(new SseEventProvider(providers)); ResteasyContext.getContextDataMap().put(SseEventSink.class, sink); ResteasyContext.getContextData(PostResourceMethodInvokers.class).addInvokers(new PostResourceMethodInvoker() {