Package io.dapr.utils

Class DefaultContentTypeConverter

java.lang.Object
io.dapr.utils.DefaultContentTypeConverter

public class DefaultContentTypeConverter extends Object
A utility class for converting event to bytes based on content type or given serializer. When an application/json or application/cloudevents+json is given as content type, the object serializer is used to serialize the data into bytes
  • Constructor Details

    • DefaultContentTypeConverter

      public DefaultContentTypeConverter()
  • Method Details

    • convertEventToBytesForHttp

      public static <T> byte[] convertEventToBytesForHttp(T event, String contentType) throws IllegalArgumentException, IOException
      Function to convert a given event to bytes for HTTP calls.
      Type Parameters:
      T - The type of the event
      Parameters:
      event - The input event
      contentType - The content type of the event
      Returns:
      the event as bytes
      Throws:
      IllegalArgumentException - on mismatch between contentType and event types
      IOException - on serialization
    • convertEventToBytesForGrpc

      public static <T> byte[] convertEventToBytesForGrpc(T event, String contentType) throws IllegalArgumentException, IOException
      Function to convert a given event to bytes for gRPC calls.
      Type Parameters:
      T - The type of the event
      Parameters:
      event - The input event
      contentType - The content type of the event
      Returns:
      the event as bytes
      Throws:
      IllegalArgumentException - on mismatch between contentType and event types
      IOException - on serialization
    • convertBytesToEventFromHttp

      public static <T> T convertBytesToEventFromHttp(byte[] event, String contentType, TypeRef<T> typeRef) throws IllegalArgumentException, IOException
      Function to convert a bytes array from HTTP input into event based on given object deserializer.
      Type Parameters:
      T - The type of the event
      Parameters:
      event - The input event
      contentType - The content type of the event
      typeRef - The type to convert the event to
      Returns:
      the event as bytes
      Throws:
      IllegalArgumentException - on mismatch between contentType and event types
      IOException - on serialization
    • convertBytesToEventFromGrpc

      public static <T> T convertBytesToEventFromGrpc(byte[] event, String contentType, TypeRef<T> typeRef) throws IllegalArgumentException, IOException
      Function to convert a bytes array from gRPC input into event based on given object deserializer.
      Type Parameters:
      T - The type of the event
      Parameters:
      event - The input event
      contentType - The content type of the event
      typeRef - The type to convert the event to
      Returns:
      the event as bytes
      Throws:
      IllegalArgumentException - on mismatch between contentType and event types
      IOException - on serialization
    • isCloudEventContentType

      public static boolean isCloudEventContentType(String contentType)
    • isJsonContentType

      public static boolean isJsonContentType(String contentType)
    • isStringContentType

      public static boolean isStringContentType(String contentType)
    • isBinaryContentType

      public static boolean isBinaryContentType(String contentType)