Skip to content

Commit

Permalink
Polishing contribution
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Nov 29, 2019
1 parent e858b21 commit 25f3465
Show file tree
Hide file tree
Showing 15 changed files with 157 additions and 173 deletions.
2 changes: 1 addition & 1 deletion spring-messaging/spring-messaging.gradle
Expand Up @@ -13,9 +13,9 @@ 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")
optional("com.google.protobuf:protobuf-java-util")
testCompile("javax.inject:javax.inject-tck")
testCompile("javax.servlet:javax.servlet-api")
testCompile("javax.validation:validation-api")
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,10 +17,10 @@
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;
import java.util.Arrays;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -48,7 +48,7 @@ public abstract class AbstractMessageConverter implements SmartMessageConverter

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

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

@Nullable
private ContentTypeResolver contentTypeResolver = new DefaultContentTypeResolver();
Expand All @@ -59,28 +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) {
setSupportedMimeTypes(Collections.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(Collection<MimeType> supportedMimeTypes) {
setSupportedMimeTypes(new ArrayList<>(supportedMimeTypes));
protected AbstractMessageConverter(MimeType... supportedMimeTypes) {
this(Arrays.asList(supportedMimeTypes));
}

/**
* Construct an {@code AbstractMessageConverter} supporting multiple MIME types.
* Constructor with a Collection of MIME types.
* @param supportedMimeTypes the supported MIME types
* @since 5.2.2
*/
protected AbstractMessageConverter(MimeType... supportedMimeTypes) {
setSupportedMimeTypes(Arrays.asList(supportedMimeTypes));
protected AbstractMessageConverter(Collection<MimeType> supportedMimeTypes) {
this.supportedMimeTypes.addAll(supportedMimeTypes);
}


Expand All @@ -92,12 +92,11 @@ public List<MimeType> getSupportedMimeTypes() {
}

/**
* Set the list of {@link MimeType} objects supported by this converter.
* Allows sub-classes to add more supported mime types.
* @since 5.2.2
*/
protected void setSupportedMimeTypes(List<MimeType> supportedMimeTypes) {
Assert.notNull(supportedMimeTypes, "supportedMimeTypes must not be null");
this.supportedMimeTypes = supportedMimeTypes;
protected void addSupportedMimeTypes(MimeType... supportedMimeTypes) {
this.supportedMimeTypes.addAll(Arrays.asList(supportedMimeTypes));
}

/**
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
@@ -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
@@ -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);
}

}
Expand Up @@ -23,12 +23,12 @@
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Map;

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

import org.springframework.lang.Nullable;
import org.springframework.messaging.MessageHeaders;
import org.springframework.util.ClassUtils;
Expand Down Expand Up @@ -77,59 +77,23 @@ public class ProtobufMessageConverter extends AbstractMessageConverter {


/**
* Construct a new {@code ProtobufMessageConverter}.
* Constructor with a default instance of {@link ExtensionRegistry}.
*/
public ProtobufMessageConverter() {
this((ProtobufFormatSupport) null, (ExtensionRegistry) null);
this(null, null);
}

/**
* Construct a new {@code ProtobufMessageConverter} with a registry that specifies
* protocol message extensions.
*
* @param extensionRegistry the registry to populate
* Constructor with a given {@code ExtensionRegistry}.
*/
public ProtobufMessageConverter(@Nullable ExtensionRegistry extensionRegistry) {
public ProtobufMessageConverter(ExtensionRegistry extensionRegistry) {
this(null, extensionRegistry);
}

/**
* Construct a new {@code ProtobufMessageConverter} with the given
* {@code JsonFormat.Parser} and {@code JsonFormat.Printer} configuration.
*
* @param parser the JSON parser configuration
* @param printer the JSON printer configuration
*/
public ProtobufMessageConverter(@Nullable JsonFormat.Parser parser, @Nullable JsonFormat.Printer printer) {
this(new ProtobufJavaUtilSupport(parser, printer), (ExtensionRegistry) null);
}
ProtobufMessageConverter(@Nullable ProtobufFormatSupport formatSupport,
@Nullable ExtensionRegistry extensionRegistry) {

/**
* Construct a new {@code ProtobufMessageConverter} with the given
* {@code JsonFormat.Parser} and {@code JsonFormat.Printer} configuration, also
* accepting a registry that specifies protocol message extensions.
*
* @param parser the JSON parser configuration
* @param printer the JSON printer configuration
* @param extensionRegistry the registry to populate
*/
public ProtobufMessageConverter(@Nullable JsonFormat.Parser parser,
@Nullable JsonFormat.Printer printer, @Nullable ExtensionRegistry extensionRegistry) {

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

/**
* Construct a new {@code ProtobufMessageConverter} with the given
* {@code ProtobufFormatSupport} configuration, also
* accepting a registry that specifies protocol message extensions.
*
* @param formatSupport support third party
* @param extensionRegistry the registry to populate
*/
public ProtobufMessageConverter(@Nullable ProtobufFormatSupport formatSupport,
@Nullable ExtensionRegistry extensionRegistry) {
super(PROTOBUF);
super(PROTOBUF, TEXT_PLAIN);

if (formatSupport != null) {
this.protobufFormatSupport = formatSupport;
Expand All @@ -142,12 +106,13 @@ else if (ClassUtils.isPresent("com.google.protobuf.util.JsonFormat", getClass().
}

if (this.protobufFormatSupport != null) {
setSupportedMimeTypes(Arrays.asList(protobufFormatSupport.supportedMediaTypes()));
addSupportedMimeTypes(this.protobufFormatSupport.supportedMediaTypes());
}

this.extensionRegistry = (extensionRegistry == null ? ExtensionRegistry.newInstance() : extensionRegistry);
}


@Override
protected boolean supports(Class<?> clazz) {
return Message.class.isAssignableFrom(clazz);
Expand All @@ -161,7 +126,9 @@ protected boolean canConvertTo(Object payload, @Nullable MessageHeaders headers)
}

@Override
protected Object convertFromInternal(org.springframework.messaging.Message<?> message, Class<?> targetClass, @Nullable Object conversionHint) {
protected Object convertFromInternal(org.springframework.messaging.Message<?> message,
Class<?> targetClass, @Nullable Object conversionHint) {

MimeType contentType = getMimeType(message.getHeaders());
final Object payload = message.getPayload();

Expand All @@ -175,32 +142,31 @@ protected Object convertFromInternal(org.springframework.messaging.Message<?> me
}

Message.Builder builder = getMessageBuilder(targetClass);

try {
if (PROTOBUF.isCompatibleWith(contentType)) {
builder.mergeFrom((byte[]) payload, this.extensionRegistry);
}
else if (protobufFormatSupport != null) {
this.protobufFormatSupport.merge(
message, charset, contentType, this.extensionRegistry, builder);
else if (this.protobufFormatSupport != null) {
this.protobufFormatSupport.merge(message, charset, contentType, this.extensionRegistry, builder);
}
}
catch (IOException e) {
throw new MessageConversionException(message, "Could not read proto message" + e.getMessage(), e);
catch (IOException ex) {
throw new MessageConversionException(message, "Could not read proto message" + ex.getMessage(), ex);
}

return builder.build();
}


@Override
protected Object convertToInternal(Object payload, @Nullable MessageHeaders headers, @Nullable Object conversionHint) {
protected Object convertToInternal(
Object payload, @Nullable MessageHeaders headers, @Nullable Object conversionHint) {

final Message message = (Message) payload;

MimeType contentType = getMimeType(headers);
if (contentType == null) {
contentType = PROTOBUF;

}

Charset charset = contentType.getCharset();
Expand All @@ -220,14 +186,13 @@ else if (this.protobufFormatSupport != null) {
payload = new String(outputStream.toByteArray(), charset);
}
}
catch (IOException e) {
throw new MessageConversionException("Could not write proto message" + e.getMessage(), e);
catch (IOException ex) {
throw new MessageConversionException("Could not write proto message" + ex.getMessage(), ex);

}
return payload;
}


/**
* Create a new {@code Message.Builder} instance for the given class.
* <p>This method uses a ConcurrentReferenceHashMap for caching method lookups.
Expand Down Expand Up @@ -257,9 +222,9 @@ interface ProtobufFormatSupport {

boolean supportsWriteOnly(@Nullable MimeType mediaType);

void merge(org.springframework.messaging.Message<?> message, Charset charset, MimeType contentType,
ExtensionRegistry extensionRegistry, Message.Builder builder)
throws IOException, MessageConversionException;
void merge(org.springframework.messaging.Message<?> message,
Charset charset, MimeType contentType, ExtensionRegistry extensionRegistry,
Message.Builder builder) throws IOException, MessageConversionException;

void print(Message message, OutputStream output, MimeType contentType, Charset charset)
throws IOException, MessageConversionException;
Expand All @@ -283,7 +248,7 @@ public ProtobufJavaUtilSupport(@Nullable JsonFormat.Parser parser, @Nullable Jso

@Override
public MimeType[] supportedMediaTypes() {
return new MimeType[]{PROTOBUF, TEXT_PLAIN, APPLICATION_JSON};
return new MimeType[]{APPLICATION_JSON};
}

@Override
Expand All @@ -292,8 +257,8 @@ public boolean supportsWriteOnly(@Nullable MimeType mimeType) {
}

@Override
public void merge(org.springframework.messaging.Message<?> message, Charset charset, MimeType contentType,
ExtensionRegistry extensionRegistry, Message.Builder builder)
public void merge(org.springframework.messaging.Message<?> message, Charset charset,
MimeType contentType, ExtensionRegistry extensionRegistry, Message.Builder builder)
throws IOException, MessageConversionException {

if (contentType.isCompatibleWith(APPLICATION_JSON)) {
Expand All @@ -313,12 +278,12 @@ public void print(Message message, OutputStream output, MimeType contentType, Ch
OutputStreamWriter writer = new OutputStreamWriter(output, charset);
this.printer.appendTo(message, writer);
writer.flush();
} else {
}
else {
throw new MessageConversionException(
"protobuf-java-util does not support printing " + contentType);
}
}
}


}

0 comments on commit 25f3465

Please sign in to comment.