Class CodecDataConverter

  • All Implemented Interfaces:
    DataConverter, PayloadCodec

    public class CodecDataConverter
    extends java.lang.Object
    implements DataConverter, PayloadCodec
    A delegating DataConverter implementation that wraps and chains both another DataConverter and several PayloadCodecs.

    The underlying DataConverter is expected to be responsible for conversion between user objects and bytes represented as Payloads, while the underlying chain of codecs is responsible for a subsequent byte <-> byte manipulation such as encryption or compression

    • Constructor Summary

      Constructors 
      Constructor Description
      CodecDataConverter​(DataConverter dataConverter, java.util.Collection<PayloadCodec> codecs)
      When serializing to Payloads: dataConverter is applied first, following by the chain of codecs.
      CodecDataConverter​(DataConverter dataConverter, java.util.Collection<PayloadCodec> codecs, boolean encodeFailureAttributes)
      When serializing to Payloads: dataConverter is applied first, following by the chain of codecs.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.List<io.temporal.api.common.v1.Payload> decode​(java.util.List<io.temporal.api.common.v1.Payload> payloads)  
      java.util.List<io.temporal.api.common.v1.Payload> encode​(java.util.List<io.temporal.api.common.v1.Payload> payloads)  
      io.temporal.api.failure.v1.Failure exceptionToFailure​(java.lang.Throwable throwable)
      Serialize an existing Throwable object into a Failure object.
      TemporalFailure failureToException​(io.temporal.api.failure.v1.Failure failure)
      Instantiate an appropriate Java Exception from a serialized Failure object.
      <T> T fromPayload​(io.temporal.api.common.v1.Payload payload, java.lang.Class<T> valueClass, java.lang.reflect.Type valueType)  
      <T> T fromPayloads​(int index, java.util.Optional<io.temporal.api.common.v1.Payloads> content, java.lang.Class<T> valueType, java.lang.reflect.Type valueGenericType)
      Implements conversion of a single Payload from the serialized Payloads.
      java.lang.Object[] fromPayloads​(java.util.Optional<io.temporal.api.common.v1.Payloads> content, java.lang.Class<?>[] parameterTypes, java.lang.reflect.Type[] genericParameterTypes)
      Implements conversion of the whole content Payloads into an array of values of different types.
      <T> java.util.Optional<io.temporal.api.common.v1.Payload> toPayload​(T value)  
      java.util.Optional<io.temporal.api.common.v1.Payloads> toPayloads​(java.lang.Object... values)
      Implements conversion of a list of values.
      CodecDataConverter withContext​(SerializationContext context)
      A correct implementation of this interface should have a fully functional "contextless" implementation.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CodecDataConverter

        public CodecDataConverter​(DataConverter dataConverter,
                                  java.util.Collection<PayloadCodec> codecs)
        When serializing to Payloads:
        • dataConverter is applied first, following by the chain of codecs.
        • codecs are applied last to first meaning the earlier encoders wrap the later ones
        When deserializing from Payloads:
        • codecs are applied first to last to reverse the effect following by the dataConverter
        • dataConverter is applied last
        See CodecDataConverter(DataConverter, Collection, boolean) to enable encryption of Failure attributes.
        Parameters:
        dataConverter - to delegate data conversion to
        codecs - to delegate bytes encoding/decoding to. When encoding, the codecs are applied last to first meaning the earlier encoders wrap the later ones. When decoding, the decoders are applied first to last to reverse the effect
      • CodecDataConverter

        public CodecDataConverter​(DataConverter dataConverter,
                                  java.util.Collection<PayloadCodec> codecs,
                                  boolean encodeFailureAttributes)
        When serializing to Payloads:
        • dataConverter is applied first, following by the chain of codecs.
        • codecs are applied last to first meaning the earlier encoders wrap the later ones
        When deserializing from Payloads:
        • codecs are applied first to last to reverse the effect following by the dataConverter
        • dataConverter is applied last
        Setting encodeFailureAttributes to true enables codec encoding of Failure attributes. This can be used in conjunction with an encrypting codec to enable encryption of failures message and stack traces. Note that failure's details are always codec-encoded, without regard to encodeFailureAttributes.
        Parameters:
        dataConverter - to delegate data conversion to
        codecs - to delegate bytes encoding/decoding to. When encoding, the codecs are applied last to first meaning the earlier encoders wrap the later ones. When decoding, the decoders are applied first to last to reverse the effect
        encodeFailureAttributes - enable encoding of Failure attributes (message and stack trace)
    • Method Detail

      • toPayload

        public <T> java.util.Optional<io.temporal.api.common.v1.Payload> toPayload​(T value)
        Specified by:
        toPayload in interface DataConverter
        Parameters:
        value - value to convert
        Returns:
        a Payload which is a protobuf message containing byte-array serialized representation of value. Optional here is for legacy and backward compatibility reasons. This Optional is expected to always be filled.
      • fromPayload

        public <T> T fromPayload​(io.temporal.api.common.v1.Payload payload,
                                 java.lang.Class<T> valueClass,
                                 java.lang.reflect.Type valueType)
        Specified by:
        fromPayload in interface DataConverter
      • toPayloads

        public java.util.Optional<io.temporal.api.common.v1.Payloads> toPayloads​(java.lang.Object... values)
                                                                          throws DataConverterException
        Description copied from interface: DataConverter
        Implements conversion of a list of values.
        Specified by:
        toPayloads in interface DataConverter
        Parameters:
        values - Java values to convert to String.
        Returns:
        converted value. Return empty Optional if values are empty.
        Throws:
        DataConverterException - if conversion of the value passed as parameter failed for any reason.
      • fromPayloads

        public <T> T fromPayloads​(int index,
                                  java.util.Optional<io.temporal.api.common.v1.Payloads> content,
                                  java.lang.Class<T> valueType,
                                  java.lang.reflect.Type valueGenericType)
                           throws DataConverterException
        Description copied from interface: DataConverter
        Implements conversion of a single Payload from the serialized Payloads.
        Specified by:
        fromPayloads in interface DataConverter
        Parameters:
        index - index of the value in the payloads
        content - serialized value to convert to Java objects.
        valueType - type of the value stored in the content
        valueGenericType - generic type of the value stored in the content
        Returns:
        converted Java object
        Throws:
        DataConverterException - if conversion of the data passed as parameter failed for any reason.
      • fromPayloads

        public java.lang.Object[] fromPayloads​(java.util.Optional<io.temporal.api.common.v1.Payloads> content,
                                               java.lang.Class<?>[] parameterTypes,
                                               java.lang.reflect.Type[] genericParameterTypes)
                                        throws DataConverterException
        Description copied from interface: DataConverter
        Implements conversion of the whole content Payloads into an array of values of different types.

        Implementation note
        This method is expected to return an array of the same length as parameterTypes. If content has not enough Payload elements, this method provides default instances.

        Specified by:
        fromPayloads in interface DataConverter
        Parameters:
        content - serialized value to convert to Java objects.
        parameterTypes - types of the values stored in the @code content}
        genericParameterTypes - generic types of the values stored in the content
        Returns:
        array if converted Java objects
        Throws:
        DataConverterException - if conversion of the data passed as parameter failed for any reason.
      • exceptionToFailure

        @Nonnull
        public io.temporal.api.failure.v1.Failure exceptionToFailure​(@Nonnull
                                                                     java.lang.Throwable throwable)
        Description copied from interface: DataConverter
        Serialize an existing Throwable object into a Failure object. The default implementation delegates the conversion process to an instance of FailureConverter, using this data converter for payload encoding.
        Specified by:
        exceptionToFailure in interface DataConverter
        Parameters:
        throwable - a Throwable object to serialize into a Failure protobuf object
      • failureToException

        @Nonnull
        public TemporalFailure failureToException​(@Nonnull
                                                  io.temporal.api.failure.v1.Failure failure)
        Description copied from interface: DataConverter
        Instantiate an appropriate Java Exception from a serialized Failure object. The default implementation delegates the conversion process to an instance of FailureConverter, using this data converter for payload decoding.
        Specified by:
        failureToException in interface DataConverter
        Parameters:
        failure - Failure protobuf object to deserialize into an exception
      • withContext

        @Nonnull
        public CodecDataConverter withContext​(@Nonnull
                                              SerializationContext context)
        Description copied from interface: DataConverter
        A correct implementation of this interface should have a fully functional "contextless" implementation. Temporal SDK will call this method when a knowledge of the context exists, but DataConverter can be used directly by user code and sometimes SDK itself without any context.

        Note: this method is expected to be cheap and fast. Temporal SDK doesn't always cache the instances and may be calling this method very often. Users are responsible to make sure that this method doesn't recreate expensive objects like Jackson's ObjectMapper on every call.

        Specified by:
        withContext in interface DataConverter
        Specified by:
        withContext in interface PayloadCodec
        Parameters:
        context - provides information to the data converter about the abstraction the data belongs to
        Returns:
        an instance of DataConverter that may use the provided context for serialization
        See Also:
        SerializationContext
      • encode

        @Nonnull
        public java.util.List<io.temporal.api.common.v1.Payload> encode​(@Nonnull
                                                                        java.util.List<io.temporal.api.common.v1.Payload> payloads)
        Specified by:
        encode in interface PayloadCodec
      • decode

        @Nonnull
        public java.util.List<io.temporal.api.common.v1.Payload> decode​(@Nonnull
                                                                        java.util.List<io.temporal.api.common.v1.Payload> payloads)
        Specified by:
        decode in interface PayloadCodec