123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- package app.insa.clav.Reseau;
- import app.insa.clav.Core.Utilisateurs;
- import app.insa.clav.Messages.MessagePseudo;
-
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.lang.reflect.Type;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.nio.charset.StandardCharsets;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
-
- import com.google.gson.Gson;
- import com.google.gson.GsonBuilder;
- import com.google.gson.reflect.TypeToken;
-
- public class ServletConnection {
-
- private static ServletConnection instance = null;
-
- public final GsonBuilder builder;
- public final Gson gson;
- public String baseURL;
-
- private ServletConnection(String urlServeur){
- builder = new GsonBuilder();
- gson = builder.create();;
- //baseURL = "https://srv-gei-tomcat.insa-toulouse.fr/ServletJiggly/";
- baseURL = urlServeur;
- }
-
- public static ServletConnection getInstance(String urlServeur){
- synchronized(ServletConnection.class){
- if (instance == null) {
- instance = new ServletConnection(urlServeur);
- }
- }
- return instance;
- }
-
- public static ServletConnection getInstance(){
- if (instance == null) {
- System.out.println("ATTENTION : Pointeur servlet null");
- }
- return instance;
- }
-
- public ArrayList<Utilisateurs> getRemoteActiveUsers(){
- URL url = null;
- HttpURLConnection con = null;
- ArrayList<Utilisateurs> resList = null;
- try {
- url = new URL(baseURL + "getOutdoorUsers");
- } catch (MalformedURLException e) {
- System.out.println("Erreur recup user externes actif : création URL avec " + url.toString());
- //e.printStackTrace();
- }
- try {
- con = (HttpURLConnection) url.openConnection();
- con.setRequestMethod("GET");
- int status = con.getResponseCode();
- System.out.println("Error code HTTP request = " + status);
- BufferedReader in = new BufferedReader(
- new InputStreamReader(con.getInputStream()));
- String inputLine;
- StringBuffer content = new StringBuffer();
- while ((inputLine = in.readLine()) != null) {
- content.append(inputLine);
- }
- in.close();
- System.out.println("Response : " + content);
- Type listType = new TypeToken<ArrayList<Utilisateurs>>(){}.getType();
- resList = gson.fromJson(content.toString(),listType);
- //resList = new Utilisateurs[Integer.parseInt(con.getHeaderField("sizeArray"))];
- } catch (IOException e) {
- System.out.println("Erreur recup user externes actif : Utilisation URL avec " + url.toString());
- e.printStackTrace();
- }
- return resList;
- }
-
- public void submitConnectionIndoor(Utilisateurs user){
- System.out.println("On envoi l'user au serveur avec " + user.toString());
- URL url = null;
- HttpURLConnection con = null;
- try {
- url = new URL(baseURL + "submitConnectionIndoor");
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
-
- try {
- con = (HttpURLConnection) url.openConnection();
- con.setRequestMethod("POST");
- con.setRequestProperty("Content-Type", "application/json; utf-8");
- con.setDoOutput(true);
- try(OutputStream os = con.getOutputStream()) {
- byte[] input = gson.toJson(user).getBytes(StandardCharsets.UTF_8);
- os.write(input, 0, input.length);
- }
- int status = con.getResponseCode();
- System.out.println("Error code HTTP request submit indoor = " + status);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- public void submitConnectionOutdoor(Utilisateurs user) {
- URL url = null;
- HttpURLConnection con = null;
- try {
- url = new URL(baseURL + "submitConnectionOutdoor");
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
-
- try {
- con = (HttpURLConnection) url.openConnection();
- con.setRequestMethod("POST");
- con.setRequestProperty("Content-Type", "application/json; utf-8");
- con.setDoOutput(true);
- try(OutputStream os = con.getOutputStream()) {
- byte[] input = gson.toJson(user).getBytes(StandardCharsets.UTF_8);
- os.write(input, 0, input.length);
- }
- int status = con.getResponseCode();
- System.out.println("Error code HTTP request submit outdoor = " + status);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- public ArrayList<Utilisateurs> getAllActiveUsers() {
- URL url = null;
- HttpURLConnection con = null;
- ArrayList<Utilisateurs> resList = null;
- try {
- url = new URL(baseURL + "getAllUsers");
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- try {
- con = (HttpURLConnection) url.openConnection();
- con.setRequestMethod("GET");
- int status = con.getResponseCode();
- System.out.println("Error code HTTP request = " + status);
- BufferedReader in = new BufferedReader(
- new InputStreamReader(con.getInputStream()));
- String inputLine;
- StringBuffer content = new StringBuffer();
- while ((inputLine = in.readLine()) != null) {
- content.append(inputLine);
- }
- in.close();
- System.out.println("Response : " + content);
- Type listType = new TypeToken<ArrayList<Utilisateurs>>(){}.getType();
- resList = gson.fromJson(content.toString(),listType);
- } catch (IOException e) {
- e.printStackTrace();
- }
- return resList;
- }
-
- public void submitDeconnectionOutdoor(Utilisateurs user){
- URL url = null;
- HttpURLConnection con = null;
- try {
- url = new URL(baseURL + "submitDeconnectionOutdoor");
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
-
- try {
- con = (HttpURLConnection) url.openConnection();
- con.setRequestMethod("POST");
- con.setRequestProperty("Content-Type", "application/json; utf-8");
- con.setDoOutput(true);
- try(OutputStream os = con.getOutputStream()) {
- byte[] input = gson.toJson(user).getBytes(StandardCharsets.UTF_8);
- os.write(input, 0, input.length);
- }
- int status = con.getResponseCode();
- System.out.println("Error code HTTP request submit deco outdoor = " + status);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- public void submitDeconnectionIndoor(Utilisateurs user){
- URL url = null;
- HttpURLConnection con = null;
- try {
- url = new URL(baseURL + "submitDeconnectionIndoor");
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
-
- try {
- con = (HttpURLConnection) url.openConnection();
- con.setRequestMethod("POST");
- con.setRequestProperty("Content-Type", "application/json; utf-8");
- con.setDoOutput(true);
- try(OutputStream os = con.getOutputStream()) {
- byte[] input = gson.toJson(user).getBytes(StandardCharsets.UTF_8);
- os.write(input, 0, input.length);
- }
- int status = con.getResponseCode();
- System.out.println("Error code HTTP request submit deco indoor = " + status);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
|