Skip to content

Commit

Permalink
Do not use ResteasyProviderFactory.getInstance() in SseEventProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
asoldano committed Sep 7, 2020
1 parent 803280b commit 3b7b8c4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,6 +38,16 @@ public class SseEventProvider implements AsyncMessageBodyWriter<OutboundSseEvent
{
public static final MediaType GENERAL_STREAM_TYPE = new MediaType("application", "x-stream-general");

private final Providers providers;

public SseEventProvider() {
this(null);
}

public SseEventProvider(final Providers providers) {
this.providers = providers;
}

@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -268,8 +280,9 @@ public CompletionStage<Void> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
{
Expand Down

0 comments on commit 3b7b8c4

Please sign in to comment.