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

    Fields
    Modifier and Type
    Field
    Description
    static 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

    Constructors
    Constructor
    Description
    ProtobufferCodec(com.google.protobuf.ExtensionRegistry extensionRegistry)
    Default constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    <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 new Message.Builder instance for the given class.
    void
    setMediaTypes(List<io.micronaut.http.MediaType> mediaTypes)
    Method to customize media types for this codec.
    boolean
     

    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

      public static final String 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

      public static final String PROTOBUFFER_ENCODED
      Protobuffer encoded data: application/x-protobuf.
      See Also:
    • PROTOBUFFER_ENCODED2

      public static final String PROTOBUFFER_ENCODED2
      Protobuffer encoded data: application/protobuf.
      See Also:
    • PROTOBUFFER_ENCODED_TYPE

      public static final io.micronaut.http.MediaType PROTOBUFFER_ENCODED_TYPE
      Protobuffer encoded data: application/x-protobuf.
    • PROTOBUFFER_ENCODED_TYPE2

      public static final io.micronaut.http.MediaType PROTOBUFFER_ENCODED_TYPE2
      Protobuffer encoded data: application/protobuf.
    • DEFAULT_MEDIA_TYPES

      public static final List<io.micronaut.http.MediaType> 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

      public boolean supportsType(Class<?> type)
      Specified by:
      supportsType in interface io.micronaut.http.codec.MediaTypeCodec
    • getMediaTypes

      public Collection<io.micronaut.http.MediaType> getMediaTypes()
      Specified by:
      getMediaTypes in interface io.micronaut.http.codec.MediaTypeCodec
    • setMediaTypes

      public void setMediaTypes(List<io.micronaut.http.MediaType> mediaTypes)
      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 interface io.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 interface io.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 interface io.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 interface io.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 interface io.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 interface io.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 new Message.Builder instance for the given class.

      This method uses a ConcurrentHashMap for caching method lookups.

      Parameters:
      clazz - The class.
      Returns:
      The message builder