FIX : SecurityToken.java

ADD : ApiCall.java
ADD : ScheduledTask.java
This commit is contained in:
2020-12-21 22:26:47 +01:00
parent 710726c5ef
commit fd96876544
13 changed files with 323 additions and 105 deletions

View File

@@ -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<HttpResponse<String>> response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
String result = response.thenApply(HttpResponse::body).get(5, TimeUnit.SECONDS);
return result;
}
}

View File

@@ -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}
]
}
}

View File

View File

@@ -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)))

View File

@@ -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
}
}

View File

@@ -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<HttpResponse<String>> 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"));
}
}

View File

@@ -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(" .");
}
}
}