|
| 1 | +/* |
| 2 | + * Copyright 2002-2014 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 | + * http://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 | + |
| 17 | +package org.springframework.http.converter.protobuf; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.InputStreamReader; |
| 21 | +import java.io.OutputStreamWriter; |
| 22 | +import java.io.StringWriter; |
| 23 | +import java.io.UnsupportedEncodingException; |
| 24 | +import java.lang.reflect.Method; |
| 25 | +import java.nio.charset.Charset; |
| 26 | +import java.util.concurrent.ConcurrentHashMap; |
| 27 | + |
| 28 | +import com.google.protobuf.ExtensionRegistry; |
| 29 | +import com.google.protobuf.Message; |
| 30 | +import com.google.protobuf.TextFormat; |
| 31 | +import com.googlecode.protobuf.format.HtmlFormat; |
| 32 | +import com.googlecode.protobuf.format.JsonFormat; |
| 33 | +import com.googlecode.protobuf.format.XmlFormat; |
| 34 | + |
| 35 | +import org.springframework.http.HttpHeaders; |
| 36 | +import org.springframework.http.HttpInputMessage; |
| 37 | +import org.springframework.http.HttpOutputMessage; |
| 38 | +import org.springframework.http.MediaType; |
| 39 | +import org.springframework.http.converter.AbstractHttpMessageConverter; |
| 40 | +import org.springframework.http.converter.HttpMessageNotReadableException; |
| 41 | +import org.springframework.http.converter.HttpMessageNotWritableException; |
| 42 | +import org.springframework.util.Assert; |
| 43 | +import org.springframework.util.FileCopyUtils; |
| 44 | + |
| 45 | + |
| 46 | +/** |
| 47 | + * Implementation of {@link org.springframework.http.converter.HttpMessageConverter} |
| 48 | + * that can read and write Protobuf {@code Message}s using the |
| 49 | + * <a href="https://developers.google.com/protocol-buffers/">Google Protocol buffers</a> library. |
| 50 | + * |
| 51 | + * <p>By default, it supports {@code application/json}, {@code application/xml}, {@code text/plain} |
| 52 | + * and {@code application/x-protobuf}. {@code text/html} is only supported when writing messages. |
| 53 | + * |
| 54 | + * <p>In order to generate Message Java classes, you should install the protoc binary on your system. |
| 55 | + * <p>Tested against Protobuf version 2.5.0. |
| 56 | + * |
| 57 | + * @author Alex Antonov |
| 58 | + * @author Brian Clozel |
| 59 | + * @since 4.1 |
| 60 | + */ |
| 61 | +public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<Message> { |
| 62 | + |
| 63 | + public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); |
| 64 | + |
| 65 | + public static final MediaType PROTOBUF = new MediaType("application", "x-protobuf", DEFAULT_CHARSET); |
| 66 | + |
| 67 | + public static final String X_PROTOBUF_SCHEMA_HEADER = "X-Protobuf-Schema"; |
| 68 | + public static final String X_PROTOBUF_MESSAGE_HEADER = "X-Protobuf-Message"; |
| 69 | + |
| 70 | + private static final ConcurrentHashMap<Class<?>, Method> newBuilderMethodCache = |
| 71 | + new ConcurrentHashMap<Class<?>, Method>(); |
| 72 | + |
| 73 | + private ExtensionRegistry extensionRegistry = ExtensionRegistry.newInstance(); |
| 74 | + |
| 75 | + /** |
| 76 | + * Construct a new {@code ProtobufHttpMessageConverter}. |
| 77 | + */ |
| 78 | + public ProtobufHttpMessageConverter() { |
| 79 | + this(null); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Construct a new {@code ProtobufHttpMessageConverter} with a {@link ExtensionRegistryInitializer}, |
| 84 | + * allowing to register message extensions. |
| 85 | + */ |
| 86 | + public ProtobufHttpMessageConverter(ExtensionRegistryInitializer registryInitializer) { |
| 87 | + |
| 88 | + //TODO: should we handle "*+json", "*+xml" as well? |
| 89 | + super(PROTOBUF, MediaType.TEXT_PLAIN, |
| 90 | + MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON); |
| 91 | + Assert.notNull(registryInitializer, "ExtensionRegistryInitializer must not be null"); |
| 92 | + registryInitializer.initializeExtensionRegistry(extensionRegistry); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + protected boolean supports(Class<?> clazz) { |
| 97 | + return Message.class.isAssignableFrom(clazz); |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + @Override |
| 102 | + protected Message readInternal(Class<? extends Message> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { |
| 103 | + MediaType contentType = inputMessage.getHeaders().getContentType(); |
| 104 | + contentType = contentType != null ? contentType : PROTOBUF; |
| 105 | + |
| 106 | + InputStreamReader reader = new InputStreamReader(inputMessage.getBody(), getCharset(inputMessage.getHeaders())); |
| 107 | + |
| 108 | + try { |
| 109 | + Message.Builder builder = createMessageBuilder(clazz); |
| 110 | + |
| 111 | + if (MediaType.APPLICATION_JSON.isCompatibleWith(contentType)) { |
| 112 | + JsonFormat.merge(reader, extensionRegistry, builder); |
| 113 | + } |
| 114 | + else if (MediaType.TEXT_PLAIN.isCompatibleWith(contentType)) { |
| 115 | + TextFormat.merge(reader, extensionRegistry, builder); |
| 116 | + } |
| 117 | + else if (MediaType.APPLICATION_XML.isCompatibleWith(contentType)) { |
| 118 | + XmlFormat.merge(reader, extensionRegistry, builder); |
| 119 | + } |
| 120 | + else { // ProtobufHttpMessageConverter.PROTOBUF |
| 121 | + builder.mergeFrom(inputMessage.getBody(), extensionRegistry); |
| 122 | + } |
| 123 | + return builder.build(); |
| 124 | + } |
| 125 | + catch (Exception e) { |
| 126 | + throw new HttpMessageNotReadableException("Could not read Protobuf message: " + e.getMessage(), e); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private Charset getCharset(HttpHeaders headers) { |
| 131 | + if (headers == null || headers.getContentType() == null || headers.getContentType().getCharSet() == null) { |
| 132 | + return DEFAULT_CHARSET; |
| 133 | + } |
| 134 | + return headers.getContentType().getCharSet(); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Create a new {@code Message.Builder} instance for the given class. |
| 139 | + * <p>This method uses a ConcurrentHashMap for caching method lookups. |
| 140 | + */ |
| 141 | + private Message.Builder createMessageBuilder(Class<? extends Message> clazz) throws Exception { |
| 142 | + Method m = newBuilderMethodCache.get(clazz); |
| 143 | + if (m == null) { |
| 144 | + m = clazz.getMethod("newBuilder"); |
| 145 | + newBuilderMethodCache.put(clazz, m); |
| 146 | + } |
| 147 | + return (Message.Builder) m.invoke(clazz); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * This method overrides the parent implementation, since this HttpMessageConverter |
| 152 | + * can also produce {@code MediaType.HTML "text/html"} ContentType. |
| 153 | + */ |
| 154 | + @Override |
| 155 | + protected boolean canWrite(MediaType mediaType) { |
| 156 | + return super.canWrite(mediaType) || MediaType.TEXT_HTML.isCompatibleWith(mediaType); |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + protected void writeInternal(Message message, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { |
| 161 | + MediaType contentType = outputMessage.getHeaders().getContentType(); |
| 162 | + Charset charset = getCharset(contentType); |
| 163 | + OutputStreamWriter writer = new OutputStreamWriter(outputMessage.getBody(), charset); |
| 164 | + |
| 165 | + if (MediaType.TEXT_HTML.isCompatibleWith(contentType)) { |
| 166 | + HtmlFormat.print(message, writer); |
| 167 | + } |
| 168 | + else if (MediaType.APPLICATION_JSON.isCompatibleWith(contentType)) { |
| 169 | + JsonFormat.print(message, writer); |
| 170 | + } |
| 171 | + else if (MediaType.TEXT_PLAIN.isCompatibleWith(contentType)) { |
| 172 | + TextFormat.print(message, writer); |
| 173 | + } |
| 174 | + else if (MediaType.APPLICATION_XML.isCompatibleWith(contentType)) { |
| 175 | + XmlFormat.print(message, writer); |
| 176 | + } |
| 177 | + else if (PROTOBUF.isCompatibleWith(contentType)) { |
| 178 | + setProtoHeader(outputMessage, message); |
| 179 | + FileCopyUtils.copy(message.toByteArray(), outputMessage.getBody()); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + private Charset getCharset(MediaType contentType) { |
| 184 | + return contentType.getCharSet() != null ? contentType.getCharSet() : DEFAULT_CHARSET; |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Set the "X-Protobuf-*" HTTP headers when responding with |
| 189 | + * a message of ContentType "application/x-protobuf" |
| 190 | + */ |
| 191 | + private void setProtoHeader(final HttpOutputMessage response, final Message message) { |
| 192 | + response.getHeaders().set(X_PROTOBUF_SCHEMA_HEADER, |
| 193 | + message.getDescriptorForType().getFile().getName()); |
| 194 | + |
| 195 | + response.getHeaders().set(X_PROTOBUF_MESSAGE_HEADER, |
| 196 | + message.getDescriptorForType().getFullName()); |
| 197 | + } |
| 198 | + |
| 199 | + @Override |
| 200 | + protected MediaType getDefaultContentType(Message message) { |
| 201 | + return PROTOBUF; |
| 202 | + } |
| 203 | + |
| 204 | +} |
0 commit comments