spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.58k stars 38.13k forks source link

SseEmitter cannot use "send(SseEventBuilder builder)" method with custom SseEventBuilder implementation [SPR-13591] #18169

Closed spring-projects-issues closed 8 years ago

spring-projects-issues commented 9 years ago

Maksym Shalak opened SPR-13591 and commented

SseEmitter class contains method "send(SseEventBuilder builder)" which is supposed to be used for custom messages. But the first line of this method contains code

Set<DataWithMediaType> dataToSend = ((SseEventBuilderImpl) builder).build();

That means if we pass SseEventBuilder to the method we receive a ClassCastException since we cannot cast our implementation of interface SseEventBuilder to built-in implementation SseEventBuilderImpl. And SseEventBuilderImpl is a private class, so we will not be able to override it. So basically that means we cannot create our custom messages at all.


Affects: 4.2.1

Attachments:

Referenced from: commits https://github.com/spring-projects/spring-framework/commit/1c382be00e39060a0a4005d80098365f2bee45d4

spring-projects-issues commented 9 years ago

Rossen Stoyanchev commented

I've removed the class cast and added a build method to SseEventBuilder.

By the way I'd interested to hear more about your use case with creating a custom message, i.e. the underlying reason for wanting to extend SseEventBuilder.

spring-projects-issues commented 9 years ago

Maksym Shalak commented

We just wanted to get rid of the "data:" prefix and pass pure JSON into the stream.

spring-projects-issues commented 9 years ago

Rossen Stoyanchev commented

Is this for Server-Sent Events you mean? According to the spec the field name should be: event, data, id, retry, or otherwise should be ignored. Or if you just want to stream data to the response (not necessarily SSE-formatted) why not use ResponseBodyEmitter instead of SseEmitter?

spring-projects-issues commented 9 years ago

Maksym Shalak commented

Yep, looks like ResponseBodyEmitter better suites my needs, thanks.