org.springframework.http.codec.ServerSentEventHttpMessageWriter Java Examples

The following examples show how to use org.springframework.http.codec.ServerSentEventHttpMessageWriter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BodyInsertersTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void createContext() {
	final List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
	messageWriters.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
	messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly()));
	messageWriters.add(new ResourceHttpMessageWriter());
	messageWriters.add(new EncoderHttpMessageWriter<>(new Jaxb2XmlEncoder()));
	Jackson2JsonEncoder jsonEncoder = new Jackson2JsonEncoder();
	messageWriters.add(new EncoderHttpMessageWriter<>(jsonEncoder));
	messageWriters.add(new ServerSentEventHttpMessageWriter(jsonEncoder));
	messageWriters.add(new FormHttpMessageWriter());
	messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
	messageWriters.add(new MultipartHttpMessageWriter(messageWriters));

	this.context = new BodyInserter.Context() {
		@Override
		public List<HttpMessageWriter<?>> messageWriters() {
			return messageWriters;
		}
		@Override
		public Optional<ServerHttpRequest> serverRequest() {
			return Optional.empty();
		}
		@Override
		public Map<String, Object> hints() {
			return hints;
		}
	};
	this.hints = new HashMap<>();
}
 
Example #2
Source File: ServerCodecConfigurerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void jackson2EncoderOverride() {
	Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
	this.configurer.defaultCodecs().jackson2JsonEncoder(encoder);

	assertSame(encoder, this.configurer.getWriters().stream()
			.filter(writer -> ServerSentEventHttpMessageWriter.class.equals(writer.getClass()))
			.map(writer -> (ServerSentEventHttpMessageWriter) writer)
			.findFirst()
			.map(ServerSentEventHttpMessageWriter::getEncoder)
			.filter(e -> e == encoder).orElse(null));
}
 
Example #3
Source File: ServerCodecConfigurerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void assertSseWriter(List<HttpMessageWriter<?>> writers) {
	HttpMessageWriter<?> writer = writers.get(this.index.getAndIncrement());
	assertEquals(ServerSentEventHttpMessageWriter.class, writer.getClass());
	Encoder<?> encoder = ((ServerSentEventHttpMessageWriter) writer).getEncoder();
	assertNotNull(encoder);
	assertEquals(Jackson2JsonEncoder.class, encoder.getClass());
}
 
Example #4
Source File: BodyInsertersTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void createContext() {
	final List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
	messageWriters.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
	messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly()));
	messageWriters.add(new ResourceHttpMessageWriter());
	messageWriters.add(new EncoderHttpMessageWriter<>(new Jaxb2XmlEncoder()));
	Jackson2JsonEncoder jsonEncoder = new Jackson2JsonEncoder();
	messageWriters.add(new EncoderHttpMessageWriter<>(jsonEncoder));
	messageWriters.add(new ServerSentEventHttpMessageWriter(jsonEncoder));
	messageWriters.add(new FormHttpMessageWriter());
	messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
	messageWriters.add(new MultipartHttpMessageWriter(messageWriters));

	this.context = new BodyInserter.Context() {
		@Override
		public List<HttpMessageWriter<?>> messageWriters() {
			return messageWriters;
		}
		@Override
		public Optional<ServerHttpRequest> serverRequest() {
			return Optional.empty();
		}
		@Override
		public Map<String, Object> hints() {
			return hints;
		}
	};
	this.hints = new HashMap<>();
}
 
Example #5
Source File: ServerCodecConfigurerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void jackson2EncoderOverride() {
	Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
	this.configurer.defaultCodecs().jackson2JsonEncoder(encoder);

	assertSame(encoder, this.configurer.getWriters().stream()
			.filter(writer -> ServerSentEventHttpMessageWriter.class.equals(writer.getClass()))
			.map(writer -> (ServerSentEventHttpMessageWriter) writer)
			.findFirst()
			.map(ServerSentEventHttpMessageWriter::getEncoder)
			.filter(e -> e == encoder).orElse(null));
}
 
Example #6
Source File: ServerCodecConfigurerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void assertSseWriter(List<HttpMessageWriter<?>> writers) {
	HttpMessageWriter<?> writer = writers.get(this.index.getAndIncrement());
	assertEquals(ServerSentEventHttpMessageWriter.class, writer.getClass());
	Encoder<?> encoder = ((ServerSentEventHttpMessageWriter) writer).getEncoder();
	assertNotNull(encoder);
	assertEquals(Jackson2JsonEncoder.class, encoder.getClass());
}
 
Example #7
Source File: JacksonJsonCodecInitializer.java    From spring-fu with Apache License 2.0 5 votes vote down vote up
@Override
protected void register(GenericApplicationContext context, CodecConfigurer configurer) {
	ObjectMapper mapper = context.getBean(ObjectMapper.class);
	Jackson2JsonEncoder encoder = new Jackson2JsonEncoder(mapper);
	configurer.customCodecs().decoder(new Jackson2JsonDecoder(mapper));
	configurer.customCodecs().encoder(encoder);
	configurer.customCodecs().writer(new ServerSentEventHttpMessageWriter(encoder));
}
 
Example #8
Source File: ServerDefaultCodecsImpl.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
protected void extendObjectWriters(List<HttpMessageWriter<?>> objectWriters) {
	objectWriters.add(new ServerSentEventHttpMessageWriter(getSseEncoder()));
}
 
Example #9
Source File: ServerDefaultCodecsImpl.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
protected void extendObjectWriters(List<HttpMessageWriter<?>> objectWriters) {
	objectWriters.add(new ServerSentEventHttpMessageWriter(getSseEncoder()));
}