Class ToCloudEventTransformer

All Implemented Interfaces:
Aware, BeanFactoryAware, BeanNameAware, InitializingBean, ApplicationContextAware, ComponentSourceAware, ExpressionCapable, GenericTransformer<Message<?>, Message<?>>, NamedComponent, Transformer

public class ToCloudEventTransformer extends AbstractTransformer
Transform messages to CloudEvent format with attributes and extensions mapping.

This transformer supports two output modes:

  • Structured Content Mode: When an EventFormat is available (either configured via setEventFormat(EventFormat) or resolved via setEventFormatContentTypeExpression(Expression)), the CloudEvent is serialized into the message payload using that format (e.g., JSON, XML). The output message contains the serialized CloudEvent as payload with MessageHeaders.CONTENT_TYPE set to the format's serialized content type.
  • Binary Content Mode: When no EventFormat is available, the transformer uses ToCloudEventTransformer.MessageBuilderMessageWriter to convert the CloudEvent into a message where CloudEvent attributes are mapped to headers with a configurable prefix (default: "ce-"), and the CloudEvent data becomes the payload. The output message contains the original data as payload with MessageHeaders.CONTENT_TYPE set to "application/cloudevents".
Since:
7.1
Author:
Glenn Renfro, Artem Bilan
  • Constructor Details

    • ToCloudEventTransformer

      public ToCloudEventTransformer()
      Create a ToCloudEventTransformer with no extensionPatterns.
    • ToCloudEventTransformer

      public ToCloudEventTransformer(String... extensionPatterns)
      Create a ToCloudEventTransformer.
      Parameters:
      extensionPatterns - patterns to evaluate whether message headers should be added as extensions to the CloudEvent
  • Method Details

    • onInit

      protected void onInit()
      Description copied from class: IntegrationObjectSupport
      Subclasses may implement this for initialization logic.
      Overrides:
      onInit in class IntegrationObjectSupport
    • setEventIdExpression

      public void setEventIdExpression(Expression eventIdExpression)
      Set the Expression to create CloudEvent id. Default extracts the id from the MessageHeaders of the message.
      Parameters:
      eventIdExpression - the expression to create the id for each CloudEvent
    • setSourceExpression

      public void setSourceExpression(Expression sourceExpression)
      Set the Expression to create CloudEvent source. Default is "/spring/" + appName + "." + getBeanName()).
      Parameters:
      sourceExpression - the expression to create the source for each CloudEvent
    • setTypeExpression

      public void setTypeExpression(Expression typeExpression)
      Set the Expression to extract the type for the CloudEvent. The default is spring.message.
      Parameters:
      typeExpression - the expression to create the type for each CloudEvent
    • setDataSchemaExpression

      public void setDataSchemaExpression(Expression dataSchemaExpression)
      Set the Expression to create the dataSchema for the CloudEvent.
      Parameters:
      dataSchemaExpression - the expression to create the dataSchema for each CloudEvent
    • setSubjectExpression

      public void setSubjectExpression(Expression subjectExpression)
      Set the Expression to create the subject for the CloudEvent.
      Parameters:
      subjectExpression - the expression to create the subject for each CloudEvent
    • setEventFormat

      public void setEventFormat(io.cloudevents.core.format.EventFormat eventFormat)
      Set the EventFormat to use for CloudEvent serialization. If eventFormat and the eventFormatContentTypeExpression are provided, the eventFormat has a precedence.
      Parameters:
      eventFormat - the event format for serializing CloudEvents
    • setEventFormatContentTypeExpression

      public void setEventFormatContentTypeExpression(Expression eventFormatContentTypeExpression)
      Set the Expression to produce a cloud event format content type when EventFormatProvider is to be used to determine EventFormat. If eventFormat and the eventFormatContentTypeExpression are provided, the eventFormat has a precedence.
      Parameters:
      eventFormatContentTypeExpression - the expression to create content type for the EventFormatProvider.resolveFormat(String)
      See Also:
      • ContentType
    • setCloudEventPrefix

      public void setCloudEventPrefix(String cloudEventPrefix)
      Set the prefix for CloudEvent headers in binary content mode. Defaults to ce-.
      Parameters:
      cloudEventPrefix - the prefix to use for CloudEvent headers
    • doTransform

      protected Object doTransform(Message<?> message)
      Description copied from class: AbstractTransformer
      Subclasses must implement this method to provide the transformation logic. If the return value is itself a Message, it will be used as the result. Otherwise, any non-null return value will be used as the payload of the result Message.
      Specified by:
      doTransform in class AbstractTransformer
      Parameters:
      message - The message.
      Returns:
      The result of the transformation.
    • getComponentType

      public String getComponentType()