Browse Source

Polishing in Protobuf support

See gh-35403
pull/35684/head
rstoyanchev 5 months ago
parent
commit
87607cccff
  1. 4
      spring-web/src/main/java/org/springframework/http/codec/protobuf/ProtobufCodecSupport.java
  2. 3
      spring-web/src/main/java/org/springframework/http/codec/protobuf/ProtobufDecoder.java
  3. 21
      spring-web/src/main/java/org/springframework/http/codec/protobuf/ProtobufJsonDecoder.java
  4. 1
      spring-web/src/main/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverter.java

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

@ -47,8 +47,8 @@ public abstract class ProtobufCodecSupport { @@ -47,8 +47,8 @@ public abstract class ProtobufCodecSupport {
if (mimeType == null) {
return true;
}
for (MimeType m : MIME_TYPES) {
if (m.isCompatibleWith(mimeType)) {
for (MimeType supportedMimeType : MIME_TYPES) {
if (supportedMimeType.isCompatibleWith(mimeType)) {
return true;
}
}

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

@ -129,8 +129,7 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder<Mes @@ -129,8 +129,7 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder<Mes
public Flux<Message> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType,
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
MessageDecoderFunction decoderFunction =
new MessageDecoderFunction(elementType, this.maxMessageSize);
MessageDecoderFunction decoderFunction = new MessageDecoderFunction(elementType, this.maxMessageSize);
return Flux.from(inputStream)
.flatMapIterable(decoderFunction)

21
spring-web/src/main/java/org/springframework/http/codec/protobuf/ProtobufJsonDecoder.java

@ -131,12 +131,19 @@ public class ProtobufJsonDecoder implements Decoder<Message> { @@ -131,12 +131,19 @@ public class ProtobufJsonDecoder implements Decoder<Message> {
}
@Override
public Flux<Message> decode(Publisher<DataBuffer> inputStream, ResolvableType targetType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
return Flux.error(new UnsupportedOperationException("Protobuf decoder does not support Flux, use Mono<List<...>> instead."));
public Flux<Message> decode(
Publisher<DataBuffer> inputStream, ResolvableType targetType, @Nullable MimeType mimeType,
@Nullable Map<String, Object> hints) {
return Flux.error(new UnsupportedOperationException(
"Protobuf decoder does not support Flux, use Mono<List<...>> instead."));
}
@Override
public Message decode(DataBuffer dataBuffer, ResolvableType targetType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) throws DecodingException {
public Message decode(
DataBuffer dataBuffer, ResolvableType targetType, @Nullable MimeType mimeType,
@Nullable Map<String, Object> hints) throws DecodingException {
try {
Message.Builder builder = getMessageBuilder(targetType.toClass());
this.parser.merge(new InputStreamReader(dataBuffer.asInputStream()), builder);
@ -164,10 +171,14 @@ public class ProtobufJsonDecoder implements Decoder<Message> { @@ -164,10 +171,14 @@ public class ProtobufJsonDecoder implements Decoder<Message> {
}
@Override
public Mono<Message> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
public Mono<Message> decodeToMono(
Publisher<DataBuffer> inputStream, ResolvableType elementType, @Nullable MimeType mimeType,
@Nullable Map<String, Object> hints) {
return DataBufferUtils.join(inputStream, this.maxMessageSize)
.map(dataBuffer -> decode(dataBuffer, elementType, mimeType, hints))
.onErrorMap(DataBufferLimitException.class, exc -> new DecodingException("Could not decode JSON as Protobuf message", exc));
.onErrorMap(DataBufferLimitException.class,
exc -> new DecodingException("Could not decode JSON as Protobuf message", exc));
}
}

1
spring-web/src/main/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverter.java

@ -257,6 +257,7 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M @@ -257,6 +257,7 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M
return true;
}
/**
* Protobuf format support.
*/

Loading…
Cancel
Save