parent
62ce9afe3a
commit
977734cc59
|
@ -68,7 +68,6 @@ configure(allprojects) { project ->
|
|||
ext.okhttp3Version = "3.2.0"
|
||||
ext.openjpaVersion = "2.4.1"
|
||||
ext.poiVersion = "3.14"
|
||||
ext.protobufVersion = "2.6.1"
|
||||
ext.reactorVersion = "2.0.7.RELEASE"
|
||||
ext.romeVersion = "1.6.0"
|
||||
ext.seleniumVersion = "2.53.0"
|
||||
|
@ -721,8 +720,8 @@ project("spring-web") {
|
|||
exclude group: "javax.servlet", module: "javax.servlet-api"
|
||||
}
|
||||
optional("log4j:log4j:1.2.17")
|
||||
optional("com.googlecode.protobuf-java-format:protobuf-java-format:1.2")
|
||||
optional("com.google.protobuf:protobuf-java:${protobufVersion}")
|
||||
optional("com.google.protobuf:protobuf-java:2.6.1")
|
||||
optional("com.googlecode.protobuf-java-format:protobuf-java-format:1.4")
|
||||
optional("javax.mail:javax.mail-api:${javamailVersion}")
|
||||
testCompile(project(":spring-context-support")) // for JafMediaTypeFactory
|
||||
testCompile("xmlunit:xmlunit:${xmlunitVersion}")
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
|
@ -28,9 +28,9 @@ import com.google.protobuf.Message;
|
|||
import com.google.protobuf.TextFormat;
|
||||
import com.googlecode.protobuf.format.HtmlFormat;
|
||||
import com.googlecode.protobuf.format.JsonFormat;
|
||||
import com.googlecode.protobuf.format.ProtobufFormatter;
|
||||
import com.googlecode.protobuf.format.XmlFormat;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpInputMessage;
|
||||
import org.springframework.http.HttpOutputMessage;
|
||||
import org.springframework.http.MediaType;
|
||||
|
@ -41,20 +41,19 @@ import org.springframework.util.FileCopyUtils;
|
|||
|
||||
|
||||
/**
|
||||
* An {@code HttpMessageConverter} that can read and write Protobuf
|
||||
* {@link com.google.protobuf.Message} using
|
||||
* <a href="https://developers.google.com/protocol-buffers/">Google Protocol buffers</a>.
|
||||
* 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>.
|
||||
*
|
||||
* <p>By default it supports {@code "application/json"}, {@code "application/xml"},
|
||||
* {@code "text/plain"} and {@code "application/x-protobuf"} while writing also
|
||||
* supports {@code "text/html"}
|
||||
* <p>By default, it supports {@code "application/x-protobuf"}, {@code "text/plain"},
|
||||
* {@code "application/json"}, {@code "application/xml"}, while also writing {@code "text/html"}.
|
||||
*
|
||||
* <p>To generate Message Java classes you need to install the protoc binary.
|
||||
* <p>To generate {@code Message} Java classes, you need to install the {@code protoc} binary.
|
||||
*
|
||||
* <p>Tested against Protobuf version 2.5.0.
|
||||
* <p>Requires Protobuf 2.6 and Protobuf Java Format 1.4, as of Spring 4.3.
|
||||
*
|
||||
* @author Alex Antonov
|
||||
* @author Brian Clozel
|
||||
* @author Juergen Hoeller
|
||||
* @since 4.1
|
||||
*/
|
||||
public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<Message> {
|
||||
|
@ -67,10 +66,17 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M
|
|||
|
||||
public static final String X_PROTOBUF_MESSAGE_HEADER = "X-Protobuf-Message";
|
||||
|
||||
|
||||
private static final ProtobufFormatter JSON_FORMAT = new JsonFormat();
|
||||
|
||||
private static final ProtobufFormatter XML_FORMAT = new XmlFormat();
|
||||
|
||||
private static final ProtobufFormatter HTML_FORMAT = new HtmlFormat();
|
||||
|
||||
|
||||
private static final ConcurrentHashMap<Class<?>, Method> methodCache = new ConcurrentHashMap<Class<?>, Method>();
|
||||
|
||||
|
||||
private ExtensionRegistry extensionRegistry = ExtensionRegistry.newInstance();
|
||||
private final ExtensionRegistry extensionRegistry = ExtensionRegistry.newInstance();
|
||||
|
||||
|
||||
/**
|
||||
|
@ -85,7 +91,7 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M
|
|||
* that allows the registration of message extensions.
|
||||
*/
|
||||
public ProtobufHttpMessageConverter(ExtensionRegistryInitializer registryInitializer) {
|
||||
super(PROTOBUF, MediaType.TEXT_PLAIN, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON);
|
||||
super(PROTOBUF, MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML);
|
||||
if (registryInitializer != null) {
|
||||
registryInitializer.initializeExtensionRegistry(this.extensionRegistry);
|
||||
}
|
||||
|
@ -97,65 +103,53 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M
|
|||
return Message.class.isAssignableFrom(clazz);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MediaType getDefaultContentType(Message message) {
|
||||
return PROTOBUF;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Message readInternal(Class<? extends Message> clazz, HttpInputMessage inputMessage)
|
||||
throws IOException, HttpMessageNotReadableException {
|
||||
|
||||
MediaType contentType = inputMessage.getHeaders().getContentType();
|
||||
contentType = (contentType != null ? contentType : PROTOBUF);
|
||||
|
||||
Charset charset = getCharset(inputMessage.getHeaders());
|
||||
InputStreamReader reader = new InputStreamReader(inputMessage.getBody(), charset);
|
||||
if (contentType == null) {
|
||||
contentType = PROTOBUF;
|
||||
}
|
||||
Charset charset = contentType.getCharSet();
|
||||
if (charset == null) {
|
||||
charset = DEFAULT_CHARSET;
|
||||
}
|
||||
|
||||
try {
|
||||
Message.Builder builder = getMessageBuilder(clazz);
|
||||
|
||||
if (MediaType.APPLICATION_JSON.isCompatibleWith(contentType)) {
|
||||
JsonFormat.merge(reader, this.extensionRegistry, builder);
|
||||
}
|
||||
else if (MediaType.TEXT_PLAIN.isCompatibleWith(contentType)) {
|
||||
if (MediaType.TEXT_PLAIN.isCompatibleWith(contentType)) {
|
||||
InputStreamReader reader = new InputStreamReader(inputMessage.getBody(), charset);
|
||||
TextFormat.merge(reader, this.extensionRegistry, builder);
|
||||
}
|
||||
else if (MediaType.APPLICATION_JSON.isCompatibleWith(contentType)) {
|
||||
JSON_FORMAT.merge(inputMessage.getBody(), charset, this.extensionRegistry, builder);
|
||||
}
|
||||
else if (MediaType.APPLICATION_XML.isCompatibleWith(contentType)) {
|
||||
XmlFormat.merge(reader, this.extensionRegistry, builder);
|
||||
XML_FORMAT.merge(inputMessage.getBody(), charset, this.extensionRegistry, builder);
|
||||
}
|
||||
else {
|
||||
builder.mergeFrom(inputMessage.getBody(), this.extensionRegistry);
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new HttpMessageNotReadableException("Could not read Protobuf message: " + e.getMessage(), e);
|
||||
catch (Exception ex) {
|
||||
throw new HttpMessageNotReadableException("Could not read Protobuf message: " + ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
private Charset getCharset(HttpHeaders headers) {
|
||||
if (headers == null || headers.getContentType() == null || headers.getContentType().getCharSet() == null) {
|
||||
return DEFAULT_CHARSET;
|
||||
}
|
||||
return headers.getContentType().getCharSet();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@code Message.Builder} instance for the given class.
|
||||
* <p>This method uses a ConcurrentHashMap for caching method lookups.
|
||||
*/
|
||||
private Message.Builder getMessageBuilder(Class<? extends Message> clazz) throws Exception {
|
||||
Method method = methodCache.get(clazz);
|
||||
if (method == null) {
|
||||
method = clazz.getMethod("newBuilder");
|
||||
methodCache.put(clazz, method);
|
||||
}
|
||||
return (Message.Builder) method.invoke(clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method overrides the parent implementation, since this HttpMessageConverter
|
||||
* can also produce {@code MediaType.HTML "text/html"} ContentType.
|
||||
*/
|
||||
@Override
|
||||
protected boolean canWrite(MediaType mediaType) {
|
||||
return super.canWrite(mediaType) || MediaType.TEXT_HTML.isCompatibleWith(mediaType);
|
||||
return (super.canWrite(mediaType) || MediaType.TEXT_HTML.isCompatibleWith(mediaType));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -163,27 +157,27 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M
|
|||
throws IOException, HttpMessageNotWritableException {
|
||||
|
||||
MediaType contentType = outputMessage.getHeaders().getContentType();
|
||||
Charset charset = getCharset(contentType);
|
||||
if (contentType == null) {
|
||||
contentType = getDefaultContentType(message);
|
||||
}
|
||||
Charset charset = contentType.getCharSet();
|
||||
if (charset == null) {
|
||||
charset = DEFAULT_CHARSET;
|
||||
}
|
||||
|
||||
if (MediaType.TEXT_HTML.isCompatibleWith(contentType)) {
|
||||
final OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputMessage.getBody(), charset);
|
||||
HtmlFormat.print(message, outputStreamWriter);
|
||||
outputStreamWriter.flush();
|
||||
}
|
||||
else if (MediaType.APPLICATION_JSON.isCompatibleWith(contentType)) {
|
||||
final OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputMessage.getBody(), charset);
|
||||
JsonFormat.print(message, outputStreamWriter);
|
||||
outputStreamWriter.flush();
|
||||
}
|
||||
else if (MediaType.TEXT_PLAIN.isCompatibleWith(contentType)) {
|
||||
final OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputMessage.getBody(), charset);
|
||||
if (MediaType.TEXT_PLAIN.isCompatibleWith(contentType)) {
|
||||
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputMessage.getBody(), charset);
|
||||
TextFormat.print(message, outputStreamWriter);
|
||||
outputStreamWriter.flush();
|
||||
}
|
||||
else if (MediaType.APPLICATION_JSON.isCompatibleWith(contentType)) {
|
||||
JSON_FORMAT.print(message, outputMessage.getBody(), charset);
|
||||
}
|
||||
else if (MediaType.APPLICATION_XML.isCompatibleWith(contentType)) {
|
||||
final OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputMessage.getBody(), charset);
|
||||
XmlFormat.print(message, outputStreamWriter);
|
||||
outputStreamWriter.flush();
|
||||
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);
|
||||
|
@ -191,10 +185,6 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M
|
|||
}
|
||||
}
|
||||
|
||||
private Charset getCharset(MediaType contentType) {
|
||||
return contentType.getCharSet() != null ? contentType.getCharSet() : DEFAULT_CHARSET;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the "X-Protobuf-*" HTTP headers when responding with a message of
|
||||
* content type "application/x-protobuf"
|
||||
|
@ -206,9 +196,18 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M
|
|||
response.getHeaders().set(X_PROTOBUF_MESSAGE_HEADER, message.getDescriptorForType().getFullName());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MediaType getDefaultContentType(Message message) {
|
||||
return PROTOBUF;
|
||||
|
||||
/**
|
||||
* Create a new {@code Message.Builder} instance for the given class.
|
||||
* <p>This method uses a ConcurrentHashMap for caching method lookups.
|
||||
*/
|
||||
private static Message.Builder getMessageBuilder(Class<? extends Message> clazz) throws Exception {
|
||||
Method method = methodCache.get(clazz);
|
||||
if (method == null) {
|
||||
method = clazz.getMethod("newBuilder");
|
||||
methodCache.put(clazz, method);
|
||||
}
|
||||
return (Message.Builder) method.invoke(clazz);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
|
@ -31,9 +31,9 @@ import org.springframework.protobuf.SecondMsg;
|
|||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
|
||||
/**
|
||||
* Test suite for {@link ProtobufHttpMessageConverter}
|
||||
* Test suite for {@link ProtobufHttpMessageConverter}.
|
||||
*
|
||||
* @author Alex Antonov
|
||||
*/
|
||||
public class ProtobufHttpMessageConverterTests {
|
||||
|
@ -52,6 +52,7 @@ public class ProtobufHttpMessageConverterTests {
|
|||
this.testMsg = Msg.newBuilder().setFoo("Foo").setBlah(SecondMsg.newBuilder().setBlah(123).build()).build();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void extensionRegistryInitialized() {
|
||||
verify(this.registryInitializer, times(1)).initializeExtensionRegistry(anyObject());
|
||||
|
@ -74,6 +75,7 @@ public class ProtobufHttpMessageConverterTests {
|
|||
assertTrue(this.converter.canRead(Msg.class, MediaType.APPLICATION_JSON));
|
||||
assertTrue(this.converter.canRead(Msg.class, MediaType.APPLICATION_XML));
|
||||
assertTrue(this.converter.canRead(Msg.class, MediaType.TEXT_PLAIN));
|
||||
|
||||
// only supported as an output format
|
||||
assertFalse(this.converter.canRead(Msg.class, MediaType.TEXT_HTML));
|
||||
}
|
||||
|
@ -115,9 +117,11 @@ public class ProtobufHttpMessageConverterTests {
|
|||
Message result = Msg.parseFrom(outputMessage.getBodyAsBytes());
|
||||
assertEquals(this.testMsg, result);
|
||||
|
||||
String messageHeader = outputMessage.getHeaders().getFirst(ProtobufHttpMessageConverter.X_PROTOBUF_MESSAGE_HEADER);
|
||||
String messageHeader =
|
||||
outputMessage.getHeaders().getFirst(ProtobufHttpMessageConverter.X_PROTOBUF_MESSAGE_HEADER);
|
||||
assertEquals("Msg", messageHeader);
|
||||
String schemaHeader = outputMessage.getHeaders().getFirst(ProtobufHttpMessageConverter.X_PROTOBUF_SCHEMA_HEADER);
|
||||
String schemaHeader =
|
||||
outputMessage.getHeaders().getFirst(ProtobufHttpMessageConverter.X_PROTOBUF_SCHEMA_HEADER);
|
||||
assertEquals("sample.proto", schemaHeader);
|
||||
}
|
||||
|
||||
|
@ -133,4 +137,5 @@ public class ProtobufHttpMessageConverterTests {
|
|||
this.converter.write(this.testMsg, contentType, outputMessage);
|
||||
assertEquals(-1, outputMessage.getHeaders().getContentLength());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue