mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-17 16:39:29 +00:00
Allow to set MimeType's in ProtobufCodecSupport
See gh-35403
This commit is contained in:
+15
-5
@@ -21,17 +21,19 @@ import java.util.List;
|
||||
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.MimeType;
|
||||
|
||||
/**
|
||||
* Base class providing support methods for Protobuf encoding and decoding.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 5.1
|
||||
*/
|
||||
public abstract class ProtobufCodecSupport {
|
||||
|
||||
static final MimeType[] MIME_TYPES = new MimeType[]{
|
||||
protected static final MimeType[] MIME_TYPES = new MimeType[] {
|
||||
new MimeType("application", "x-protobuf"),
|
||||
new MimeType("application", "*+x-protobuf"),
|
||||
new MimeType("application", "octet-stream"),
|
||||
@@ -43,6 +45,18 @@ public abstract class ProtobufCodecSupport {
|
||||
static final String DELIMITED_VALUE = "true";
|
||||
|
||||
|
||||
private List<MimeType> mimeTypes = Arrays.asList(MIME_TYPES);
|
||||
|
||||
|
||||
protected void setMimeTypes(List<MimeType> mimeTypes) {
|
||||
Assert.notEmpty(mimeTypes, "MimeType List must not be empty");
|
||||
this.mimeTypes = List.copyOf(mimeTypes);
|
||||
}
|
||||
|
||||
protected List<MimeType> getMimeTypes() {
|
||||
return this.mimeTypes;
|
||||
}
|
||||
|
||||
protected boolean supportsMimeType(@Nullable MimeType mimeType) {
|
||||
if (mimeType == null) {
|
||||
return true;
|
||||
@@ -55,8 +69,4 @@ public abstract class ProtobufCodecSupport {
|
||||
return false;
|
||||
}
|
||||
|
||||
protected List<MimeType> getMimeTypes() {
|
||||
return Arrays.asList(MIME_TYPES);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+29
-17
@@ -17,6 +17,7 @@
|
||||
package org.springframework.http.codec.protobuf;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@@ -57,10 +58,32 @@ import org.springframework.util.MimeType;
|
||||
*/
|
||||
public class ProtobufEncoder extends ProtobufCodecSupport implements HttpMessageEncoder<Message> {
|
||||
|
||||
private static final List<MediaType> streamingMediaTypes = Arrays.stream(MIME_TYPES)
|
||||
.map(mimeType -> new MediaType(mimeType.getType(), mimeType.getSubtype(),
|
||||
Collections.singletonMap(DELIMITED_KEY, DELIMITED_VALUE)))
|
||||
.toList();
|
||||
private List<MediaType> mediaTypes = iniMediaTypes(Arrays.asList(MIME_TYPES));
|
||||
|
||||
|
||||
@Override
|
||||
protected void setMimeTypes(List<MimeType> mimeTypes) {
|
||||
super.setMimeTypes(mimeTypes);
|
||||
this.mediaTypes = iniMediaTypes(mimeTypes);
|
||||
}
|
||||
|
||||
private static List<MediaType> iniMediaTypes(List<MimeType> mimeTypes) {
|
||||
return mimeTypes.stream()
|
||||
.map(mimeType -> new MediaType(mimeType.getType(), mimeType.getSubtype(),
|
||||
Collections.singletonMap(DELIMITED_KEY, DELIMITED_VALUE)))
|
||||
.toList();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<MediaType> getStreamingMediaTypes() {
|
||||
return this.mediaTypes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MimeType> getEncodableMimeTypes() {
|
||||
return getMimeTypes();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@@ -84,14 +107,13 @@ public class ProtobufEncoder extends ProtobufCodecSupport implements HttpMessage
|
||||
}
|
||||
|
||||
private DataBuffer encodeValue(Message message, DataBufferFactory bufferFactory, boolean delimited) {
|
||||
|
||||
FastByteArrayOutputStream bos = new FastByteArrayOutputStream();
|
||||
try {
|
||||
if (delimited) {
|
||||
message.writeDelimitedTo(bos);
|
||||
message.writeDelimitedTo((OutputStream) bos);
|
||||
}
|
||||
else {
|
||||
message.writeTo(bos);
|
||||
message.writeTo((OutputStream) bos);
|
||||
}
|
||||
byte[] bytes = bos.toByteArrayUnsafe();
|
||||
return bufferFactory.wrap(bytes);
|
||||
@@ -101,14 +123,4 @@ public class ProtobufEncoder extends ProtobufCodecSupport implements HttpMessage
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MediaType> getStreamingMediaTypes() {
|
||||
return streamingMediaTypes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MimeType> getEncodableMimeTypes() {
|
||||
return getMimeTypes();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user