Refactor DefaultCodecs.protobufWriter into protobufEncoder

Includes nullability declarations for the protobuf package.

Issue: SPR-15776
This commit is contained in:
Juergen Hoeller 2018-07-25 14:15:50 +02:00
parent fd8e4abe5d
commit 3899b7a909
7 changed files with 75 additions and 39 deletions

View File

@ -119,12 +119,13 @@ public interface CodecConfigurer {
void protobufDecoder(Decoder<?> decoder); void protobufDecoder(Decoder<?> decoder);
/** /**
* Override the default Protobuf {@code HttpMessageReader}. * Override the default Protobuf {@code Encoder}.
* @param decoder the decoder instance to use * @param encoder the encoder instance to use
* @since 5.1 * @since 5.1
* @see org.springframework.http.codec.protobuf.ProtobufEncoder
* @see org.springframework.http.codec.protobuf.ProtobufHttpMessageWriter * @see org.springframework.http.codec.protobuf.ProtobufHttpMessageWriter
*/ */
void protobufWriter(HttpMessageWriter<?> decoder); void protobufEncoder(Encoder<?> encoder);
/** /**
* Whether to log form data at DEBUG level, and headers at TRACE level. * Whether to log form data at DEBUG level, and headers at TRACE level.

View File

@ -48,4 +48,5 @@ public abstract class ProtobufCodecSupport {
protected List<MimeType> getMimeTypes() { protected List<MimeType> getMimeTypes() {
return MIME_TYPES; return MIME_TYPES;
} }
} }

View File

@ -20,7 +20,7 @@ import java.io.IOException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap;
import java.util.function.Function; import java.util.function.Function;
import com.google.protobuf.CodedInputStream; import com.google.protobuf.CodedInputStream;
@ -35,7 +35,9 @@ import org.springframework.core.codec.Decoder;
import org.springframework.core.codec.DecodingException; import org.springframework.core.codec.DecodingException;
import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils; import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ConcurrentReferenceHashMap;
import org.springframework.util.MimeType; import org.springframework.util.MimeType;
/** /**
@ -66,7 +68,7 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder<Mes
*/ */
protected static final int DEFAULT_MESSAGE_MAX_SIZE = 64 * 1024; protected static final int DEFAULT_MESSAGE_MAX_SIZE = 64 * 1024;
private static final ConcurrentHashMap<Class<?>, Method> methodCache = new ConcurrentHashMap<>(); private static final ConcurrentMap<Class<?>, Method> methodCache = new ConcurrentReferenceHashMap<>();
private final ExtensionRegistry extensionRegistry; private final ExtensionRegistry extensionRegistry;
@ -90,18 +92,20 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder<Mes
this.extensionRegistry = extensionRegistry; this.extensionRegistry = extensionRegistry;
} }
public void setMaxMessageSize(int maxMessageSize) { public void setMaxMessageSize(int maxMessageSize) {
this.maxMessageSize = maxMessageSize; this.maxMessageSize = maxMessageSize;
} }
@Override @Override
public boolean canDecode(ResolvableType elementType, MimeType mimeType) { public boolean canDecode(ResolvableType elementType, @Nullable MimeType mimeType) {
return Message.class.isAssignableFrom(elementType.getRawClass()) && supportsMimeType(mimeType); return Message.class.isAssignableFrom(elementType.toClass()) && supportsMimeType(mimeType);
} }
@Override @Override
public Flux<Message> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType, public Flux<Message> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType,
MimeType mimeType, Map<String, Object> hints) { @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
return Flux.from(inputStream) return Flux.from(inputStream)
.concatMap(new MessageDecoderFunction(elementType, this.maxMessageSize)); .concatMap(new MessageDecoderFunction(elementType, this.maxMessageSize));
@ -109,10 +113,11 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder<Mes
@Override @Override
public Mono<Message> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType, public Mono<Message> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType,
MimeType mimeType, Map<String, Object> hints) { @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
return DataBufferUtils.join(inputStream).map(dataBuffer -> { return DataBufferUtils.join(inputStream).map(dataBuffer -> {
try { try {
Message.Builder builder = getMessageBuilder(elementType.getRawClass()); Message.Builder builder = getMessageBuilder(elementType.toClass());
builder.mergeFrom(CodedInputStream.newInstance(dataBuffer.asByteBuffer()), this.extensionRegistry); builder.mergeFrom(CodedInputStream.newInstance(dataBuffer.asByteBuffer()), this.extensionRegistry);
Message message = builder.build(); Message message = builder.build();
DataBufferUtils.release(dataBuffer); DataBufferUtils.release(dataBuffer);
@ -153,6 +158,7 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder<Mes
private final int maxMessageSize; private final int maxMessageSize;
@Nullable
private DataBuffer output; private DataBuffer output;
private int messageBytesToRead; private int messageBytesToRead;
@ -162,10 +168,10 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder<Mes
this.maxMessageSize = maxMessageSize; this.maxMessageSize = maxMessageSize;
} }
// TODO Instead of the recursive call, loop over the current DataBuffer, produce a list of as many messages as are contained, and save any remaining bytes with flatMapIterable // TODO Instead of the recursive call, loop over the current DataBuffer,
// produce a list of as many messages as are contained, and save any remaining bytes with flatMapIterable
@Override @Override
public Publisher<? extends Message> apply(DataBuffer input) { public Publisher<? extends Message> apply(DataBuffer input) {
try { try {
if (this.output == null) { if (this.output == null) {
int firstByte = input.read(); int firstByte = input.read();
@ -187,7 +193,7 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder<Mes
this.messageBytesToRead -= chunkBytesToRead; this.messageBytesToRead -= chunkBytesToRead;
Message message = null; Message message = null;
if (this.messageBytesToRead == 0) { if (this.messageBytesToRead == 0) {
Message.Builder builder = getMessageBuilder(this.elementType.getRawClass()); Message.Builder builder = getMessageBuilder(this.elementType.toClass());
builder.mergeFrom(CodedInputStream.newInstance(this.output.asByteBuffer()), extensionRegistry); builder.mergeFrom(CodedInputStream.newInstance(this.output.asByteBuffer()), extensionRegistry);
message = builder.build(); message = builder.build();
DataBufferUtils.release(this.output); DataBufferUtils.release(this.output);
@ -209,4 +215,5 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder<Mes
} }
} }
} }
} }

View File

@ -33,6 +33,7 @@ import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory; import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.codec.HttpMessageEncoder; import org.springframework.http.codec.HttpMessageEncoder;
import org.springframework.lang.Nullable;
import org.springframework.util.MimeType; import org.springframework.util.MimeType;
/** /**
@ -58,17 +59,20 @@ public class ProtobufEncoder extends ProtobufCodecSupport implements HttpMessage
private static final List<MediaType> streamingMediaTypes = MIME_TYPES private static final List<MediaType> streamingMediaTypes = MIME_TYPES
.stream() .stream()
.map(mimeType -> new MediaType(mimeType.getType(), mimeType.getSubtype(), Collections.singletonMap(DELIMITED_KEY, DELIMITED_VALUE))) .map(mimeType -> new MediaType(mimeType.getType(), mimeType.getSubtype(),
Collections.singletonMap(DELIMITED_KEY, DELIMITED_VALUE)))
.collect(Collectors.toList()); .collect(Collectors.toList());
@Override @Override
public boolean canEncode(ResolvableType elementType, MimeType mimeType) { public boolean canEncode(ResolvableType elementType, @Nullable MimeType mimeType) {
return Message.class.isAssignableFrom(elementType.getRawClass()) && supportsMimeType(mimeType); return Message.class.isAssignableFrom(elementType.toClass()) && supportsMimeType(mimeType);
} }
@Override @Override
public Flux<DataBuffer> encode(Publisher<? extends Message> inputStream, public Flux<DataBuffer> encode(Publisher<? extends Message> inputStream, DataBufferFactory bufferFactory,
DataBufferFactory bufferFactory, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) { ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
return Flux return Flux
.from(inputStream) .from(inputStream)
.map(message -> encodeMessage(message, bufferFactory, !(inputStream instanceof Mono))); .map(message -> encodeMessage(message, bufferFactory, !(inputStream instanceof Mono)));
@ -100,4 +104,5 @@ public class ProtobufEncoder extends ProtobufCodecSupport implements HttpMessage
public List<MimeType> getEncodableMimeTypes() { public List<MimeType> getEncodableMimeTypes() {
return getMimeTypes(); return getMimeTypes();
} }
} }

View File

@ -19,7 +19,7 @@ package org.springframework.http.codec.protobuf;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap;
import com.google.protobuf.Descriptors; import com.google.protobuf.Descriptors;
import com.google.protobuf.Message; import com.google.protobuf.Message;
@ -29,11 +29,13 @@ import reactor.core.publisher.Mono;
import org.springframework.core.ResolvableType; import org.springframework.core.ResolvableType;
import org.springframework.core.codec.DecodingException; import org.springframework.core.codec.DecodingException;
import org.springframework.core.codec.Encoder;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ReactiveHttpOutputMessage; import org.springframework.http.ReactiveHttpOutputMessage;
import org.springframework.http.codec.EncoderHttpMessageWriter; import org.springframework.http.codec.EncoderHttpMessageWriter;
import org.springframework.http.codec.HttpMessageEncoder; import org.springframework.http.codec.HttpMessageEncoder;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.ConcurrentReferenceHashMap;
/** /**
* {@code HttpMessageWriter} that can write a protobuf {@link Message} and adds * {@code HttpMessageWriter} that can write a protobuf {@link Message} and adds
@ -49,18 +51,25 @@ import org.springframework.lang.Nullable;
*/ */
public class ProtobufHttpMessageWriter extends EncoderHttpMessageWriter<Message> { public class ProtobufHttpMessageWriter extends EncoderHttpMessageWriter<Message> {
private static final ConcurrentHashMap<Class<?>, Method> methodCache = new ConcurrentHashMap<>();
private static final String X_PROTOBUF_SCHEMA_HEADER = "X-Protobuf-Schema"; private static final String X_PROTOBUF_SCHEMA_HEADER = "X-Protobuf-Schema";
private static final String X_PROTOBUF_MESSAGE_HEADER = "X-Protobuf-Message"; private static final String X_PROTOBUF_MESSAGE_HEADER = "X-Protobuf-Message";
private static final ConcurrentMap<Class<?>, Method> methodCache = new ConcurrentReferenceHashMap<>();
/**
* Create a new {@code ProtobufHttpMessageWriter} with a default {@link ProtobufEncoder}.
*/
public ProtobufHttpMessageWriter() { public ProtobufHttpMessageWriter() {
super(new ProtobufEncoder()); super(new ProtobufEncoder());
} }
public ProtobufHttpMessageWriter(ProtobufEncoder encoder) { /**
* Create a new {@code ProtobufHttpMessageWriter} with the given encoder.
* @param encoder the Protobuf message encoder to use
*/
public ProtobufHttpMessageWriter(Encoder<Message> encoder) {
super(encoder); super(encoder);
} }
@ -71,7 +80,7 @@ public class ProtobufHttpMessageWriter extends EncoderHttpMessageWriter<Message>
@Nullable MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints) { @Nullable MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints) {
try { try {
Message.Builder builder = getMessageBuilder(elementType.getRawClass()); Message.Builder builder = getMessageBuilder(elementType.toClass());
Descriptors.Descriptor descriptor = builder.getDescriptorForType(); Descriptors.Descriptor descriptor = builder.getDescriptorForType();
message.getHeaders().add(X_PROTOBUF_SCHEMA_HEADER, descriptor.getFile().getName()); message.getHeaders().add(X_PROTOBUF_SCHEMA_HEADER, descriptor.getFile().getName());
message.getHeaders().add(X_PROTOBUF_MESSAGE_HEADER, descriptor.getFullName()); message.getHeaders().add(X_PROTOBUF_MESSAGE_HEADER, descriptor.getFullName());

View File

@ -0,0 +1,10 @@
/**
* Provides an encoder and a decoder for
* <a href="https://developers.google.com/protocol-buffers/">Google Protocol Buffers</a>.
*/
@NonNullApi
@NonNullFields
package org.springframework.http.codec.protobuf;
import org.springframework.lang.NonNullApi;
import org.springframework.lang.NonNullFields;

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.http.codec.support; package org.springframework.http.codec.support;
import java.util.ArrayList; import java.util.ArrayList;
@ -42,6 +43,7 @@ import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.http.codec.json.Jackson2SmileDecoder; import org.springframework.http.codec.json.Jackson2SmileDecoder;
import org.springframework.http.codec.json.Jackson2SmileEncoder; import org.springframework.http.codec.json.Jackson2SmileEncoder;
import org.springframework.http.codec.protobuf.ProtobufDecoder; import org.springframework.http.codec.protobuf.ProtobufDecoder;
import org.springframework.http.codec.protobuf.ProtobufEncoder;
import org.springframework.http.codec.protobuf.ProtobufHttpMessageWriter; import org.springframework.http.codec.protobuf.ProtobufHttpMessageWriter;
import org.springframework.http.codec.xml.Jaxb2XmlDecoder; import org.springframework.http.codec.xml.Jaxb2XmlDecoder;
import org.springframework.http.codec.xml.Jaxb2XmlEncoder; import org.springframework.http.codec.xml.Jaxb2XmlEncoder;
@ -53,6 +55,7 @@ import org.springframework.util.ClassUtils;
* as a base for client and server specific variants. * as a base for client and server specific variants.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @author Sebastien Deleuze
*/ */
class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs { class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs {
@ -77,14 +80,14 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs {
@Nullable @Nullable
private Decoder<?> jackson2JsonDecoder; private Decoder<?> jackson2JsonDecoder;
@Nullable
private Decoder<?> protobufDecoder;
@Nullable @Nullable
private Encoder<?> jackson2JsonEncoder; private Encoder<?> jackson2JsonEncoder;
@Nullable @Nullable
private HttpMessageWriter<?> protobufWriter; private Decoder<?> protobufDecoder;
@Nullable
private Encoder<?> protobufEncoder;
private boolean enableLoggingRequestDetails = false; private boolean enableLoggingRequestDetails = false;
@ -107,8 +110,8 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs {
} }
@Override @Override
public void protobufWriter(HttpMessageWriter<?> writer) { public void protobufEncoder(Encoder<?> encoder) {
this.protobufWriter = writer; this.protobufEncoder = encoder;
} }
@Override @Override
@ -205,6 +208,7 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs {
* or for multipart requests only ("true"). Generally the two sets are the * or for multipart requests only ("true"). Generally the two sets are the
* same except for the multipart writer itself. * same except for the multipart writer itself.
*/ */
@SuppressWarnings("unchecked")
final List<HttpMessageWriter<?>> getTypedWriters(boolean forMultipart) { final List<HttpMessageWriter<?>> getTypedWriters(boolean forMultipart) {
if (!this.registerDefaults) { if (!this.registerDefaults) {
return Collections.emptyList(); return Collections.emptyList();
@ -220,7 +224,7 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs {
extendTypedWriters(writers); extendTypedWriters(writers);
} }
if (protobufPresent) { if (protobufPresent) {
writers.add(getProtobufWriter()); writers.add(new ProtobufHttpMessageWriter((Encoder) getProtobufEncoder()));
} }
return writers; return writers;
} }
@ -277,23 +281,22 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs {
} }
// Accessors for use in sub-classes... // Accessors for use in subclasses...
protected Decoder<?> getJackson2JsonDecoder() { protected Decoder<?> getJackson2JsonDecoder() {
return (this.jackson2JsonDecoder != null ? this.jackson2JsonDecoder : new Jackson2JsonDecoder()); return (this.jackson2JsonDecoder != null ? this.jackson2JsonDecoder : new Jackson2JsonDecoder());
} }
protected Decoder<?> getProtobufDecoder() {
return (this.protobufDecoder != null ? this.protobufDecoder : new ProtobufDecoder());
}
protected Encoder<?> getJackson2JsonEncoder() { protected Encoder<?> getJackson2JsonEncoder() {
return (this.jackson2JsonEncoder != null ? this.jackson2JsonEncoder : new Jackson2JsonEncoder()); return (this.jackson2JsonEncoder != null ? this.jackson2JsonEncoder : new Jackson2JsonEncoder());
} }
protected HttpMessageWriter<?> getProtobufWriter() { protected Decoder<?> getProtobufDecoder() {
return (this.protobufWriter != null ? this.protobufWriter : new ProtobufHttpMessageWriter()); return (this.protobufDecoder != null ? this.protobufDecoder : new ProtobufDecoder());
}
protected Encoder<?> getProtobufEncoder() {
return (this.protobufEncoder != null ? this.protobufEncoder : new ProtobufEncoder());
} }
} }