Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#859]:Allow SseEventSink.close() to throw IOException #863

Merged
merged 1 commit into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ public void startDomain(@PathParam("id") final String id,
sseEventSink.close();
} catch (final InterruptedException e) {
e.printStackTrace();
} catch (IOException ioe) {
//handle I/O error
}

});
}
}
4 changes: 3 additions & 1 deletion jaxrs-api/src/main/java/jakarta/ws/rs/sse/SseEventSink.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package jakarta.ws.rs.sse;

import java.io.IOException;
import java.util.concurrent.CompletionStage;

/**
Expand Down Expand Up @@ -71,7 +72,8 @@ public interface SseEventSink extends AutoCloseable {
* <p>
* Subsequent calls have no effect and are ignored. Once the {@link SseEventSink} is closed, invoking any method other
* than this one and {@link #isClosed()} would result in an {@link IllegalStateException} being thrown.
* @throws IOException if an I/O error occurs.
*/
@Override
void close();
void close() throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.sse.Sse;
import jakarta.ws.rs.sse.SseEventSink;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

@Path("close")
public class CloseResource {
Expand All @@ -33,6 +36,8 @@ public class CloseResource {

private static volatile boolean isClosed = false;

private static final Logger LOG = Logger.getLogger(CloseResource.class.getName());

@GET
@Path("reset")
@Produces(MediaType.SERVER_SENT_EVENTS)
Expand All @@ -41,6 +46,8 @@ public void reset(@Context SseEventSink sink, @Context Sse sse) {
isClosed = false;
try (SseEventSink s = sink) {
s.send(sse.newEvent("RESET"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}

Expand All @@ -52,15 +59,19 @@ public void send(@Context SseEventSink sink, @Context Sse sse) {
public void run() {
SseEventSink s = sink;
s.send(sse.newEvent(SSEMessage.MESSAGE));
s.close();
isClosed = s.isClosed();
if (!isClosed)
return;
s.close();
isClosed = s.isClosed();
if (!isClosed)
return;
s.close();
try {
s.close();
isClosed = s.isClosed();
if (!isClosed)
return;
s.close();
isClosed = s.isClosed();
if (!isClosed)
return;
s.close();
} catch (IOException e) {
//ignore this exception and isClosed will be checked later.
}
isClosed = s.isClosed();
if (!isClosed)
return;
Expand Down Expand Up @@ -88,6 +99,8 @@ public void check(@Context SseEventSink sink, @Context Sse sse) {
return;
}
s.send(sse.newEvent("CHECK"));
} catch (IOException e) {
LOG.log(Level.WARNING, "Failed to close SseEventSink", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@

package ee.jakarta.tck.ws.rs.jaxrs21.ee.sse.sseeventsink;

import ee.jakarta.tck.ws.rs.jaxrs21.ee.sse.sseeventsource.ServiceUnavailableResource;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.namespace.QName;

import ee.jakarta.tck.ws.rs.common.impl.SinglevaluedMap;
Expand All @@ -46,14 +49,16 @@
@Path("mbw")
public class MBWCheckResource {
static final String MESSAGE = SSEMessage.MESSAGE;

private static final Logger LOG = Logger.getLogger(MBWCheckResource.class.getName());
@GET
@Path("boolean")
@Produces(MediaType.SERVER_SENT_EVENTS)
public void sendBoolean(@Context SseEventSink sink, @Context Sse sse) {
try (SseEventSink s = sink) {
s.send(sse.newEventBuilder().data(true)
.mediaType(MediaType.TEXT_PLAIN_TYPE).build());
} catch (IOException e) {
LOG.log(Level.WARNING, "Failed to close SseEventSink", e);
}
}

Expand All @@ -64,6 +69,8 @@ public void sendByteArray(@Context SseEventSink sink, @Context Sse sse) {
try (SseEventSink s = sink) {
s.send(sse.newEventBuilder().data(MESSAGE.getBytes())
.mediaType(MediaType.WILDCARD_TYPE).build());
} catch (IOException e) {
LOG.log(Level.WARNING, "Failed to close SseEventSink", e);
}
}

Expand All @@ -74,6 +81,8 @@ public void sendChar(@Context SseEventSink sink, @Context Sse sse) {
try (SseEventSink s = sink) {
s.send(sse.newEventBuilder().data(MESSAGE.charAt(0))
.mediaType(MediaType.TEXT_PLAIN_TYPE).build());
} catch (IOException e) {
LOG.log(Level.WARNING, "Failed to close SseEventSink", e);
}
}

Expand All @@ -85,6 +94,8 @@ public void sendDatasource(@Context SseEventSink sink, @Context Sse sse) {
s.send(sse.newEventBuilder()
.data(new StringDataSource(MESSAGE, MediaType.TEXT_PLAIN_TYPE))
.mediaType(MediaType.WILDCARD_TYPE).build());
} catch (IOException e) {
LOG.log(Level.WARNING, "Failed to close SseEventSink", e);
}
}

Expand All @@ -95,6 +106,8 @@ public void sendDouble(@Context SseEventSink sink, @Context Sse sse) {
try (SseEventSink s = sink) {
s.send(sse.newEventBuilder().data(Double.MAX_VALUE)
.mediaType(MediaType.TEXT_PLAIN_TYPE).build());
} catch (IOException e) {
LOG.log(Level.WARNING, "Failed to close SseEventSink", e);
}
}

Expand All @@ -115,6 +128,8 @@ public void sendFile(@Context SseEventSink sink, @Context Sse sse) {
s.send(sse.newEvent(e.getMessage()));
throw new RuntimeException(e); // log to server log
}
} catch (IOException e) {
LOG.log(Level.WARNING, "Failed to close SseEventSink", e);
}
}

Expand All @@ -126,6 +141,8 @@ public void sendInputStream(@Context SseEventSink sink, @Context Sse sse) {
s.send(sse.newEventBuilder()
.data(new ByteArrayInputStream(MESSAGE.getBytes()))
.mediaType(MediaType.WILDCARD_TYPE).build());
} catch (IOException e) {
LOG.log(Level.WARNING, "Failed to close SseEventSink", e);
}
}

Expand All @@ -136,6 +153,8 @@ public void sendInt(@Context SseEventSink sink, @Context Sse sse) {
try (SseEventSink s = sink) {
s.send(sse.newEventBuilder().data(Integer.MIN_VALUE)
.mediaType(MediaType.TEXT_PLAIN_TYPE).build());
} catch (IOException e) {
LOG.log(Level.WARNING, "Failed to close SseEventSink", e);
}
}

Expand All @@ -148,6 +167,8 @@ public void sendJAXBElement(@Context SseEventSink sink, @Context Sse sse) {
String.class, MESSAGE);
s.send(sse.newEventBuilder().data(element)
.mediaType(MediaType.APPLICATION_XML_TYPE).build());
} catch (IOException e) {
LOG.log(Level.WARNING, "Failed to close SseEventSink", e);
}
}

Expand All @@ -160,6 +181,8 @@ public void sendMultivaluedMap(@Context SseEventSink sink, @Context Sse sse) {
map.add("name", MESSAGE);
s.send(sse.newEventBuilder().data(map)
.mediaType(MediaType.APPLICATION_FORM_URLENCODED_TYPE).build());
} catch (IOException e) {
LOG.log(Level.WARNING, "Failed to close SseEventSink", e);
}
}

Expand All @@ -172,6 +195,8 @@ public void sendReader(@Context SseEventSink sink, @Context Sse sse) {
.data(new InputStreamReader(
new ByteArrayInputStream(MESSAGE.getBytes())))
.mediaType(MediaType.WILDCARD_TYPE).build());
} catch (IOException e) {
LOG.log(Level.WARNING, "Failed to close SseEventSink", e);
}
}

