From 333f6aab2117f652f5404d25371446269ce4a38c Mon Sep 17 00:00:00 2001 From: Marcel Date: Sun, 14 Nov 2021 20:46:42 +0100 Subject: [PATCH] finished websub implementation --- .../tapas/TapasAuctionHouseApplication.java | 12 ++------ .../common/clients/WebSubSubscriber.java | 4 +-- ...tionStartedEventListenerWebSubAdapter.java | 30 ++++++++++++++----- .../websub/ValidateIntentWebSubAdapter.java | 3 -- 4 files changed, 27 insertions(+), 22 deletions(-) diff --git a/tapas-auction-house/src/main/java/ch/unisg/tapas/TapasAuctionHouseApplication.java b/tapas-auction-house/src/main/java/ch/unisg/tapas/TapasAuctionHouseApplication.java index 3459cff..1f958d9 100644 --- a/tapas-auction-house/src/main/java/ch/unisg/tapas/TapasAuctionHouseApplication.java +++ b/tapas-auction-house/src/main/java/ch/unisg/tapas/TapasAuctionHouseApplication.java @@ -27,23 +27,17 @@ public class TapasAuctionHouseApplication { public static void main(String[] args) { SpringApplication tapasAuctioneerApp = new SpringApplication(TapasAuctionHouseApplication.class); - + // We will use these bootstrap methods in Week 6: + bootstrapMarketplaceWithWebSub(); + // bootstrapMarketplaceWithMqtt(); tapasAuctioneerApp.run(args); - - // We will use these bootstrap methods in Week 6: - - // bootstrapMarketplaceWithMqtt(); - bootstrapMarketplaceWithWebSub(); } - /** * Discovers auction houses and subscribes to WebSub notifications */ private static void bootstrapMarketplaceWithWebSub() { - System.out.println("HAHA"); List auctionHouseEndpoints = discoverAuctionHouseEndpoints(); - LOGGER.info("Found auction house endpoints: " + auctionHouseEndpoints); WebSubSubscriber subscriber = new WebSubSubscriber(); diff --git a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/common/clients/WebSubSubscriber.java b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/common/clients/WebSubSubscriber.java index 8066010..5b3fc32 100644 --- a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/common/clients/WebSubSubscriber.java +++ b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/common/clients/WebSubSubscriber.java @@ -9,10 +9,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import org.json.JSONObject; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; -import org.springframework.stereotype.Component; /** * Subscribes to the WebSub hubs of auction houses discovered at run time. This class is instantiated @@ -38,6 +35,7 @@ public class WebSubSubscriber { subscribeToWebSub(topic); + // Shoudl be done :D // TODO Subscribe to the auction house endpoint via WebSub: // 1. Send a request to the auction house in order to discover the WebSub hub to subscribe to. // The request URI should depend on the design of the Auction House HTTP API. diff --git a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/in/messaging/websub/AuctionStartedEventListenerWebSubAdapter.java b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/in/messaging/websub/AuctionStartedEventListenerWebSubAdapter.java index 9e7a356..4f67dad 100644 --- a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/in/messaging/websub/AuctionStartedEventListenerWebSubAdapter.java +++ b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/in/messaging/websub/AuctionStartedEventListenerWebSubAdapter.java @@ -1,6 +1,18 @@ package ch.unisg.tapas.auctionhouse.adapter.in.messaging.websub; +import ch.unisg.tapas.auctionhouse.adapter.common.formats.AuctionJsonRepresentation; import ch.unisg.tapas.auctionhouse.application.handler.AuctionStartedHandler; +import ch.unisg.tapas.auctionhouse.application.port.in.AuctionStartedEvent; +import ch.unisg.tapas.auctionhouse.domain.Auction; +import ch.unisg.tapas.auctionhouse.domain.Auction.AuctionDeadline; +import ch.unisg.tapas.auctionhouse.domain.Auction.AuctionHouseUri; +import ch.unisg.tapas.auctionhouse.domain.Auction.AuctionId; +import ch.unisg.tapas.auctionhouse.domain.Auction.AuctionedTaskType; +import ch.unisg.tapas.auctionhouse.domain.Auction.AuctionedTaskUri; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Collection; import org.json.JSONArray; import org.springframework.http.HttpStatus; @@ -20,16 +32,20 @@ public class AuctionStartedEventListenerWebSubAdapter { /** * Controller which listens to auction-started callbacks * @return 200 OK + * @throws URISyntaxException **/ @PostMapping(path = "/auction-started") - public ResponseEntity handleExecutorAddedEvent(@RequestBody String payload) { + public ResponseEntity handleExecutorAddedEvent(@RequestBody Collection payload) throws URISyntaxException { - // Payload should be a JSONArray with auctions - JSONArray jsonArray = new JSONArray(payload); - for (Object auction : jsonArray) { - System.out.println(auction); - // TODO logic to call handleAuctionStartedEvent() - // auctionStartedHandler.handleAuctionStartedEvent(auctionStartedEvent) + for (AuctionJsonRepresentation auction : payload) { + auctionStartedHandler.handleAuctionStartedEvent( + new AuctionStartedEvent( + new Auction(new AuctionId(auction.getAuctionId()), + new AuctionHouseUri(new URI(auction.getAuctionHouseUri())), + new AuctionedTaskUri(new URI(auction.getTaskUri())), + new AuctionedTaskType(auction.getTaskType()), + new AuctionDeadline(auction.getDeadline())) + )); } return new ResponseEntity<>(HttpStatus.OK); diff --git a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/in/messaging/websub/ValidateIntentWebSubAdapter.java b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/in/messaging/websub/ValidateIntentWebSubAdapter.java index 9e25a69..8509b09 100644 --- a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/in/messaging/websub/ValidateIntentWebSubAdapter.java +++ b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/in/messaging/websub/ValidateIntentWebSubAdapter.java @@ -18,9 +18,6 @@ public class ValidateIntentWebSubAdapter { @GetMapping(path = "/auction-started") public ResponseEntity handleExecutorAddedEvent(@RequestParam("hub.challenge") String challenge) { - - - // Different implementation depending on local development or production if (environment.equalsIgnoreCase("development")) { HttpHeaders headers = new HttpHeaders();