Browse Source

Refactor DefaultCodecs.protobufWriter into protobufEncoder

Includes nullability declarations for the protobuf package.

Issue: SPR-15776
pull/1897/head
Juergen Hoeller 6 years ago
parent
commit
3899b7a909
  1. 7
      spring-web/src/main/java/org/springframework/http/codec/CodecConfigurer.java
  2. 1
      spring-web/src/main/java/org/springframework/http/codec/protobuf/ProtobufCodecSupport.java
  3. 29
      spring-web/src/main/java/org/springframework/http/codec/protobuf/ProtobufDecoder.java
  4. 15
      spring-web/src/main/java/org/springframework/http/codec/protobuf/ProtobufEncoder.java
  5. 19
      spring-web/src/main/java/org/springframework/http/codec/protobuf/ProtobufHttpMessageWriter.java
  6. 10
      spring-web/src/main/java/org/springframework/http/codec/protobuf/package-info.java
  7. 31
      spring-web/src/main/java/org/springframework/http/codec/support/BaseDefaultCodecs.java

7
spring-web/src/main/java/org/springframework/http/codec/CodecConfigurer.java

@ -119,12 +119,13 @@ public interface CodecConfigurer { @@ -119,12 +119,13 @@ public interface CodecConfigurer {
void protobufDecoder(Decoder<?> decoder);
/**
* Override the default Protobuf {@code HttpMessageReader}.
* @param decoder the decoder instance to use
* Override the default Protobuf {@code Encoder}.
* @param encoder the encoder instance to use
* @since 5.1
* @see org.springframework.http.codec.protobuf.ProtobufEncoder
* @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.

1
spring-web/src/main/java/org/springframework/http/codec/protobuf/ProtobufCodecSupport.java

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

29
spring-web/src/main/java/org/springframework/http/codec/protobuf/ProtobufDecoder.java

@ -20,7 +20,7 @@ import java.io.IOException; @@ -20,7 +20,7 @@ import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;
import com.google.protobuf.CodedInputStream;
@ -35,7 +35,9 @@ import org.springframework.core.codec.Decoder; @@ -35,7 +35,9 @@ import org.springframework.core.codec.Decoder;
import org.springframework.core.codec.DecodingException;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ConcurrentReferenceHashMap;
import org.springframework.util.MimeType;
/**
@ -66,7 +68,7 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder<Mes @@ -66,7 +68,7 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder<Mes
*/
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;
@ -90,18 +92,20 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder<Mes @@ -90,18 +92,20 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder<Mes
this.extensionRegistry = extensionRegistry;
}
public void setMaxMessageSize(int maxMessageSize) {
this.maxMessageSize = maxMessageSize;
}
@Override
public boolean canDecode(ResolvableType elementType, MimeType mimeType) {
return Message.class.isAssignableFrom(elementType.getRawClass()) && supportsMimeType(mimeType);
public boolean canDecode(ResolvableType elementType, @Nullable MimeType mimeType) {
return Message.class.isAssignableFrom(elementType.toClass()) && supportsMimeType(mimeType);
}
@Override
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)
.concatMap(new MessageDecoderFunction(elementType, this.maxMessageSize));
@ -109,10 +113,11 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder<Mes @@ -109,10 +113,11 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder<Mes
@Override
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 -> {
try {
Message.Builder builder = getMessageBuilder(elementType.getRawClass());
Message.Builder builder = getMessageBuilder(elementType.toClass());
builder.mergeFrom(CodedInputStream.newInstance(dataBuffer.asByteBuffer()), this.extensionRegistry);
Message message = builder.build();
DataBufferUtils.release(dataBuffer);
@ -153,6 +158,7 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder<Mes @@ -153,6 +158,7 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder<Mes
private final int maxMessageSize;
@Nullable
private DataBuffer output;
private int messageBytesToRead;
@ -162,10 +168,10 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder<Mes @@ -162,10 +168,10 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder<Mes
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
public Publisher<? extends Message> apply(DataBuffer input) {
try {
if (this.output == null) {
int firstByte = input.read();
@ -176,7 +182,7 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder<Mes @@ -176,7 +182,7 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder<Mes
if (this.messageBytesToRead > this.maxMessageSize) {
return Flux.error(new DecodingException(
"The number of bytes to read parsed in the incoming stream (" +
this.messageBytesToRead + ") exceeds the configured limit (" + this.maxMessageSize + ")"));
this.messageBytesToRead + ") exceeds the configured limit (" + this.maxMessageSize + ")"));
}
this.output = input.factory().allocateBuffer(this.messageBytesToRead);
}
@ -187,7 +193,7 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder<Mes @@ -187,7 +193,7 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder<Mes
this.messageBytesToRead -= chunkBytesToRead;
Message message = null;
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);
message = builder.build();
DataBufferUtils.release(this.output);
@ -209,4 +215,5 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder<Mes @@ -209,4 +215,5 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder<Mes
}
}
}
}

15
spring-web/src/main/java/org/springframework/http/codec/protobuf/ProtobufEncoder.java

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

19
spring-web/src/main/java/org/springframework/http/codec/protobuf/ProtobufHttpMessageWriter.java

