1
1
/*
2
- * Copyright 2002-2016 the original author or authors.
2
+ * Copyright 2002-2017 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
50
50
*
51
51
* <p>Other formats can be supported with additional libraries:
52
52
* <ul>
53
- * <li>{@code "application/json"} with the official library
54
- * {@code "com.google.protobuf:protobuf-java-util"}
55
- * <li>{@code "application/json"}, {@code "application/xml"} and {@code "text/html"} (write only)
56
- * can be supported with the 3rd party library
57
- * {@code "com.googlecode.protobuf-java-format:protobuf-java-format"}
53
+ * <li>{@code "application/json"} with the official library
54
+ * {@code "com.google.protobuf:protobuf-java-util"}
55
+ * <li>{@code "application/json"}, {@code "application/xml"} and {@code "text/html"} (write only)
56
+ * can be supported with the 3rd party library
57
+ * {@code "com.googlecode.protobuf-java-format:protobuf-java-format"}
58
58
* </ul>
59
59
*
60
60
* <p>To generate {@code Message} Java classes, you need to install the {@code protoc} binary.
61
61
*
62
- * <p>Requires Protobuf 2.6 and Protobuf Java Format 1.4, as of Spring 4.3.
63
- * Supports up to Protobuf 3.0.0.
62
+ * <p>Requires Protobuf 2.6 or 3.x and Protobuf Java Format 1.4 or higher, as of Spring 5.0.
64
63
*
65
64
* @author Alex Antonov
66
65
* @author Brian Clozel
@@ -83,14 +82,15 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M
83
82
private static final boolean isProtobufJavaFormatPresent =
84
83
ClassUtils .isPresent ("com.googlecode.protobuf.format.JsonFormat" , ProtobufHttpMessageConverter .class .getClassLoader ());
85
84
86
- private static final MediaType [] SUPPORTED_MEDIATYPES ;
87
-
88
- private final ProtobufFormatsSupport protobufFormatsSupport ;
89
-
90
85
private static final ConcurrentHashMap <Class <?>, Method > methodCache = new ConcurrentHashMap <>();
91
86
87
+ private final ProtobufFormatSupport protobufFormatSupport ;
88
+
92
89
private final ExtensionRegistry extensionRegistry = ExtensionRegistry .newInstance ();
93
90
91
+
92
+ private static final MediaType [] SUPPORTED_MEDIATYPES ;
93
+
94
94
static {
95
95
if (isProtobufJavaFormatPresent ) {
96
96
SUPPORTED_MEDIATYPES = new MediaType [] {PROTOBUF , MediaType .TEXT_PLAIN , MediaType .APPLICATION_XML ,
@@ -104,27 +104,28 @@ else if (isProtobufJavaUtilPresent) {
104
104
}
105
105
}
106
106
107
+
107
108
/**
108
- * Construct a new instance .
109
+ * Construct a new {@code ProtobufHttpMessageConverter} .
109
110
*/
110
111
public ProtobufHttpMessageConverter () {
111
112
this (null );
112
113
}
113
114
114
115
/**
115
- * Construct a new instance with an {@link ExtensionRegistryInitializer}
116
- * that allows the registration of message extensions.
116
+ * Construct a new {@code ProtobufHttpMessageConverter} with an
117
+ * initializer that allows the registration of message extensions.
117
118
*/
118
119
public ProtobufHttpMessageConverter (ExtensionRegistryInitializer registryInitializer ) {
119
120
super (SUPPORTED_MEDIATYPES );
120
121
if (isProtobufJavaFormatPresent ) {
121
- this .protobufFormatsSupport = new ProtobufJavaFormatSupport ();
122
+ this .protobufFormatSupport = new ProtobufJavaFormatSupport ();
122
123
}
123
124
else if (isProtobufJavaUtilPresent ) {
124
- this .protobufFormatsSupport = new ProtobufJavaUtilSupport ();
125
+ this .protobufFormatSupport = new ProtobufJavaUtilSupport ();
125
126
}
126
127
else {
127
- this .protobufFormatsSupport = null ;
128
+ this .protobufFormatSupport = null ;
128
129
}
129
130
if (registryInitializer != null ) {
130
131
registryInitializer .initializeExtensionRegistry (this .extensionRegistry );
@@ -165,7 +166,7 @@ else if (MediaType.TEXT_PLAIN.isCompatibleWith(contentType)) {
165
166
TextFormat .merge (reader , this .extensionRegistry , builder );
166
167
}
167
168
else if (isProtobufJavaUtilPresent || isProtobufJavaFormatPresent ) {
168
- this .protobufFormatsSupport .merge (inputMessage .getBody (), charset , contentType ,
169
+ this .protobufFormatSupport .merge (inputMessage .getBody (), charset , contentType ,
169
170
this .extensionRegistry , builder );
170
171
}
171
172
return builder .build ();
@@ -211,7 +212,7 @@ else if (MediaType.TEXT_PLAIN.isCompatibleWith(contentType)) {
211
212
outputMessage .getBody ().flush ();
212
213
}
213
214
else if (isProtobufJavaUtilPresent || isProtobufJavaFormatPresent ) {
214
- this .protobufFormatsSupport .print (message , outputMessage .getBody (), contentType , charset );
215
+ this .protobufFormatSupport .print (message , outputMessage .getBody (), contentType , charset );
215
216
outputMessage .getBody ().flush ();
216
217
}
217
218
}
@@ -241,15 +242,17 @@ private static Message.Builder getMessageBuilder(Class<? extends Message> clazz)
241
242
return (Message .Builder ) method .invoke (clazz );
242
243
}
243
244
244
- private interface ProtobufFormatsSupport {
245
245
246
- void merge (InputStream input , Charset cs , MediaType contentType , ExtensionRegistry extensionRegistry ,
246
+ private interface ProtobufFormatSupport {
247
+
248
+ void merge (InputStream input , Charset charset , MediaType contentType , ExtensionRegistry extensionRegistry ,
247
249
Message .Builder builder ) throws IOException ;
248
250
249
251
void print (Message message , OutputStream output , MediaType contentType , Charset cs ) throws IOException ;
250
252
}
251
253
252
- private class ProtobufJavaUtilSupport implements ProtobufFormatsSupport {
254
+
255
+ private class ProtobufJavaUtilSupport implements ProtobufFormatSupport {
253
256
254
257
private final com .google .protobuf .util .JsonFormat .Parser parser ;
255
258
@@ -261,16 +264,16 @@ public ProtobufJavaUtilSupport() {
261
264
}
262
265
263
266
@ Override
264
- public void merge (InputStream input , Charset cs , MediaType contentType ,
267
+ public void merge (InputStream input , Charset charset , MediaType contentType ,
265
268
ExtensionRegistry extensionRegistry , Message .Builder builder ) throws IOException {
266
269
267
270
if (contentType .isCompatibleWith (MediaType .APPLICATION_JSON )) {
268
- InputStreamReader reader = new InputStreamReader (input , cs );
271
+ InputStreamReader reader = new InputStreamReader (input , charset );
269
272
this .parser .merge (reader , builder );
270
273
}
271
274
else {
272
- throw new UnsupportedOperationException ( "com.googlecode.protobuf:protobuf-java-util does not support "
273
- + contentType . toString () + " format" );
275
+ throw new IOException (
276
+ "com.googlecode.protobuf:protobuf-java-util does not support " + contentType + " format" );
274
277
}
275
278
}
276
279
@@ -280,14 +283,14 @@ public void print(Message message, OutputStream output, MediaType contentType, C
280
283
this .printer .appendTo (message , new OutputStreamWriter (output , cs ));
281
284
}
282
285
else {
283
- throw new UnsupportedOperationException ( "com.googlecode.protobuf:protobuf-java-util does not support "
284
- + contentType . toString () + " format" );
286
+ throw new IOException (
287
+ "com.googlecode.protobuf:protobuf-java-util does not support " + contentType + " format" );
285
288
}
286
289
}
287
-
288
290
}
289
291
290
- private class ProtobufJavaFormatSupport implements ProtobufFormatsSupport {
292
+
293
+ private class ProtobufJavaFormatSupport implements ProtobufFormatSupport {
291
294
292
295
private final FormatFactory FORMAT_FACTORY ;
293
296
@@ -305,17 +308,17 @@ public ProtobufJavaFormatSupport() {
305
308
}
306
309
307
310
@ Override
308
- public void merge (InputStream input , Charset cs , MediaType contentType ,
311
+ public void merge (InputStream input , Charset charset , MediaType contentType ,
309
312
ExtensionRegistry extensionRegistry , Message .Builder builder ) throws IOException {
313
+
310
314
if (contentType .isCompatibleWith (MediaType .APPLICATION_JSON )) {
311
- JSON_FORMATTER .merge (input , cs , extensionRegistry , builder );
315
+ JSON_FORMATTER .merge (input , charset , extensionRegistry , builder );
312
316
}
313
317
else if (contentType .isCompatibleWith (MediaType .APPLICATION_XML )) {
314
- XML_FORMATTER .merge (input , cs , extensionRegistry , builder );
318
+ XML_FORMATTER .merge (input , charset , extensionRegistry , builder );
315
319
}
316
320
else {
317
- throw new UnsupportedOperationException ("com.google.protobuf.util does not support "
318
- + contentType .toString () + " format" );
321
+ throw new IOException ("com.google.protobuf.util does not support " + contentType + " format" );
319
322
}
320
323
}
321
324
@@ -331,8 +334,7 @@ else if (contentType.isCompatibleWith(MediaType.TEXT_HTML)) {
331
334
HTML_FORMATTER .print (message , output , cs );
332
335
}
333
336
else {
334
- throw new UnsupportedOperationException ("com.google.protobuf.util does not support "
335
- + contentType .toString () + " format" );
337
+ throw new IOException ("com.google.protobuf.util does not support " + contentType + " format" );
336
338
}
337
339
}
338
340
}
0 commit comments