Search in sources :

Example 1 with SseEventSink

use of jakarta.ws.rs.sse.SseEventSink in project jaxrs-api by eclipse-ee4j.

the class MBWCheckResource method sendJAXBElement.

@GET
@Path("jaxbelement")
@Produces(MediaType.SERVER_SENT_EVENTS)
public void sendJAXBElement(@Context SseEventSink sink, @Context Sse sse) {
    try (SseEventSink s = sink) {
        JAXBElement<String> element = new JAXBElement<String>(new QName("name"), String.class, MESSAGE);
        s.send(sse.newEventBuilder().data(element).mediaType(MediaType.APPLICATION_XML_TYPE).build());
    }
}
Also used : QName(javax.xml.namespace.QName) SseEventSink(jakarta.ws.rs.sse.SseEventSink) JAXBElement(jakarta.xml.bind.JAXBElement) Path(jakarta.ws.rs.Path) Produces(jakarta.ws.rs.Produces) GET(jakarta.ws.rs.GET)

Example 2 with SseEventSink

use of jakarta.ws.rs.sse.SseEventSink in project jaxrs-api by eclipse-ee4j.

the class MBWCheckResource method sendStreamingOutput.

@GET
@Path("streamingoutput")
@Produces(MediaType.SERVER_SENT_EVENTS)
public void sendStreamingOutput(@Context SseEventSink sink, @Context Sse sse) {
    try (SseEventSink s = sink) {
        StringStreamingOutput output = new StringStreamingOutput(MESSAGE);
        s.send(sse.newEventBuilder().data(output).mediaType(MediaType.WILDCARD_TYPE).build());
    }
}
Also used : SseEventSink(jakarta.ws.rs.sse.SseEventSink) StringStreamingOutput(ee.jakarta.tck.ws.rs.common.impl.StringStreamingOutput) Path(jakarta.ws.rs.Path) Produces(jakarta.ws.rs.Produces) GET(jakarta.ws.rs.GET)

Example 3 with SseEventSink

use of jakarta.ws.rs.sse.SseEventSink in project jaxrs-api by eclipse-ee4j.

the class MediaTypeResource method sendMap.

@GET
@Path("map")
@Produces(MediaType.SERVER_SENT_EVENTS)
public void sendMap(@Context SseEventSink sink, @Context Sse sse) {
    try (SseEventSink s = sink) {
        SinglevaluedMap<String, String> map = new SinglevaluedMap<>();
        map.add("key", SSEMessage.MESSAGE);
        s.send(sse.newEventBuilder().data(map).mediaType(mediaType).build());
    }
}
Also used : SseEventSink(jakarta.ws.rs.sse.SseEventSink) SinglevaluedMap(ee.jakarta.tck.ws.rs.common.impl.SinglevaluedMap) Path(jakarta.ws.rs.Path) Produces(jakarta.ws.rs.Produces) GET(jakarta.ws.rs.GET)

Example 4 with SseEventSink

use of jakarta.ws.rs.sse.SseEventSink in project jaxrs-api by eclipse-ee4j.

the class MediaTypeResource method sendJAXB.

@GET
@Path("jaxb")
@Produces(MediaType.SERVER_SENT_EVENTS)
public void sendJAXB(@Context SseEventSink sink, @Context Sse sse) {
    try (SseEventSink s = sink) {
        JAXBElement<String> element = new JAXBElement<String>(new QName("name"), String.class, SSEMessage.MESSAGE);
        s.send(sse.newEventBuilder().data(element).mediaType(mediaType).build());
    }
}
Also used : QName(javax.xml.namespace.QName) SseEventSink(jakarta.ws.rs.sse.SseEventSink) JAXBElement(jakarta.xml.bind.JAXBElement) Path(jakarta.ws.rs.Path) Produces(jakarta.ws.rs.Produces) GET(jakarta.ws.rs.GET)

Example 5 with SseEventSink

use of jakarta.ws.rs.sse.SseEventSink in project jaxrs-api by eclipse-ee4j.

the class ServiceUnavailableResource method sseLost.

@GET
@Path("sselost")
@Produces(MediaType.SERVER_SENT_EVENTS)
public void sseLost(@Context SseEventSink sink, @Context Sse sse) {
    synchronized (isServiceUnavailable) {
        count++;
        if (isConnectionLost != 0) {
            isConnectionLost--;
            sink.close();
        /*
         * To cancel a stream from the server, respond with a non
         * "text/event-stream" Content-Type or return an HTTP status other than
         * 200 OK and 503 Service Unavailable (e.g. 404 Not Found).
         */
        } else {
            try (SseEventSink s = sink) {
                s.send(sse.newEvent(MESSAGE));
            }
        }
    }
}
Also used : SseEventSink(jakarta.ws.rs.sse.SseEventSink) Path(jakarta.ws.rs.Path) Produces(jakarta.ws.rs.Produces) GET(jakarta.ws.rs.GET)

Aggregations

GET (jakarta.ws.rs.GET)10 Produces (jakarta.ws.rs.Produces)10 SseEventSink (jakarta.ws.rs.sse.SseEventSink)10 Path (jakarta.ws.rs.Path)9 SinglevaluedMap (ee.jakarta.tck.ws.rs.common.impl.SinglevaluedMap)2 JAXBElement (jakarta.xml.bind.JAXBElement)2 QName (javax.xml.namespace.QName)2 JaxbKeyValueBean (ee.jakarta.tck.ws.rs.common.impl.JaxbKeyValueBean)1 StringStreamingOutput (ee.jakarta.tck.ws.rs.common.impl.StringStreamingOutput)1 File (java.io.File)1 IOException (java.io.IOException)1