org.springframework.http.codec.protobuf.ProtobufEncoder Java Examples

The following examples show how to use org.springframework.http.codec.protobuf.ProtobufEncoder. 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: BaseDefaultCodecs.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Return writers that support specific types.
 * @param forMultipart whether to returns writers for general use ("false"),
 * or for multipart requests only ("true"). Generally the two sets are the
 * same except for the multipart writer itself.
 */
@SuppressWarnings("unchecked")
final List<HttpMessageWriter<?>> getTypedWriters(boolean forMultipart) {
	if (!this.registerDefaults) {
		return Collections.emptyList();
	}
	List<HttpMessageWriter<?>> writers = new ArrayList<>();
	writers.add(new EncoderHttpMessageWriter<>(new ByteArrayEncoder()));
	writers.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
	writers.add(new EncoderHttpMessageWriter<>(new DataBufferEncoder()));
	writers.add(new ResourceHttpMessageWriter());
	writers.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly()));
	// No client or server specific multipart writers currently..
	if (!forMultipart) {
		extendTypedWriters(writers);
	}
	if (protobufPresent) {
		Encoder<?> encoder = this.protobufEncoder != null ? this.protobufEncoder : new ProtobufEncoder();
		writers.add(new ProtobufHttpMessageWriter((Encoder) encoder));
	}
	return writers;
}
 
Example #2
Source File: CodecConfigurerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void encoderDecoderOverrides() {
	Jackson2JsonDecoder jacksonDecoder = new Jackson2JsonDecoder();
	Jackson2JsonEncoder jacksonEncoder = new Jackson2JsonEncoder();
	ProtobufDecoder protobufDecoder = new ProtobufDecoder(ExtensionRegistry.newInstance());
	ProtobufEncoder protobufEncoder = new ProtobufEncoder();
	Jaxb2XmlEncoder jaxb2Encoder = new Jaxb2XmlEncoder();
	Jaxb2XmlDecoder jaxb2Decoder = new Jaxb2XmlDecoder();

	this.configurer.defaultCodecs().jackson2JsonDecoder(jacksonDecoder);
	this.configurer.defaultCodecs().jackson2JsonEncoder(jacksonEncoder);
	this.configurer.defaultCodecs().protobufDecoder(protobufDecoder);
	this.configurer.defaultCodecs().protobufEncoder(protobufEncoder);
	this.configurer.defaultCodecs().jaxb2Decoder(jaxb2Decoder);
	this.configurer.defaultCodecs().jaxb2Encoder(jaxb2Encoder);

	assertDecoderInstance(jacksonDecoder);
	assertDecoderInstance(protobufDecoder);
	assertDecoderInstance(jaxb2Decoder);
	assertEncoderInstance(jacksonEncoder);
	assertEncoderInstance(protobufEncoder);
	assertEncoderInstance(jaxb2Encoder);
}
 
Example #3
Source File: BaseDefaultCodecs.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Return writers that support specific types.
 * @param forMultipart whether to returns writers for general use ("false"),
 * or for multipart requests only ("true"). Generally the two sets are the
 * same except for the multipart writer itself.
 */
@SuppressWarnings("unchecked")
final List<HttpMessageWriter<?>> getTypedWriters(boolean forMultipart) {
	if (!this.registerDefaults) {
		return Collections.emptyList();
	}
	List<HttpMessageWriter<?>> writers = new ArrayList<>();
	writers.add(new EncoderHttpMessageWriter<>(new ByteArrayEncoder()));
	writers.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
	writers.add(new EncoderHttpMessageWriter<>(new DataBufferEncoder()));
	writers.add(new ResourceHttpMessageWriter());
	writers.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly()));
	// No client or server specific multipart writers currently..
	if (!forMultipart) {
		extendTypedWriters(writers);
	}
	if (protobufPresent) {
		Encoder<?> encoder = this.protobufEncoder != null ? this.protobufEncoder : new ProtobufEncoder();
		writers.add(new ProtobufHttpMessageWriter((Encoder) encoder));
	}
	return writers;
}
 
Example #4
Source File: CodecConfigurerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void encoderDecoderOverrides() {
	Jackson2JsonDecoder jacksonDecoder = new Jackson2JsonDecoder();
	Jackson2JsonEncoder jacksonEncoder = new Jackson2JsonEncoder();
	ProtobufDecoder protobufDecoder = new ProtobufDecoder(ExtensionRegistry.newInstance());
	ProtobufEncoder protobufEncoder = new ProtobufEncoder();
	Jaxb2XmlEncoder jaxb2Encoder = new Jaxb2XmlEncoder();
	Jaxb2XmlDecoder jaxb2Decoder = new Jaxb2XmlDecoder();

	this.configurer.defaultCodecs().jackson2JsonDecoder(jacksonDecoder);
	this.configurer.defaultCodecs().jackson2JsonEncoder(jacksonEncoder);
	this.configurer.defaultCodecs().protobufDecoder(protobufDecoder);
	this.configurer.defaultCodecs().protobufEncoder(protobufEncoder);
	this.configurer.defaultCodecs().jaxb2Decoder(jaxb2Decoder);
	this.configurer.defaultCodecs().jaxb2Encoder(jaxb2Encoder);

	assertDecoderInstance(jacksonDecoder);
	assertDecoderInstance(protobufDecoder);
	assertDecoderInstance(jaxb2Decoder);
	assertEncoderInstance(jacksonEncoder);
	assertEncoderInstance(protobufEncoder);
	assertEncoderInstance(jaxb2Encoder);
}
 
Example #5
Source File: CancelWithoutDemandCodecTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test // gh-22543
public void cancelWithProtobufEncoder() {
	ProtobufEncoder encoder = new ProtobufEncoder();
	Msg msg = Msg.newBuilder().setFoo("Foo").setBlah(SecondMsg.newBuilder().setBlah(123).build()).build();

	Flux<DataBuffer> flux = encoder.encode(Mono.just(msg),
			this.bufferFactory, ResolvableType.forClass(Msg.class),
			new MimeType("application", "x-protobuf"), Collections.emptyMap());

	BaseSubscriber<DataBuffer> subscriber = new ZeroDemandSubscriber();
	flux.subscribe(subscriber); // Assume sync execution (e.g. encoding with Flux.just)..
	subscriber.cancel();
}
 
Example #6
Source File: ProtobufCodecInitializer.java    From spring-fu with Apache License 2.0 4 votes vote down vote up
@Override
protected void register(GenericApplicationContext context, CodecConfigurer configurer) {
	configurer.customCodecs().encoder(new ProtobufEncoder());
	configurer.customCodecs().decoder(new ProtobufDecoder());

}