Skip to content

Commit 6ebb207

Browse files
committed
Allow to set MimeType's in ProtobufCodecSupport
See spring-projectsgh-35403
1 parent 754372a commit 6ebb207

File tree

2 files changed

+44
-22
lines changed

2 files changed

+44
-22
lines changed

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@
2121

2222
import org.jspecify.annotations.Nullable;
2323

24+
import org.springframework.util.Assert;
2425
import org.springframework.util.MimeType;
2526

2627
/**
2728
* Base class providing support methods for Protobuf encoding and decoding.
2829
*
2930
* @author Sebastien Deleuze
31+
* @author Rossen Stoyanchev
3032
* @since 5.1
3133
*/
3234
public abstract class ProtobufCodecSupport {
3335

34-
static final MimeType[] MIME_TYPES = new MimeType[]{
36+
protected static final MimeType[] MIME_TYPES = new MimeType[] {
3537
new MimeType("application", "x-protobuf"),
3638
new MimeType("application", "*+x-protobuf"),
3739
new MimeType("application", "octet-stream"),
@@ -43,6 +45,18 @@ public abstract class ProtobufCodecSupport {
4345
static final String DELIMITED_VALUE = "true";
4446

4547

48+
private List<MimeType> mimeTypes = Arrays.asList(MIME_TYPES);
49+
50+
51+
protected void setMimeTypes(List<MimeType> mimeTypes) {
52+
Assert.notEmpty(mimeTypes, "MimeType List must not be empty");
53+
this.mimeTypes = List.copyOf(mimeTypes);
54+
}
55+
56+
protected List<MimeType> getMimeTypes() {
57+
return this.mimeTypes;
58+
}
59+
4660
protected boolean supportsMimeType(@Nullable MimeType mimeType) {
4761
if (mimeType == null) {
4862
return true;
@@ -55,8 +69,4 @@ protected boolean supportsMimeType(@Nullable MimeType mimeType) {
5569
return false;
5670
}
5771

58-
protected List<MimeType> getMimeTypes() {
59-
return Arrays.asList(MIME_TYPES);
60-
}
61-
6272
}

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

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.http.codec.protobuf;
1818

1919
import java.io.IOException;
20+
import java.io.OutputStream;
2021
import java.util.Arrays;
2122
import java.util.Collections;
2223
import java.util.List;
@@ -57,10 +58,32 @@
5758
*/
5859
public class ProtobufEncoder extends ProtobufCodecSupport implements HttpMessageEncoder<Message> {
5960

60-
private static final List<MediaType> streamingMediaTypes = Arrays.stream(MIME_TYPES)
61-
.map(mimeType -> new MediaType(mimeType.getType(), mimeType.getSubtype(),
62-
Collections.singletonMap(DELIMITED_KEY, DELIMITED_VALUE)))
63-
.toList();
61+
private List<MediaType> mediaTypes = iniMediaTypes(Arrays.asList(MIME_TYPES));
62+
63+
64+
@Override
65+
protected void setMimeTypes(List<MimeType> mimeTypes) {
66+
super.setMimeTypes(mimeTypes);
67+
this.mediaTypes = iniMediaTypes(mimeTypes);
68+
}
69+
70+
private static List<MediaType> iniMediaTypes(List<MimeType> mimeTypes) {
71+
return mimeTypes.stream()
72+
.map(mimeType -> new MediaType(mimeType.getType(), mimeType.getSubtype(),
73+
Collections.singletonMap(DELIMITED_KEY, DELIMITED_VALUE)))
74+
.toList();
75+
}
76+
77+
78+
@Override
79+
public List<MediaType> getStreamingMediaTypes() {
80+
return this.mediaTypes;
81+
}
82+
83+
@Override
84+
public List<MimeType> getEncodableMimeTypes() {
85+
return getMimeTypes();
86+
}
6487

6588

6689
@Override
@@ -84,14 +107,13 @@ public DataBuffer encodeValue(Message message, DataBufferFactory bufferFactory,
84107
}
85108

86109
private DataBuffer encodeValue(Message message, DataBufferFactory bufferFactory, boolean delimited) {
87-
88110
FastByteArrayOutputStream bos = new FastByteArrayOutputStream();
89111
try {
90112
if (delimited) {
91-
message.writeDelimitedTo(bos);
113+
message.writeDelimitedTo((OutputStream) bos);
92114
}
93115
else {
94-
message.writeTo(bos);
116+
message.writeTo((OutputStream) bos);
95117
}
96118
byte[] bytes = bos.toByteArrayUnsafe();
97119
return bufferFactory.wrap(bytes);
@@ -101,14 +123,4 @@ private DataBuffer encodeValue(Message message, DataBufferFactory bufferFactory,
101123
}
102124
}
103125

104-
@Override
105-
public List<MediaType> getStreamingMediaTypes() {
106-
return streamingMediaTypes;
107-
}
108-
109-
@Override
110-
public List<MimeType> getEncodableMimeTypes() {
111-
return getMimeTypes();
112-
}
113-
114126
}

0 commit comments

Comments
 (0)