Skip to content

Commit

Permalink
Deprecate @RestSseElementType in favour of @RestStreamElementType
Browse files Browse the repository at this point in the history
  • Loading branch information
Sgitario committed Jan 20, 2022
1 parent 584cfde commit 9f1823d
Show file tree
Hide file tree
Showing 16 changed files with 106 additions and 63 deletions.
4 changes: 2 additions & 2 deletions docs/src/main/asciidoc/kafka-schema-registry-avro.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ import javax.ws.rs.core.MediaType;
import org.acme.kafka.quarkus.Movie;
import org.eclipse.microprofile.reactive.messaging.Channel;
import org.jboss.resteasy.reactive.RestSseElementType;
import org.jboss.resteasy.reactive.RestStreamElementType;
import io.smallrye.mutiny.Multi;
Expand All @@ -242,7 +242,7 @@ public class ConsumedMovieResource {
@GET
@Produces(MediaType.SERVER_SENT_EVENTS)
@RestSseElementType(MediaType.TEXT_PLAIN)
@RestStreamElementType(MediaType.TEXT_PLAIN)
public Multi<String> stream() {
return movies.map(movie -> String.format("'%s' from %s", movie.getTitle(), movie.getYear()));
}
Expand Down
6 changes: 3 additions & 3 deletions docs/src/main/asciidoc/resteasy-reactive.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ https://html.spec.whatwg.org/multipage/server-sent-events.html[Server-Sent Event
by just annotating your endpoint method with
link:{jaxrsapi}/javax/ws/rs/Produces.html[`@Produces(MediaType.SERVER_SENT_EVENTS)`]
and specifying that each element should be <<json,serialised to JSON>> with
`@RestSseElementType(MediaType.APPLICATION_JSON)`.
`@RestStreamElementType(MediaType.APPLICATION_JSON)`.

[source,java]
----
Expand All @@ -720,7 +720,7 @@ import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.jboss.resteasy.reactive.RestSseElementType;
import org.jboss.resteasy.reactive.RestStreamElementType;
import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;
Expand All @@ -739,7 +739,7 @@ public class Endpoint {
// Send the stream over SSE
@Produces(MediaType.SERVER_SENT_EVENTS)
// Each element will be sent as JSON
@RestSseElementType(MediaType.APPLICATION_JSON)
@RestStreamElementType(MediaType.APPLICATION_JSON)
public Multi<Book> stream() {
return books;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import javax.ws.rs.sse.SseBroadcaster;
import javax.ws.rs.sse.SseEventSink;

import org.jboss.resteasy.reactive.RestSseElementType;
import org.jboss.resteasy.reactive.RestStreamElementType;
import org.jboss.resteasy.reactive.common.util.RestMediaType;

import io.smallrye.common.annotation.Blocking;
Expand Down Expand Up @@ -43,7 +43,7 @@ public Multi<String> multiText() {
@Path("json")
@GET
@Produces(MediaType.SERVER_SENT_EVENTS)
@RestSseElementType(MediaType.APPLICATION_JSON)
@RestStreamElementType(MediaType.APPLICATION_JSON)
public void sseJson(Sse sse, SseEventSink sink) throws IOException {
if (sink == null) {
throw new IllegalStateException("No client connected.");
Expand All @@ -60,7 +60,7 @@ public void sseJson(Sse sse, SseEventSink sink) throws IOException {
@Path("blocking/json")
@GET
@Produces(MediaType.SERVER_SENT_EVENTS)
@RestSseElementType(MediaType.APPLICATION_JSON)
@RestStreamElementType(MediaType.APPLICATION_JSON)
public void blockingSseJson(Sse sse, SseEventSink sink) throws IOException {
if (sink == null) {
throw new IllegalStateException("No client connected.");
Expand Down Expand Up @@ -94,7 +94,7 @@ public void sseJson2(Sse sse, SseEventSink sink) throws IOException {
@Path("json/multi")
@GET
@Produces(MediaType.SERVER_SENT_EVENTS)
@RestSseElementType(MediaType.APPLICATION_JSON)
@RestStreamElementType(MediaType.APPLICATION_JSON)
public Multi<Message> multiJson() {
return Multi.createFrom().items(new Message("hello"), new Message("stef"));
}
Expand All @@ -109,15 +109,15 @@ public Multi<Message> multiDefaultElementType() {
@Path("ndjson/multi")
@GET
@Produces(RestMediaType.APPLICATION_NDJSON)
@RestSseElementType(MediaType.APPLICATION_JSON)
@RestStreamElementType(MediaType.APPLICATION_JSON)
public Multi<Message> multiNdJson() {
return Multi.createFrom().items(new Message("hello"), new Message("stef"));
}

@Path("stream-json/multi")
@GET
@Produces(RestMediaType.APPLICATION_STREAM_JSON)
@RestSseElementType(MediaType.APPLICATION_JSON)
@RestStreamElementType(MediaType.APPLICATION_JSON)
public Multi<Message> multiStreamJson() {
return Multi.createFrom().items(new Message("hello"), new Message("stef"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.jboss.resteasy.reactive.RestSseElementType;
import org.jboss.resteasy.reactive.RestStreamElementType;
import org.jboss.resteasy.reactive.client.impl.MultiInvoker;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
Expand Down Expand Up @@ -154,7 +154,7 @@ public Multi<String> multiText() {
@Path("xml")
@GET
@Produces(MediaType.SERVER_SENT_EVENTS)
@RestSseElementType(MediaType.APPLICATION_XML)
@RestStreamElementType(MediaType.APPLICATION_XML)
public void sseXml(Sse sse, SseEventSink sink) {
if (sink == null) {
throw new IllegalStateException("No client connected.");
Expand All @@ -170,7 +170,7 @@ public void sseXml(Sse sse, SseEventSink sink) {
@Path("blocking/xml")
@GET
@Produces(MediaType.SERVER_SENT_EVENTS)
@RestSseElementType(MediaType.APPLICATION_XML)
@RestStreamElementType(MediaType.APPLICATION_XML)
public void blockingSseXml(Sse sse, SseEventSink sink) {
if (sink == null) {
throw new IllegalStateException("No client connected.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javax.ws.rs.sse.SseBroadcaster;
import javax.ws.rs.sse.SseEventSink;

// Using `@RestStreamElementType` on purpose to ensure the backward compatibility.
import org.jboss.resteasy.reactive.RestSseElementType;
import org.jboss.resteasy.reactive.common.util.RestMediaType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private boolean isDefaultJson() {
}

private boolean hasJson(ServerResourceMethod method) {
return hasJson(method.getProduces()) || hasJson(method.getConsumes()) || isJson(method.getSseElementType());
return hasJson(method.getProduces()) || hasJson(method.getConsumes()) || isJson(method.getStreamElementType());
}

private boolean hasJson(String[] types) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.REST_QUERY_PARAM;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.REST_RESPONSE;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.REST_SSE_ELEMENT_TYPE;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.REST_STREAM_ELEMENT_TYPE;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.SET;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.SORTED_SET;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.STRING;
Expand Down Expand Up @@ -329,15 +330,10 @@ protected List<ResourceMethod> createEndpoints(ClassInfo currentClassInfo,
String[] classProduces = extractProducesConsumesValues(getAnnotationStore().getAnnotation(currentClassInfo, PRODUCES));
String[] classConsumes = extractProducesConsumesValues(getAnnotationStore().getAnnotation(currentClassInfo, CONSUMES));

String classSseElementType = null;
AnnotationInstance classSseElementTypeAnnotation = getAnnotationStore().getAnnotation(currentClassInfo,
REST_SSE_ELEMENT_TYPE);
if (classSseElementTypeAnnotation != null) {
classSseElementType = classSseElementTypeAnnotation.value().asString();
}
String classStreamElementType = getStreamAnnotationValue(currentClassInfo);

BasicResourceClassInfo basicResourceClassInfo = new BasicResourceClassInfo(resourceClassPath, classProduces,
classConsumes, pathParameters, classSseElementType);
classConsumes, pathParameters, classStreamElementType);

Set<String> classNameBindings = NameBindingUtil.nameBindingNames(index, currentClassInfo);

Expand Down Expand Up @@ -563,19 +559,19 @@ private ResourceMethod createResourceMethod(ClassInfo currentClassInfo, ClassInf
produces = applyDefaultProduces(produces, nonAsyncReturnType);
produces = addDefaultCharsets(produces);

String sseElementType = basicResourceClassInfo.getSseElementType();
AnnotationInstance sseElementTypeAnnotation = getAnnotationStore().getAnnotation(currentMethodInfo,
REST_SSE_ELEMENT_TYPE);
if (sseElementTypeAnnotation != null) {
sseElementType = sseElementTypeAnnotation.value().asString();
String streamElementType = basicResourceClassInfo.getStreamElementType();
String streamElementTypeInMethod = getStreamAnnotationValue(currentMethodInfo);
if (streamElementTypeInMethod != null) {
streamElementType = streamElementTypeInMethod;
}

boolean returnsMultipart = false;
if (produces != null && produces.length == 1) {
if (sseElementType == null && MediaType.SERVER_SENT_EVENTS.equals(produces[0])) {
if (streamElementType == null && MediaType.SERVER_SENT_EVENTS.equals(produces[0])) {
// Handle server sent events responses
String[] defaultProducesForType = applyAdditionalDefaults(nonAsyncReturnType);
if (defaultProducesForType.length == 1) {
sseElementType = defaultProducesForType[0];
streamElementType = defaultProducesForType[0];
}
} else if (MediaType.MULTIPART_FORM_DATA.equals(produces[0])) {
// Handle multipart form data responses
Expand Down Expand Up @@ -614,7 +610,7 @@ private ResourceMethod createResourceMethod(ClassInfo currentClassInfo, ClassInf
.setBlocking(blocking)
.setSuspended(suspended)
.setSse(sse)
.setSseElementType(sseElementType)
.setStreamElementType(streamElementType)
.setFormParamRequired(formParamRequired)
.setMultipart(multipart)
.setParameters(methodParameters)
Expand Down Expand Up @@ -644,6 +640,25 @@ protected void handleClientSubResource(ResourceMethod resourceMethod, MethodInfo

}

private String getStreamAnnotationValue(AnnotationTarget target) {
String value = getAnnotationValueAsString(target, REST_STREAM_ELEMENT_TYPE);
if (value == null) {
value = getAnnotationValueAsString(target, REST_SSE_ELEMENT_TYPE);
}

return value;
}

private String getAnnotationValueAsString(AnnotationTarget target, DotName annotationType) {
String value = null;
AnnotationInstance annotation = getAnnotationStore().getAnnotation(target, annotationType);
if (annotation != null) {
value = annotation.value().asString();
}

return value;
}

private boolean isBlocking(MethodInfo info, BlockingDefault defaultValue) {
Map.Entry<AnnotationTarget, AnnotationInstance> blockingAnnotation = getInheritableAnnotation(info, BLOCKING);
Map.Entry<AnnotationTarget, AnnotationInstance> nonBlockingAnnotation = getInheritableAnnotation(info,
Expand Down Expand Up @@ -1374,15 +1389,15 @@ public static class BasicResourceClassInfo {
private final String[] produces;
private final String[] consumes;
private final Set<String> pathParameters;
private final String sseElementType;
private final String streamElementType;

public BasicResourceClassInfo(String path, String[] produces, String[] consumes, Set<String> pathParameters,
String sseElementType) {
String streamElementType) {
this.path = path;
this.produces = produces;
this.consumes = consumes;
this.pathParameters = pathParameters;
this.sseElementType = sseElementType;
this.streamElementType = streamElementType;
}

public String getPath() {
Expand All @@ -1401,8 +1416,8 @@ public Set<String> getPathParameters() {
return pathParameters;
}

public String getSseElementType() {
return sseElementType;
public String getStreamElementType() {
return streamElementType;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
import org.jboss.resteasy.reactive.RestQuery;
import org.jboss.resteasy.reactive.RestResponse;
import org.jboss.resteasy.reactive.RestSseElementType;
import org.jboss.resteasy.reactive.RestStreamElementType;
import org.reactivestreams.Publisher;

public final class ResteasyReactiveDotNames {
Expand All @@ -111,6 +112,7 @@ public final class ResteasyReactiveDotNames {
.createSimple("org.jboss.resteasy.reactive.server.spi.ServerRequestContext");

public static final DotName REST_SSE_ELEMENT_TYPE = DotName.createSimple(RestSseElementType.class.getName());
public static final DotName REST_STREAM_ELEMENT_TYPE = DotName.createSimple(RestStreamElementType.class.getName());
public static final DotName CONSUMES = DotName.createSimple(Consumes.class.getName());
public static final DotName PRODUCES = DotName.createSimple(Produces.class.getName());
public static final DotName PROVIDER = DotName.createSimple(Provider.class.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@

/**
* Defines the MIME type of each SSE element in the annotated stream.
*
* @deprecated replaced by {@link RestStreamElementType}
*/
@Inherited
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Deprecated
public @interface RestSseElementType {
String value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.jboss.resteasy.reactive;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Defines the MIME type of each SSE element in the annotated stream.
*/
@Inherited
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RestStreamElementType {
String value();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import org.jboss.resteasy.reactive.RestSseElementType;
import org.jboss.resteasy.reactive.RestStreamElementType;

/**
* A representation of a REST endpoint. This is passed directly to recorders so must be bytecode serializable.
Expand All @@ -30,10 +31,11 @@ public class ResourceMethod {
private String[] produces;

/**
* The value of the {@link RestSseElementType} annotation, if none is specified on the method
* The value of the {@link RestStreamElementType} or the {@link RestSseElementType} annotation, if none is specified on the
* method
* then this represents the value inherited from the class level, or null if not specified.
*/
private String sseElementType;
private String streamElementType;

/**
* The value of the {@link Consumes} annotation, if none is specified on the method
Expand Down Expand Up @@ -68,14 +70,14 @@ public class ResourceMethod {
public ResourceMethod() {
}

public ResourceMethod(String httpMethod, String path, String[] produces, String sseElementType, String[] consumes,
public ResourceMethod(String httpMethod, String path, String[] produces, String streamElementType, String[] consumes,
Set<String> nameBindingNames, String name, String returnType, String simpleReturnType, MethodParameter[] parameters,
boolean blocking, boolean suspended, boolean isSse, boolean isFormParamRequired, boolean isMultipart,
List<ResourceMethod> subResourceMethods) {
this.httpMethod = httpMethod;
this.path = path;
this.produces = produces;
this.sseElementType = sseElementType;
this.streamElementType = streamElementType;
this.consumes = consumes;
this.nameBindingNames = nameBindingNames;
this.name = name;
Expand Down Expand Up @@ -220,13 +222,13 @@ public ResourceMethod setMultipart(boolean isMultipart) {
return this;
}

public ResourceMethod setSseElementType(String sseElementType) {
this.sseElementType = sseElementType;
public ResourceMethod setStreamElementType(String streamElementType) {
this.streamElementType = streamElementType;
return this;
}

public String getSseElementType() {
return sseElementType;
public String getStreamElementType() {
return streamElementType;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
/**
* Extended media types in Resteasy Reactive.
*/
public final class RestMediaType {
public class RestMediaType extends MediaType {

public static final String APPLICATION_NDJSON = "application/x-ndjson";
public static final MediaType APPLICATION_NDJSON_TYPE = new MediaType("application", "x-ndjson");
public static final RestMediaType APPLICATION_NDJSON_TYPE = new RestMediaType("application", "x-ndjson");
public static final String APPLICATION_STREAM_JSON = "application/stream+json";
public static final MediaType APPLICATION_STREAM_JSON_TYPE = new MediaType("application", "stream+json");

private RestMediaType() {
public static final RestMediaType APPLICATION_STREAM_JSON_TYPE = new RestMediaType("application", "stream+json");

public RestMediaType(String type, String subtype) {
super(type, subtype);
}
}
Loading

0 comments on commit 9f1823d

Please sign in to comment.