This repository has been archived by the owner on Oct 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new endpoint for emitting raw events (#12)
- Loading branch information
Showing
5 changed files
with
110 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/main/java/com/backbase/eo/testing/events/emitter/RawEmittingController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.backbase.eo.testing.events.emitter; | ||
|
||
import com.backbase.buildingblocks.backend.communication.context.OriginatorContext; | ||
import com.backbase.buildingblocks.backend.communication.event.EnvelopedEvent; | ||
import com.backbase.buildingblocks.backend.communication.event.scs.EventMessageProcessor; | ||
import java.time.Instant; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import lombok.Data; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.cloud.stream.function.StreamBridge; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.messaging.Message; | ||
import org.springframework.messaging.support.MessageBuilder; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** | ||
* {@link RawEmittingController} | ||
* <br> | ||
* {@code com.backbase.eo.testing.events.emitter.RawEmittingController} | ||
* <br> | ||
* | ||
* @author Jaco Botha | ||
* @since 02 December 2022 | ||
*/ | ||
@Slf4j | ||
@RestController | ||
@RequiredArgsConstructor | ||
public class RawEmittingController { | ||
|
||
@Autowired | ||
private StreamBridge bridge; | ||
|
||
@Autowired(required = false) | ||
private List<EventMessageProcessor> eventMessageProcessors = Collections.emptyList(); | ||
|
||
@PostMapping( | ||
path = "/events/raw", | ||
consumes = {MediaType.APPLICATION_JSON_VALUE} | ||
) | ||
public ResponseEntity<Void> emitEvent(@RequestBody RawEventPayload payload) { | ||
|
||
OriginatorContext originatorContext = new OriginatorContext(); | ||
originatorContext.setRequestUuid(payload.getRequestId()); | ||
originatorContext.setCreationTime(payload.getCreationTime()); | ||
|
||
EnvelopedEvent envelopedEvent = new EnvelopedEvent(); | ||
envelopedEvent.setOriginatorContext(originatorContext); | ||
envelopedEvent.setEvent(payload.getBody()); | ||
|
||
MessageBuilder eventMessageBuilder = MessageBuilder.withPayload(payload.getBody()).setHeader("bbEventType", payload.getEventType()); | ||
this.eventMessageProcessors.forEach((processor) -> { | ||
processor.prepareEventMessage(eventMessageBuilder, envelopedEvent); | ||
}); | ||
Message message = eventMessageBuilder.build(); | ||
log.debug("Event emitted: {}", message.getPayload()); | ||
|
||
this.bridge.send(payload.getDestination(), message); | ||
return ResponseEntity.status(HttpStatus.CREATED).build(); | ||
} | ||
|
||
@Data | ||
static class RawEventPayload { | ||
|
||
private String destination; | ||
private String eventType; | ||
private String requestId = UUID.randomUUID().toString(); | ||
private long creationTime = Instant.EPOCH.toEpochMilli(); | ||
private Map<String, Object> body; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters