Skip to content

Commit

Permalink
Merge pull request #24087
Browse files Browse the repository at this point in the history
Closes gh-24087
  • Loading branch information
rstoyanchev committed Nov 29, 2019
2 parents d9ebc3b + 25f3465 commit 47779df
Show file tree
Hide file tree
Showing 16 changed files with 1,721 additions and 40 deletions.
1 change: 1 addition & 0 deletions spring-messaging/spring-messaging.gradle
Expand Up @@ -13,6 +13,7 @@ dependencies {
optional("io.rsocket:rsocket-transport-netty")
optional("com.fasterxml.jackson.core:jackson-databind")
optional("javax.xml.bind:jaxb-api")
optional("com.google.protobuf:protobuf-java-util")
optional("org.jetbrains.kotlinx:kotlinx-coroutines-core")
optional("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
testCompile("javax.inject:javax.inject-tck")
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@
package org.springframework.messaging.converter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -47,7 +48,7 @@ public abstract class AbstractMessageConverter implements SmartMessageConverter

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

private final List<MimeType> supportedMimeTypes;
private final List<MimeType> supportedMimeTypes = new ArrayList<>(4);

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


/**
* Construct an {@code AbstractMessageConverter} supporting a single MIME type.
* Constructor with a single MIME type.
* @param supportedMimeType the supported MIME type
*/
protected AbstractMessageConverter(MimeType supportedMimeType) {
Assert.notNull(supportedMimeType, "supportedMimeType is required");
this.supportedMimeTypes = Collections.<MimeType>singletonList(supportedMimeType);
this(Collections.singletonList(supportedMimeType));
}

/**
* Construct an {@code AbstractMessageConverter} supporting multiple MIME types.
* Constructor with one or more MIME types via vararg.
* @param supportedMimeTypes the supported MIME types
* @since 5.2.2
*/
protected AbstractMessageConverter(MimeType... supportedMimeTypes) {
this(Arrays.asList(supportedMimeTypes));
}

/**
* Constructor with a Collection of MIME types.
* @param supportedMimeTypes the supported MIME types
*/
protected AbstractMessageConverter(Collection<MimeType> supportedMimeTypes) {
Assert.notNull(supportedMimeTypes, "supportedMimeTypes must not be null");
this.supportedMimeTypes = new ArrayList<>(supportedMimeTypes);
this.supportedMimeTypes.addAll(supportedMimeTypes);
}


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

/**
* Allows sub-classes to add more supported mime types.
* @since 5.2.2
*/
protected void addSupportedMimeTypes(MimeType... supportedMimeTypes) {
this.supportedMimeTypes.addAll(Arrays.asList(supportedMimeTypes));
}

/**
* Configure the {@link ContentTypeResolver} to use to resolve the content
* type of an input message.
Expand Down
Expand Up @@ -22,7 +22,6 @@
import java.io.Writer;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicReference;

import com.fasterxml.jackson.annotation.JsonView;
Expand Down Expand Up @@ -84,7 +83,7 @@ public MappingJackson2MessageConverter() {
* @since 4.1.5
*/
public MappingJackson2MessageConverter(MimeType... supportedMimeTypes) {
super(Arrays.asList(supportedMimeTypes));
super(supportedMimeTypes);
this.objectMapper = initObjectMapper();
}

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,7 +21,6 @@
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Arrays;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
Expand Down Expand Up @@ -70,7 +69,7 @@ public MarshallingMessageConverter() {
* @param supportedMimeTypes the MIME types
*/
public MarshallingMessageConverter(MimeType... supportedMimeTypes) {
super(Arrays.asList(supportedMimeTypes));
super(supportedMimeTypes);
}

/**
Expand Down
@@ -0,0 +1,67 @@
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.messaging.converter;

import com.google.protobuf.ExtensionRegistry;
import com.google.protobuf.util.JsonFormat;

import org.springframework.lang.Nullable;

/**
* Subclass of {@link ProtobufMessageConverter} for use with the official
* {@code "com.google.protobuf:protobuf-java-util"} library for JSON support.
*
* <p>Most importantly, this class allows for custom JSON parser and printer
* configurations through the {@link JsonFormat} utility. If no special parser
* or printer configuration is given, default variants will be used instead.
*
* <p>Requires Protobuf 3.x and {@code "com.google.protobuf:protobuf-java-util"} 3.x,
* with 3.3 or higher recommended.
*
* @author Rossen Stoyanchev
* @since 5.2.2
*/
public class ProtobufJsonFormatMessageConverter extends ProtobufMessageConverter {

/**
* Constructor with default instances of {@link JsonFormat.Parser},
* {@link JsonFormat.Printer}, and {@link ExtensionRegistry}.
*/
public ProtobufJsonFormatMessageConverter(@Nullable ExtensionRegistry extensionRegistry) {
this(null, null);
}

/**
* Constructor with given instances of {@link JsonFormat.Parser},
* {@link JsonFormat.Printer}, and a default instance of {@link ExtensionRegistry}.
*/
public ProtobufJsonFormatMessageConverter(
@Nullable JsonFormat.Parser parser, @Nullable JsonFormat.Printer printer) {

this(parser, printer, null);
}

/**
* Constructor with given instances of {@link JsonFormat.Parser},
* {@link JsonFormat.Printer}, and {@link ExtensionRegistry}.
*/
public ProtobufJsonFormatMessageConverter(@Nullable JsonFormat.Parser parser,
@Nullable JsonFormat.Printer printer, @Nullable ExtensionRegistry extensionRegistry) {

super(new ProtobufJavaUtilSupport(parser, printer), extensionRegistry);
}

}

0 comments on commit 47779df

Please sign in to comment.