001/*
002 * Copyright 2002-2020 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.http.converter.protobuf;
018
019import com.google.protobuf.ExtensionRegistry;
020import com.google.protobuf.util.JsonFormat;
021
022import org.springframework.lang.Nullable;
023
024/**
025 * Subclass of {@link ProtobufHttpMessageConverter} which enforces the use of Protobuf 3 and
026 * its official library {@code "com.google.protobuf:protobuf-java-util"} for JSON processing.
027 *
028 * <p>Most importantly, this class allows for custom JSON parser and printer configurations
029 * through the {@link JsonFormat} utility. If no special parser or printer configuration is
030 * given, default variants will be used instead.
031 *
032 * <p>Requires Protobuf 3.x and {@code "com.google.protobuf:protobuf-java-util"} 3.x,
033 * with 3.3 or higher recommended.
034 *
035 * @author Juergen Hoeller
036 * @author Sebastien Deleuze
037 * @since 5.0
038 * @see JsonFormat#parser()
039 * @see JsonFormat#printer()
040 * @see #ProtobufJsonFormatHttpMessageConverter(com.google.protobuf.util.JsonFormat.Parser, com.google.protobuf.util.JsonFormat.Printer)
041 */
042public class ProtobufJsonFormatHttpMessageConverter extends ProtobufHttpMessageConverter {
043
044        /**
045         * Constructor with default instances of
046         * {@link com.google.protobuf.util.JsonFormat.Parser JsonFormat.Parser},
047         * {@link com.google.protobuf.util.JsonFormat.Printer JsonFormat.Printer},
048         * and {@link ExtensionRegistry}.
049         */
050        public ProtobufJsonFormatHttpMessageConverter() {
051                this(null, null);
052        }
053
054        /**
055         * Constructor with given instances of
056         * {@link com.google.protobuf.util.JsonFormat.Parser JsonFormat.Parser},
057         * {@link com.google.protobuf.util.JsonFormat.Printer JsonFormat.Printer},
058         * and a default instance of {@link ExtensionRegistry}.
059         */
060        public ProtobufJsonFormatHttpMessageConverter(
061                        @Nullable JsonFormat.Parser parser, @Nullable JsonFormat.Printer printer) {
062
063                this(parser, printer, (ExtensionRegistry) null);
064        }
065
066        /**
067         * Constructor with given instances of
068         * {@link com.google.protobuf.util.JsonFormat.Parser JsonFormat.Parser},
069         * {@link com.google.protobuf.util.JsonFormat.Printer JsonFormat.Printer},
070         * and {@link ExtensionRegistry}.
071         */
072        public ProtobufJsonFormatHttpMessageConverter(@Nullable JsonFormat.Parser parser,
073                        @Nullable JsonFormat.Printer printer, @Nullable ExtensionRegistry extensionRegistry) {
074
075                super(new ProtobufJavaUtilSupport(parser, printer), extensionRegistry);
076        }
077
078        /**
079         * Construct a new {@code ProtobufJsonFormatHttpMessageConverter} with the given
080         * {@code JsonFormat.Parser} and {@code JsonFormat.Printer} configuration, also
081         * accepting an initializer that allows the registration of message extensions.
082         * @param parser the JSON parser configuration
083         * @param printer the JSON printer configuration
084         * @param registryInitializer an initializer for message extensions
085         * @deprecated as of 5.1, in favor of
086         * {@link #ProtobufJsonFormatHttpMessageConverter(com.google.protobuf.util.JsonFormat.Parser, com.google.protobuf.util.JsonFormat.Printer, ExtensionRegistry)}
087         */
088        @Deprecated
089        public ProtobufJsonFormatHttpMessageConverter(@Nullable JsonFormat.Parser parser,
090                        @Nullable JsonFormat.Printer printer, @Nullable ExtensionRegistryInitializer registryInitializer) {
091
092                super(new ProtobufJavaUtilSupport(parser, printer), null);
093                if (registryInitializer != null) {
094                        registryInitializer.initializeExtensionRegistry(this.extensionRegistry);
095                }
096        }
097
098}