Skip to content
Snippets Groups Projects
Commit 967ddad3 authored by btalhaoui's avatar btalhaoui
Browse files

client get the token from the api

parent 70f34e20
No related branches found
No related tags found
No related merge requests found
......@@ -38,7 +38,7 @@ import java.util.Properties;
import java.util.Scanner;
public class Main {
public static String token;
public static String token="";
public static void main(String[] args) {
Properties properties = new Properties();
if(new File("config.properties").isFile()) {
......@@ -52,7 +52,8 @@ public class Main {
// get the property value and print it out
token = properties.getProperty("token");
System.out.println(token);
System.out.println("Insert the following token in organization management :");
System.out.println("\""+token+"\"");
} catch (IOException ex) {
ex.printStackTrace();
} finally {
......@@ -65,11 +66,20 @@ public class Main {
}
}
}else{
System.out.println("Veuillez entrer le token fourni par l'interface d'administration");
Scanner s = new Scanner(System.in);
token = s.nextLine();
Uploader up = new Uploader();
up.initialize();
while(token=="") {
try {
token = up.register();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
properties.setProperty("token", token);
System.out.println("Insert the following token in organization management :");
System.out.println("\""+token+"\"");
// Save the grades properties using store() and an output stream
FileOutputStream out;
try {
......@@ -77,7 +87,7 @@ public class Main {
"config.properties");
properties.store(out, null);
out.close();
System.out.println("Fichier de configuration créer");
System.out.println("configuration file created");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
......@@ -89,11 +99,12 @@ public class Main {
Monitor monitor = new Monitor();
Map<String, Object> analyze_result = monitor.analyze();
ObjectMapper mapper = new ObjectMapper();
Uploader up;
try {
// Convert object to JSON string and pretty print
String json_string = mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(analyze_result);
Uploader up = new Uploader(json_string);
up = new Uploader(json_string);
up.initialize();
try {
up.post();
......
......@@ -5,30 +5,38 @@ import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class Uploader {
private String json;
private HttpClient httpclient;
private HttpPost httppost ;
private String url = "http://192.168.0.71/Laravel/public/api/sensors";
private String baseUrl = "http://192.168.0.71/Laravel/public/api/";
public Uploader(String json) {
// TODO Auto-generated constructor stub
this.json = json;
}
public Uploader() {
}
public void initialize() {
httpclient = HttpClients.createDefault();
httppost = new HttpPost(url);
}
public void post() throws Exception {
String url = "sensors/";
httppost = new HttpPost(baseUrl+url);
List<NameValuePair> params = new ArrayList<NameValuePair>(1);
params.add(new BasicNameValuePair("content", json));
params.add(new BasicNameValuePair("token",Main.token));
......@@ -39,10 +47,26 @@ private String url = "http://192.168.0.71/Laravel/public/api/sensors";
if (entity != null) {
InputStream instream = entity.getContent();
try {
System.out.println(instream.toString());
instream.toString();
} finally {
instream.close();
}
}
}
public String register() throws Exception{
String url = "register";
HttpGet httpget = new HttpGet(baseUrl+url);
HttpResponse response = httpclient.execute(httpget);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = null;
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity);
}
return result;
} else if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
return "401 SC_UNAUTHORIZED";
}
return "UNKNOWN ERROR";
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment