1
1
/*
2
- * Copyright 2002-2022 the original author or authors.
2
+ * Copyright 2002-2023 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.
32
32
import com .google .protobuf .Message ;
33
33
import com .google .protobuf .TextFormat ;
34
34
import com .google .protobuf .util .JsonFormat ;
35
- import com .googlecode .protobuf .format .FormatFactory ;
36
- import com .googlecode .protobuf .format .ProtobufFormatter ;
37
35
38
36
import org .springframework .http .HttpInputMessage ;
39
37
import org .springframework .http .HttpOutputMessage ;
48
46
import org .springframework .util .ConcurrentReferenceHashMap ;
49
47
50
48
import static org .springframework .http .MediaType .APPLICATION_JSON ;
51
- import static org .springframework .http .MediaType .APPLICATION_XML ;
52
- import static org .springframework .http .MediaType .TEXT_HTML ;
53
49
import static org .springframework .http .MediaType .TEXT_PLAIN ;
54
50
55
51
/**
60
56
* <p>To generate {@code Message} Java classes, you need to install the {@code protoc} binary.
61
57
*
62
58
* <p>This converter supports by default {@code "application/x-protobuf"} and {@code "text/plain"}
63
- * with the official {@code "com.google.protobuf:protobuf-java"} library. Other formats can be
64
- * supported with one of the following additional libraries on the classpath:
65
- * <ul>
66
- * <li>{@code "application/json"}, {@code "application/xml"}, and {@code "text/html"} (write-only)
67
- * with the {@code "com.googlecode.protobuf-java-format:protobuf-java-format"} third-party library
68
- * <li>{@code "application/json"} with the official {@code "com.google.protobuf:protobuf-java-util"}
69
- * for Protobuf 3 (see {@link ProtobufJsonFormatHttpMessageConverter} for a configurable variant)
70
- * </ul>
59
+ * with the official {@code "com.google.protobuf:protobuf-java"} library.
60
+ * The {@code "application/json"} format is also supported with the {@code "com.google.protobuf:protobuf-java-util"}
61
+ * dependency. See {@link ProtobufJsonFormatHttpMessageConverter} for a configurable variant.
71
62
*
72
- * <p>Requires Protobuf 2.6 or higher (and Protobuf Java Format 1.4 or higher for formatting).
73
- * This converter will auto-adapt to Protobuf 3 and its default {@code protobuf-java-util} JSON
74
- * format if the Protobuf 2 based {@code protobuf-java-format} isn't present; however, for more
75
- * explicit JSON setup on Protobuf 3, consider {@link ProtobufJsonFormatHttpMessageConverter}.
63
+ * <p>This converter requires Protobuf 3 or higher as of Spring Framework 6.1.
76
64
*
77
65
* @author Alex Antonov
78
66
* @author Brian Clozel
79
67
* @author Juergen Hoeller
80
68
* @author Sebastien Deleuze
81
69
* @since 4.1
82
- * @see FormatFactory
83
70
* @see JsonFormat
84
71
* @see ProtobufJsonFormatHttpMessageConverter
85
72
*/
@@ -105,18 +92,10 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M
105
92
*/
106
93
public static final String X_PROTOBUF_MESSAGE_HEADER = "X-Protobuf-Message" ;
107
94
108
- private static final boolean protobufFormatFactoryPresent ;
109
-
110
- private static final boolean protobufJsonFormatPresent ;
95
+ private static final boolean protobufJsonFormatPresent = ClassUtils .isPresent ("com.google.protobuf.util.JsonFormat" , ProtobufHttpMessageConverter .class .getClassLoader ());
111
96
112
97
private static final Map <Class <?>, Method > methodCache = new ConcurrentReferenceHashMap <>();
113
98
114
- static {
115
- ClassLoader classLoader = ProtobufHttpMessageConverter .class .getClassLoader ();
116
- protobufFormatFactoryPresent = ClassUtils .isPresent ("com.googlecode.protobuf.format.FormatFactory" , classLoader );
117
- protobufJsonFormatPresent = ClassUtils .isPresent ("com.google.protobuf.util.JsonFormat" , classLoader );
118
- }
119
-
120
99
121
100
final ExtensionRegistry extensionRegistry ;
122
101
@@ -146,9 +125,6 @@ public ProtobufHttpMessageConverter(ExtensionRegistry extensionRegistry) {
146
125
if (formatSupport != null ) {
147
126
this .protobufFormatSupport = formatSupport ;
148
127
}
149
- else if (protobufFormatFactoryPresent ) {
150
- this .protobufFormatSupport = new ProtobufJavaFormatSupport ();
151
- }
152
128
else if (protobufJsonFormatPresent ) {
153
129
this .protobufFormatSupport = new ProtobufJavaUtilSupport (null , null );
154
130
}
@@ -290,73 +266,6 @@ void print(Message message, OutputStream output, MediaType contentType, Charset
290
266
}
291
267
292
268
293
- /**
294
- * {@link ProtobufFormatSupport} implementation used when
295
- * {@code com.googlecode.protobuf.format.FormatFactory} is available.
296
- */
297
- static class ProtobufJavaFormatSupport implements ProtobufFormatSupport {
298
-
299
- private final ProtobufFormatter jsonFormatter ;
300
-
301
- private final ProtobufFormatter xmlFormatter ;
302
-
303
- private final ProtobufFormatter htmlFormatter ;
304
-
305
- public ProtobufJavaFormatSupport () {
306
- FormatFactory formatFactory = new FormatFactory ();
307
- this .jsonFormatter = formatFactory .createFormatter (FormatFactory .Formatter .JSON );
308
- this .xmlFormatter = formatFactory .createFormatter (FormatFactory .Formatter .XML );
309
- this .htmlFormatter = formatFactory .createFormatter (FormatFactory .Formatter .HTML );
310
- }
311
-
312
- @ Override
313
- public MediaType [] supportedMediaTypes () {
314
- return new MediaType [] {PROTOBUF , TEXT_PLAIN , APPLICATION_XML , APPLICATION_JSON };
315
- }
316
-
317
- @ Override
318
- public boolean supportsWriteOnly (@ Nullable MediaType mediaType ) {
319
- return TEXT_HTML .isCompatibleWith (mediaType );
320
- }
321
-
322
- @ Override
323
- public void merge (InputStream input , Charset charset , MediaType contentType ,
324
- ExtensionRegistry extensionRegistry , Message .Builder builder )
325
- throws IOException , HttpMessageConversionException {
326
-
327
- if (contentType .isCompatibleWith (APPLICATION_JSON )) {
328
- this .jsonFormatter .merge (input , charset , extensionRegistry , builder );
329
- }
330
- else if (contentType .isCompatibleWith (APPLICATION_XML )) {
331
- this .xmlFormatter .merge (input , charset , extensionRegistry , builder );
332
- }
333
- else {
334
- throw new HttpMessageConversionException (
335
- "protobuf-java-format does not support parsing " + contentType );
336
- }
337
- }
338
-
339
- @ Override
340
- public void print (Message message , OutputStream output , MediaType contentType , Charset charset )
341
- throws IOException , HttpMessageConversionException {
342
-
343
- if (contentType .isCompatibleWith (APPLICATION_JSON )) {
344
- this .jsonFormatter .print (message , output , charset );
345
- }
346
- else if (contentType .isCompatibleWith (APPLICATION_XML )) {
347
- this .xmlFormatter .print (message , output , charset );
348
- }
349
- else if (contentType .isCompatibleWith (TEXT_HTML )) {
350
- this .htmlFormatter .print (message , output , charset );
351
- }
352
- else {
353
- throw new HttpMessageConversionException (
354
- "protobuf-java-format does not support printing " + contentType );
355
- }
356
- }
357
- }
358
-
359
-
360
269
/**
361
270
* {@link ProtobufFormatSupport} implementation used when
362
271
* {@code com.google.protobuf.util.JsonFormat} is available.
0 commit comments