diff --git a/Collections/src/main/java/com/team3/collections/Model/GatewayWork.java b/Collections/src/main/java/com/team3/collections/Model/GatewayWork.java index 69eaed9..3f9e5df 100644 --- a/Collections/src/main/java/com/team3/collections/Model/GatewayWork.java +++ b/Collections/src/main/java/com/team3/collections/Model/GatewayWork.java @@ -140,6 +140,9 @@ public ArrayList getNameApiFunctions() throws ClassNotFoundException { if(last == -1) continue; str = str.substring(0,last); + if(str.charAt(0) != '/'){ + str = "/" + str; + } arrayList.add(new Document("path", rm+str).append("api_version", "v1")); } } diff --git a/Collections/src/main/resources/application.properties b/Collections/src/main/resources/application.properties index 9b11452..e54adf6 100644 --- a/Collections/src/main/resources/application.properties +++ b/Collections/src/main/resources/application.properties @@ -5,8 +5,8 @@ mongodb.collectionName=collections server.port=8081 server.host=localhost -service.name=CONTENT MANAGEMENT -service.version=0.1.0beta +service.name=Collections +service.version=1.0.0 gateway.host=http://localhost gateway.port=8085 diff --git a/Rating/src/main/java/com/team3/rating/Application.java b/Rating/src/main/java/com/team3/rating/Application.java index 9430c9f..afc54a9 100644 --- a/Rating/src/main/java/com/team3/rating/Application.java +++ b/Rating/src/main/java/com/team3/rating/Application.java @@ -1,13 +1,18 @@ package com.team3.rating; +import com.team3.rating.Model.GatewayWork; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ApplicationContext; @SpringBootApplication public class Application { public static void main(String[] args) { - SpringApplication.run(Application.class, args); + ApplicationContext ctx = SpringApplication.run(Application.class, args); + + GatewayWork printThread1 = (GatewayWork) ctx.getBean("gatewayWork"); + printThread1.start(); } } diff --git a/Rating/src/main/java/com/team3/rating/Model/GatewayWork.java b/Rating/src/main/java/com/team3/rating/Model/GatewayWork.java new file mode 100644 index 0000000..d44d61b --- /dev/null +++ b/Rating/src/main/java/com/team3/rating/Model/GatewayWork.java @@ -0,0 +1,181 @@ +package com.team3.rating.Model; + +import org.bson.Document; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Scope; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Component; +import org.springframework.web.client.HttpServerErrorException; +import org.springframework.web.client.RestTemplate; + +import java.io.UnsupportedEncodingException; +import java.lang.annotation.Annotation; +import java.lang.reflect.Method; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Set; + +@Component +@Scope("prototype") +public class GatewayWork extends Thread { + @Value("${gateway.host}") + private String gatewayHost; //Host service Gateway + + @Value("${gateway.port}") + private String gatewayPort; //Port service Gateway + + @Value("${server.host}") + private String host; + + @Value("${server.port}") + private String port; + + @Value("${service.name}") + private String serviceName; + + @Value("${service.version}") + private String serviceVersion; + + + private String instanceId; + private int pingInterval; + + @Autowired + private RestTemplate restTemplate; + + @Override + public void run() { + + while (true) { + try { + registration(); //To register for the service + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + + try { + ready(); + } catch (HttpServerErrorException e) { + e.printStackTrace(); + } + + try { + ping(); //Ping + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + public void ping() throws InterruptedException { + String url = gatewayHost + ":" + gatewayPort + "/gateway/ping/" + instanceId; + + while (true) { + ResponseEntity result = restTemplate.getForEntity(url, String.class); + + //If an instance with this ID has not published the API + if (!(result.getStatusCode().equals(HttpStatus.OK))) + return; //Then go to publishing + + Thread.sleep(pingInterval); + } + } + + public void registration() throws UnsupportedEncodingException, ClassNotFoundException { + //Creating a request address + String url = gatewayHost + ":" + gatewayPort + "/gateway/publish"; + URI uri = null; + try { + uri = new URI(url); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + + //Add address, port, name_service, version_service + Document requestBody = readConfigService(); + + //Add api + requestBody.append("api", getNameApiFunctions()); + + //Registering the service + ResponseEntity result = restTemplate.postForEntity(uri, requestBody, Document.class); + instanceId = result.getBody().getString("instance_id"); + pingInterval = (int) result.getBody().getInteger("ping_interval"); + + } + + public void ready() { + String url = gatewayHost + ":" + gatewayPort + "/gateway/ready/" + instanceId; + + ResponseEntity result = restTemplate.getForEntity(url, String.class); + } + + public ArrayList getNameApiFunctions() throws ClassNotFoundException { + ArrayList arrayList = new ArrayList<>(); + + Class clazz = Class.forName("com.team3.rating.Controller.Controller"); + + String rm = ""; + for(Annotation an : clazz.getAnnotations()){ + rm = an.toString(); + int ix = rm.indexOf("RequestMapping"); + if(ix != -1){ + rm = rm.substring(ix+"RequestMapping".length()); + int first = rm.indexOf("value={") + "value={".length() + 1; + rm = rm.substring(first); + int last = rm.indexOf("\"},"); + rm = rm.substring(0,last); + break; + } + } + rm = "/" + rm; + + for(Method method :clazz.getMethods()){ + for(Annotation annotation :method.getDeclaredAnnotations()){ + + String str = annotation.toString(); + int first = annotation.toString().indexOf("value={") + "value={".length() + 1; + if(first != -1) { + str = str.substring(first); + int last = str.indexOf("\"},"); + if(last == -1) + continue; + str = str.substring(0,last); + if(str.charAt(0) != '/'){ + str = "/" + str; + } + arrayList.add(new Document("path", rm+str).append("api_version", "v1")); + } + } + } + + Set foo = new HashSet(arrayList); + ArrayList mainList = new ArrayList(); + mainList.addAll(foo); + + return mainList; + } + + public Document readConfigService() { + Document requestBody = new Document(); + requestBody.append("address", gatewayHost); + requestBody.append("port", port); + requestBody.append("name_service", serviceName); + requestBody.append("version_service", serviceVersion); + return requestBody; + } + + public void setGatewayHost(String gatewayHost) { + this.gatewayHost = gatewayHost; + } + + public void setGatewayPort(String gatewayPort) { + this.gatewayPort = gatewayPort; + } +} diff --git a/Rating/src/main/resources/application.properties b/Rating/src/main/resources/application.properties index 93d320c..be0c5b5 100644 --- a/Rating/src/main/resources/application.properties +++ b/Rating/src/main/resources/application.properties @@ -3,3 +3,11 @@ mongodb.port=27017 mongodb.databaseName=mydb mongodb.ratingsCollectionName=ratings mongodb.averageRatingsCollectionName=averageRatings + +server.port=8082 +server.host=localhost +service.name=Rating +service.version=1.0.0 + +gateway.host=http://localhost +gateway.port=8085 \ No newline at end of file