org.occurrent.application.converter.jackson.JacksonCloudEventConverter Maven / Gradle / Ivy
Show all versions of cloudevent-converter-jackson Show documentation
/*
*
* Copyright 2021 Johan Haleby
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.occurrent.application.converter.jackson;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.cloudevents.CloudEvent;
import io.cloudevents.CloudEventData;
import io.cloudevents.core.builder.CloudEventBuilder;
import io.cloudevents.core.data.PojoCloudEventData;
import org.occurrent.application.converter.CloudEventConverter;
import org.occurrent.application.converter.typemapper.CloudEventTypeMapper;
import org.occurrent.application.converter.typemapper.ReflectionCloudEventTypeMapper;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
import java.time.OffsetDateTime;
import java.util.Map;
import java.util.UUID;
import java.util.function.Function;
import static java.time.ZoneOffset.UTC;
import static java.util.Objects.requireNonNull;
/**
* An {@link CloudEventConverter} that uses a Jackson {@link ObjectMapper} to serialize a domain event to JSON (content type {@value #DEFAULT_CONTENT_TYPE}) that is used as data in a {@link CloudEvent}.
*
* @param The type of your domain event(s) to convert
*/
public class JacksonCloudEventConverter implements CloudEventConverter {
private static final String DEFAULT_CONTENT_TYPE = "application/json";
private final ObjectMapper objectMapper;
private final URI cloudEventSource;
private final Function idMapper;
private final CloudEventTypeMapper cloudEventTypeMapper;
private final Function timeMapper;
private final Function subjectMapper;
private final String contentType;
/**
* Create a new instance of the {@link JacksonCloudEventConverter} that does the following:
*
* - Uses a random UUID as cloud event id
* - Uses the fully-qualified name of the domain event class as cloud event type. You should definitely change this in production!
* - Uses {@code OffsetDateTime.now(UTC)} as cloud event time
* - Uses charset UTF-8 when converting the domain event to/from JSON
* - No subject
*
*
* See cloud event documentation for info on what the cloud event attributes mean.
* Use {@link Builder} for more advanced configuration.
*
* @param objectMapper The ObjectMapper instance to use
* @param cloudEventSource The cloud event source.
* @see Builder The Builder for more advanced configuration
*/
public JacksonCloudEventConverter(ObjectMapper objectMapper, URI cloudEventSource) {
this(objectMapper, cloudEventSource, defaultIdMapperFunction(), defaultTypeMapper(), defaultTimeMapperFunction(), defaultSubjectMapperFunction(), DEFAULT_CONTENT_TYPE);
}
private JacksonCloudEventConverter(ObjectMapper objectMapper, URI cloudEventSource, Function idMapper, CloudEventTypeMapper cloudEventTypeMapper, Function timeMapper, Function subjectMapper, String contentType) {
requireNonNull(objectMapper, ObjectMapper.class.getSimpleName() + " cannot be null");
requireNonNull(cloudEventSource, "cloudEventSource cannot be null");
requireNonNull(idMapper, "idMapper cannot be null");
requireNonNull(cloudEventTypeMapper, CloudEventTypeMapper.class.getSimpleName() + " cannot be null");
requireNonNull(timeMapper, "timeMapper cannot be null");
requireNonNull(subjectMapper, "subjectMapper cannot be null");
this.objectMapper = objectMapper;
this.cloudEventSource = cloudEventSource;
this.idMapper = idMapper;
this.timeMapper = timeMapper;
this.subjectMapper = subjectMapper;
this.contentType = contentType;
this.cloudEventTypeMapper = cloudEventTypeMapper;
}
/**
* Converts the {@code domainEvent} into a {@link CloudEvent} using {@link ObjectMapper}.
*
* @param domainEvent The domain event to convert
* @return A {@link CloudEvent} converted from the domainEvent
.
*/
@Override
public CloudEvent toCloudEvent(T domainEvent) {
requireNonNull(domainEvent, "Domain event cannot be null");
// @formatter:off
PojoCloudEventData