@ -19,7 +19,7 @@ package org.springframework.http.codec.protobuf; @@ -19,7 +19,7 @@ package org.springframework.http.codec.protobuf;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import com.google.protobuf.Descriptors;
import com.google.protobuf.Message;
@ -29,11 +29,13 @@ import reactor.core.publisher.Mono; @@ -29,11 +29,13 @@ import reactor.core.publisher.Mono;
import org.springframework.core.ResolvableType;
import org.springframework.core.codec.DecodingException;
import org.springframework.core.codec.Encoder;
import org.springframework.http.MediaType;
import org.springframework.http.ReactiveHttpOutputMessage;
import org.springframework.http.codec.EncoderHttpMessageWriter;
import org.springframework.http.codec.HttpMessageEncoder;
import org.springframework.lang.Nullable;
import org.springframework.util.ConcurrentReferenceHashMap;
/**
* {@code HttpMessageWriter} that can write a protobuf {@link Message} and adds
@ -49,18 +51,25 @@ import org.springframework.lang.Nullable; @@ -49,18 +51,25 @@ import org.springframework.lang.Nullable;
*/
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_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() {
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);
}
@ -71,7 +80,7 @@ public class ProtobufHttpMessageWriter extends EncoderHttpMessageWriter<Message> @@ -71,7 +80,7 @@ public class ProtobufHttpMessageWriter extends EncoderHttpMessageWriter<Message>
@Nullable MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints) {
try {
Message.Builder builder = getMessageBuilder(elementType.getRawClass());
Message.Builder builder = getMessageBuilder(elementType.toClass());
Descriptors.Descriptor descriptor = builder.getDescriptorForType();
message.getHeaders().add(X_PROTOBUF_SCHEMA_HEADER, descriptor.getFile().getName());
message.getHeaders().add(X_PROTOBUF_MESSAGE_HEADER, descriptor.getFullName());

10
spring-web/src/main/java/org/springframework/http/codec/protobuf/package-info.java

@ -0,0 +1,10 @@ @@ -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;

31
spring-web/src/main/java/org/springframework/http/codec/support/BaseDefaultCodecs.java

@ -13,6 +13,7 @@ @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.http.codec.support;
import java.util.ArrayList;
@ -42,6 +43,7 @@ import org.springframework.http.codec.json.Jackson2JsonEncoder; @@ -42,6 +43,7 @@ import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.http.codec.json.Jackson2SmileDecoder;
import org.springframework.http.codec.json.Jackson2SmileEncoder;
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.xml.Jaxb2XmlDecoder;
import org.springframework.http.codec.xml.Jaxb2XmlEncoder;
@ -53,6 +55,7 @@ import org.springframework.util.ClassUtils; @@ -53,6 +55,7 @@ import org.springframework.util.ClassUtils;
* as a base for client and server specific variants.
*
* @author Rossen Stoyanchev
* @author Sebastien Deleuze
*/
class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs {
@ -78,13 +81,13 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs { @@ -78,13 +81,13 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs {
private Decoder<?> jackson2JsonDecoder;
@Nullable
private Decoder<?> protobufDecoder;
private Encoder<?> jackson2JsonEncoder;
@Nullable
private Encoder<?> jackson2JsonEncoder;
private Decoder<?> protobufDecoder;
@Nullable
private HttpMessageWriter<?> protobufWriter;
private Encoder<?> protobufEncoder;
private boolean enableLoggingRequestDetails = false;
@ -107,8 +110,8 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs { @@ -107,8 +110,8 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs {
}
@Override
public void protobufWriter(HttpMessageWriter<?> writer) {
this.protobufWriter = writer;
public void protobufEncoder(Encoder<?> encoder) {
this.protobufEncoder = encoder;
}
@Override
@ -205,6 +208,7 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs { @@ -205,6 +208,7 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs {
* 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();
@ -220,7 +224,7 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs { @@ -220,7 +224,7 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs {
extendTypedWriters(writers);
}
if (protobufPresent) {
writers.add(getProtobufWriter());
writers.add(new ProtobufHttpMessageWriter((Encoder) getProtobufEncoder()));
}
return writers;
}
@ -277,23 +281,22 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs { @@ -277,23 +281,22 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs {
}
// Accessors for use in sub-classes...
// Accessors for use in subclasses...
protected Decoder<?> getJackson2JsonDecoder() {
return (this.jackson2JsonDecoder != null ? this.jackson2JsonDecoder : new Jackson2JsonDecoder());
}
protected Decoder<?> getProtobufDecoder() {
return (this.protobufDecoder != null ? this.protobufDecoder : new ProtobufDecoder());
}
protected Encoder<?> getJackson2JsonEncoder() {
return (this.jackson2JsonEncoder != null ? this.jackson2JsonEncoder : new Jackson2JsonEncoder());
}
protected HttpMessageWriter<?> getProtobufWriter() {
return (this.protobufWriter != null ? this.protobufWriter : new ProtobufHttpMessageWriter());
protected Decoder<?> getProtobufDecoder() {
return (this.protobufDecoder != null ? this.protobufDecoder : new ProtobufDecoder());
}
}
protected Encoder<?> getProtobufEncoder() {
return (this.protobufEncoder != null ? this.protobufEncoder : new ProtobufEncoder());
}
}

Loading…
Cancel
Save