Skip to content

Commit

Permalink
SseTypeResolver
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Kec <[email protected]>
  • Loading branch information
danielkec committed Jun 15, 2020
1 parent 135a0ea commit 58d0a4f
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,4 @@
package org.glassfish.jersey.internal.jsr166;

public interface JerseyFlowSubscriber<T> extends Flow.Subscriber<T> {
static boolean isSubscriber(Class<?> clazz) {
return org.glassfish.jersey.internal.jsr166.Flow.Subscriber.class.equals(clazz);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<Class> PARAM_ANNOTATION_SET = createParamAnnotationSet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Class<?>> SUPPORTED_SSE_SINK_TYPES;

static {
Set<Class<?>> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -60,7 +60,7 @@ protected Function<ContainerRequest, SseEventSink> 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);
}
Expand Down

0 comments on commit 58d0a4f

Please sign in to comment.