Support Protobuf serialization in WebFlux

This commit introduces Protobuf support in WebFlux via dedicated
codecs.

Flux<Message> are serialized/deserialized using delimited Protobuf
messages with the size of each message specified before the message
itself. In that case, a "delimited=true" parameter is added to the
content type.

Mono<Message> are expected to use regular Protobuf message
format (without the size prepended before the message).

Related HttpMessageReader/Writer are automatically registered when the
"com.google.protobuf:protobuf-java" library is detected in the classpath,
and can be customized easily if needed via CodecConfigurer, for example
to specify protocol extensions via the ExtensionRegistry based
constructors.

Both "application/x-protobuf" and "application/octet-stream" mime types
are supported.

Issue: SPR-15776
This commit is contained in:
sdeleuze
2018-03-19 18:16:46 +01:00
committed by Sebastien Deleuze
parent 4475c67ba8
commit 36a07aa897
23 changed files with 2225 additions and 18 deletions

View File

@@ -95,7 +95,7 @@ public class DelegatingWebFluxConfigurationTests {
assertNotNull(initializer);
assertTrue(initializer.getValidator() instanceof LocalValidatorFactoryBean);
assertSame(formatterRegistry.getValue(), initializer.getConversionService());
assertEquals(12, codecsConfigurer.getValue().getReaders().size());
assertEquals(13, codecsConfigurer.getValue().getReaders().size());
}
@Test

View File

@@ -24,6 +24,7 @@ import java.util.List;
import java.util.Map;
import javax.xml.bind.annotation.XmlRootElement;
import com.google.protobuf.Message;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
@@ -148,7 +149,7 @@ public class WebFluxConfigurationSupportTests {
assertNotNull(adapter);
List<HttpMessageReader<?>> readers = adapter.getMessageReaders();
assertEquals(12, readers.size());
assertEquals(13, readers.size());
ResolvableType multiValueMapType = forClassWithGenerics(MultiValueMap.class, String.class, String.class);
@@ -156,6 +157,7 @@ public class WebFluxConfigurationSupportTests {
assertHasMessageReader(readers, forClass(ByteBuffer.class), APPLICATION_OCTET_STREAM);
assertHasMessageReader(readers, forClass(String.class), TEXT_PLAIN);
assertHasMessageReader(readers, forClass(Resource.class), IMAGE_PNG);
assertHasMessageReader(readers, forClass(Message.class), new MediaType("application", "x-protobuf"));
assertHasMessageReader(readers, multiValueMapType, APPLICATION_FORM_URLENCODED);
assertHasMessageReader(readers, forClass(TestBean.class), APPLICATION_XML);
assertHasMessageReader(readers, forClass(TestBean.class), APPLICATION_JSON);
@@ -202,12 +204,13 @@ public class WebFluxConfigurationSupportTests {
assertEquals(0, handler.getOrder());
List<HttpMessageWriter<?>> writers = handler.getMessageWriters();
assertEquals(10, writers.size());
assertEquals(11, writers.size());
assertHasMessageWriter(writers, forClass(byte[].class), APPLICATION_OCTET_STREAM);
assertHasMessageWriter(writers, forClass(ByteBuffer.class), APPLICATION_OCTET_STREAM);
assertHasMessageWriter(writers, forClass(String.class), TEXT_PLAIN);
assertHasMessageWriter(writers, forClass(Resource.class), IMAGE_PNG);
assertHasMessageWriter(writers, forClass(Message.class), new MediaType("application", "x-protobuf"));
assertHasMessageWriter(writers, forClass(TestBean.class), APPLICATION_XML);
assertHasMessageWriter(writers, forClass(TestBean.class), APPLICATION_JSON);
assertHasMessageWriter(writers, forClass(TestBean.class), new MediaType("application", "x-jackson-smile"));
@@ -229,12 +232,13 @@ public class WebFluxConfigurationSupportTests {
assertEquals(100, handler.getOrder());
List<HttpMessageWriter<?>> writers = handler.getMessageWriters();
assertEquals(10, writers.size());
assertEquals(11, writers.size());
assertHasMessageWriter(writers, forClass(byte[].class), APPLICATION_OCTET_STREAM);
assertHasMessageWriter(writers, forClass(ByteBuffer.class), APPLICATION_OCTET_STREAM);
assertHasMessageWriter(writers, forClass(String.class), TEXT_PLAIN);
assertHasMessageWriter(writers, forClass(Resource.class), IMAGE_PNG);
assertHasMessageWriter(writers, forClass(Message.class), new MediaType("application", "x-protobuf"));
assertHasMessageWriter(writers, forClass(TestBean.class), APPLICATION_XML);
assertHasMessageWriter(writers, forClass(TestBean.class), APPLICATION_JSON);
assertHasMessageWriter(writers, forClass(TestBean.class), new MediaType("application", "x-jackson-smile"));

View File

@@ -0,0 +1,654 @@
/*
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: sample.proto
package org.springframework.web.reactive.protobuf;
/**
* Protobuf type {@code Msg}
*/
public final class Msg extends
com.google.protobuf.GeneratedMessage
implements MsgOrBuilder {
// Use Msg.newBuilder() to construct.
private Msg(com.google.protobuf.GeneratedMessage.Builder<?> builder) {
super(builder);
this.unknownFields = builder.getUnknownFields();
}
private Msg(boolean noInit) { this.unknownFields = com.google.protobuf.UnknownFieldSet.getDefaultInstance(); }
private static final Msg defaultInstance;
public static Msg getDefaultInstance() {
return defaultInstance;
}
public Msg getDefaultInstanceForType() {
return defaultInstance;
}
private final com.google.protobuf.UnknownFieldSet unknownFields;
@Override
public final com.google.protobuf.UnknownFieldSet
getUnknownFields() {
return this.unknownFields;
}
private Msg(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
initFields();
@SuppressWarnings("unused")
int mutable_bitField0_ = 0;
com.google.protobuf.UnknownFieldSet.Builder unknownFields =
com.google.protobuf.UnknownFieldSet.newBuilder();
try {
boolean done = false;
while (!done) {
int tag = input.readTag();
switch (tag) {
case 0:
done = true;
break;
default: {
if (!parseUnknownField(input, unknownFields,
extensionRegistry, tag)) {
done = true;
}
break;
}
case 10: {
bitField0_ |= 0x00000001;
foo_ = input.readBytes();
break;
}
case 18: {
SecondMsg.Builder subBuilder = null;
if (((bitField0_ & 0x00000002) == 0x00000002)) {
subBuilder = blah_.toBuilder();
}
blah_ = input.readMessage(SecondMsg.PARSER, extensionRegistry);
if (subBuilder != null) {
subBuilder.mergeFrom(blah_);
blah_ = subBuilder.buildPartial();
}
bitField0_ |= 0x00000002;
break;
}
}
}
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
throw e.setUnfinishedMessage(this);
} catch (java.io.IOException e) {
throw new com.google.protobuf.InvalidProtocolBufferException(
e.getMessage()).setUnfinishedMessage(this);
} finally {
this.unknownFields = unknownFields.build();
makeExtensionsImmutable();
}
}
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return OuterSample.internal_static_Msg_descriptor;
}
protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
internalGetFieldAccessorTable() {
return OuterSample.internal_static_Msg_fieldAccessorTable
.ensureFieldAccessorsInitialized(
Msg.class, Msg.Builder.class);
}
public static com.google.protobuf.Parser<Msg> PARSER =
new com.google.protobuf.AbstractParser<Msg>() {
public Msg parsePartialFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return new Msg(input, extensionRegistry);
}
};
@Override
public com.google.protobuf.Parser<Msg> getParserForType() {
return PARSER;
}
private int bitField0_;
// optional string foo = 1;
public static final int FOO_FIELD_NUMBER = 1;
private Object foo_;
/**
* <code>optional string foo = 1;</code>
*/
public boolean hasFoo() {
return ((bitField0_ & 0x00000001) == 0x00000001);
}
/**
* <code>optional string foo = 1;</code>
*/
public String getFoo() {
Object ref = foo_;
if (ref instanceof String) {
return (String) ref;
} else {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
String s = bs.toStringUtf8();
if (bs.isValidUtf8()) {
foo_ = s;
}
return s;
}
}
/**
* <code>optional string foo = 1;</code>
*/
public com.google.protobuf.ByteString
getFooBytes() {
Object ref = foo_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(String) ref);
foo_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
// optional .SecondMsg blah = 2;
public static final int BLAH_FIELD_NUMBER = 2;
private SecondMsg blah_;
/**
* <code>optional .SecondMsg blah = 2;</code>
*/
public boolean hasBlah() {
return ((bitField0_ & 0x00000002) == 0x00000002);
}
/**
* <code>optional .SecondMsg blah = 2;</code>
*/
public SecondMsg getBlah() {
return blah_;
}
/**
* <code>optional .SecondMsg blah = 2;</code>
*/
public SecondMsgOrBuilder getBlahOrBuilder() {
return blah_;
}
private void initFields() {
foo_ = "";
blah_ = SecondMsg.getDefaultInstance();
}
private byte memoizedIsInitialized = -1;
public final boolean isInitialized() {
byte isInitialized = memoizedIsInitialized;
if (isInitialized != -1) return isInitialized == 1;
memoizedIsInitialized = 1;
return true;
}
public void writeTo(com.google.protobuf.CodedOutputStream output)
throws java.io.IOException {
getSerializedSize();
if (((bitField0_ & 0x00000001) == 0x00000001)) {
output.writeBytes(1, getFooBytes());
}
if (((bitField0_ & 0x00000002) == 0x00000002)) {
output.writeMessage(2, blah_);
}
getUnknownFields().writeTo(output);
}
private int memoizedSerializedSize = -1;
public int getSerializedSize() {
int size = memoizedSerializedSize;
if (size != -1) return size;
size = 0;
if (((bitField0_ & 0x00000001) == 0x00000001)) {
size += com.google.protobuf.CodedOutputStream
.computeBytesSize(1, getFooBytes());
}
if (((bitField0_ & 0x00000002) == 0x00000002)) {
size += com.google.protobuf.CodedOutputStream
.computeMessageSize(2, blah_);
}
size += getUnknownFields().getSerializedSize();
memoizedSerializedSize = size;
return size;
}
private static final long serialVersionUID = 0L;
@Override
protected Object writeReplace()
throws java.io.ObjectStreamException {
return super.writeReplace();
}
public static Msg parseFrom(
com.google.protobuf.ByteString data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static Msg parseFrom(
com.google.protobuf.ByteString data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static Msg parseFrom(byte[] data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static Msg parseFrom(
byte[] data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static Msg parseFrom(java.io.InputStream input)
throws java.io.IOException {
return PARSER.parseFrom(input);
}
public static Msg parseFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseFrom(input, extensionRegistry);
}
public static Msg parseDelimitedFrom(java.io.InputStream input)
throws java.io.IOException {
return PARSER.parseDelimitedFrom(input);
}
public static Msg parseDelimitedFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseDelimitedFrom(input, extensionRegistry);
}
public static Msg parseFrom(
com.google.protobuf.CodedInputStream input)
throws java.io.IOException {
return PARSER.parseFrom(input);
}
public static Msg parseFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseFrom(input, extensionRegistry);
}
public static Builder newBuilder() { return Builder.create(); }
public Builder newBuilderForType() { return newBuilder(); }
public static Builder newBuilder(Msg prototype) {
return newBuilder().mergeFrom(prototype);
}
public Builder toBuilder() { return newBuilder(this); }
@Override
protected Builder newBuilderForType(
com.google.protobuf.GeneratedMessage.BuilderParent parent) {
Builder builder = new Builder(parent);
return builder;
}
/**
* Protobuf type {@code Msg}
*/
public static final class Builder extends
com.google.protobuf.GeneratedMessage.Builder<Builder>
implements MsgOrBuilder {
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return OuterSample.internal_static_Msg_descriptor;
}
protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
internalGetFieldAccessorTable() {
return OuterSample.internal_static_Msg_fieldAccessorTable
.ensureFieldAccessorsInitialized(
Msg.class, Msg.Builder.class);
}
// Construct using org.springframework.protobuf.Msg.newBuilder()
private Builder() {
maybeForceBuilderInitialization();
}
private Builder(
com.google.protobuf.GeneratedMessage.BuilderParent parent) {
super(parent);
maybeForceBuilderInitialization();
}
private void maybeForceBuilderInitialization() {
if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) {
getBlahFieldBuilder();
}
}
private static Builder create() {
return new Builder();
}
public Builder clear() {
super.clear();
foo_ = "";
bitField0_ = (bitField0_ & ~0x00000001);
if (blahBuilder_ == null) {
blah_ = SecondMsg.getDefaultInstance();
} else {
blahBuilder_.clear();
}
bitField0_ = (bitField0_ & ~0x00000002);
return this;
}
public Builder clone() {
return create().mergeFrom(buildPartial());
}
public com.google.protobuf.Descriptors.Descriptor
getDescriptorForType() {
return OuterSample.internal_static_Msg_descriptor;
}
public Msg getDefaultInstanceForType() {
return Msg.getDefaultInstance();
}
public Msg build() {
Msg result = buildPartial();
if (!result.isInitialized()) {
throw newUninitializedMessageException(result);
}
return result;
}
public Msg buildPartial() {
Msg result = new Msg(this);
int from_bitField0_ = bitField0_;
int to_bitField0_ = 0;
if (((from_bitField0_ & 0x00000001) == 0x00000001)) {
to_bitField0_ |= 0x00000001;
}
result.foo_ = foo_;
if (((from_bitField0_ & 0x00000002) == 0x00000002)) {
to_bitField0_ |= 0x00000002;
}
if (blahBuilder_ == null) {
result.blah_ = blah_;
} else {
result.blah_ = blahBuilder_.build();
}
result.bitField0_ = to_bitField0_;
onBuilt();
return result;
}
public Builder mergeFrom(com.google.protobuf.Message other) {
if (other instanceof Msg) {
return mergeFrom((Msg)other);
} else {
super.mergeFrom(other);
return this;
}
}
public Builder mergeFrom(Msg other) {
if (other == Msg.getDefaultInstance()) return this;
if (other.hasFoo()) {
bitField0_ |= 0x00000001;
foo_ = other.foo_;
onChanged();
}
if (other.hasBlah()) {
mergeBlah(other.getBlah());
}
this.mergeUnknownFields(other.getUnknownFields());
return this;
}
public final boolean isInitialized() {
return true;
}
public Builder mergeFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
Msg parsedMessage = null;
try {
parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
parsedMessage = (Msg) e.getUnfinishedMessage();
throw e;
} finally {
if (parsedMessage != null) {
mergeFrom(parsedMessage);
}
}
return this;
}
private int bitField0_;
// optional string foo = 1;
private Object foo_ = "";
/**
* <code>optional string foo = 1;</code>
*/
public boolean hasFoo() {
return ((bitField0_ & 0x00000001) == 0x00000001);
}
/**
* <code>optional string foo = 1;</code>
*/
public String getFoo() {
Object ref = foo_;
if (!(ref instanceof String)) {
String s = ((com.google.protobuf.ByteString) ref)
.toStringUtf8();
foo_ = s;
return s;
} else {
return (String) ref;
}
}
/**
* <code>optional string foo = 1;</code>
*/
public com.google.protobuf.ByteString
getFooBytes() {
Object ref = foo_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(String) ref);
foo_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
/**
* <code>optional string foo = 1;</code>
*/
public Builder setFoo(
String value) {
if (value == null) {
throw new NullPointerException();
}
bitField0_ |= 0x00000001;
foo_ = value;
onChanged();
return this;
}
/**
* <code>optional string foo = 1;</code>
*/
public Builder clearFoo() {
bitField0_ = (bitField0_ & ~0x00000001);
foo_ = getDefaultInstance().getFoo();
onChanged();
return this;
}
/**
* <code>optional string foo = 1;</code>
*/
public Builder setFooBytes(
com.google.protobuf.ByteString value) {
if (value == null) {
throw new NullPointerException();
}
bitField0_ |= 0x00000001;
foo_ = value;
onChanged();
return this;
}
// optional .SecondMsg blah = 2;
private SecondMsg blah_ = SecondMsg.getDefaultInstance();
private com.google.protobuf.SingleFieldBuilder<
SecondMsg, SecondMsg.Builder,
SecondMsgOrBuilder> blahBuilder_;
/**
* <code>optional .SecondMsg blah = 2;</code>
*/
public boolean hasBlah() {
return ((bitField0_ & 0x00000002) == 0x00000002);
}
/**
* <code>optional .SecondMsg blah = 2;</code>
*/
public SecondMsg getBlah() {
if (blahBuilder_ == null) {
return blah_;
} else {
return blahBuilder_.getMessage();
}
}
/**
* <code>optional .SecondMsg blah = 2;</code>
*/
public Builder setBlah(SecondMsg value) {
if (blahBuilder_ == null) {
if (value == null) {
throw new NullPointerException();
}
blah_ = value;
onChanged();
} else {
blahBuilder_.setMessage(value);
}
bitField0_ |= 0x00000002;
return this;
}
/**
* <code>optional .SecondMsg blah = 2;</code>
*/
public Builder setBlah(
SecondMsg.Builder builderForValue) {
if (blahBuilder_ == null) {
blah_ = builderForValue.build();
onChanged();
} else {
blahBuilder_.setMessage(builderForValue.build());
}
bitField0_ |= 0x00000002;
return this;
}
/**
* <code>optional .SecondMsg blah = 2;</code>
*/
public Builder mergeBlah(SecondMsg value) {
if (blahBuilder_ == null) {
if (((bitField0_ & 0x00000002) == 0x00000002) &&
blah_ != SecondMsg.getDefaultInstance()) {
blah_ =
SecondMsg.newBuilder(blah_).mergeFrom(value).buildPartial();
} else {
blah_ = value;
}
onChanged();
} else {
blahBuilder_.mergeFrom(value);
}
bitField0_ |= 0x00000002;
return this;
}
/**
* <code>optional .SecondMsg blah = 2;</code>
*/
public Builder clearBlah() {
if (blahBuilder_ == null) {
blah_ = SecondMsg.getDefaultInstance();
onChanged();
} else {
blahBuilder_.clear();
}
bitField0_ = (bitField0_ & ~0x00000002);
return this;
}
/**
* <code>optional .SecondMsg blah = 2;</code>
*/
public SecondMsg.Builder getBlahBuilder() {
bitField0_ |= 0x00000002;
onChanged();
return getBlahFieldBuilder().getBuilder();
}
/**
* <code>optional .SecondMsg blah = 2;</code>
*/
public SecondMsgOrBuilder getBlahOrBuilder() {
if (blahBuilder_ != null) {
return blahBuilder_.getMessageOrBuilder();
} else {
return blah_;
}
}
/**
* <code>optional .SecondMsg blah = 2;</code>
*/
private com.google.protobuf.SingleFieldBuilder<
SecondMsg, SecondMsg.Builder,
SecondMsgOrBuilder>
getBlahFieldBuilder() {
if (blahBuilder_ == null) {
blahBuilder_ = new com.google.protobuf.SingleFieldBuilder<>(
blah_,
getParentForChildren(),
isClean());
blah_ = null;
}
return blahBuilder_;
}
// @@protoc_insertion_point(builder_scope:Msg)
}
static {
defaultInstance = new Msg(true);
defaultInstance.initFields();
}
// @@protoc_insertion_point(class_scope:Msg)
}

View File

@@ -0,0 +1,37 @@
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: sample.proto
package org.springframework.web.reactive.protobuf;
public interface MsgOrBuilder
extends com.google.protobuf.MessageOrBuilder {
// optional string foo = 1;
/**
* <code>optional string foo = 1;</code>
*/
boolean hasFoo();
/**
* <code>optional string foo = 1;</code>
*/
String getFoo();
/**
* <code>optional string foo = 1;</code>
*/
com.google.protobuf.ByteString
getFooBytes();
// optional .SecondMsg blah = 2;
/**
* <code>optional .SecondMsg blah = 2;</code>
*/
boolean hasBlah();
/**
* <code>optional .SecondMsg blah = 2;</code>
*/
SecondMsg getBlah();
/**
* <code>optional .SecondMsg blah = 2;</code>
*/
SecondMsgOrBuilder getBlahOrBuilder();
}

View File

@@ -0,0 +1,62 @@
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: sample.proto
package org.springframework.web.reactive.protobuf;
public class OuterSample {
private OuterSample() {}
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistry registry) {
}
static com.google.protobuf.Descriptors.Descriptor
internal_static_Msg_descriptor;
static
com.google.protobuf.GeneratedMessage.FieldAccessorTable
internal_static_Msg_fieldAccessorTable;
static com.google.protobuf.Descriptors.Descriptor
internal_static_SecondMsg_descriptor;
static
com.google.protobuf.GeneratedMessage.FieldAccessorTable
internal_static_SecondMsg_fieldAccessorTable;
public static com.google.protobuf.Descriptors.FileDescriptor
getDescriptor() {
return descriptor;
}
private static com.google.protobuf.Descriptors.FileDescriptor
descriptor;
static {
String[] descriptorData = {
"\n\014sample.proto\",\n\003Msg\022\013\n\003foo\030\001 \001(\t\022\030\n\004bl" +
"ah\030\002 \001(\0132\n.SecondMsg\"\031\n\tSecondMsg\022\014\n\004bla" +
"h\030\001 \001(\005B-\n\034org.springframework.protobufB" +
"\013OuterSampleP\001"
};
com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner =
new com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner() {
public com.google.protobuf.ExtensionRegistry assignDescriptors(
com.google.protobuf.Descriptors.FileDescriptor root) {
descriptor = root;
internal_static_Msg_descriptor =
getDescriptor().getMessageTypes().get(0);
internal_static_Msg_fieldAccessorTable = new
com.google.protobuf.GeneratedMessage.FieldAccessorTable(
internal_static_Msg_descriptor,
new String[] { "Foo", "Blah", });
internal_static_SecondMsg_descriptor =
getDescriptor().getMessageTypes().get(1);
internal_static_SecondMsg_fieldAccessorTable = new
com.google.protobuf.GeneratedMessage.FieldAccessorTable(
internal_static_SecondMsg_descriptor,
new String[] { "Blah", });
return null;
}
};
com.google.protobuf.Descriptors.FileDescriptor
.internalBuildGeneratedFileFrom(descriptorData,
new com.google.protobuf.Descriptors.FileDescriptor[] {
}, assigner);
}
// @@protoc_insertion_point(outer_class_scope)
}

