Skip to content

Commit 47779df

Browse files
committed
Merge pull request #24087
Closes gh-24087
2 parents d9ebc3b + 25f3465 commit 47779df

File tree

16 files changed

+1721
-40
lines changed

16 files changed

+1721
-40
lines changed

spring-messaging/spring-messaging.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ dependencies {
1313
optional("io.rsocket:rsocket-transport-netty")
1414
optional("com.fasterxml.jackson.core:jackson-databind")
1515
optional("javax.xml.bind:jaxb-api")
16+
optional("com.google.protobuf:protobuf-java-util")
1617
optional("org.jetbrains.kotlinx:kotlinx-coroutines-core")
1718
optional("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
1819
testCompile("javax.inject:javax.inject-tck")

spring-messaging/src/main/java/org/springframework/messaging/converter/AbstractMessageConverter.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.messaging.converter;
1818

1919
import java.util.ArrayList;
20+
import java.util.Arrays;
2021
import java.util.Collection;
2122
import java.util.Collections;
2223
import java.util.List;
@@ -47,7 +48,7 @@ public abstract class AbstractMessageConverter implements SmartMessageConverter
4748

4849
protected final Log logger = LogFactory.getLog(getClass());
4950

50-
private final List<MimeType> supportedMimeTypes;
51+
private final List<MimeType> supportedMimeTypes = new ArrayList<>(4);
5152

5253
@Nullable
5354
private ContentTypeResolver contentTypeResolver = new DefaultContentTypeResolver();
@@ -58,21 +59,28 @@ public abstract class AbstractMessageConverter implements SmartMessageConverter
5859

5960

6061
/**
61-
* Construct an {@code AbstractMessageConverter} supporting a single MIME type.
62+
* Constructor with a single MIME type.
6263
* @param supportedMimeType the supported MIME type
6364
*/
6465
protected AbstractMessageConverter(MimeType supportedMimeType) {
65-
Assert.notNull(supportedMimeType, "supportedMimeType is required");
66-
this.supportedMimeTypes = Collections.<MimeType>singletonList(supportedMimeType);
66+
this(Collections.singletonList(supportedMimeType));
6767
}
6868

6969
/**
70-
* Construct an {@code AbstractMessageConverter} supporting multiple MIME types.
70+
* Constructor with one or more MIME types via vararg.
71+
* @param supportedMimeTypes the supported MIME types
72+
* @since 5.2.2
73+
*/
74+
protected AbstractMessageConverter(MimeType... supportedMimeTypes) {
75+
this(Arrays.asList(supportedMimeTypes));
76+
}
77+
78+
/**
79+
* Constructor with a Collection of MIME types.
7180
* @param supportedMimeTypes the supported MIME types
7281
*/
7382
protected AbstractMessageConverter(Collection<MimeType> supportedMimeTypes) {
74-
Assert.notNull(supportedMimeTypes, "supportedMimeTypes must not be null");
75-
this.supportedMimeTypes = new ArrayList<>(supportedMimeTypes);
83+
this.supportedMimeTypes.addAll(supportedMimeTypes);
7684
}
7785

7886

@@ -83,6 +91,14 @@ public List<MimeType> getSupportedMimeTypes() {
8391
return Collections.unmodifiableList(this.supportedMimeTypes);
8492
}
8593

94+
/**
95+
* Allows sub-classes to add more supported mime types.
96+
* @since 5.2.2
97+
*/
98+
protected void addSupportedMimeTypes(MimeType... supportedMimeTypes) {
99+
this.supportedMimeTypes.addAll(Arrays.asList(supportedMimeTypes));
100+
}
101+
86102
/**
87103
* Configure the {@link ContentTypeResolver} to use to resolve the content
88104
* type of an input message.

spring-messaging/src/main/java/org/springframework/messaging/converter/MappingJackson2MessageConverter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.io.Writer;
2323
import java.lang.reflect.Type;
2424
import java.nio.charset.Charset;
25-
import java.util.Arrays;
2625
import java.util.concurrent.atomic.AtomicReference;
2726

2827
import com.fasterxml.jackson.annotation.JsonView;
@@ -84,7 +83,7 @@ public MappingJackson2MessageConverter() {
8483
* @since 4.1.5
8584
*/
8685
public MappingJackson2MessageConverter(MimeType... supportedMimeTypes) {
87-
super(Arrays.asList(supportedMimeTypes));
86+
super(supportedMimeTypes);
8887
this.objectMapper = initObjectMapper();
8988
}
9089

spring-messaging/src/main/java/org/springframework/messaging/converter/MarshallingMessageConverter.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,7 +21,6 @@
2121
import java.io.StringReader;
2222
import java.io.StringWriter;
2323
import java.io.Writer;
24-
import java.util.Arrays;
2524

2625
import javax.xml.transform.Result;
2726
import javax.xml.transform.Source;
@@ -70,7 +69,7 @@ public MarshallingMessageConverter() {
7069
* @param supportedMimeTypes the MIME types
7170
*/
7271
public MarshallingMessageConverter(MimeType... supportedMimeTypes) {
73-
super(Arrays.asList(supportedMimeTypes));
72+
super(supportedMimeTypes);
7473
}
7574

7675
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2002-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.messaging.converter;
17+
18+
import com.google.protobuf.ExtensionRegistry;
19+
import com.google.protobuf.util.JsonFormat;
20+
21+
import org.springframework.lang.Nullable;
22+
23+
/**
24+
* Subclass of {@link ProtobufMessageConverter} for use with the official
25+
* {@code "com.google.protobuf:protobuf-java-util"} library for JSON support.
26+
*
27+
* <p>Most importantly, this class allows for custom JSON parser and printer
28+
* configurations through the {@link JsonFormat} utility. If no special parser
29+
* or printer configuration is given, default variants will be used instead.
30+
*
31+
* <p>Requires Protobuf 3.x and {@code "com.google.protobuf:protobuf-java-util"} 3.x,
32+
* with 3.3 or higher recommended.
33+
*
34+
* @author Rossen Stoyanchev
35+
* @since 5.2.2
36+
*/
37+
public class ProtobufJsonFormatMessageConverter extends ProtobufMessageConverter {
38+
39+
/**
40+
* Constructor with default instances of {@link JsonFormat.Parser},
41+
* {@link JsonFormat.Printer}, and {@link ExtensionRegistry}.
42+
*/
43+
public ProtobufJsonFormatMessageConverter(@Nullable ExtensionRegistry extensionRegistry) {
44+
this(null, null);
45+
}
46+
47+
/**
48+
* Constructor with given instances of {@link JsonFormat.Parser},
49+
* {@link JsonFormat.Printer}, and a default instance of {@link ExtensionRegistry}.
50+
*/
51+
public ProtobufJsonFormatMessageConverter(
52+
@Nullable JsonFormat.Parser parser, @Nullable JsonFormat.Printer printer) {
53+
54+
this(parser, printer, null);
55+
}
56+
57+
/**
58+
* Constructor with given instances of {@link JsonFormat.Parser},
59+
* {@link JsonFormat.Printer}, and {@link ExtensionRegistry}.
60+
*/
61+
public ProtobufJsonFormatMessageConverter(@Nullable JsonFormat.Parser parser,
62+
@Nullable JsonFormat.Printer printer, @Nullable ExtensionRegistry extensionRegistry) {
63+
64+
super(new ProtobufJavaUtilSupport(parser, printer), extensionRegistry);
65+
}
66+
67+
}

0 commit comments

Comments
 (0)