From fd96876544f95d062c498162abda106585375dea Mon Sep 17 00:00:00 2001 From: "kevin.shehu" Date: Mon, 21 Dec 2020 22:26:47 +0100 Subject: [PATCH] FIX : SecurityToken.java ADD : ApiCall.java ADD : ScheduledTask.java --- .idea/codeStyles/Project.xml | 5 - .idea/jarRepositories.xml | 20 +++ .idea/misc.xml | 5 +- .idea/uiDesigner.xml | 124 ++++++++++++++++++ README.md | 1 + pom.xml | 16 ++- src/main/java/ApiCall.java | 81 ++++++++++++ src/main/java/Data/city-distances.json | 28 ---- src/main/java/Data/securityToken.json | 0 src/main/java/JSON2RDF/JSON2RDF.java | 40 +++--- src/main/java/ScheduledTask.java | 29 ++++ .../ApiCallYookr.java => SecurityToken.java} | 32 ++--- src/main/java/main.java | 47 ++----- 13 files changed, 323 insertions(+), 105 deletions(-) delete mode 100644 .idea/codeStyles/Project.xml create mode 100644 .idea/jarRepositories.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 src/main/java/ApiCall.java delete mode 100644 src/main/java/Data/city-distances.json create mode 100644 src/main/java/Data/securityToken.json create mode 100644 src/main/java/ScheduledTask.java rename src/main/java/{JSON2RDF/ApiCallYookr.java => SecurityToken.java} (56%) diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml deleted file mode 100644 index 81b59bb..0000000 --- a/.idea/codeStyles/Project.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 4b661a5..3fe5f5c 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,9 @@ + + - + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 91575a4..9511c33 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ The list of inferred active goals is offered to other services of the system. **Rules (General structure, if premises then consequence)** 1. Explicit given by expert (before and during runtime) 2. Trained through AI + **Goals** 1. Actions that should be taken by the user to be successful. 2. Goals can be active and inactive. diff --git a/pom.xml b/pom.xml index 7199704..12d6e77 100644 --- a/pom.xml +++ b/pom.xml @@ -13,8 +13,8 @@ org.apache.maven.plugins maven-compiler-plugin - 7 - 7 + 11 + 11 @@ -47,6 +47,18 @@ javax.json 1.1.4 + + com.googlecode.json-simple + json-simple + 1.1.1 + + + + org.json + json + 20201115 + + diff --git a/src/main/java/ApiCall.java b/src/main/java/ApiCall.java new file mode 100644 index 0000000..0b7c520 --- /dev/null +++ b/src/main/java/ApiCall.java @@ -0,0 +1,81 @@ + + + +import org.eclipse.jetty.http.HttpHeader; +import org.json.JSONObject; + +import java.net.PasswordAuthentication; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.http.HttpClient; +import java.net.http.HttpHeaders; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + + +public class ApiCall { + private static String token ="0"; + private static String oldId="1"; + + public static String FetchDataFromApi(String extension) throws InterruptedException, ExecutionException, TimeoutException { + + final String username = "kevin.shehu@hochschule-rhein-waal.de"; + final String password = "DK7SxFkGJgnLhnU3"; + String baseURL = "https://api.dev.whysor.com/"; + String outputToken = null; + + if (token.equals(oldId)){ + //do nothing + } + else { + SecurityToken securityToken = new SecurityToken(); + try { + outputToken = securityToken.OnCallMethod(username,password); + JSONObject obj = new JSONObject(outputToken); + token = obj.getString("id"); + oldId = token; + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (ExecutionException e) { + e.printStackTrace(); + } catch (TimeoutException e) { + e.printStackTrace(); + } + } + + + + + System.out.println(token); + var client = HttpClient.newBuilder() + .version(HttpClient.Version.HTTP_1_1) + .followRedirects(HttpClient.Redirect.ALWAYS) + .build(); + HttpRequest request = null; + + try { + + request = HttpRequest.newBuilder(new URI(baseURL+extension)) + .POST(HttpRequest.BodyPublishers.ofString(extension)) + .setHeader("Authorization" , token) + .setHeader("Content-Type", "application/json") + .setHeader("Accept", "application/json") + .build(); + + } catch (URISyntaxException e) { + e.printStackTrace(); + } + + CompletableFuture> response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString()); + String result = response.thenApply(HttpResponse::body).get(5, TimeUnit.SECONDS); + return result; + + + } + + +} diff --git a/src/main/java/Data/city-distances.json b/src/main/java/Data/city-distances.json deleted file mode 100644 index 957f72d..0000000 --- a/src/main/java/Data/city-distances.json +++ /dev/null @@ -1,28 +0,0 @@ -{ - "desc" : "Distances between several cities, in kilometers.", - "updated" : "2014-02-04T18:50:45", - "uptodate": true, - "author" : null, - "cities" : { - "Brussels": [ - {"to": "London", "distance": 322}, - {"to": "Paris", "distance": 265}, - {"to": "Amsterdam", "distance": 173} - ], - "London": [ - {"to": "Brussels", "distance": 322}, - {"to": "Paris", "distance": 344}, - {"to": "Amsterdam", "distance": 358} - ], - "Paris": [ - {"to": "Brussels", "distance": 265}, - {"to": "London", "distance": 344}, - {"to": "Amsterdam", "distance": 431} - ], - "Amsterdam": [ - {"to": "Brussels", "distance": 173}, - {"to": "London", "distance": 358}, - {"to": "Paris", "distance": 431} - ] - } -} diff --git a/src/main/java/Data/securityToken.json b/src/main/java/Data/securityToken.json new file mode 100644 index 0000000..e69de29 diff --git a/src/main/java/JSON2RDF/JSON2RDF.java b/src/main/java/JSON2RDF/JSON2RDF.java index 00022c2..c525046 100644 --- a/src/main/java/JSON2RDF/JSON2RDF.java +++ b/src/main/java/JSON2RDF/JSON2RDF.java @@ -1,17 +1,16 @@ package JSON2RDF; -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.Reader; +import java.io.*; import java.net.URI; +import java.net.URISyntaxException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; + +import org.json.simple.JSONArray; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + import org.apache.jena.riot.system.StreamRDF; import org.apache.jena.riot.system.StreamRDFLib; import picocli.CommandLine; @@ -21,14 +20,24 @@ public class JSON2RDF { private final InputStream jsonIn; private final OutputStream rdfOut; - @CommandLine.Parameters(paramLabel = "base", index = "0", description = "Base URI of the RDF output data\nExample: https://localhost/") + + @CommandLine.Parameters(paramLabel = "https://localhost/" , index = "0", description = "Base URI of the RDF output data") private URI baseURI; + { + try { + baseURI = new URI("https://localhost/"); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + } + +// @CommandLine.Parameters(paramLabel = "D:\\WORK\\GGD\\src\\main\\java\\Data", description = "json file") @CommandLine.Option(names = { "--input-charset" }, description = "Input charset (default: ${DEFAULT-VALUE})") - private Charset inputCharset = StandardCharsets.UTF_8; + private final Charset inputCharset = StandardCharsets.UTF_8; @CommandLine.Option(names = { "--output-charset" }, description = "Output charset (default: ${DEFAULT-VALUE})") - private Charset outputCharset = StandardCharsets.UTF_8; + private final Charset outputCharset = StandardCharsets.UTF_8; public static void main(String[] args) throws IOException { @@ -46,14 +55,15 @@ public class JSON2RDF { } } - public JSON2RDF(InputStream csvIn, OutputStream rdfOut) - { + public JSON2RDF(InputStream csvIn, OutputStream rdfOut) { + + this.jsonIn = csvIn; this.rdfOut = rdfOut; } - public void convert() throws IOException - { + + public void convert() throws IOException { if (jsonIn.available() == 0) throw new IllegalStateException("JSON input not provided"); try (Reader reader = new BufferedReader(new InputStreamReader(jsonIn, inputCharset))) diff --git a/src/main/java/ScheduledTask.java b/src/main/java/ScheduledTask.java new file mode 100644 index 0000000..4d37aef --- /dev/null +++ b/src/main/java/ScheduledTask.java @@ -0,0 +1,29 @@ +import java.util.TimerTask; +import java.util.Date; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; + +// Create a class extends with TimerTask +public class ScheduledTask extends TimerTask { + + Date now; // to display current time + + // Add your task here + public void run() { + // Display current time + now = new Date(); // initialize date + System.out.println("Time is :" + now); + ApiCall apiCall = new ApiCall(); + String output = null; + try { + output = apiCall.FetchDataFromApi("devices"); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (ExecutionException e) { + e.printStackTrace(); + } catch (TimeoutException e) { + e.printStackTrace(); + } + System.out.println("Output :" + output); // Display current output + } +} \ No newline at end of file diff --git a/src/main/java/JSON2RDF/ApiCallYookr.java b/src/main/java/SecurityToken.java similarity index 56% rename from src/main/java/JSON2RDF/ApiCallYookr.java rename to src/main/java/SecurityToken.java index 2f55898..d9c4eb0 100644 --- a/src/main/java/JSON2RDF/ApiCallYookr.java +++ b/src/main/java/SecurityToken.java @@ -1,23 +1,18 @@ -package JSON2RDF; - - 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.Base64; - -public class ApiCallYookr { - public static void main(String[] args) { - - +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +public class SecurityToken { + public static String OnCallMethod(String username, String password) throws InterruptedException, ExecutionException, TimeoutException { //CONFIG PARAMETERS: //BEGIN------------CONFIG PARAMETERS BELOW TO YOUR ENVIRONMENT--------------------------------------- String baseURL = "https://api.dev.whysor.com/users/login"; - final String username = "kevin.shehu@hochschule-rhein-waal.de"; - final String password = "DK7SxFkGJgnLhnU3"; final String body= String.format("{\"email\": \"%s\", \"password\": \"%s\", \"domain\": \"my.dev.yookr.org\"}", username, password); var client = HttpClient.newBuilder() @@ -37,12 +32,19 @@ public class ApiCallYookr { } catch (URISyntaxException e) { e.printStackTrace(); } - client.sendAsync(request, HttpResponse.BodyHandlers.ofString()) - .thenApply(HttpResponse::body) - .thenAccept(System.out::println) - .join(); +// client.sendAsync(request, HttpResponse.BodyHandlers.ofString()) +// .thenApply(HttpResponse::body) +// .thenAccept(System.out::println) +// .join(); + + CompletableFuture> response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString()); + String result = response.thenApply(HttpResponse::body).get(5, TimeUnit.SECONDS); + return result; +// return HttpResponse.BodyHandlers.ofFile(Paths.get("D:\\WORK\\GGD\\src\\main\\java\\Data\\securityToken.json")); + } + } diff --git a/src/main/java/main.java b/src/main/java/main.java index 6aa1378..b00559b 100644 --- a/src/main/java/main.java +++ b/src/main/java/main.java @@ -1,49 +1,18 @@ - -import JSON2RDF.JSON2RDF; -import org.apache.jena.rdf.model.*; -import org.apache.jena.vocabulary.*; +import java.util.Timer; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; public class main { - public static void main (String args[]) { + public static void main(String[] args){ + + Timer time = new Timer(); // Instantiate Timer Object + ScheduledTask scheduledTask = new ScheduledTask(); // Instantiate SheduledTask class + time.schedule(scheduledTask, 0, 1000 ); // Create Repetitively task for every 1 secs - // some definitions - String personURI = "http://localhost:3030/GGD/JohnSmith"; - String givenName = "John"; - String familyName = "Smith"; - String fullName = givenName + " " + familyName; - // create an empty model - Model model = ModelFactory.createDefaultModel(); - // create the resource - // and add the properties cascading style - Resource johnSmith - = model.createResource(personURI) - .addProperty(VCARD.FN, fullName) - .addProperty(VCARD.N, - model.createResource() - .addProperty(VCARD.Given, givenName) - .addProperty(VCARD.Family, familyName)); - // list the statements in the graph - StmtIterator iter = model.listStatements(); - // print out the predicate, subject and object of each statement - while (iter.hasNext()) { - Statement stmt = iter.nextStatement(); // get next statement - Resource subject = stmt.getSubject(); // get the subject - Property predicate = stmt.getPredicate(); // get the predicate - RDFNode object = stmt.getObject(); // get the object - System.out.print(subject.toString()); - System.out.print(" " + predicate.toString() + " "); - if (object instanceof Resource) { - System.out.print(object.toString()); - } else { - // object is a literal - System.out.print(" \"" + object.toString() + "\""); - } - System.out.println(" ."); - } } }