Package io.micronaut.protobuf.codec
Class ProtobufferCodec
java.lang.Object
io.micronaut.protobuf.codec.ProtobufferCodec
- All Implemented Interfaces:
io.micronaut.http.codec.MediaTypeCodec
@Singleton
@Named("protobuf")
public class ProtobufferCodec
extends Object
implements io.micronaut.http.codec.MediaTypeCodec
Protocol buffers codec.
- Author:
- graemerocher, luistrigueiros
-
Field Summary
Modifier and TypeFieldDescriptionstatic final List<io.micronaut.http.MediaType>
List of default protobuf media types.static final String
Protobuffer encoded data: application/x-protobuf.static final io.micronaut.http.MediaType
Protobuffer encoded data: application/x-protobuf.static final io.micronaut.http.MediaType
Protobuffer encoded data: application/protobuf.static final String
Protobuffer encoded data: application/protobuf.static final String
This Header is to say the fully qualified name of the message builder to use. -
Constructor Summary
ConstructorDescriptionProtobufferCodec
(com.google.protobuf.ExtensionRegistry extensionRegistry) Default constructor. -
Method Summary
Modifier and TypeMethodDescription<T> T
decode
(io.micronaut.core.type.Argument<T> type, byte[] bytes) <T> T
decode
(io.micronaut.core.type.Argument<T> type, io.micronaut.core.io.buffer.ByteBuffer<?> buffer) <T> T
decode
(io.micronaut.core.type.Argument<T> type, InputStream inputStream) <T> byte[]
encode
(T object) <T,
B> io.micronaut.core.io.buffer.ByteBuffer<B> encode
(T object, io.micronaut.core.io.buffer.ByteBufferFactory<?, B> allocator) <T> void
encode
(T object, OutputStream outputStream) com.google.protobuf.ExtensionRegistry
Collection<io.micronaut.http.MediaType>
Optional<com.google.protobuf.Message.Builder>
getMessageBuilder
(Class<? extends com.google.protobuf.Message> clazz) Create a newMessage.Builder
instance for the given class.void
setMediaTypes
(List<io.micronaut.http.MediaType> mediaTypes) Method to customize media types for this codec.boolean
supportsType
(Class<?> type) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.micronaut.http.codec.MediaTypeCodec
decode, decode, decode, decode, decode, encode, encode, encode
-
Field Details
-
X_PROTOBUF_MESSAGE_HEADER
This Header is to say the fully qualified name of the message builder to use. This is needed when the request is untyped- See Also:
-
PROTOBUFFER_ENCODED
Protobuffer encoded data: application/x-protobuf.- See Also:
-
PROTOBUFFER_ENCODED2
Protobuffer encoded data: application/protobuf.- See Also:
-
PROTOBUFFER_ENCODED_TYPE
public static final io.micronaut.http.MediaType PROTOBUFFER_ENCODED_TYPEProtobuffer encoded data: application/x-protobuf. -
PROTOBUFFER_ENCODED_TYPE2
public static final io.micronaut.http.MediaType PROTOBUFFER_ENCODED_TYPE2Protobuffer encoded data: application/protobuf. -
DEFAULT_MEDIA_TYPES
List of default protobuf media types.
-
-
Constructor Details
-
ProtobufferCodec
public ProtobufferCodec(com.google.protobuf.ExtensionRegistry extensionRegistry) Default constructor.- Parameters:
extensionRegistry
- The extension registry
-
-
Method Details
-
supportsType
- Specified by:
supportsType
in interfaceio.micronaut.http.codec.MediaTypeCodec
-
getMediaTypes
- Specified by:
getMediaTypes
in interfaceio.micronaut.http.codec.MediaTypeCodec
-
setMediaTypes
Method to customize media types for this codec.- Parameters:
mediaTypes
- media types for which need use this codec.
-
decode
public <T> T decode(io.micronaut.core.type.Argument<T> type, InputStream inputStream) throws io.micronaut.http.codec.CodecException - Specified by:
decode
in interfaceio.micronaut.http.codec.MediaTypeCodec
- Throws:
io.micronaut.http.codec.CodecException
-
decode
public <T> T decode(io.micronaut.core.type.Argument<T> type, io.micronaut.core.io.buffer.ByteBuffer<?> buffer) throws io.micronaut.http.codec.CodecException - Specified by:
decode
in interfaceio.micronaut.http.codec.MediaTypeCodec
- Throws:
io.micronaut.http.codec.CodecException
-
decode
public <T> T decode(io.micronaut.core.type.Argument<T> type, byte[] bytes) throws io.micronaut.http.codec.CodecException - Specified by:
decode
in interfaceio.micronaut.http.codec.MediaTypeCodec
- Throws:
io.micronaut.http.codec.CodecException
-
encode
public <T> void encode(T object, OutputStream outputStream) throws io.micronaut.http.codec.CodecException - Specified by:
encode
in interfaceio.micronaut.http.codec.MediaTypeCodec
- Throws:
io.micronaut.http.codec.CodecException
-
encode
public <T> byte[] encode(T object) throws io.micronaut.http.codec.CodecException - Specified by:
encode
in interfaceio.micronaut.http.codec.MediaTypeCodec
- Throws:
io.micronaut.http.codec.CodecException
-
encode
public <T,B> io.micronaut.core.io.buffer.ByteBuffer<B> encode(T object, io.micronaut.core.io.buffer.ByteBufferFactory<?, B> allocator) throws io.micronaut.http.codec.CodecException- Specified by:
encode
in interfaceio.micronaut.http.codec.MediaTypeCodec
- Throws:
io.micronaut.http.codec.CodecException
-
getExtensionRegistry
public com.google.protobuf.ExtensionRegistry getExtensionRegistry()- Returns:
- The extension registry
-
getMessageBuilder
public Optional<com.google.protobuf.Message.Builder> getMessageBuilder(Class<? extends com.google.protobuf.Message> clazz) Create a newMessage.Builder
instance for the given class.This method uses a ConcurrentHashMap for caching method lookups.
- Parameters:
clazz
- The class.- Returns:
- The message builder
-