Search in sources :

Example 6 with SseEventSink

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

the class StageCheckerResource method send.

@GET
@Produces(MediaType.SERVER_SENT_EVENTS)
public void send(@Context SseEventSink sink, @Context Sse sse) {
    try (SseEventSink s = sink) {
        CompletableFuture<?> stage = s.send(sse.newEvent(MESSAGE)).toCompletableFuture();
        while (!stage.isDone()) {
            try {
                Thread.sleep(200L);
            } catch (InterruptedException e) {
                e.printStackTrace();
                sse.newEvent(e.getMessage());
                return;
            }
        }
        s.send(sse.newEvent(DONE));
    }
}
Also used : SseEventSink(jakarta.ws.rs.sse.SseEventSink) Produces(jakarta.ws.rs.Produces) GET(jakarta.ws.rs.GET)

Example 7 with SseEventSink

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

the class CloseResource method reset.

@GET
@Path("reset")
@Produces(MediaType.SERVER_SENT_EVENTS)
public void reset(@Context SseEventSink sink, @Context Sse sse) {
    exception = false;
    isClosed = false;
    try (SseEventSink s = sink) {
        s.send(sse.newEvent("RESET"));
    }
}
Also used : SseEventSink(jakarta.ws.rs.sse.SseEventSink) Path(jakarta.ws.rs.Path) Produces(jakarta.ws.rs.Produces) GET(jakarta.ws.rs.GET)

Example 8 with SseEventSink

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

the class MBWCheckResource method sendMultivaluedMap.

@GET
@Path("multivaluedmap")
@Produces(MediaType.SERVER_SENT_EVENTS)
public void sendMultivaluedMap(@Context SseEventSink sink, @Context Sse sse) {
    try (SseEventSink s = sink) {
        SinglevaluedMap<String, String> map = new SinglevaluedMap<>();
        map.add("name", MESSAGE);
        s.send(sse.newEventBuilder().data(map).mediaType(MediaType.APPLICATION_FORM_URLENCODED_TYPE).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 9 with SseEventSink

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

the class MBWCheckResource method sendFile.

@GET
@Path("file")
@Produces(MediaType.SERVER_SENT_EVENTS)
public void sendFile(@Context SseEventSink sink, @Context Sse sse) {
    File f;
    try (SseEventSink s = sink) {
        try {
            f = File.createTempFile("tck", "tempfile");
            Files.write(f.toPath(), MESSAGE.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
            f.deleteOnExit();
            s.send(sse.newEventBuilder().data(f).mediaType(MediaType.WILDCARD_TYPE).build());
        } catch (IOException e) {
            s.send(sse.newEvent(e.getMessage()));
            // log to server log
            throw new RuntimeException(e);
        }
    }
}
Also used : SseEventSink(jakarta.ws.rs.sse.SseEventSink) IOException(java.io.IOException) File(java.io.File) Path(jakarta.ws.rs.Path) Produces(jakarta.ws.rs.Produces) GET(jakarta.ws.rs.GET)

Example 10 with SseEventSink

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

the class MediaTypeResource method sendXML.

@GET
@Path("xml")
@Produces(MediaType.SERVER_SENT_EVENTS)
public void sendXML(@Context SseEventSink sink, @Context Sse sse) {
    try (SseEventSink s = sink) {
        JaxbKeyValueBean bean = new JaxbKeyValueBean();
        bean.set("key", SSEMessage.MESSAGE);
        s.send(sse.newEventBuilder().data(bean).mediaType(mediaType).build());
    }
}
Also used : SseEventSink(jakarta.ws.rs.sse.SseEventSink) JaxbKeyValueBean(ee.jakarta.tck.ws.rs.common.impl.JaxbKeyValueBean) 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