Expand All @@ -184,6 +209,8 @@ public void sendStreamingOutput(@Context SseEventSink sink,
StringStreamingOutput output = new StringStreamingOutput(MESSAGE);
s.send(sse.newEventBuilder().data(output)
.mediaType(MediaType.WILDCARD_TYPE).build());
} catch (IOException e) {
LOG.log(Level.WARNING, "Failed to close SseEventSink", e);
}
}

Expand All @@ -194,6 +221,8 @@ public void sendString(@Context SseEventSink sink, @Context Sse sse) {
try (SseEventSink s = sink) {
s.send(sse.newEventBuilder().data(MESSAGE)
.mediaType(MediaType.WILDCARD_TYPE).build());
} catch (IOException e) {
LOG.log(Level.WARNING, "Failed to close SseEventSink", e);
}
}

Expand All @@ -205,6 +234,8 @@ public void sendTransformSource(@Context SseEventSink sink,
try (SseEventSink s = sink) {
s.send(sse.newEventBuilder().data(new StringSource(MESSAGE))
.mediaType(MediaType.TEXT_XML_TYPE).build());
} catch (IOException e) {
LOG.log(Level.WARNING, "Failed to close SseEventSink", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static ee.jakarta.tck.ws.rs.jaxrs21.ee.sse.SSEJAXRSClient.MESSAGE;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;

import jakarta.ws.rs.GET;
Expand All @@ -34,7 +35,7 @@ public class StageCheckerResource {

@GET
@Produces(MediaType.SERVER_SENT_EVENTS)
public void send(@Context SseEventSink sink, @Context Sse sse) {
public void send(@Context SseEventSink sink, @Context Sse sse) throws IOException{
try (SseEventSink s = sink) {
CompletableFuture<?> stage = s.send(sse.newEvent(MESSAGE))
.toCompletableFuture();
Expand All @@ -48,6 +49,8 @@ public void send(@Context SseEventSink sink, @Context Sse sse) {
}
}
s.send(sse.newEvent(DONE));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package ee.jakarta.tck.ws.rs.jaxrs21.ee.sse.sseeventsource;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.namespace.QName;

import ee.jakarta.tck.ws.rs.common.impl.JaxbKeyValueBean;
Expand All @@ -31,11 +34,11 @@
import jakarta.ws.rs.sse.Sse;
import jakarta.ws.rs.sse.SseEventSink;
import jakarta.xml.bind.JAXBElement;

import java.util.logging.Logger;
@Path("media")
public class MediaTypeResource {
private static MediaType mediaType = MediaType.WILDCARD_TYPE;

private static final Logger LOG = Logger.getLogger(MediaTypeResource.class.getName());
@POST
@Path("set")
public String setMediaType(String media) {
Expand All @@ -51,6 +54,8 @@ public void sendData(@Context SseEventSink sink, @Context Sse sse) {
try (SseEventSink s = sink) {
s.send(sse.newEventBuilder().data(SSEMessage.MESSAGE).mediaType(mediaType)
.build());
} catch (IOException e) {
LOG.log(Level.WARNING, "Failed to close SseEventSink", e);
}
}

Expand All @@ -62,6 +67,8 @@ public void sendJAXB(@Context SseEventSink sink, @Context Sse sse) {
JAXBElement<String> element = new JAXBElement<String>(new QName("name"),
String.class, SSEMessage.MESSAGE);
s.send(sse.newEventBuilder().data(element).mediaType(mediaType).build());
} catch (IOException e) {
LOG.log(Level.WARNING, "Failed to close SseEventSink", e);
}
}

Expand All @@ -73,6 +80,8 @@ public void sendXML(@Context SseEventSink sink, @Context Sse sse) {
JaxbKeyValueBean bean = new JaxbKeyValueBean();
bean.set("key", SSEMessage.MESSAGE);
s.send(sse.newEventBuilder().data(bean).mediaType(mediaType).build());
} catch (IOException e) {
LOG.log(Level.WARNING, "Failed to close SseEventSink", e);
}
}

Expand All @@ -84,6 +93,8 @@ public void sendMap(@Context SseEventSink sink, @Context Sse sse) {
SinglevaluedMap<String, String> map = new SinglevaluedMap<>();
map.add("key", SSEMessage.MESSAGE);
s.send(sse.newEventBuilder().data(map).mediaType(mediaType).build());
} catch (IOException e) {
LOG.log(Level.WARNING, "Failed to close SseEventSink", e);
}
}
}
Loading