diff --git a/core-common/src/main/java11/org/glassfish/jersey/internal/jsr166/JerseyFlowSubscriber.java b/core-common/src/main/java11/org/glassfish/jersey/internal/jsr166/JerseyFlowSubscriber.java index ef22ed2a0bf..f774e4f37ad 100644 --- a/core-common/src/main/java11/org/glassfish/jersey/internal/jsr166/JerseyFlowSubscriber.java +++ b/core-common/src/main/java11/org/glassfish/jersey/internal/jsr166/JerseyFlowSubscriber.java @@ -31,9 +31,4 @@ public void cancel() { } }); } - - static boolean isSubscriber(Class clazz) { - return java.util.concurrent.Flow.Subscriber.class.equals(clazz) - || org.glassfish.jersey.internal.jsr166.Flow.Subscriber.class.equals(clazz); - } } diff --git a/core-common/src/main/java8/org/glassfish/jersey/internal/jsr166/JerseyFlowSubscriber.java b/core-common/src/main/java8/org/glassfish/jersey/internal/jsr166/JerseyFlowSubscriber.java index 1fbb3d301a2..dd25372cfc4 100644 --- a/core-common/src/main/java8/org/glassfish/jersey/internal/jsr166/JerseyFlowSubscriber.java +++ b/core-common/src/main/java8/org/glassfish/jersey/internal/jsr166/JerseyFlowSubscriber.java @@ -17,7 +17,4 @@ package org.glassfish.jersey.internal.jsr166; public interface JerseyFlowSubscriber extends Flow.Subscriber { - static boolean isSubscriber(Class clazz) { - return org.glassfish.jersey.internal.jsr166.Flow.Subscriber.class.equals(clazz); - } } diff --git a/core-server/src/main/java/org/glassfish/jersey/server/model/IntrospectionModeller.java b/core-server/src/main/java/org/glassfish/jersey/server/model/IntrospectionModeller.java index c1c6d25f3b1..b2a42c9698a 100644 --- a/core-server/src/main/java/org/glassfish/jersey/server/model/IntrospectionModeller.java +++ b/core-server/src/main/java/org/glassfish/jersey/server/model/IntrospectionModeller.java @@ -40,16 +40,15 @@ import javax.ws.rs.container.AsyncResponse; import javax.ws.rs.container.Suspended; import javax.ws.rs.core.MediaType; -import javax.ws.rs.sse.SseEventSink; import org.glassfish.jersey.internal.Errors; -import org.glassfish.jersey.internal.jsr166.JerseyFlowSubscriber; import org.glassfish.jersey.internal.util.Producer; import org.glassfish.jersey.internal.util.ReflectionHelper; import org.glassfish.jersey.internal.util.Tokenizer; import org.glassfish.jersey.server.ManagedAsync; import org.glassfish.jersey.server.internal.LocalizationMessages; import org.glassfish.jersey.server.model.internal.ModelHelper; +import org.glassfish.jersey.server.model.internal.SseTypeResolver; /** * Utility class for constructing resource model from JAX-RS annotated POJO. @@ -299,7 +298,7 @@ private static void introspectAsyncFeatures(AnnotatedMethod am, ResourceMethod.B } for (Class paramType : am.getParameterTypes()) { - if (JerseyFlowSubscriber.isSubscriber(paramType) || SseEventSink.class.equals(paramType)) { + if (SseTypeResolver.isSseSinkParam(paramType)) { resourceMethodBuilder.sse(); } } diff --git a/core-server/src/main/java/org/glassfish/jersey/server/model/ResourceMethodValidator.java b/core-server/src/main/java/org/glassfish/jersey/server/model/ResourceMethodValidator.java index 30c78447a1b..aa20dc8a1ec 100644 --- a/core-server/src/main/java/org/glassfish/jersey/server/model/ResourceMethodValidator.java +++ b/core-server/src/main/java/org/glassfish/jersey/server/model/ResourceMethodValidator.java @@ -37,12 +37,11 @@ import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.QueryParam; -import javax.ws.rs.sse.SseEventSink; import org.glassfish.jersey.internal.Errors; -import org.glassfish.jersey.internal.jsr166.JerseyFlowSubscriber; import org.glassfish.jersey.server.ContainerRequest; import org.glassfish.jersey.server.internal.LocalizationMessages; +import org.glassfish.jersey.server.model.internal.SseTypeResolver; import org.glassfish.jersey.server.spi.internal.ParameterValueHelper; import org.glassfish.jersey.server.spi.internal.ValueParamProvider; @@ -86,8 +85,7 @@ private void checkMethod(ResourceMethod method) { if ("GET".equals(method.getHttpMethod())) { final long eventSinkCount = invocable.getParameters() .stream() - .filter(parameter -> JerseyFlowSubscriber.isSubscriber(parameter.getRawType()) - || SseEventSink.class.equals(parameter.getRawType())) + .filter(parameter -> SseTypeResolver.isSseSinkParam(parameter.getRawType())) .count(); final boolean isSse = eventSinkCount > 0; @@ -216,8 +214,7 @@ private void checkParameters(ResourceMethod method) { private boolean isSseInjected(final Invocable invocable) { return invocable.getParameters().stream() - .anyMatch(parameter -> JerseyFlowSubscriber.isSubscriber(parameter.getRawType()) - || SseEventSink.class.equals(parameter.getRawType())); + .anyMatch(parameter -> SseTypeResolver.isSseSinkParam(parameter.getRawType())); } private static final Set PARAM_ANNOTATION_SET = createParamAnnotationSet(); diff --git a/core-server/src/main/java/org/glassfish/jersey/server/model/internal/JavaResourceMethodDispatcherProvider.java b/core-server/src/main/java/org/glassfish/jersey/server/model/internal/JavaResourceMethodDispatcherProvider.java index bf297729348..c4598ac7d20 100644 --- a/core-server/src/main/java/org/glassfish/jersey/server/model/internal/JavaResourceMethodDispatcherProvider.java +++ b/core-server/src/main/java/org/glassfish/jersey/server/model/internal/JavaResourceMethodDispatcherProvider.java @@ -28,7 +28,6 @@ import javax.ws.rs.core.Response; import javax.ws.rs.sse.SseEventSink; -import org.glassfish.jersey.internal.jsr166.JerseyFlowSubscriber; import org.glassfish.jersey.server.ContainerRequest; import org.glassfish.jersey.server.internal.inject.ConfiguredValidator; import org.glassfish.jersey.server.model.Invocable; @@ -77,8 +76,7 @@ public ResourceMethodDispatcher create(final Invocable resourceMethod, // return type is void int i = 0; for (final Parameter parameter : resourceMethod.getParameters()) { - if (JerseyFlowSubscriber.isSubscriber(parameter.getRawType()) - || SseEventSink.class.equals(parameter.getRawType())) { + if (SseTypeResolver.isSseSinkParam(parameter.getRawType())) { resourceMethodDispatcher = new SseEventSinkInvoker(resourceMethod, invocationHandler, valueProviders, validator, i); break; diff --git a/core-server/src/main/java/org/glassfish/jersey/server/model/internal/SseTypeResolver.java b/core-server/src/main/java/org/glassfish/jersey/server/model/internal/SseTypeResolver.java new file mode 100644 index 00000000000..6a4f557661a --- /dev/null +++ b/core-server/src/main/java/org/glassfish/jersey/server/model/internal/SseTypeResolver.java @@ -0,0 +1,28 @@ +package org.glassfish.jersey.server.model.internal; + +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +public class SseTypeResolver { + + private static final Set> SUPPORTED_SSE_SINK_TYPES; + + static { + Set> set = new HashSet<>(8); + + set.add(org.glassfish.jersey.internal.jsr166.Flow.Subscriber.class); + set.add(javax.ws.rs.sse.SseEventSink.class); + try { + Class clazz = Class.forName("java.util.concurrent.Flow.Subscriber"); + set.add(clazz); + } catch (ClassNotFoundException e) { + // Not java 9+ + } + SUPPORTED_SSE_SINK_TYPES = Collections.unmodifiableSet(set); + } + + public static boolean isSseSinkParam(Class type) { + return SUPPORTED_SSE_SINK_TYPES.contains(type); + } +} diff --git a/media/sse/src/main/java/org/glassfish/jersey/media/sse/internal/SseEventSinkValueParamProvider.java b/media/sse/src/main/java/org/glassfish/jersey/media/sse/internal/SseEventSinkValueParamProvider.java index ed76f58a3a5..9bd85bd169f 100644 --- a/media/sse/src/main/java/org/glassfish/jersey/media/sse/internal/SseEventSinkValueParamProvider.java +++ b/media/sse/src/main/java/org/glassfish/jersey/media/sse/internal/SseEventSinkValueParamProvider.java @@ -24,12 +24,12 @@ import javax.inject.Inject; import javax.inject.Provider; -import org.glassfish.jersey.internal.jsr166.JerseyFlowSubscriber; import org.glassfish.jersey.server.AsyncContext; import org.glassfish.jersey.server.ContainerRequest; import org.glassfish.jersey.server.internal.inject.AbstractValueParamProvider; import org.glassfish.jersey.server.internal.inject.MultivaluedParameterExtractorProvider; import org.glassfish.jersey.server.model.Parameter; +import org.glassfish.jersey.server.model.internal.SseTypeResolver; import org.glassfish.jersey.server.spi.internal.ValueParamProvider; /** @@ -60,7 +60,7 @@ protected Function createValueProvider(Parameter } final Class rawParameterType = parameter.getRawType(); - if ((JerseyFlowSubscriber.isSubscriber(rawParameterType) || rawParameterType == SseEventSink.class) + if (SseTypeResolver.isSseSinkParam(rawParameterType) && parameter.isAnnotationPresent(Context.class)) { return new SseEventSinkValueSupplier(asyncContextSupplier); }