Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit a866d05

Browse files
committed
Removed trailing \n in inbound event raw data.
Change-Id: I8fa15b14209f123bb1764c5e33f171806f9e6daf
1 parent efcb33e commit a866d05

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

media/sse/src/main/java/org/glassfish/jersey/media/sse/InboundEvent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ private InboundEvent(final String name,
229229
this.id = id;
230230
this.comment = comment;
231231
this.reconnectDelay = reconnectDelay;
232-
this.data = data;
232+
this.data = stripLastLineBreak(data);
233233
this.messageBodyWorkers = messageBodyWorkers;
234234
this.annotations = annotations;
235235
this.mediaType = mediaType;
@@ -389,7 +389,7 @@ private <T> T readAndCast(GenericType<T> type, MediaType effectiveMediaType, Mes
389389
annotations,
390390
effectiveMediaType,
391391
headers,
392-
new ByteArrayInputStream(stripLastLineBreak(data)));
392+
new ByteArrayInputStream(data));
393393
} catch (IOException ex) {
394394
throw new ProcessingException(ex);
395395
}

media/sse/src/test/java/org/glassfish/jersey/media/sse/InboundEventReaderTest.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,15 @@
4646
import java.nio.charset.Charset;
4747
import java.util.Collections;
4848

49+
import javax.ws.rs.RuntimeType;
4950
import javax.ws.rs.core.MediaType;
5051

52+
import javax.inject.Singleton;
53+
5154
import org.glassfish.jersey.internal.util.collection.MultivaluedStringMap;
55+
import org.glassfish.jersey.message.MessageBodyWorkers;
56+
import org.glassfish.jersey.message.internal.MessageBodyFactory;
57+
import org.glassfish.jersey.message.internal.MessagingBinders;
5258

5359
import org.glassfish.hk2.api.ServiceLocator;
5460
import org.glassfish.hk2.utilities.ServiceLocatorUtilities;
@@ -94,52 +100,58 @@ public void testReadWithLF() throws Exception {
94100
ByteArrayInputStream inputStream = new ByteArrayInputStream("event: custom-message\ndata: message 1".getBytes());
95101
InboundEvent event = parse(inputStream);
96102
assertEquals("custom-message", event.getName());
97-
assertEquals("message 1\n", new String(event.getRawData(), Charset.defaultCharset()));
103+
assertDataEquals("message 1", event);
98104
}
99105

100106
@Test
101107
public void testReadWithCRLF() throws Exception {
102108
ByteArrayInputStream inputStream = new ByteArrayInputStream("event: custom-message\r\ndata: message 1".getBytes());
103109
InboundEvent event = parse(inputStream);
104110
assertEquals("custom-message", event.getName());
105-
assertEquals("message 1\n", new String(event.getRawData(), Charset.defaultCharset()));
111+
assertDataEquals("message 1", event);
106112
}
107113

108114
@Test
109115
public void testReadWithCR() throws Exception {
110116
ByteArrayInputStream inputStream = new ByteArrayInputStream("event: custom-message\rdata: message 1".getBytes());
111117
InboundEvent event = parse(inputStream);
112118
assertEquals("custom-message", event.getName());
113-
assertEquals("message 1\n", new String(event.getRawData(), Charset.defaultCharset()));
119+
assertDataEquals("message 1", event);
114120
}
115121

116122
@Test
117123
public void testReadWithMultipleSpaces() throws Exception {
118124
ByteArrayInputStream inputStream = new ByteArrayInputStream("event: custom-message\rdata: message 1".getBytes());
119125
InboundEvent event = parse(inputStream);
120126
assertEquals("custom-message", event.getName());
121-
assertEquals("message 1\n", new String(event.getRawData(), Charset.defaultCharset()));
127+
assertDataEquals("message 1", event);
122128
}
123129

124130
@Test
125131
public void testReadWithMultipleEndingDelimiter() throws Exception {
126132
ByteArrayInputStream inputStream = new ByteArrayInputStream("event: custom-message\rdata: message 1\r".getBytes());
127133
InboundEvent event = parse(inputStream);
128134
assertEquals("custom-message", event.getName());
129-
assertEquals("message 1\n", new String(event.getRawData(), Charset.defaultCharset()));
135+
assertDataEquals("message 1", event);
130136
}
131137

132138
private static InboundEvent parse(InputStream stream) throws IOException {
133139
return locator.getService(InboundEventReader.class).readFrom(InboundEvent.class, InboundEvent.class, new Annotation[0],
134140
MediaType.valueOf(SseFeature.SERVER_SENT_EVENTS), headers, stream);
135141
}
136142

143+
private void assertDataEquals(final String expectedData, final InboundEvent event) {
144+
assertEquals(expectedData, event.readData());
145+
assertEquals(expectedData, new String(event.getRawData(), Charset.defaultCharset()));
146+
}
147+
137148
private static class TestBinder extends AbstractBinder {
138149

139150
@Override
140151
protected void configure() {
141-
bind(InboundEventReader.class)
142-
.to(InboundEventReader.class);
152+
install(new MessagingBinders.MessageBodyProviders(null, RuntimeType.SERVER));
153+
bindAsContract(MessageBodyFactory.class).to(MessageBodyWorkers.class).in(Singleton.class);
154+
bind(InboundEventReader.class).to(InboundEventReader.class);
143155
}
144156
}
145157
}

0 commit comments

Comments
 (0)