Update Google Protobuf support to 3.0.0
This commit adds support for Google Protobuf 3.0.0 and make some changes in the additional formats support: * "com.googlecode.protobuf-java-format:protobuf-java-format" is no longer required and its required version has been raised to 1.3+ (this lib adds support for JSON, XML, HTML formats) * "com.google.protobuf:protobuf-java-util" is also now supported for JSON format Issue: SPR-13589
This commit is contained in:
parent
7b2196b408
commit
4d035e3ab1
|
|
@ -69,6 +69,7 @@ configure(allprojects) { project ->
|
||||||
ext.okhttpVersion = "2.7.5"
|
ext.okhttpVersion = "2.7.5"
|
||||||
ext.okhttp3Version = "3.4.1"
|
ext.okhttp3Version = "3.4.1"
|
||||||
ext.poiVersion = "3.14"
|
ext.poiVersion = "3.14"
|
||||||
|
ext.protobufVersion = "3.0.0-beta-4"
|
||||||
ext.reactivestreamsVersion = "1.0.0"
|
ext.reactivestreamsVersion = "1.0.0"
|
||||||
ext.reactorVersion = "2.0.8.RELEASE"
|
ext.reactorVersion = "2.0.8.RELEASE"
|
||||||
ext.reactorCoreVersion = '3.0.0.BUILD-SNAPSHOT'
|
ext.reactorCoreVersion = '3.0.0.BUILD-SNAPSHOT'
|
||||||
|
|
@ -734,7 +735,8 @@ project("spring-web") {
|
||||||
optional("org.eclipse.jetty:jetty-server:${jettyVersion}") {
|
optional("org.eclipse.jetty:jetty-server:${jettyVersion}") {
|
||||||
exclude group: "javax.servlet", module: "javax.servlet-api"
|
exclude group: "javax.servlet", module: "javax.servlet-api"
|
||||||
}
|
}
|
||||||
optional("com.google.protobuf:protobuf-java:2.6.1")
|
optional("com.google.protobuf:protobuf-java:${protobufVersion}")
|
||||||
|
optional("com.google.protobuf:protobuf-java-util:${protobufVersion}")
|
||||||
optional("com.googlecode.protobuf-java-format:protobuf-java-format:1.4")
|
optional("com.googlecode.protobuf-java-format:protobuf-java-format:1.4")
|
||||||
optional("javax.mail:javax.mail-api:${javamailVersion}")
|
optional("javax.mail:javax.mail-api:${javamailVersion}")
|
||||||
testCompile(project(":spring-context-support")) // for JafMediaTypeFactory
|
testCompile(project(":spring-context-support")) // for JafMediaTypeFactory
|
||||||
|
|
|
||||||
|
|
@ -17,20 +17,21 @@
|
||||||
package org.springframework.http.converter.protobuf;
|
package org.springframework.http.converter.protobuf;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.OutputStream;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
import com.google.protobuf.CodedOutputStream;
|
||||||
import com.google.protobuf.ExtensionRegistry;
|
import com.google.protobuf.ExtensionRegistry;
|
||||||
import com.google.protobuf.Message;
|
import com.google.protobuf.Message;
|
||||||
import com.google.protobuf.TextFormat;
|
import com.google.protobuf.TextFormat;
|
||||||
import com.googlecode.protobuf.format.HtmlFormat;
|
import com.googlecode.protobuf.format.FormatFactory;
|
||||||
import com.googlecode.protobuf.format.JsonFormat;
|
|
||||||
import com.googlecode.protobuf.format.ProtobufFormatter;
|
import com.googlecode.protobuf.format.ProtobufFormatter;
|
||||||
import com.googlecode.protobuf.format.XmlFormat;
|
|
||||||
|
|
||||||
import org.springframework.http.HttpInputMessage;
|
import org.springframework.http.HttpInputMessage;
|
||||||
import org.springframework.http.HttpOutputMessage;
|
import org.springframework.http.HttpOutputMessage;
|
||||||
|
|
@ -38,18 +39,28 @@ import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.converter.AbstractHttpMessageConverter;
|
import org.springframework.http.converter.AbstractHttpMessageConverter;
|
||||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||||
import org.springframework.http.converter.HttpMessageNotWritableException;
|
import org.springframework.http.converter.HttpMessageNotWritableException;
|
||||||
import org.springframework.util.FileCopyUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An {@code HttpMessageConverter} that reads and writes {@link com.google.protobuf.Message}s
|
* An {@code HttpMessageConverter} that reads and writes {@link com.google.protobuf.Message}s
|
||||||
* using <a href="https://developers.google.com/protocol-buffers/">Google Protocol Buffers</a>.
|
* using <a href="https://developers.google.com/protocol-buffers/">Google Protocol Buffers</a>.
|
||||||
*
|
*
|
||||||
* <p>By default, it supports {@code "application/x-protobuf"}, {@code "text/plain"},
|
* <p>This converter supports by default {@code "application/x-protobuf"} and {@code "text/plain"}
|
||||||
* {@code "application/json"}, {@code "application/xml"}, while also writing {@code "text/html"}.
|
* with the official {@code "com.google.protobuf:protobuf-java"} library.
|
||||||
|
*
|
||||||
|
* <p>Other formats can be supported with additional libraries:
|
||||||
|
* <ul>
|
||||||
|
* <li>{@code "application/json"} with the official library
|
||||||
|
* {@code "com.google.protobuf:protobuf-java-util"}
|
||||||
|
* <li>{@code "application/json"}, {@code "application/xml"} and {@code "text/html"} (write only)
|
||||||
|
* can be supported with the 3rd party library
|
||||||
|
* {@code "com.googlecode.protobuf-java-format:protobuf-java-format"}
|
||||||
|
* </ul>
|
||||||
*
|
*
|
||||||
* <p>To generate {@code Message} Java classes, you need to install the {@code protoc} binary.
|
* <p>To generate {@code Message} Java classes, you need to install the {@code protoc} binary.
|
||||||
*
|
*
|
||||||
* <p>Requires Protobuf 2.6 and Protobuf Java Format 1.4, as of Spring 4.3.
|
* <p>Requires Protobuf 2.6 and Protobuf Java Format 1.4, as of Spring 4.3.
|
||||||
|
* Supports up to Protobuf 3.0.0.
|
||||||
*
|
*
|
||||||
* @author Alex Antonov
|
* @author Alex Antonov
|
||||||
* @author Brian Clozel
|
* @author Brian Clozel
|
||||||
|
|
@ -66,18 +77,32 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M
|
||||||
|
|
||||||
public static final String X_PROTOBUF_MESSAGE_HEADER = "X-Protobuf-Message";
|
public static final String X_PROTOBUF_MESSAGE_HEADER = "X-Protobuf-Message";
|
||||||
|
|
||||||
|
private static final boolean isProtobufJavaUtilPresent =
|
||||||
|
ClassUtils.isPresent("com.google.protobuf.util.JsonFormat", ProtobufHttpMessageConverter.class.getClassLoader());
|
||||||
|
|
||||||
private static final ProtobufFormatter JSON_FORMAT = new JsonFormat();
|
private static final boolean isProtobufJavaFormatPresent =
|
||||||
|
ClassUtils.isPresent("com.googlecode.protobuf.format.JsonFormat", ProtobufHttpMessageConverter.class.getClassLoader());
|
||||||
|
|
||||||
private static final ProtobufFormatter XML_FORMAT = new XmlFormat();
|
private static final MediaType[] SUPPORTED_MEDIATYPES;
|
||||||
|
|
||||||
private static final ProtobufFormatter HTML_FORMAT = new HtmlFormat();
|
|
||||||
|
|
||||||
|
private final ProtobufFormatsSupport protobufFormatsSupport;
|
||||||
|
|
||||||
private static final ConcurrentHashMap<Class<?>, Method> methodCache = new ConcurrentHashMap<>();
|
private static final ConcurrentHashMap<Class<?>, Method> methodCache = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
private final ExtensionRegistry extensionRegistry = ExtensionRegistry.newInstance();
|
private final ExtensionRegistry extensionRegistry = ExtensionRegistry.newInstance();
|
||||||
|
|
||||||
|
static {
|
||||||
|
if (isProtobufJavaFormatPresent) {
|
||||||
|
SUPPORTED_MEDIATYPES = new MediaType[] {PROTOBUF, MediaType.TEXT_PLAIN, MediaType.APPLICATION_XML,
|
||||||
|
MediaType.APPLICATION_JSON};
|
||||||
|
}
|
||||||
|
else if (isProtobufJavaUtilPresent) {
|
||||||
|
SUPPORTED_MEDIATYPES = new MediaType[] {PROTOBUF, MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON};
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
SUPPORTED_MEDIATYPES = new MediaType[] {PROTOBUF, MediaType.TEXT_PLAIN};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new instance.
|
* Construct a new instance.
|
||||||
|
|
@ -91,7 +116,16 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M
|
||||||
* that allows the registration of message extensions.
|
* that allows the registration of message extensions.
|
||||||
*/
|
*/
|
||||||
public ProtobufHttpMessageConverter(ExtensionRegistryInitializer registryInitializer) {
|
public ProtobufHttpMessageConverter(ExtensionRegistryInitializer registryInitializer) {
|
||||||
super(PROTOBUF, MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML);
|
super(SUPPORTED_MEDIATYPES);
|
||||||
|
if (isProtobufJavaFormatPresent) {
|
||||||
|
this.protobufFormatsSupport = new ProtobufJavaFormatSupport();
|
||||||
|
}
|
||||||
|
else if (isProtobufJavaUtilPresent) {
|
||||||
|
this.protobufFormatsSupport = new ProtobufJavaUtilSupport();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.protobufFormatsSupport = null;
|
||||||
|
}
|
||||||
if (registryInitializer != null) {
|
if (registryInitializer != null) {
|
||||||
registryInitializer.initializeExtensionRegistry(this.extensionRegistry);
|
registryInitializer.initializeExtensionRegistry(this.extensionRegistry);
|
||||||
}
|
}
|
||||||
|
|
@ -123,18 +157,16 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Message.Builder builder = getMessageBuilder(clazz);
|
Message.Builder builder = getMessageBuilder(clazz);
|
||||||
if (MediaType.TEXT_PLAIN.isCompatibleWith(contentType)) {
|
if (PROTOBUF.isCompatibleWith(contentType)) {
|
||||||
|
builder.mergeFrom(inputMessage.getBody(), this.extensionRegistry);
|
||||||
|
}
|
||||||
|
else if (MediaType.TEXT_PLAIN.isCompatibleWith(contentType)) {
|
||||||
InputStreamReader reader = new InputStreamReader(inputMessage.getBody(), charset);
|
InputStreamReader reader = new InputStreamReader(inputMessage.getBody(), charset);
|
||||||
TextFormat.merge(reader, this.extensionRegistry, builder);
|
TextFormat.merge(reader, this.extensionRegistry, builder);
|
||||||
}
|
}
|
||||||
else if (MediaType.APPLICATION_JSON.isCompatibleWith(contentType)) {
|
else if (isProtobufJavaUtilPresent || isProtobufJavaFormatPresent) {
|
||||||
JSON_FORMAT.merge(inputMessage.getBody(), charset, this.extensionRegistry, builder);
|
this.protobufFormatsSupport.merge(inputMessage.getBody(), charset, contentType,
|
||||||
}
|
this.extensionRegistry, builder);
|
||||||
else if (MediaType.APPLICATION_XML.isCompatibleWith(contentType)) {
|
|
||||||
XML_FORMAT.merge(inputMessage.getBody(), charset, this.extensionRegistry, builder);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
builder.mergeFrom(inputMessage.getBody(), this.extensionRegistry);
|
|
||||||
}
|
}
|
||||||
return builder.build();
|
return builder.build();
|
||||||
}
|
}
|
||||||
|
|
@ -149,7 +181,8 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected boolean canWrite(MediaType mediaType) {
|
protected boolean canWrite(MediaType mediaType) {
|
||||||
return (super.canWrite(mediaType) || MediaType.TEXT_HTML.isCompatibleWith(mediaType));
|
return (super.canWrite(mediaType) ||
|
||||||
|
(isProtobufJavaFormatPresent && MediaType.TEXT_HTML.isCompatibleWith(mediaType)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -165,23 +198,21 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M
|
||||||
charset = DEFAULT_CHARSET;
|
charset = DEFAULT_CHARSET;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (MediaType.TEXT_PLAIN.isCompatibleWith(contentType)) {
|
if (PROTOBUF.isCompatibleWith(contentType)) {
|
||||||
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputMessage.getBody(), charset);
|
setProtoHeader(outputMessage, message);
|
||||||
|
CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(outputMessage.getBody());
|
||||||
|
message.writeTo(codedOutputStream);
|
||||||
|
codedOutputStream.flush();
|
||||||
|
}
|
||||||
|
else if (MediaType.TEXT_PLAIN.isCompatibleWith(contentType)) {
|
||||||
|
final OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputMessage.getBody(), charset);
|
||||||
TextFormat.print(message, outputStreamWriter);
|
TextFormat.print(message, outputStreamWriter);
|
||||||
outputStreamWriter.flush();
|
outputStreamWriter.flush();
|
||||||
|
outputMessage.getBody().flush();
|
||||||
}
|
}
|
||||||
else if (MediaType.APPLICATION_JSON.isCompatibleWith(contentType)) {
|
else if (isProtobufJavaUtilPresent || isProtobufJavaFormatPresent) {
|
||||||
JSON_FORMAT.print(message, outputMessage.getBody(), charset);
|
this.protobufFormatsSupport.print(message, outputMessage.getBody(), contentType, charset);
|
||||||
}
|
outputMessage.getBody().flush();
|
||||||
else if (MediaType.APPLICATION_XML.isCompatibleWith(contentType)) {
|
|
||||||
XML_FORMAT.print(message, outputMessage.getBody(), charset);
|
|
||||||
}
|
|
||||||
else if (MediaType.TEXT_HTML.isCompatibleWith(contentType)) {
|
|
||||||
HTML_FORMAT.print(message, outputMessage.getBody(), charset);
|
|
||||||
}
|
|
||||||
else if (PROTOBUF.isCompatibleWith(contentType)) {
|
|
||||||
setProtoHeader(outputMessage, message);
|
|
||||||
FileCopyUtils.copy(message.toByteArray(), outputMessage.getBody());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -210,4 +241,100 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M
|
||||||
return (Message.Builder) method.invoke(clazz);
|
return (Message.Builder) method.invoke(clazz);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private interface ProtobufFormatsSupport {
|
||||||
|
|
||||||
|
void merge(InputStream input, Charset cs, MediaType contentType, ExtensionRegistry extensionRegistry,
|
||||||
|
Message.Builder builder) throws IOException;
|
||||||
|
|
||||||
|
void print(Message message, OutputStream output, MediaType contentType, Charset cs) throws IOException;
|
||||||
|
}
|
||||||
|
|
||||||
|
private class ProtobufJavaUtilSupport implements ProtobufFormatsSupport {
|
||||||
|
|
||||||
|
private final com.google.protobuf.util.JsonFormat.Parser parser;
|
||||||
|
|
||||||
|
private final com.google.protobuf.util.JsonFormat.Printer printer;
|
||||||
|
|
||||||
|
public ProtobufJavaUtilSupport() {
|
||||||
|
this.parser = com.google.protobuf.util.JsonFormat.parser();
|
||||||
|
this.printer = com.google.protobuf.util.JsonFormat.printer();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void merge(InputStream input, Charset cs, MediaType contentType,
|
||||||
|
ExtensionRegistry extensionRegistry, Message.Builder builder) throws IOException {
|
||||||
|
|
||||||
|
if (contentType.isCompatibleWith(MediaType.APPLICATION_JSON)) {
|
||||||
|
InputStreamReader reader = new InputStreamReader(input, cs);
|
||||||
|
this.parser.merge(reader, builder);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw new UnsupportedOperationException("com.googlecode.protobuf:protobuf-java-util does not support "
|
||||||
|
+ contentType.toString() + " format");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void print(Message message, OutputStream output, MediaType contentType, Charset cs) throws IOException {
|
||||||
|
if (contentType.isCompatibleWith(MediaType.APPLICATION_JSON)) {
|
||||||
|
this.printer.appendTo(message, new OutputStreamWriter(output, cs));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw new UnsupportedOperationException("com.googlecode.protobuf:protobuf-java-util does not support "
|
||||||
|
+ contentType.toString() + " format");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private class ProtobufJavaFormatSupport implements ProtobufFormatsSupport {
|
||||||
|
|
||||||
|
private final FormatFactory FORMAT_FACTORY;
|
||||||
|
|
||||||
|
private final ProtobufFormatter JSON_FORMATTER;
|
||||||
|
|
||||||
|
private final ProtobufFormatter XML_FORMATTER;
|
||||||
|
|
||||||
|
private final ProtobufFormatter HTML_FORMATTER;
|
||||||
|
|
||||||
|
public ProtobufJavaFormatSupport() {
|
||||||
|
FORMAT_FACTORY = new FormatFactory();
|
||||||
|
JSON_FORMATTER = FORMAT_FACTORY.createFormatter(FormatFactory.Formatter.JSON);
|
||||||
|
XML_FORMATTER = FORMAT_FACTORY.createFormatter(FormatFactory.Formatter.XML);
|
||||||
|
HTML_FORMATTER = FORMAT_FACTORY.createFormatter(FormatFactory.Formatter.HTML);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void merge(InputStream input, Charset cs, MediaType contentType,
|
||||||
|
ExtensionRegistry extensionRegistry, Message.Builder builder) throws IOException {
|
||||||
|
if (contentType.isCompatibleWith(MediaType.APPLICATION_JSON)) {
|
||||||
|
JSON_FORMATTER.merge(input, cs, extensionRegistry, builder);
|
||||||
|
}
|
||||||
|
else if (contentType.isCompatibleWith(MediaType.APPLICATION_XML)) {
|
||||||
|
XML_FORMATTER.merge(input, cs, extensionRegistry, builder);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw new UnsupportedOperationException("com.google.protobuf.util does not support "
|
||||||
|
+ contentType.toString() + " format");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void print(Message message, OutputStream output, MediaType contentType, Charset cs) throws IOException {
|
||||||
|
if (contentType.isCompatibleWith(MediaType.APPLICATION_JSON)) {
|
||||||
|
JSON_FORMATTER.print(message, output, cs);
|
||||||
|
}
|
||||||
|
else if (contentType.isCompatibleWith(MediaType.APPLICATION_XML)) {
|
||||||
|
XML_FORMATTER.print(message, output, cs);
|
||||||
|
}
|
||||||
|
else if (contentType.isCompatibleWith(MediaType.TEXT_HTML)) {
|
||||||
|
HTML_FORMATTER.print(message, output, cs);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw new UnsupportedOperationException("com.google.protobuf.util does not support "
|
||||||
|
+ contentType.toString() + " format");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue