Skip to content

Commit

Permalink
[RESTEASY-2695] Unexpected/invalid sse event when mediaType is defined
Browse files Browse the repository at this point in the history
  • Loading branch information
ronsigal committed Sep 18, 2020
1 parent 0473e23 commit 46ff8c7
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public void writeTo(OutboundSseEvent event, Class<?> type, Type genericType, Ann
throws IOException, WebApplicationException
{
Charset charset = StandardCharsets.UTF_8;
boolean textLike = MediaTypeHelper.isTextLike(mediaType);
boolean escape = event instanceof OutboundSseEventImpl ? ((OutboundSseEventImpl)event).isEscape() : false;
if (event.getComment() != null)
{
Expand Down Expand Up @@ -136,7 +135,19 @@ public void writeTo(OutboundSseEvent event, Class<?> type, Type genericType, Ann
@Override
public void write(int b) throws IOException
{
if (textLike)
//Look at if this actually escape EOL and send valid sse message
if (escape) {
if (b == '\n' || b == '\r' || b == '\\')
{
entityStream.write('\\');
entityStream.write(b);
}
else
{
entityStream.write(b);
}
}
else
{
if (b == '\n' || b == '\r')
{
Expand All @@ -156,18 +167,6 @@ public void write(int b) throws IOException
isNewLine = false;
}
}
else
{
if (escape && (b == '\n' || b == '\r' || b == '\\'))
{
entityStream.write('\\');
entityStream.write(b);
}
else
{
entityStream.write(b);
}
}
}

@Override
Expand All @@ -179,10 +178,7 @@ public void flush() throws IOException
@Override
public void close() throws IOException
{
if (!textLike)
{
entityStream.write(SseConstants.EOL);
}
entityStream.write(SseConstants.EOL);
entityStream.close();
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
public class SseResource
{

public static final String jsonMessage = "{\n"
+ " \"message\": \"json\",\n"
+ " \"foo\": \"bar\"\n"
+ "}";
private final Object outputLock = new Object();
private final Object sseBroadcasterLock = new Object();

Expand Down Expand Up @@ -383,6 +387,44 @@ public void run()
});
}

@GET
@Path("/json")
@Produces(MediaType.SERVER_SENT_EVENTS)
public void jsonMessage(@Context SseEventSink sink) throws IOException, URISyntaxException
{
if (sink == null)
{
throw new IllegalStateException("No client connected.");
}
this.eventSink = sink;
ExecutorService service = (ExecutorService) servletContext
.getAttribute(ExecutorServletContextListener.TEST_EXECUTOR);
service.execute(new Thread()
{
public void run()
{
if (!eventSink.isClosed() && sending)
{
try
{
synchronized (openLock)
{
eventSink.send(sse.newEventBuilder().id("jsonType").
data(SseResource.jsonMessage).
mediaType(MediaType.APPLICATION_JSON_TYPE).build());
}
Thread.sleep(200);
}
catch (final InterruptedException e)
{
logger.error(e.getMessage(), e);
}

}
}
});
}

public static String toString(final Reader input) throws IOException {

final char[] buffer = new char[2048];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,45 @@ public void testBigMessage() throws Exception
Assert.assertTrue("Unexpceted big size message", m1.equals(m2));
client.close();
}

//Test for https://issues.redhat.com/browse/RESTEASY-2695
@Test
@InSequence(15)
public void testSetJsonType() throws Exception
{
final CountDownLatch latch = new CountDownLatch(1);
final AtomicInteger errors = new AtomicInteger(0);
final List<String> results = new ArrayList<String>();
Client client = ClientBuilder.newBuilder().build();
WebTarget target = client.target(generateURL("/service/server-sent-events/json"));
SseEventSource msgEventSource = SseEventSource.target(target).build();
try (SseEventSource eventSource = msgEventSource)
{
Assert.assertEquals(SseEventSourceImpl.class, eventSource.getClass());
eventSource.register(event -> {
results.add(event.readData());
latch.countDown();
}, ex -> {
errors.incrementAndGet();
logger.error(ex.getMessage(), ex);
throw new RuntimeException(ex);
}) ;
eventSource.open();
boolean waitResult = latch.await(30, TimeUnit.SECONDS);
Assert.assertEquals(0, errors.get());
Assert.assertTrue("Waiting for event to be delivered has timed out.", waitResult);
}
Assert.assertFalse("SseEventSource is not closed", msgEventSource.isOpen());
Assert.assertTrue("1 messages are expected, but is : " + results.size(), results.size() == 1);
ObjectMapper om = new ObjectMapper();
@SuppressWarnings("unchecked")
Map<String, Object> m1 = (Map<String, Object>)(om.readValue(SseResource.jsonMessage, Map.class));
@SuppressWarnings("unchecked")
Map<String, Object> m2 = (Map<String, Object>)(om.readValue(results.get(0), Map.class));
Assert.assertTrue("Unexpceted big size message", m1.equals(m2));
client.close();
}

// @Test
// //This will open a browser and test with html sse client
// public void testHtmlSse() throws Exception
Expand Down

0 comments on commit 46ff8c7

Please sign in to comment.