diff --git a/.deployment/docker-compose.yml b/.deployment/docker-compose.yml index c4796da..c6f1955 100644 --- a/.deployment/docker-compose.yml +++ b/.deployment/docker-compose.yml @@ -67,6 +67,8 @@ services: - ./:/data/ environment: mqtt.broker.uri: tcp://broker.hivemq.com:1883 + discovery.endpoint.uri: https://tapas-auction-house.86.119.35.213.nip.io/discovery/ + auction.house.uri: https://tapas-auction-house.86-119-35-40.nip.io labels: - "traefik.enable=true" - "traefik.http.routers.tapas-auction-house.rule=Host(`tapas-auction-house.${PUB_IP}.nip.io`)" @@ -86,7 +88,6 @@ services: - ./:/data/ environment: task.list.uri: http://tapas-tasks:8081 - auction.house.uri: http://tapas-auction-house:8086 executor.robot.uri: http://executor-robot:8084 executor.computation.uri: http://executor-computation:8085 mqtt.broker.uri: tcp://broker.hivemq.com:1883 diff --git a/docker-compose.yaml b/docker-compose.yaml index c53981f..8566c1b 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -72,15 +72,15 @@ services: volumes: - ./executor-computation/src:/opt/app/src - ./executor-computation/target:/opt/app/target - executor-robot: - container_name: executor-robot - build: - context: "." - dockerfile: "./executor-robot/Dockerfile" - target: development - ports: - - "8084:8084" - - "5009:5005" - volumes: - - ./executor-robot/src:/opt/app/src - - ./executor-robot/target:/opt/app/target + # executor-robot: + # container_name: executor-robot + # build: + # context: "." + # dockerfile: "./executor-robot/Dockerfile" + # target: development + # ports: + # - "8084:8084" + # - "5009:5005" + # volumes: + # - ./executor-robot/src:/opt/app/src + # - ./executor-robot/target:/opt/app/target diff --git a/executor-base/src/main/java/ch/unisg/executorBase/executor/adapter/in/web/TaskAvailableController.java b/executor-base/src/main/java/ch/unisg/executorBase/executor/adapter/in/web/TaskAvailableController.java index 32f3486..66ef496 100644 --- a/executor-base/src/main/java/ch/unisg/executorBase/executor/adapter/in/web/TaskAvailableController.java +++ b/executor-base/src/main/java/ch/unisg/executorBase/executor/adapter/in/web/TaskAvailableController.java @@ -1,4 +1,4 @@ -package ch.unisg.executorBase.executor.adapter.in.web; +package ch.unisg.executorbase.executor.adapter.in.web; import java.util.logging.Logger; diff --git a/executor-base/src/main/java/ch/unisg/executorBase/executor/adapter/out/web/ExecutionFinishedEventAdapter.java b/executor-base/src/main/java/ch/unisg/executorBase/executor/adapter/out/web/ExecutionFinishedEventAdapter.java index 5d832db..f683b81 100644 --- a/executor-base/src/main/java/ch/unisg/executorBase/executor/adapter/out/web/ExecutionFinishedEventAdapter.java +++ b/executor-base/src/main/java/ch/unisg/executorBase/executor/adapter/out/web/ExecutionFinishedEventAdapter.java @@ -1,4 +1,4 @@ -package ch.unisg.executorBase.executor.adapter.out.web; +package ch.unisg.executorbase.executor.adapter.out.web; import java.io.IOException; import java.net.URI; diff --git a/executor-base/src/main/java/ch/unisg/executorBase/executor/domain/ExecutorBase.java b/executor-base/src/main/java/ch/unisg/executorBase/executor/domain/ExecutorBase.java index be5ddd6..09ded48 100644 --- a/executor-base/src/main/java/ch/unisg/executorBase/executor/domain/ExecutorBase.java +++ b/executor-base/src/main/java/ch/unisg/executorBase/executor/domain/ExecutorBase.java @@ -1,11 +1,10 @@ package ch.unisg.executorbase.executor.domain; -import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import ch.unisg.common.valueobject.ExecutorURI; -import ch.unisg.executorBase.executor.adapter.out.web.ExecutionFinishedEventAdapter; +import ch.unisg.executorbase.executor.adapter.out.web.ExecutionFinishedEventAdapter; import ch.unisg.executorbase.executor.adapter.out.web.GetAssignmentAdapter; import ch.unisg.executorbase.executor.adapter.out.web.NotifyExecutorPoolAdapter; import ch.unisg.executorbase.executor.application.port.out.ExecutionFinishedEventPort; diff --git a/executor-base/src/main/java/ch/unisg/executorBase/executor/domain/ExecutorType.java b/executor-base/src/main/java/ch/unisg/executorBase/executor/domain/ExecutorType.java index 30460e6..ca9533a 100644 --- a/executor-base/src/main/java/ch/unisg/executorBase/executor/domain/ExecutorType.java +++ b/executor-base/src/main/java/ch/unisg/executorBase/executor/domain/ExecutorType.java @@ -1,11 +1,11 @@ package ch.unisg.executorbase.executor.domain; public enum ExecutorType { - COMPUTATION, ROBOT, HUMIDITY; + ADDITION, ROBOT; /** * Checks if the give executor type exists. - * @return Whether the given executor type exists + * @return Wheter the given executor type exists **/ public static boolean contains(String test) { diff --git a/executor-computation/src/main/java/ch/unisg/executorcomputation/ExecutorcomputationApplication.java b/executor-computation/src/main/java/ch/unisg/executorcomputation/ExecutorcomputationApplication.java index fe25430..7a22493 100644 --- a/executor-computation/src/main/java/ch/unisg/executorcomputation/ExecutorcomputationApplication.java +++ b/executor-computation/src/main/java/ch/unisg/executorcomputation/ExecutorcomputationApplication.java @@ -13,7 +13,7 @@ public class ExecutorcomputationApplication { public static void main(String[] args) { try { - TimeUnit.SECONDS.sleep(40); + TimeUnit.SECONDS.sleep(10); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); diff --git a/executor-computation/src/main/java/ch/unisg/executorcomputation/executor/domain/Executor.java b/executor-computation/src/main/java/ch/unisg/executorcomputation/executor/domain/Executor.java index 2da59db..582b57f 100644 --- a/executor-computation/src/main/java/ch/unisg/executorcomputation/executor/domain/Executor.java +++ b/executor-computation/src/main/java/ch/unisg/executorcomputation/executor/domain/Executor.java @@ -7,7 +7,7 @@ import ch.unisg.executorbase.executor.domain.ExecutorType; public class Executor extends ExecutorBase { - private static final Executor executor = new Executor(ExecutorType.COMPUTATION); + private static final Executor executor = new Executor(ExecutorType.ADDITION); public static Executor getExecutor() { return executor; diff --git a/executor-humidity/pom.xml b/executor-humidity/pom.xml deleted file mode 100644 index 27ed3b6..0000000 --- a/executor-humidity/pom.xml +++ /dev/null @@ -1,77 +0,0 @@ - - - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 2.5.5 - - - org.unisg - executor-humidity - 1.0-SNAPSHOT - executor-humidity - Demo project for Spring Boot - - - jitpack.io - https://jitpack.io - - - - - org.springframework.boot - spring-boot-autoconfigure - 2.5.5 - compile - - - org.springframework - spring-web - 5.3.10 - compile - - - ch.unisg - executor-base - 0.0.1-SNAPSHOT - compile - - - org.eclipse.californium - californium-core - 3.0.0 - compile - - - com.github.Interactions-HSG - wot-td-java - 0.1.1 - - - - - 16 - 16 - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - org.projectlombok - lombok - - - - - - - - diff --git a/executor-humidity/src/main/java/ch/unisg/executorhumidity/ExecutorhumidityApplication.java b/executor-humidity/src/main/java/ch/unisg/executorhumidity/ExecutorhumidityApplication.java deleted file mode 100644 index 181cfeb..0000000 --- a/executor-humidity/src/main/java/ch/unisg/executorhumidity/ExecutorhumidityApplication.java +++ /dev/null @@ -1,16 +0,0 @@ -package ch.unisg.executorhumidity; - - -import ch.unisg.executorhumidity.executor.domain.Executor; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -@SpringBootApplication -public class ExecutorhumidityApplication { - - public static void main(String[] args) { - SpringApplication.run(ExecutorhumidityApplication.class, args); - Executor.getExecutor(); - } - -} diff --git a/executor-humidity/src/main/java/ch/unisg/executorhumidity/executor/adapter/in/web/TaskAvailableController.java b/executor-humidity/src/main/java/ch/unisg/executorhumidity/executor/adapter/in/web/TaskAvailableController.java deleted file mode 100644 index b66472c..0000000 --- a/executor-humidity/src/main/java/ch/unisg/executorhumidity/executor/adapter/in/web/TaskAvailableController.java +++ /dev/null @@ -1,38 +0,0 @@ -package ch.unisg.executorhumidity.executor.adapter.in.web; - -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RestController; - -import ch.unisg.executorbase.executor.application.port.in.TaskAvailableCommand; -import ch.unisg.executorbase.executor.application.port.in.TaskAvailableUseCase; -import ch.unisg.executorbase.executor.domain.ExecutorType; - -import java.util.concurrent.CompletableFuture; - -@RestController -public class TaskAvailableController { - private final TaskAvailableUseCase taskAvailableUseCase; - - public TaskAvailableController(TaskAvailableUseCase taskAvailableUseCase) { - this.taskAvailableUseCase = taskAvailableUseCase; - } - - @GetMapping(path="/newtask/{taskType}") - public ResponseEntity retrieveTaskfromTaskList(@PathVariable("taskType") String taskType) { - - if (ExecutorType.contains(taskType.toUpperCase())) { - TaskAvailableCommand command = new TaskAvailableCommand( - ExecutorType.valueOf(taskType.toUpperCase())); - CompletableFuture.runAsync(() -> taskAvailableUseCase.newTaskAvailable(command)); - } - - // Add the content type as a response header - HttpHeaders responseHeaders = new HttpHeaders(); - - return new ResponseEntity<>("OK", responseHeaders, HttpStatus.OK); - } -} diff --git a/executor-humidity/src/main/java/ch/unisg/executorhumidity/executor/adapter/out/GetHumidityAdapter.java b/executor-humidity/src/main/java/ch/unisg/executorhumidity/executor/adapter/out/GetHumidityAdapter.java deleted file mode 100644 index c73e356..0000000 --- a/executor-humidity/src/main/java/ch/unisg/executorhumidity/executor/adapter/out/GetHumidityAdapter.java +++ /dev/null @@ -1,78 +0,0 @@ -package ch.unisg.executorhumidity.executor.adapter.out; - -import ch.unisg.executorhumidity.executor.application.port.out.GetHumidityPort; -import ch.unisg.ics.interactions.wot.td.ThingDescription; -import ch.unisg.ics.interactions.wot.td.affordances.Form; -import ch.unisg.ics.interactions.wot.td.affordances.PropertyAffordance; -import ch.unisg.ics.interactions.wot.td.clients.TDCoapRequest; -import ch.unisg.ics.interactions.wot.td.clients.TDCoapResponse; -import ch.unisg.ics.interactions.wot.td.io.TDGraphReader; -import ch.unisg.ics.interactions.wot.td.schemas.ObjectSchema; -import ch.unisg.ics.interactions.wot.td.vocabularies.TD; -import org.eclipse.californium.core.CoapClient; -import org.eclipse.californium.elements.exception.ConnectorException; -import org.springframework.context.annotation.Primary; -import org.springframework.stereotype.Component; - -import java.io.IOException; -import java.net.URI; -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; -import java.util.Map; -import java.util.Optional; - - -@Component -@Primary -public class GetHumidityAdapter implements GetHumidityPort { - - @Override - public String getHumidity() { - - String endpoint = "https://api.interactions.ics.unisg.ch/search/searchEngine"; - - String input = "@prefix dct: . select ?title where { ?title dct:title 'Mirogate' }"; - - var httpRequest = HttpRequest.newBuilder() - .uri(URI.create(endpoint)) - .header("Content-Type", "application/sparql-query") - .POST(HttpRequest.BodyPublishers.ofString(input)) - .build(); - - var client = HttpClient.newHttpClient(); - - try { - String description = client.send(httpRequest, HttpResponse.BodyHandlers.ofString()).body(); - - // Parse a TD from a string - ThingDescription td = TDGraphReader.readFromString(ThingDescription.TDFormat.RDF_TURTLE, description); - - Optional humidity = td.getPropertyByName("humidity"); - - if (humidity.isPresent()) { - Optional
form = humidity.get().getFirstFormForOperationType(TD.readProperty); - - if (form.isPresent()) { - TDCoapRequest request = new TDCoapRequest(form.get(), TD.readProperty); - try { - TDCoapResponse response = request.execute(); - - Map payload = response.getPayloadAsObject((ObjectSchema) humidity.get().getDataSchema()); - Object result = payload.get("https://interactions.ics.unisg.ch/mirogate#HumidityValue"); - return result.toString(); - - } catch (IOException e) { - e.printStackTrace(); - } - } - } - - } catch (IOException e) { - e.printStackTrace(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - return null; - } -} diff --git a/executor-humidity/src/main/java/ch/unisg/executorhumidity/executor/application/port/out/GetHumidityPort.java b/executor-humidity/src/main/java/ch/unisg/executorhumidity/executor/application/port/out/GetHumidityPort.java deleted file mode 100644 index d2cda18..0000000 --- a/executor-humidity/src/main/java/ch/unisg/executorhumidity/executor/application/port/out/GetHumidityPort.java +++ /dev/null @@ -1,9 +0,0 @@ -package ch.unisg.executorhumidity.executor.application.port.out; - -import org.eclipse.californium.elements.exception.ConnectorException; - -import java.io.IOException; - -public interface GetHumidityPort { - String getHumidity(); -} diff --git a/executor-humidity/src/main/java/ch/unisg/executorhumidity/executor/application/service/TaskAvailableService.java b/executor-humidity/src/main/java/ch/unisg/executorhumidity/executor/application/service/TaskAvailableService.java deleted file mode 100644 index 0e467c4..0000000 --- a/executor-humidity/src/main/java/ch/unisg/executorhumidity/executor/application/service/TaskAvailableService.java +++ /dev/null @@ -1,27 +0,0 @@ -package ch.unisg.executorhumidity.executor.application.service; - -import ch.unisg.executorbase.executor.domain.ExecutorStatus; -import ch.unisg.executorhumidity.executor.domain.Executor; -import org.springframework.stereotype.Component; -import ch.unisg.executorbase.executor.application.port.in.TaskAvailableCommand; -import ch.unisg.executorbase.executor.application.port.in.TaskAvailableUseCase; -import lombok.RequiredArgsConstructor; - -import javax.transaction.Transactional; - -@RequiredArgsConstructor -@Component -@Transactional -public class TaskAvailableService implements TaskAvailableUseCase { - - @Override - public void newTaskAvailable(TaskAvailableCommand command) { - - Executor executor = Executor.getExecutor(); - - if(executor.getExecutorType() == command.getTaskType() && - executor.getStatus() == ExecutorStatus.IDLING) { - executor.getAssignment(); - } - } -} diff --git a/executor-humidity/src/main/java/ch/unisg/executorhumidity/executor/domain/Executor.java b/executor-humidity/src/main/java/ch/unisg/executorhumidity/executor/domain/Executor.java deleted file mode 100644 index ac73e65..0000000 --- a/executor-humidity/src/main/java/ch/unisg/executorhumidity/executor/domain/Executor.java +++ /dev/null @@ -1,31 +0,0 @@ -package ch.unisg.executorhumidity.executor.domain; - -import ch.unisg.executorbase.executor.domain.ExecutorBase; -import ch.unisg.executorbase.executor.domain.ExecutorType; -import ch.unisg.executorhumidity.executor.adapter.out.GetHumidityAdapter; -import ch.unisg.executorhumidity.executor.application.port.out.GetHumidityPort; -import org.eclipse.californium.elements.exception.ConnectorException; - -import java.io.IOException; - -public class Executor extends ExecutorBase{ - - private static final Executor executor = new Executor(ExecutorType.HUMIDITY); - - private final GetHumidityPort getHumidityPort = new GetHumidityAdapter(); - - private Executor(ExecutorType executorType) {super(executorType);} - - public static Executor getExecutor() {return executor;} - - - @Override - protected - String execution(String input) { - //TODO: Fill - - String result = getHumidityPort.getHumidity(); - - return result; - } -} diff --git a/executor-pool/src/main/resources/application.properties b/executor-pool/src/main/resources/application.properties index c8f67f1..20953a7 100644 --- a/executor-pool/src/main/resources/application.properties +++ b/executor-pool/src/main/resources/application.properties @@ -1,6 +1,7 @@ server.port=8083 -mqtt.broker.uri=tcp://localhost:1883 +# mqtt.broker.uri=tcp://localhost:1883 +mqtt.broker.uri=tcp://broker.hivemq.com spring.data.mongodb.uri=mongodb://root:password@localhost:27017 spring.data.mongodb.database=tapas-executors diff --git a/executor-robot/pom.xml b/executor-robot/pom.xml index 570bd18..ca95edf 100644 --- a/executor-robot/pom.xml +++ b/executor-robot/pom.xml @@ -18,12 +18,6 @@ scs-asse-fs21-group1 https://sonarcloud.io - - - jitpack.io - https://jitpack.io - - org.springframework.boot @@ -51,43 +45,7 @@ executor-base 0.0.1-SNAPSHOT - - ch.unisg - executor-base - 0.0.1-SNAPSHOT - compile - - - ch.unisg - executor-base - 0.0.1-SNAPSHOT - compile - - - ch.unisg - executor-base - 0.0.1-SNAPSHOT - compile - - - ch.unisg - executor-base - 0.0.1-SNAPSHOT - compile - - - ch.unisg - executor-base - 0.0.1-SNAPSHOT - compile - - - com.github.Interactions-HSG - wot-td-java - 0.1.1 - compile - - + diff --git a/executor-robot/src/main/java/ch/unisg/executorrobot/executor/adapter/out/DeleteUserFromRobotAdapter.java b/executor-robot/src/main/java/ch/unisg/executorrobot/executor/adapter/out/DeleteUserFromRobotAdapter.java index 37b605a..157bc3e 100644 --- a/executor-robot/src/main/java/ch/unisg/executorrobot/executor/adapter/out/DeleteUserFromRobotAdapter.java +++ b/executor-robot/src/main/java/ch/unisg/executorrobot/executor/adapter/out/DeleteUserFromRobotAdapter.java @@ -17,16 +17,16 @@ public class DeleteUserFromRobotAdapter implements DeleteUserFromRobotPort { @Override public boolean deleteUserFromRobot(String key) { - String url = "https://api.interactions.ics.unisg.ch/leubot1/v1.3.4/user/" + key; - + String url = "https://api.interactions.ics.unisg.ch/leubot1/v1.3.0/user/" + key; + var request = HttpRequest.newBuilder() .uri(URI.create(url)) .header("Content-Type", "application/json") .DELETE() .build(); - + var client = HttpClient.newHttpClient(); - + try { var response = client.send(request, HttpResponse.BodyHandlers.ofString()); System.out.println(response.statusCode()); @@ -38,5 +38,5 @@ public class DeleteUserFromRobotAdapter implements DeleteUserFromRobotPort { } return false; } - + } diff --git a/executor-robot/src/main/java/ch/unisg/executorrobot/executor/adapter/out/InstructionToRobotAdapter.java b/executor-robot/src/main/java/ch/unisg/executorrobot/executor/adapter/out/InstructionToRobotAdapter.java index 531b5eb..c7507e4 100644 --- a/executor-robot/src/main/java/ch/unisg/executorrobot/executor/adapter/out/InstructionToRobotAdapter.java +++ b/executor-robot/src/main/java/ch/unisg/executorrobot/executor/adapter/out/InstructionToRobotAdapter.java @@ -4,21 +4,7 @@ import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; -import ch.unisg.ics.interactions.wot.td.ThingDescription; -import ch.unisg.ics.interactions.wot.td.affordances.ActionAffordance; -import ch.unisg.ics.interactions.wot.td.affordances.Form; -import ch.unisg.ics.interactions.wot.td.clients.TDHttpRequest; -import ch.unisg.ics.interactions.wot.td.clients.TDHttpResponse; -import ch.unisg.ics.interactions.wot.td.io.TDGraphReader; -import ch.unisg.ics.interactions.wot.td.schemas.DataSchema; -import ch.unisg.ics.interactions.wot.td.schemas.ObjectSchema; -import ch.unisg.ics.interactions.wot.td.schemas.StringSchema; -import ch.unisg.ics.interactions.wot.td.security.APIKeySecurityScheme; -import ch.unisg.ics.interactions.wot.td.vocabularies.TD; import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; @@ -30,70 +16,9 @@ public class InstructionToRobotAdapter implements InstructionToRobotPort { @Override public boolean instructionToRobot(String key) { - - String endpoint = "https://api.interactions.ics.unisg.ch/search/searchEngine"; - - String input = "@prefix dct: . select ?title where { ?title dct:title 'Mirogate' }"; - - var httpRequest = HttpRequest.newBuilder() - .uri(URI.create(endpoint)) - .header("Content-Type", "application/json") - .POST(HttpRequest.BodyPublishers.ofString(input)) - .build(); - - var client = HttpClient.newHttpClient(); - - try { - var description = client.send(httpRequest, HttpResponse.BodyHandlers.ofString()).body(); - - // Parse a TD from a string - ThingDescription td = TDGraphReader.readFromString(ThingDescription.TDFormat.RDF_TURTLE, description); - - // Create the payload to be sent with the Http request - Map elbowPayload = new HashMap<>(); - elbowPayload.put("http://www.w3.org/2001/XMLSchema#int", 400); - - // Get the affordance "setElbow" from the TD - Optional action = td.getActionByName("setElbow"); - - // Get the first form - if (action.isPresent()) { - Optional form = action.get().getFirstForm(); - - // Retrieve the input data schema from the action affordance - Optional inputSchema = action.get().getInputSchema(); - - // If a form is found, use it to create and execute the Http request - if (form.isPresent()) { - TDHttpRequest request = new TDHttpRequest(form.get(), TD.invokeAction); - - if (inputSchema.isPresent()) { - request.setObjectPayload((ObjectSchema) inputSchema.get(), elbowPayload); - - try { - TDHttpResponse response = request.execute(); - System.out.println("Received response with status code: " + response.getStatusCode()); - return true; - } catch (IOException e) { - e.printStackTrace(); - } - } - } - } - - } catch (IOException e) { - e.printStackTrace(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - return false; - - - - - /* + String putEndpoint = "https://api.interactions.ics.unisg.ch/leubot1/v1.3.0/elbow"; - + String inputJson = "{ \"value\": 400}"; var request = HttpRequest.newBuilder() .uri(URI.create(putEndpoint)) @@ -101,9 +26,9 @@ public class InstructionToRobotAdapter implements InstructionToRobotPort { .header("X-API-KEY", key) .PUT(HttpRequest.BodyPublishers.ofString(inputJson)) .build(); - + var client = HttpClient.newHttpClient(); - + try { var response = client.send(request, HttpResponse.BodyHandlers.ofString()); System.out.println(response.statusCode()); @@ -115,7 +40,7 @@ public class InstructionToRobotAdapter implements InstructionToRobotPort { e.printStackTrace(); } return false; - */ - } + } + } diff --git a/executor-robot/src/main/java/ch/unisg/executorrobot/executor/adapter/out/UserToRobotAdapter.java b/executor-robot/src/main/java/ch/unisg/executorrobot/executor/adapter/out/UserToRobotAdapter.java index 7c20616..92ca8c1 100644 --- a/executor-robot/src/main/java/ch/unisg/executorrobot/executor/adapter/out/UserToRobotAdapter.java +++ b/executor-robot/src/main/java/ch/unisg/executorrobot/executor/adapter/out/UserToRobotAdapter.java @@ -4,20 +4,7 @@ import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; -import ch.unisg.ics.interactions.wot.td.ThingDescription; -import ch.unisg.ics.interactions.wot.td.affordances.ActionAffordance; -import ch.unisg.ics.interactions.wot.td.affordances.Form; -import ch.unisg.ics.interactions.wot.td.clients.TDHttpRequest; -import ch.unisg.ics.interactions.wot.td.clients.TDHttpResponse; -import ch.unisg.ics.interactions.wot.td.io.TDGraphReader; -import ch.unisg.ics.interactions.wot.td.schemas.DataSchema; -import ch.unisg.ics.interactions.wot.td.schemas.ObjectSchema; -import ch.unisg.ics.interactions.wot.td.vocabularies.TD; -import org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration; import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; @@ -29,77 +16,17 @@ public class UserToRobotAdapter implements UserToRobotPort { @Override public String userToRobot() { - - String endpoint = "https://api.interactions.ics.unisg.ch/search/searchEngine"; - - String input = "@prefix dct: . select ?title where { ?title dct:title 'Mirogate' }"; - - var httpRequest = HttpRequest.newBuilder() - .uri(URI.create(endpoint)) - .header("Content-Type", "application/json") - .POST(HttpRequest.BodyPublishers.ofString(input)) - .build(); - - var client = HttpClient.newHttpClient(); - - try { - var description = client.send(httpRequest, HttpResponse.BodyHandlers.ofString()).body(); - - // Parse a TD from a string - ThingDescription td = TDGraphReader.readFromString(ThingDescription.TDFormat.RDF_TURTLE, description); - - // Create the payload to be sent with the HTTP request - Map logInPayload = new HashMap<>(); - logInPayload.put("http://xmlns.com/foaf/0.1/Name", "keanu rahimian"); - logInPayload.put("http://xmlns.com/foaf/0.1/Mbox", "keanu.rahimian@student.unisg.ch"); - - // Get the affordance "Log-In" from the TD - Optional action = td.getActionByName("logIn"); - - // Get the first form - if (action.isPresent()) { - Optional form = action.get().getFirstForm(); - - // Retrieve the input data schema from the action affordance - Optional inputSchema = action.get().getInputSchema(); - - // If a form is found, use it to create and execute the HTTP request - if (form.isPresent()) { - TDHttpRequest request = new TDHttpRequest(form.get(), TD.invokeAction); - - if (inputSchema.isPresent()) { - request.setObjectPayload((ObjectSchema) inputSchema.get(), logInPayload); - - try { - TDHttpResponse response = request.execute(); - System.out.println("Received response with status code: " + response.getStatusCode()); - - // TODO: Get the key from the response and return it - // Not exactly sure how to get the headers from the payload, as we need them - // to get the key - - return null; - - } catch (IOException e) { - e.printStackTrace(); - } - } - } - } - } catch (IOException e) { - e.printStackTrace(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - /* + String postEndpoint = "https://api.interactions.ics.unisg.ch/leubot1/v1.3.0/user"; + String inputJson = "{ \"name\":\"keanu rahimian\", \"email\":\"keanu.rahimian@student.unisg.ch\"}"; var request = HttpRequest.newBuilder() .uri(URI.create(postEndpoint)) .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString(inputJson)) .build(); - + + var client = HttpClient.newHttpClient(); + try { var response = client.send(request, HttpResponse.BodyHandlers.ofString()); System.out.println(response.statusCode()); @@ -113,10 +40,7 @@ public class UserToRobotAdapter implements UserToRobotPort { e.printStackTrace(); } return null; - */ - - - return null; + } - + } diff --git a/roster/src/main/java/ch/unisg/roster/RosterApplication.java b/roster/src/main/java/ch/unisg/roster/RosterApplication.java index 973e8f1..7f31c91 100644 --- a/roster/src/main/java/ch/unisg/roster/RosterApplication.java +++ b/roster/src/main/java/ch/unisg/roster/RosterApplication.java @@ -33,7 +33,7 @@ public class RosterApplication { SpringApplication rosterApp = new SpringApplication(RosterApplication.class); ENVIRONMENT = rosterApp.run(args).getEnvironment(); bootstrapMarketplaceWithMqtt(); - initialiseRoster(); + // initialiseRoster(); } /** diff --git a/roster/src/main/java/ch/unisg/roster/roster/adapter/in/web/DeleteTaskController.java b/roster/src/main/java/ch/unisg/roster/roster/adapter/in/web/DeleteTaskController.java index 182fb36..eef8b71 100644 --- a/roster/src/main/java/ch/unisg/roster/roster/adapter/in/web/DeleteTaskController.java +++ b/roster/src/main/java/ch/unisg/roster/roster/adapter/in/web/DeleteTaskController.java @@ -3,7 +3,6 @@ package ch.unisg.roster.roster.adapter.in.web; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @@ -23,10 +22,10 @@ public class DeleteTaskController { * Controller to delete a task * @return 200 OK, 409 Conflict **/ - @DeleteMapping(path = "/task/{taskId}") - public ResponseEntity deleteTask(@PathVariable("taskId") String taskId) { + @DeleteMapping(path = "/task", consumes = {"application/task+json"}) + public ResponseEntity applyForTask(@RequestBody Task task) { - DeleteTaskCommand command = new DeleteTaskCommand(taskId); + DeleteTaskCommand command = new DeleteTaskCommand(task.getTaskID(), task.getTaskType()); if (deleteTaskUseCase.deleteTask(command)) { return new ResponseEntity<>(HttpStatus.OK); diff --git a/roster/src/main/java/ch/unisg/roster/roster/adapter/in/web/NewTaskController.java b/roster/src/main/java/ch/unisg/roster/roster/adapter/in/web/NewTaskController.java index e153322..7ff5349 100644 --- a/roster/src/main/java/ch/unisg/roster/roster/adapter/in/web/NewTaskController.java +++ b/roster/src/main/java/ch/unisg/roster/roster/adapter/in/web/NewTaskController.java @@ -31,7 +31,7 @@ public class NewTaskController { logger.info("New task with id:" + task.getTaskID()); - NewTaskCommand command = new NewTaskCommand(task.getTaskID(), task.getTaskUri(), task.getTaskType(), + NewTaskCommand command = new NewTaskCommand(task.getTaskID(), task.getTaskType(), task.getInputData()); boolean success = newTaskUseCase.addNewTaskToQueue(command); diff --git a/roster/src/main/java/ch/unisg/roster/roster/adapter/out/web/LaunchAuctionEventAdapter.java b/roster/src/main/java/ch/unisg/roster/roster/adapter/out/web/LaunchAuctionEventAdapter.java deleted file mode 100644 index f6972ac..0000000 --- a/roster/src/main/java/ch/unisg/roster/roster/adapter/out/web/LaunchAuctionEventAdapter.java +++ /dev/null @@ -1,62 +0,0 @@ -package ch.unisg.roster.roster.adapter.out.web; - -import java.io.IOException; -import java.net.URI; -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; -import java.util.HashMap; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; - -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Primary; -import org.springframework.stereotype.Component; - -import ch.unisg.roster.roster.application.port.out.LaunchAuctionEventPort; -import ch.unisg.roster.roster.domain.event.LaunchAuctionEvent; - -@Component -@Primary -public class LaunchAuctionEventAdapter implements LaunchAuctionEventPort { - - @Value("${auction.house.uri}") - String server; - - @Override - public void launchAuctionEvent(LaunchAuctionEvent launchAuctionEvent) { - - var values = new HashMap() {{ - - put("taskUri", launchAuctionEvent.taskUri); - put("taskType", launchAuctionEvent.taskType.getValue()); - - }}; - - var objectMapper = new ObjectMapper(); - String requestBody = null; - try { - requestBody = objectMapper.writeValueAsString(values); - } catch (JsonProcessingException e) { - e.printStackTrace(); - } - - HttpClient client = HttpClient.newHttpClient(); - HttpRequest request = HttpRequest.newBuilder() - .uri(URI.create(server+"/auctions/")) - .header("Content-Type", "application/task+json") - .POST(HttpRequest.BodyPublishers.ofString(requestBody)) - .build(); - - try { - HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); - } catch (IOException e) { - e.printStackTrace(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - } - -} diff --git a/roster/src/main/java/ch/unisg/roster/roster/application/port/in/DeleteTaskCommand.java b/roster/src/main/java/ch/unisg/roster/roster/application/port/in/DeleteTaskCommand.java index 503428e..9f59dc3 100644 --- a/roster/src/main/java/ch/unisg/roster/roster/application/port/in/DeleteTaskCommand.java +++ b/roster/src/main/java/ch/unisg/roster/roster/application/port/in/DeleteTaskCommand.java @@ -13,8 +13,12 @@ public class DeleteTaskCommand extends SelfValidating { @NotNull private final String taskId; - public DeleteTaskCommand(String taskId) { + @NotNull + private final ExecutorType taskType; + + public DeleteTaskCommand(String taskId, ExecutorType taskType) { this.taskId = taskId; + this.taskType = taskType; this.validateSelf(); } } diff --git a/roster/src/main/java/ch/unisg/roster/roster/application/port/in/NewTaskCommand.java b/roster/src/main/java/ch/unisg/roster/roster/application/port/in/NewTaskCommand.java index f312518..5db2b9f 100644 --- a/roster/src/main/java/ch/unisg/roster/roster/application/port/in/NewTaskCommand.java +++ b/roster/src/main/java/ch/unisg/roster/roster/application/port/in/NewTaskCommand.java @@ -1,7 +1,5 @@ package ch.unisg.roster.roster.application.port.in; -import java.net.URI; - import javax.validation.constraints.NotNull; import ch.unisg.roster.roster.domain.valueobject.ExecutorType; @@ -22,12 +20,8 @@ public class NewTaskCommand extends SelfValidating { @NotNull private final String inputData; - @NotNull - private final String taskUri; - - public NewTaskCommand(String taskID, String taskUri, ExecutorType taskType, String inputData) { + public NewTaskCommand(String taskID, ExecutorType taskType, String inputData) { this.taskID = taskID; - this.taskUri = taskUri; this.taskType = taskType; this.inputData = inputData; this.validateSelf(); diff --git a/roster/src/main/java/ch/unisg/roster/roster/application/port/out/LaunchAuctionEventPort.java b/roster/src/main/java/ch/unisg/roster/roster/application/port/out/LaunchAuctionEventPort.java deleted file mode 100644 index dd828f4..0000000 --- a/roster/src/main/java/ch/unisg/roster/roster/application/port/out/LaunchAuctionEventPort.java +++ /dev/null @@ -1,8 +0,0 @@ -package ch.unisg.roster.roster.application.port.out; - -import ch.unisg.roster.roster.domain.event.LaunchAuctionEvent; - -public interface LaunchAuctionEventPort { - - void launchAuctionEvent(LaunchAuctionEvent launchAuctionEvent); -} diff --git a/roster/src/main/java/ch/unisg/roster/roster/application/service/DeleteTaskService.java b/roster/src/main/java/ch/unisg/roster/roster/application/service/DeleteTaskService.java index 69036e5..a6b4841 100644 --- a/roster/src/main/java/ch/unisg/roster/roster/application/service/DeleteTaskService.java +++ b/roster/src/main/java/ch/unisg/roster/roster/application/service/DeleteTaskService.java @@ -21,7 +21,7 @@ public class DeleteTaskService implements DeleteTaskUseCase { @Override public boolean deleteTask(DeleteTaskCommand command) { Roster roster = Roster.getInstance(); - return roster.deleteTask(command.getTaskId()); + return roster.deleteTask(command.getTaskId(), command.getTaskType()); } } diff --git a/roster/src/main/java/ch/unisg/roster/roster/application/service/NewTaskService.java b/roster/src/main/java/ch/unisg/roster/roster/application/service/NewTaskService.java index 84cae57..c1aab5c 100644 --- a/roster/src/main/java/ch/unisg/roster/roster/application/service/NewTaskService.java +++ b/roster/src/main/java/ch/unisg/roster/roster/application/service/NewTaskService.java @@ -6,12 +6,10 @@ import org.springframework.stereotype.Component; import ch.unisg.roster.roster.application.port.in.NewTaskCommand; import ch.unisg.roster.roster.application.port.in.NewTaskUseCase; -import ch.unisg.roster.roster.application.port.out.LaunchAuctionEventPort; import ch.unisg.roster.roster.application.port.out.NewTaskEventPort; import ch.unisg.roster.roster.domain.ExecutorRegistry; import ch.unisg.roster.roster.domain.Roster; import ch.unisg.roster.roster.domain.Task; -import ch.unisg.roster.roster.domain.event.LaunchAuctionEvent; import ch.unisg.roster.roster.domain.event.NewTaskEvent; import lombok.RequiredArgsConstructor; @@ -22,8 +20,6 @@ public class NewTaskService implements NewTaskUseCase { private final NewTaskEventPort newTaskEventPort; - private final LaunchAuctionEventPort launchAuctionEventPort; - /** * Checks if we can execute the give task, if yes the task gets added to the task queue and return true. * If the task can not be executed by an internal or auction house executor, the method return false. @@ -35,10 +31,7 @@ public class NewTaskService implements NewTaskUseCase { ExecutorRegistry executorRegistry = ExecutorRegistry.getInstance(); if (!executorRegistry.containsTaskType(command.getTaskType())) { - LaunchAuctionEvent launchAuctionEvent = new LaunchAuctionEvent( command.getTaskUri(), - command.getTaskType()); - launchAuctionEventPort.launchAuctionEvent(launchAuctionEvent); - return true; + return false; } Task task = new Task(command.getTaskID(), command.getTaskType(), command.getInputData()); diff --git a/roster/src/main/java/ch/unisg/roster/roster/domain/Roster.java b/roster/src/main/java/ch/unisg/roster/roster/domain/Roster.java index 1153caa..3893566 100644 --- a/roster/src/main/java/ch/unisg/roster/roster/domain/Roster.java +++ b/roster/src/main/java/ch/unisg/roster/roster/domain/Roster.java @@ -76,14 +76,9 @@ public class Roster { * Deletes a task if it is still in the queue. * @return Whether the task got deleted or not **/ - public boolean deleteTask(String taskID) { + public boolean deleteTask(String taskID, ExecutorType taskType) { logger.log(Level.INFO, "Try to delete task with id {0}", taskID); - for(var listOfTasks : queues.entrySet()){ - if(listOfTasks.getValue().removeIf(task -> task.getTaskID().equals(taskID))){ - return true; - } - } - return false; + return queues.get(taskType.getValue()).removeIf(task -> task.getTaskID().equalsIgnoreCase(taskID)); } public void initialiseRoster(List rosterItemList){ diff --git a/roster/src/main/java/ch/unisg/roster/roster/domain/Task.java b/roster/src/main/java/ch/unisg/roster/roster/domain/Task.java index 25cfcc0..ee30763 100644 --- a/roster/src/main/java/ch/unisg/roster/roster/domain/Task.java +++ b/roster/src/main/java/ch/unisg/roster/roster/domain/Task.java @@ -12,9 +12,6 @@ public class Task { @Getter private ExecutorType taskType; - @Getter - private String taskUri; - @Getter @Setter private String inputData; @@ -43,12 +40,6 @@ public class Task { this.inputData = inputData; } - public Task(String taskID, String taskUri, ExecutorType taskType) { - this.taskID = taskID; - this.taskUri = taskUri; - this.taskType = taskType; - } - public Task() {} } diff --git a/roster/src/main/java/ch/unisg/roster/roster/domain/event/LaunchAuctionEvent.java b/roster/src/main/java/ch/unisg/roster/roster/domain/event/LaunchAuctionEvent.java deleted file mode 100644 index e68f823..0000000 --- a/roster/src/main/java/ch/unisg/roster/roster/domain/event/LaunchAuctionEvent.java +++ /dev/null @@ -1,14 +0,0 @@ -package ch.unisg.roster.roster.domain.event; - -import ch.unisg.roster.roster.domain.valueobject.ExecutorType; - -public class LaunchAuctionEvent { - public final String taskUri; - public final ExecutorType taskType; - - public LaunchAuctionEvent(String taskUri, ExecutorType taskType) { - this.taskUri = taskUri; - this.taskType = taskType; - } - -} diff --git a/roster/src/main/resources/application.properties b/roster/src/main/resources/application.properties index 3edf4b5..d7c1dcb 100644 --- a/roster/src/main/resources/application.properties +++ b/roster/src/main/resources/application.properties @@ -1,9 +1,9 @@ server.port=8082 executor.robot.uri=http://127.0.0.1:8084 executor.computation.uri=http://127.0.0.1:8085 -auction.house.uri=http://127.0.0.1:8086 task.list.uri=http://127.0.0.1:8081 -mqtt.broker.uri=tcp://localhost:1883 +# mqtt.broker.uri=tcp://localhost:1883 +mqtt.broker.uri=tcp://broker.hivemq.com spring.data.mongodb.uri=mongodb://root:password@localhost:27017/ spring.data.mongodb.database=tapas-roster diff --git a/roster/src/test/java/ch/unisg/roster/roster/domain/RosterTest.java b/roster/src/test/java/ch/unisg/roster/roster/domain/RosterTest.java index d2e62e6..7fd1081 100644 --- a/roster/src/test/java/ch/unisg/roster/roster/domain/RosterTest.java +++ b/roster/src/test/java/ch/unisg/roster/roster/domain/RosterTest.java @@ -31,7 +31,7 @@ public class RosterTest { assertThat(task.getTaskType().getValue().toString()).isEqualTo("TEST-TYPE"); assertThat(task.getTaskID()).isEqualTo("TEST-ID"); - boolean empty_queue = roster.deleteTask("TEST-ID"); + boolean empty_queue = roster.deleteTask("TEST-ID", new ExecutorType("TEST-TYPE")); // TODO test that the task was removed from the Queue similar to below --> I don't know if it actually gets deleted or not //assertThat(empty_queue).isEqualTo(true); //assertThat(queues.size()).isEqualTo(0); @@ -44,8 +44,8 @@ public class RosterTest { queues.clear(); roster.addTaskToQueue(new Task("TEST-ID", "TEST-TYPE")); - boolean test = roster.deleteTask("TEST-ID"); - // TODO Fix assert for queue + boolean test = roster.deleteTask("TEST-ID", new ExecutorType("TEST-TYPE")); + assertThat(test).isEqualTo(true); assertThat(queues.size()).isEqualTo(1); } 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 6aa001d..1d72579 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 @@ -3,20 +3,19 @@ package ch.unisg.tapas; import ch.unisg.tapas.auctionhouse.adapter.common.clients.TapasMqttClient; import ch.unisg.tapas.auctionhouse.adapter.in.messaging.mqtt.AuctionEventsMqttDispatcher; import ch.unisg.tapas.auctionhouse.domain.AuctionHouseDiscovery; +import ch.unisg.tapas.auctionhouse.domain.AuctionHouseDiscoveryInformation; import ch.unisg.tapas.auctionhouse.adapter.common.clients.WebSubSubscriber; import ch.unisg.tapas.common.AuctionHouseResourceDirectory; -import ch.unisg.tapas.common.ConfigProperties; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.eclipse.paho.client.mqttv3.MqttException; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.Environment; import java.net.URI; -import java.util.List; +import java.net.URISyntaxException; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -28,35 +27,35 @@ import java.util.concurrent.TimeUnit; public class TapasAuctionHouseApplication { private static final Logger LOGGER = LogManager.getLogger(TapasAuctionHouseApplication.class); - public static String RESOURCE_DIRECTORY = "http://localhost:3500"; public static String DEFAULT_MQTT_BROKER = "tcp://broker.hivemq.com:1883"; private static ConfigurableEnvironment ENVIRONMENT; - public static void main(String[] args) { SpringApplication tapasAuctioneerApp = new SpringApplication(TapasAuctionHouseApplication.class); ENVIRONMENT = tapasAuctioneerApp.run(args).getEnvironment(); - // TODO Set start up of message services with config // We will use these bootstrap methods in Week 6: bootstrapMarketplaceWithWebSub(); - bootstrapMarketplaceWithMqtt(); - - ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); - executor.scheduleAtFixedRate(crawlerRunnable, 30, 30, TimeUnit.SECONDS); + //bootstrapMarketplaceWithMqtt(); } /** * Discovers auction houses and subscribes to WebSub notifications */ private static void bootstrapMarketplaceWithWebSub() { - discoverAuctionHouseEndpoints(); + // discoverAuctionHouseEndpoints(); - // WebSubSubscriber subscriber = new WebSubSubscriber(); + WebSubSubscriber subscriber = new WebSubSubscriber(ENVIRONMENT.getProperty("auction.house.uri")); - // for (String endpoint : auctionHouseEndpoints) { - // subscriber.subscribeToAuctionHouseEndpoint(URI.create(endpoint)); + // for (AuctionHouseDiscoveryInformation endpoint : AuctionHouseDiscovery.getInstance().getAuctionHouseDiscoveryList()) { + // subscriber.subscribeToAuctionHouseEndpoint(endpoint.getWebSubUri().getValue()); // } + try { + subscriber.subscribeToAuctionHouseEndpoint(new URI("http://6b4e-130-82-250-227.ngrok.io/websub-discovery")); + } catch (URISyntaxException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } /** @@ -81,22 +80,26 @@ public class TapasAuctionHouseApplication { } } - private static void discoverAuctionHouseEndpoints() { - AuctionHouseResourceDirectory rd = new AuctionHouseResourceDirectory( - URI.create(RESOURCE_DIRECTORY) - ); + // private static void discoverAuctionHouseEndpoints() { - AuctionHouseDiscovery.getInstance().addAuctionHouseDiscoveryInformation(rd.retrieveAuctionHouseEndpoints()); - } + // AuctionHouseResourceDirectory rd = new AuctionHouseResourceDirectory( + // URI.create(ENVIRONMENT.getProperty("discovery.endpoint.uri")) + // ); + + // AuctionHouseDiscovery.getInstance().addAuctionHouseDiscoveryInformation(rd.retrieveAuctionHouseEndpoints()); + + // ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); + // executor.scheduleAtFixedRate(crawlerRunnable, 300, 300, TimeUnit.SECONDS); + // } - private static Runnable crawlerRunnable = new Runnable() { - public void run() { - AuctionHouseResourceDirectory rd = new AuctionHouseResourceDirectory( - URI.create(RESOURCE_DIRECTORY) - ); + // private static Runnable crawlerRunnable = new Runnable() { + // public void run() { + // AuctionHouseResourceDirectory rd = new AuctionHouseResourceDirectory( + // URI.create(ENVIRONMENT.getProperty("discovery.endpoint.uri")) + // ); - AuctionHouseDiscovery.getInstance().addAuctionHouseDiscoveryInformation(rd.retrieveAuctionHouseEndpoints()); - } - }; + // AuctionHouseDiscovery.getInstance().addAuctionHouseDiscoveryInformation(rd.retrieveAuctionHouseEndpoints()); + // } + // }; } 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 5b3fc32..ea74603 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 @@ -2,9 +2,11 @@ package ch.unisg.tapas.auctionhouse.adapter.common.clients; import java.io.IOException; import java.net.URI; +import java.net.URISyntaxException; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; +import java.util.HashMap; import java.util.logging.Level; import java.util.logging.Logger; @@ -18,25 +20,24 @@ import org.springframework.http.HttpStatus; */ public class WebSubSubscriber { - // TODO get this somehow from properties file. But on clue how to do this with static variables - static String WEBSUB_HUB_ENDPOINT = "http://localhost:3000"; - static String AUCTION_HOUSE_ENDPOINT = "http://localhost:8086"; - Logger logger = Logger.getLogger(WebSubSubscriber.class.getName()); - public void subscribeToAuctionHouseEndpoint(URI endpoint) { - // TODO decide with other groups about auction house endpoint uri to discover websub topics - // and replace the hardcoded one with it - String topic = discoverWebSubTopic("http://localhost:3100/websub"); + String AUCTION_HOUSE_ENDPOINT; - if (topic == null) { + public WebSubSubscriber(String AUCTION_HOUSE_ENDPOINT) { + this.AUCTION_HOUSE_ENDPOINT = AUCTION_HOUSE_ENDPOINT; + } + + public void subscribeToAuctionHouseEndpoint(URI endpoint) { + + HashMap links = discoverWebSubTopic(endpoint); + + if (links.isEmpty()) { return; } - subscribeToWebSub(topic); + subscribeToWebSub(links.get("hub"), links.get("self")); - // 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. // 2. Send a subscription request to the discovered WebSub hub to subscribe to events relevant @@ -52,23 +53,30 @@ public class WebSubSubscriber { // - the implementation notes of the WebSub hub you are using to distribute events } - private String discoverWebSubTopic(String endpoint) { + private HashMap discoverWebSubTopic(URI endpoint) { HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() - .uri(URI.create(endpoint)) + .uri(endpoint) .header("Content-Type", "application/json") .GET() .build(); + HashMap links = new HashMap<>(); try { HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); if (response.statusCode() == HttpStatus.OK.value()) { - // TODO decide with other groups about response structure and replace the hardcoded - // uri with response uri - JSONObject jsonObject = new JSONObject(response.body()); - System.out.println(jsonObject); - return jsonObject.getString("topic"); + for (String link : response.headers().allValues("link")) { + if (link.contains("rel=\"hub\"")) { + String hub = link.split(">")[0]; + links.put("hub", hub.substring(1)); + } else if(link.contains("rel=\"self\"")) { + String self = link.split(">")[0]; + links.put("self", self.substring(1)); + } + System.out.println(link); + } + // TODO check for HTML tags second if links are not present in headers } else { logger.log(Level.SEVERE, "Could not find a websub uri"); } @@ -78,24 +86,26 @@ public class WebSubSubscriber { } catch (IOException e) { logger.log(Level.SEVERE, e.getLocalizedMessage(), e); } - return null; + return links; } - private void subscribeToWebSub(String topic) { + private void subscribeToWebSub(String hub, String topic) { HttpClient client = HttpClient.newHttpClient(); - String body = new JSONObject() - .put("hub.callback", AUCTION_HOUSE_ENDPOINT + "/auction-started") - .put("hub.mode", "subscribe") - .put("hub.topic", topic) - .put("hub.ws", false) - .toString(); - + URI hubURI; + try { + hubURI = new URI(hub); + } catch (URISyntaxException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return; + } HttpRequest request = HttpRequest.newBuilder() - .uri(URI.create(WEBSUB_HUB_ENDPOINT)) - .header("Content-Type", "application/json") - .POST(HttpRequest.BodyPublishers.ofString(body)) + .uri(hubURI) + .header("Content-Type", "application/x-www-form-urlencoded") + .POST(HttpRequest.BodyPublishers.ofString("hub.mode=subscribe&hub.callback=" + AUCTION_HOUSE_ENDPOINT + + "/auction-started/74c72c7f-2739-4124-aa35-a3225171a97c" + "&hub.topic=" + topic)) .build(); diff --git a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/common/formats/AuctionJsonRepresentation.java b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/common/formats/AuctionJsonRepresentation.java index 757c8c8..3004755 100644 --- a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/common/formats/AuctionJsonRepresentation.java +++ b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/common/formats/AuctionJsonRepresentation.java @@ -3,12 +3,16 @@ package ch.unisg.tapas.auctionhouse.adapter.common.formats; import ch.unisg.tapas.auctionhouse.domain.Auction; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.Getter; import lombok.Setter; +import java.net.URI; import java.sql.Timestamp; +import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.Date; /** * Used to expose a representation of the state of an auction through an interface. This class is @@ -60,4 +64,28 @@ public class AuctionJsonRepresentation { mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); return mapper.writeValueAsString(representation); } + + public static Auction deserialize(String auctionJson) throws JsonProcessingException { + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode jsonNode = objectMapper.readTree(auctionJson); + Auction.AuctionId auctionId = new Auction.AuctionId(jsonNode.get("auctionId").asText()); + Auction.AuctionHouseUri auctionHouseUri = new Auction.AuctionHouseUri(URI.create(jsonNode.get("auctionHouseUri").asText())); + Auction.AuctionedTaskUri taskUri = new Auction.AuctionedTaskUri(URI.create(jsonNode.get("taskUri").asText())); + Auction.AuctionedTaskType taskType = new Auction.AuctionedTaskType(jsonNode.get("taskType").asText()); + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + Date parsedDate; + try { + parsedDate = dateFormat.parse(jsonNode.get("deadline").toString()); + Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime()); + Auction.AuctionDeadline deadline = new Auction.AuctionDeadline(timestamp); + + Auction auction = new Auction(auctionId, auctionHouseUri, taskUri, taskType, deadline); + return auction; + } catch (ParseException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + + } } 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 4f67dad..520e0a5 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,20 +1,31 @@ package ch.unisg.tapas.auctionhouse.adapter.in.messaging.websub; import ch.unisg.tapas.auctionhouse.adapter.common.formats.AuctionJsonRepresentation; +import ch.unisg.tapas.auctionhouse.adapter.common.formats.BidJsonRepresentation; 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.Bid; 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.io.IOException; import java.net.URI; import java.net.URISyntaxException; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.security.Timestamp; import java.util.Collection; +import java.util.Date; + +import com.fasterxml.jackson.core.JsonProcessingException; import org.json.JSONArray; +import org.json.JSONObject; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -34,19 +45,88 @@ public class AuctionStartedEventListenerWebSubAdapter { * @return 200 OK * @throws URISyntaxException **/ - @PostMapping(path = "/auction-started") - public ResponseEntity handleExecutorAddedEvent(@RequestBody Collection payload) throws URISyntaxException { + // TODO generate a new capability ID instead of using a hardcoded one. + @PostMapping(path = "/auction-started/74c72c7f-2739-4124-aa35-a3225171a97c") + public ResponseEntity handleExecutorAddedEvent(@RequestBody String payload) throws URISyntaxException { - 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())) - )); - } + System.out.println("new auctions :O"); + System.out.println(payload); + + + JSONArray auctions = new JSONArray(payload); + if (auctions.length() > 0) { + JSONObject auction = auctions.getJSONObject(0); + System.out.print(auction); + // try { + // System.out.println(auction.getString("deadline")); + // System.out.println(AuctionJsonRepresentation.deserialize(auction.toString())); + + // auctionStartedHandler.handleAuctionStartedEvent( + // new AuctionStartedEvent(AuctionJsonRepresentation.deserialize(auction.toString()))); + // } catch (JsonProcessingException e) { + // // TODO Auto-generated catch block + // e.printStackTrace(); + // } + + + // String auctionHouseURI = "https://tapas-auction-house.86-119-35-40.nip.io/"; + String auctionHouseURI = "http://b311-130-82-247-153.eu.ngrok.io"; + + // String taskListURI = "https://tapas-tasks.86-119-35-40.nip.io"; + String taskListURI = "http://c64f-130-82-247-153.ngrok.io"; + + // TODO Sanitize URIs + String auctionId = auction.getString("auctionId"); + String auctionHouseUri = auction.getString("auctionHouseUri"); + String taskUri = auction.getString("taskUri"); + String taskType = auction.getString("taskType"); + String deadline = auction.getString("deadline"); + + var bid = new Bid( + new Auction.AuctionId(auctionId), + new Bid.BidderName("Group-1"), + new Bid.BidderAuctionHouseUri(URI.create(auctionHouseURI)), + new Bid.BidderTaskListUri(URI.create(taskListURI)) + ); + + String body; + try { + body = BidJsonRepresentation.serialize(bid); + //LOGGER.info(body); + var postURI = URI.create(auctionHouseUri + "/bid"); + HttpRequest postRequest = HttpRequest.newBuilder() + .uri(postURI) + .header("Content-Type", BidJsonRepresentation.MEDIA_TYPE) + .POST(HttpRequest.BodyPublishers.ofString(body)) + .build(); + + HttpClient client = HttpClient.newHttpClient(); + var postResponse = client.send(postRequest, HttpResponse.BodyHandlers.ofString()); + } catch (JsonProcessingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + //LOGGER.info(postResponse.statusCode()); + }; + + // for (JSONObject auction : auctions) { + // 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/DiscoverWebSubAdapter.java b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/in/messaging/websub/DiscoverWebSubAdapter.java new file mode 100644 index 0000000..f279c43 --- /dev/null +++ b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/in/messaging/websub/DiscoverWebSubAdapter.java @@ -0,0 +1,33 @@ +package ch.unisg.tapas.auctionhouse.adapter.in.messaging.websub; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpHeaders; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +/** + * This class is a template for handling auction started events received via WebSub + */ +@RestController +public class DiscoverWebSubAdapter { + + @Value("${websub.hub.uri}") + private String webSubHubUri; + + @Value("${auction.house.uri}") + private String auctionHouseUri; + + /** + * Controller to discover our websub topic + * @return 200 OK + **/ + @GetMapping(path = "/websub/auctions") + public ResponseEntity handleDiscoverWebSubAuction() { + + HttpHeaders header = new HttpHeaders(); + header.add("link", "<" + auctionHouseUri + "/auctions/>; rel=\"self\""); + header.add("link", "<" + webSubHubUri + ">; rel=\"hub\""); + + return ResponseEntity.ok().headers(header).body(""); + } +} 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 7bfb450..e3a9da9 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 @@ -13,21 +13,9 @@ import org.springframework.web.bind.annotation.*; @RestController public class ValidateIntentWebSubAdapter { - @Value("${application.environment}") - private String environment; - - @GetMapping(path = "/auction-started") + // TODO generate a new capability ID instead of using a hardcoded one. + @GetMapping(path = "/auction-started/74c72c7f-2739-4124-aa35-a3225171a97c") public ResponseEntity validateIntent(@RequestParam("hub.challenge") String challenge) { - // Different implementation depending on local development or production - if (environment.equalsIgnoreCase("development")) { - HttpHeaders headers = new HttpHeaders(); - headers.add("Content-Type", "application/json"); - String body = new JSONObject() - .put("hub.challenge", challenge) - .toString(); - return new ResponseEntity<>(body, headers, HttpStatus.OK); - } else { - return new ResponseEntity<>(challenge, HttpStatus.OK); - } + return new ResponseEntity<>(challenge, HttpStatus.OK); } } diff --git a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/out/messaging/mqtt/PublishAuctionStartedEventMqttAdapter.java b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/out/messaging/mqtt/PublishAuctionStartedEventMqttAdapter.java index a041b4f..4472519 100644 --- a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/out/messaging/mqtt/PublishAuctionStartedEventMqttAdapter.java +++ b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/out/messaging/mqtt/PublishAuctionStartedEventMqttAdapter.java @@ -15,7 +15,7 @@ import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; @Component -@Primary +// @Primary public class PublishAuctionStartedEventMqttAdapter implements AuctionStartedEventPort { private static final Logger LOGGER = LogManager.getLogger(PublishAuctionStartedEventMqttAdapter.class); diff --git a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/out/messaging/websub/PublishAuctionStartedEventWebSubAdapter.java b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/out/messaging/websub/PublishAuctionStartedEventWebSubAdapter.java index 228f43b..02aeb75 100644 --- a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/out/messaging/websub/PublishAuctionStartedEventWebSubAdapter.java +++ b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/out/messaging/websub/PublishAuctionStartedEventWebSubAdapter.java @@ -1,41 +1,27 @@ package ch.unisg.tapas.auctionhouse.adapter.out.messaging.websub; import ch.unisg.tapas.auctionhouse.application.port.out.AuctionStartedEventPort; -import ch.unisg.tapas.auctionhouse.domain.Auction; import ch.unisg.tapas.auctionhouse.domain.AuctionStartedEvent; -import ch.unisg.tapas.common.ConfigProperties; -import org.json.JSONObject; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Primary; -import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; import java.io.IOException; import java.net.URI; -import java.net.URLEncoder; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; -import java.nio.charset.StandardCharsets; -import java.util.HashMap; -import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; -import java.util.stream.Collectors; /** * This class is a template for publishing auction started events via WebSub. */ @Component +@Primary public class PublishAuctionStartedEventWebSubAdapter implements AuctionStartedEventPort { - // You can use this object to retrieve properties from application.properties, e.g. the - // WebSub hub publish endpoint, etc. - @Autowired - private ConfigProperties config; - - @Value("${auctionhouse.uri}") + @Value("${auction.house.uri}") private String auctionHouseUri; @Value("${websub.hub.uri}") @@ -47,21 +33,20 @@ public class PublishAuctionStartedEventWebSubAdapter implements AuctionStartedEv public void publishAuctionStartedEvent(AuctionStartedEvent event) { HttpClient client = HttpClient.newHttpClient(); - String body = new JSONObject() - .put("hub.url", auctionHouseUri + "/auctions") - .put("hub.mode", "publish") - .toString(); + String body = "hub.url=" + auctionHouseUri + "/auctions/&hub.mode=publish"; + logger.info("Auctions updated:" + body); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(webSubHubUri)) - .header("Content-Type", "application/json") + .header("Content-Type", "application/x-www-form-urlencoded") .POST(HttpRequest.BodyPublishers.ofString(body)) .build(); try { client.send(request, HttpResponse.BodyHandlers.ofString()); + logger.info("WEBSUB send successfuly"); } catch (InterruptedException e) { logger.log(Level.SEVERE, e.getLocalizedMessage(), e); Thread.currentThread().interrupt(); diff --git a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/out/web/AuctionWonEventHttpAdapter.java b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/out/web/AuctionWonEventHttpAdapter.java index ec04b2b..066a246 100644 --- a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/out/web/AuctionWonEventHttpAdapter.java +++ b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/out/web/AuctionWonEventHttpAdapter.java @@ -43,6 +43,7 @@ public class AuctionWonEventHttpAdapter implements AuctionWonEventPort { .uri(auction.get().getTaskUri().getValue()) .GET() .build(); + var response = client.send(request, HttpResponse.BodyHandlers.ofString()); LOGGER.info(response.body()); JSONObject responseBody = new JSONObject(response.body()); diff --git a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/domain/AuctionHouseDiscovery.java b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/domain/AuctionHouseDiscovery.java index 5088ff1..c234b64 100644 --- a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/domain/AuctionHouseDiscovery.java +++ b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/domain/AuctionHouseDiscovery.java @@ -20,6 +20,9 @@ public class AuctionHouseDiscovery { } }; + // TODO load from config + // static String AUCTION_HOUSE_URI = "http://localhost:8086"; + @Getter private List auctionHouseDiscoveryList = new ArrayList<>() { }; @@ -29,7 +32,7 @@ public class AuctionHouseDiscovery { // Add our information to list auctionHouseDiscoveryList.add(new AuctionHouseDiscoveryInformation( new AuctionHouseDiscoveryInformation.AuctionHouseUri(new URI("http://localhost:8086")), - new AuctionHouseDiscoveryInformation.WebSubUri(new URI("http://localhost:8086/websub")), + new AuctionHouseDiscoveryInformation.WebSubUri(new URI("http://localhost:8086/websub/auctions")), new AuctionHouseDiscoveryInformation.TaskTypes(tasktypes), new AuctionHouseDiscoveryInformation.TimeStamp(new Timestamp(new Date().getTime())), new AuctionHouseDiscoveryInformation.GroupName("Group 1") diff --git a/tapas-auction-house/src/main/resources/application.properties b/tapas-auction-house/src/main/resources/application.properties index dd9735d..3546905 100644 --- a/tapas-auction-house/src/main/resources/application.properties +++ b/tapas-auction-house/src/main/resources/application.properties @@ -10,6 +10,8 @@ auction.house.uri=https://tapas-auction-house.86-119-35-40.nip.io tasks.list.uri=http://localhost:8081 application.environment=development -auctionhouse.uri=http://localhost:8086 -websub.hub.uri=http://localhost:3000 -mqtt.broker.uri=tcp://localhost:1883 +websub.hub.uri=https://pubsubhubbub.appspot.com +mqtt.broker.uri=tcp://broker.hivemq.com +# mqtt.broker.uri=tcp://localhost:1883 + +discovery.endpoint.uri=http://localhost:3500 diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/DeleteTaskWebController.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/DeleteTaskWebController.java index 11c2442..ef79e6a 100644 --- a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/DeleteTaskWebController.java +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/DeleteTaskWebController.java @@ -25,7 +25,6 @@ public class DeleteTaskWebController { this.deleteClassUseCase = deleteClassUseCase; } - // TODO change to DELETE and why are we using task URI here? @PostMapping(path="/tasks/deleteTask", consumes = {TaskJsonRepresentation.MEDIA_TYPE}) public ResponseEntity deleteTask (@RequestBody Task task){ try { @@ -36,7 +35,6 @@ public class DeleteTaskWebController { // Check if the task with the given identifier exists if (deleteATask.isEmpty()) { // If not, through a 404 Not Found status code - // TODO is 404 the best here? throw new ResponseStatusException(HttpStatus.NOT_FOUND); } diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/out/web/CanTaskBeDeletedWebAdapter.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/out/web/CanTaskBeDeletedWebAdapter.java index 2b5c495..a69f2e5 100644 --- a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/out/web/CanTaskBeDeletedWebAdapter.java +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/out/web/CanTaskBeDeletedWebAdapter.java @@ -8,7 +8,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Primary; -import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; import java.io.IOException; @@ -26,24 +25,36 @@ public class CanTaskBeDeletedWebAdapter implements CanTaskBeDeletedPort { String server; @Override - public boolean canTaskBeDeletedEvent(DeleteTaskEvent event){ + public void canTaskBeDeletedEvent(DeleteTaskEvent event){ + + var values = new HashMap<> () {{ + put("taskId", event.taskId); + put("taskUri", event.taskUri); + }}; + + var objectMapper = new ObjectMapper(); + String requestBody = null; + try { + requestBody = objectMapper.writeValueAsString(values); + } catch (JsonProcessingException e){ + e.printStackTrace(); + } + //Todo: Question: How do we include the URI from the DeleteTaskEvent? Do we even need it? HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() - .uri(URI.create(server+"$/task/" + event.taskId)) + .uri(URI.create(server+"task")) .header("Content-Type", "application/task+json") - .DELETE() + .POST(HttpRequest.BodyPublishers.ofString(requestBody)) .build(); + //Todo: The following parameters probably need to be changed to get the right error code try { HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); - return response.statusCode() == HttpStatus.OK.value(); } catch (IOException e){ e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } - - return false; } } diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/port/out/CanTaskBeDeletedPort.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/port/out/CanTaskBeDeletedPort.java index 5a0707f..67bde16 100644 --- a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/port/out/CanTaskBeDeletedPort.java +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/port/out/CanTaskBeDeletedPort.java @@ -3,5 +3,5 @@ package ch.unisg.tapastasks.tasks.application.port.out; import ch.unisg.tapastasks.tasks.domain.DeleteTaskEvent; public interface CanTaskBeDeletedPort { - boolean canTaskBeDeletedEvent(DeleteTaskEvent event); + void canTaskBeDeletedEvent(DeleteTaskEvent event); } diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/DeleteTaskService.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/DeleteTaskService.java index c3d392c..35685a3 100644 --- a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/DeleteTaskService.java +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/DeleteTaskService.java @@ -3,8 +3,6 @@ package ch.unisg.tapastasks.tasks.application.service; import ch.unisg.tapastasks.tasks.application.port.in.DeleteTaskCommand; import ch.unisg.tapastasks.tasks.application.port.in.DeleteTaskUseCase; -import ch.unisg.tapastasks.tasks.application.port.out.CanTaskBeDeletedPort; -import ch.unisg.tapastasks.tasks.domain.DeleteTaskEvent; import ch.unisg.tapastasks.tasks.domain.Task; import ch.unisg.tapastasks.tasks.domain.TaskList; import jdk.jshell.spi.ExecutionControl; @@ -19,21 +17,17 @@ import java.util.Optional; @Transactional public class DeleteTaskService implements DeleteTaskUseCase { - private final CanTaskBeDeletedPort canTaskBeDeletedPort; - @Override public Optional deleteTask(DeleteTaskCommand command){ TaskList taskList = TaskList.getTapasTaskList(); Optional updatedTask = taskList.retrieveTaskById(command.getTaskId()); - - if(updatedTask.isPresent() && updatedTask.get().getTaskStatus().getValue().equals(Task.Status.OPEN)){ - // If task exists, and it is open then we try deleting it from the roster - if(canTaskBeDeletedPort.canTaskBeDeletedEvent(new DeleteTaskEvent(command.getTaskId().getValue(), command.getTaskUri().getValue()))){ - return taskList.deleteTaskById(command.getTaskId()); - } + Task newTask = updatedTask.get(); + // TODO: Fill in the right condition into the if-statement and the else-statement + if (true){ + return taskList.deleteTaskById(command.getTaskId()); } - + // TODO Handle with a return message return Optional.empty(); } }