View File

@@ -0,0 +1,389 @@
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: sample.proto
package org.springframework.web.reactive.protobuf;
/**
* Protobuf type {@code SecondMsg}
*/
public final class SecondMsg extends
com.google.protobuf.GeneratedMessage
implements SecondMsgOrBuilder {
// Use SecondMsg.newBuilder() to construct.
private SecondMsg(com.google.protobuf.GeneratedMessage.Builder<?> builder) {
super(builder);
this.unknownFields = builder.getUnknownFields();
}
private SecondMsg(boolean noInit) { this.unknownFields = com.google.protobuf.UnknownFieldSet.getDefaultInstance(); }
private static final SecondMsg defaultInstance;
public static SecondMsg getDefaultInstance() {
return defaultInstance;
}
public SecondMsg getDefaultInstanceForType() {
return defaultInstance;
}
private final com.google.protobuf.UnknownFieldSet unknownFields;
@Override
public final com.google.protobuf.UnknownFieldSet
getUnknownFields() {
return this.unknownFields;
}
private SecondMsg(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
initFields();
@SuppressWarnings("unused")
int mutable_bitField0_ = 0;
com.google.protobuf.UnknownFieldSet.Builder unknownFields =
com.google.protobuf.UnknownFieldSet.newBuilder();
try {
boolean done = false;
while (!done) {
int tag = input.readTag();
switch (tag) {
case 0:
done = true;
break;
default: {
if (!parseUnknownField(input, unknownFields,
extensionRegistry, tag)) {
done = true;
}
break;
}
case 8: {
bitField0_ |= 0x00000001;
blah_ = input.readInt32();
break;
}
}
}
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
throw e.setUnfinishedMessage(this);
} catch (java.io.IOException e) {
throw new com.google.protobuf.InvalidProtocolBufferException(
e.getMessage()).setUnfinishedMessage(this);
} finally {
this.unknownFields = unknownFields.build();
makeExtensionsImmutable();
}
}
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return OuterSample.internal_static_SecondMsg_descriptor;
}
protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
internalGetFieldAccessorTable() {
return OuterSample.internal_static_SecondMsg_fieldAccessorTable
.ensureFieldAccessorsInitialized(
SecondMsg.class, SecondMsg.Builder.class);
}
public static com.google.protobuf.Parser<SecondMsg> PARSER =
new com.google.protobuf.AbstractParser<SecondMsg>() {
public SecondMsg parsePartialFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return new SecondMsg(input, extensionRegistry);
}
};
@Override
public com.google.protobuf.Parser<SecondMsg> getParserForType() {
return PARSER;
}
private int bitField0_;
// optional int32 blah = 1;
public static final int BLAH_FIELD_NUMBER = 1;
private int blah_;
/**
* <code>optional int32 blah = 1;</code>
*/
public boolean hasBlah() {
return ((bitField0_ & 0x00000001) == 0x00000001);
}
/**
* <code>optional int32 blah = 1;</code>
*/
public int getBlah() {
return blah_;
}
private void initFields() {
blah_ = 0;
}
private byte memoizedIsInitialized = -1;
public final boolean isInitialized() {
byte isInitialized = memoizedIsInitialized;
if (isInitialized != -1) return isInitialized == 1;
memoizedIsInitialized = 1;
return true;
}
public void writeTo(com.google.protobuf.CodedOutputStream output)
throws java.io.IOException {
getSerializedSize();
if (((bitField0_ & 0x00000001) == 0x00000001)) {
output.writeInt32(1, blah_);
}
getUnknownFields().writeTo(output);
}
private int memoizedSerializedSize = -1;
public int getSerializedSize() {
int size = memoizedSerializedSize;
if (size != -1) return size;
size = 0;
if (((bitField0_ & 0x00000001) == 0x00000001)) {
size += com.google.protobuf.CodedOutputStream
.computeInt32Size(1, blah_);
}
size += getUnknownFields().getSerializedSize();
memoizedSerializedSize = size;
return size;
}
private static final long serialVersionUID = 0L;
@Override
protected Object writeReplace()
throws java.io.ObjectStreamException {
return super.writeReplace();
}
public static SecondMsg parseFrom(
com.google.protobuf.ByteString data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static SecondMsg parseFrom(
com.google.protobuf.ByteString data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static SecondMsg parseFrom(byte[] data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static SecondMsg parseFrom(
byte[] data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static SecondMsg parseFrom(java.io.InputStream input)
throws java.io.IOException {
return PARSER.parseFrom(input);
}
public static SecondMsg parseFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseFrom(input, extensionRegistry);
}
public static SecondMsg parseDelimitedFrom(java.io.InputStream input)
throws java.io.IOException {
return PARSER.parseDelimitedFrom(input);
}
public static SecondMsg parseDelimitedFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseDelimitedFrom(input, extensionRegistry);
}
public static SecondMsg parseFrom(
com.google.protobuf.CodedInputStream input)
throws java.io.IOException {
return PARSER.parseFrom(input);
}
public static SecondMsg parseFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseFrom(input, extensionRegistry);
}
public static Builder newBuilder() { return Builder.create(); }
public Builder newBuilderForType() { return newBuilder(); }
public static Builder newBuilder(SecondMsg prototype) {
return newBuilder().mergeFrom(prototype);
}
public Builder toBuilder() { return newBuilder(this); }
@Override
protected Builder newBuilderForType(
com.google.protobuf.GeneratedMessage.BuilderParent parent) {
Builder builder = new Builder(parent);
return builder;
}
/**
* Protobuf type {@code SecondMsg}
*/
public static final class Builder extends
com.google.protobuf.GeneratedMessage.Builder<Builder>
implements SecondMsgOrBuilder {
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return OuterSample.internal_static_SecondMsg_descriptor;
}
protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
internalGetFieldAccessorTable() {
return OuterSample.internal_static_SecondMsg_fieldAccessorTable
.ensureFieldAccessorsInitialized(
SecondMsg.class, SecondMsg.Builder.class);
}
// Construct using org.springframework.protobuf.SecondMsg.newBuilder()
private Builder() {
maybeForceBuilderInitialization();
}
private Builder(
com.google.protobuf.GeneratedMessage.BuilderParent parent) {
super(parent);
maybeForceBuilderInitialization();
}
private void maybeForceBuilderInitialization() {
if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) {
}
}
private static Builder create() {
return new Builder();
}
public Builder clear() {
super.clear();
blah_ = 0;
bitField0_ = (bitField0_ & ~0x00000001);
return this;
}
public Builder clone() {
return create().mergeFrom(buildPartial());
}
public com.google.protobuf.Descriptors.Descriptor
getDescriptorForType() {
return OuterSample.internal_static_SecondMsg_descriptor;
}
public SecondMsg getDefaultInstanceForType() {
return SecondMsg.getDefaultInstance();
}
public SecondMsg build() {
SecondMsg result = buildPartial();
if (!result.isInitialized()) {
throw newUninitializedMessageException(result);
}
return result;
}
public SecondMsg buildPartial() {
SecondMsg result = new SecondMsg(this);
int from_bitField0_ = bitField0_;
int to_bitField0_ = 0;
if (((from_bitField0_ & 0x00000001) == 0x00000001)) {
to_bitField0_ |= 0x00000001;
}
result.blah_ = blah_;
result.bitField0_ = to_bitField0_;
onBuilt();
return result;
}
public Builder mergeFrom(com.google.protobuf.Message other) {
if (other instanceof SecondMsg) {
return mergeFrom((SecondMsg)other);
} else {
super.mergeFrom(other);
return this;
}
}
public Builder mergeFrom(SecondMsg other) {
if (other == SecondMsg.getDefaultInstance()) return this;
if (other.hasBlah()) {
setBlah(other.getBlah());
}
this.mergeUnknownFields(other.getUnknownFields());
return this;
}
public final boolean isInitialized() {
return true;
}
public Builder mergeFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
SecondMsg parsedMessage = null;
try {
parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
parsedMessage = (SecondMsg) e.getUnfinishedMessage();
throw e;
} finally {
if (parsedMessage != null) {
mergeFrom(parsedMessage);
}
}
return this;
}
private int bitField0_;
// optional int32 blah = 1;
private int blah_ ;
/**
* <code>optional int32 blah = 1;</code>
*/
public boolean hasBlah() {
return ((bitField0_ & 0x00000001) == 0x00000001);
}
/**
* <code>optional int32 blah = 1;</code>
*/
public int getBlah() {
return blah_;
}
/**
* <code>optional int32 blah = 1;</code>
*/
public Builder setBlah(int value) {
bitField0_ |= 0x00000001;
blah_ = value;
onChanged();
return this;
}
/**
* <code>optional int32 blah = 1;</code>
*/
public Builder clearBlah() {
bitField0_ = (bitField0_ & ~0x00000001);
blah_ = 0;
onChanged();
return this;
}
// @@protoc_insertion_point(builder_scope:SecondMsg)
}
static {
defaultInstance = new SecondMsg(true);
defaultInstance.initFields();
}
// @@protoc_insertion_point(class_scope:SecondMsg)
}

View File

@@ -0,0 +1,18 @@
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: sample.proto
package org.springframework.web.reactive.protobuf;
public interface SecondMsgOrBuilder
extends com.google.protobuf.MessageOrBuilder {
// optional int32 blah = 1;
/**
* <code>optional int32 blah = 1;</code>
*/
boolean hasBlah();
/**
* <code>optional int32 blah = 1;</code>
*/
int getBlah();
}

View File

@@ -0,0 +1,164 @@
/*
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.reactive.result.method.annotation;
import java.time.Duration;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.config.EnableWebFlux;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.protobuf.Msg;
import org.springframework.web.reactive.protobuf.SecondMsg;
/**
* Integration tests for Protobuf support.
*
* @author Sebastien Deleuze
*/
public class ProtobufIntegrationTests extends AbstractRequestMappingIntegrationTests {
public static final Msg TEST_MSG = Msg.newBuilder().setFoo("Foo").setBlah(SecondMsg.newBuilder().setBlah(123).build()).build();
private WebClient webClient;
@Override
protected ApplicationContext initApplicationContext() {
AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext();
wac.register(TestConfiguration .class);
wac.refresh();
return wac;
}
@Override
@Before
public void setup() throws Exception {
super.setup();
this.webClient = WebClient.create("http://localhost:" + this.port);
}
@Test
public void value() {
Mono<Msg> result = this.webClient.get()
.uri("/message")
.exchange()
.doOnNext(response -> {
Assert.assertFalse(response.headers().contentType().get().getParameters().containsKey("delimited"));
Assert.assertEquals("sample.proto", response.headers().header("X-Protobuf-Schema").get(0));
Assert.assertEquals("Msg", response.headers().header("X-Protobuf-Message").get(0));
})
.flatMap(response -> response.bodyToMono(Msg.class));
StepVerifier.create(result)
.expectNext(TEST_MSG)
.verifyComplete();
}
@Test
public void values() {
Flux<Msg> result = this.webClient.get()
.uri("/messages")
.exchange()
.doOnNext(response -> {
Assert.assertEquals("true", response.headers().contentType().get().getParameters().get("delimited"));
Assert.assertEquals("sample.proto", response.headers().header("X-Protobuf-Schema").get(0));
Assert.assertEquals("Msg", response.headers().header("X-Protobuf-Message").get(0));
})
.flatMapMany(response -> response.bodyToFlux(Msg.class));
StepVerifier.create(result)
.expectNext(TEST_MSG)
.expectNext(TEST_MSG)
.expectNext(TEST_MSG)
.verifyComplete();
}
@Test
public void streaming() {
Flux<Msg> result = this.webClient.get()
.uri("/message-stream")
.exchange()
.doOnNext(response -> {
Assert.assertEquals("true", response.headers().contentType().get().getParameters().get("delimited"));
Assert.assertEquals("sample.proto", response.headers().header("X-Protobuf-Schema").get(0));
Assert.assertEquals("Msg", response.headers().header("X-Protobuf-Message").get(0));
})
.flatMapMany(response -> response.bodyToFlux(Msg.class));
StepVerifier.create(result)
.expectNext(Msg.newBuilder().setFoo("Foo").setBlah(SecondMsg.newBuilder().setBlah(0).build()).build())
.expectNext(Msg.newBuilder().setFoo("Foo").setBlah(SecondMsg.newBuilder().setBlah(1).build()).build())
.thenCancel()
.verify();
}
@Test
public void empty() {
Mono<Msg> result = this.webClient.get()
.uri("/empty")
.retrieve()
.bodyToMono(Msg.class);
StepVerifier.create(result)
.verifyComplete();
}
@RestController
@SuppressWarnings("unused")
static class ProtobufController {
@GetMapping("/message")
Mono<Msg> message() {
return Mono.just(TEST_MSG);
}
@GetMapping("/messages")
Flux<Msg> messages() {
return Flux.just(TEST_MSG, TEST_MSG, TEST_MSG);
}
@GetMapping(value = "/message-stream", produces = "application/x-protobuf;delimited=true")
Flux<Msg> messageStream() {
return testInterval(Duration.ofMillis(50), 5).map(l -> Msg.newBuilder().setFoo("Foo").setBlah(SecondMsg.newBuilder().setBlah(l.intValue()).build()).build());
}
@GetMapping("/empty")
Mono<Msg> empty() {
return Mono.empty();
}
}
@Configuration
@EnableWebFlux
@ComponentScan(resourcePattern = "**/ProtobufIntegrationTests*.class")
@SuppressWarnings("unused")
static class TestConfiguration {
}
}

View File

@@ -0,0 +1,12 @@
option java_package = "org.springframework.web.reactive.protobuf";
option java_outer_classname = "OuterSample";
option java_multiple_files = true;
message Msg {
optional string foo = 1;
optional SecondMsg blah = 2;
}
message SecondMsg {
optional int32 blah = 1;
}