No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ServletConnection.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package app.insa.clav.Reseau;
  2. import app.insa.clav.Core.Utilisateurs;
  3. import app.insa.clav.Messages.MessagePseudo;
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.io.OutputStream;
  8. import java.lang.reflect.Type;
  9. import java.net.HttpURLConnection;
  10. import java.net.MalformedURLException;
  11. import java.net.URL;
  12. import java.nio.charset.StandardCharsets;
  13. import java.util.ArrayList;
  14. import java.util.Arrays;
  15. import java.util.List;
  16. import com.google.gson.Gson;
  17. import com.google.gson.GsonBuilder;
  18. import com.google.gson.reflect.TypeToken;
  19. public class ServletConnection {
  20. private static ServletConnection instance = null;
  21. public final GsonBuilder builder;
  22. public final Gson gson;
  23. public String baseURL;
  24. private ServletConnection(String urlServeur){
  25. builder = new GsonBuilder();
  26. gson = builder.create();;
  27. //baseURL = "https://srv-gei-tomcat.insa-toulouse.fr/ServletJiggly/";
  28. baseURL = urlServeur;
  29. }
  30. public static ServletConnection getInstance(String urlServeur){
  31. synchronized(ServletConnection.class){
  32. if (instance == null) {
  33. instance = new ServletConnection(urlServeur);
  34. }
  35. }
  36. return instance;
  37. }
  38. public static ServletConnection getInstance(){
  39. if (instance == null) {
  40. System.out.println("ATTENTION : Pointeur servlet null");
  41. }
  42. return instance;
  43. }
  44. public ArrayList<Utilisateurs> getRemoteActiveUsers(){
  45. URL url = null;
  46. HttpURLConnection con = null;
  47. ArrayList<Utilisateurs> resList = null;
  48. try {
  49. url = new URL(baseURL + "getOutdoorUsers");
  50. } catch (MalformedURLException e) {
  51. System.out.println("Erreur recup user externes actif : création URL avec " + url.toString());
  52. //e.printStackTrace();
  53. }
  54. try {
  55. con = (HttpURLConnection) url.openConnection();
  56. con.setRequestMethod("GET");
  57. int status = con.getResponseCode();
  58. System.out.println("Error code HTTP request = " + status);
  59. BufferedReader in = new BufferedReader(
  60. new InputStreamReader(con.getInputStream()));
  61. String inputLine;
  62. StringBuffer content = new StringBuffer();
  63. while ((inputLine = in.readLine()) != null) {
  64. content.append(inputLine);
  65. }
  66. in.close();
  67. System.out.println("Response : " + content);
  68. Type listType = new TypeToken<ArrayList<Utilisateurs>>(){}.getType();
  69. resList = gson.fromJson(content.toString(),listType);
  70. //resList = new Utilisateurs[Integer.parseInt(con.getHeaderField("sizeArray"))];
  71. } catch (IOException e) {
  72. System.out.println("Erreur recup user externes actif : Utilisation URL avec " + url.toString());
  73. e.printStackTrace();
  74. }
  75. return resList;
  76. }
  77. public void submitConnectionIndoor(Utilisateurs user){
  78. System.out.println("On envoi l'user au serveur avec " + user.toString());
  79. URL url = null;
  80. HttpURLConnection con = null;
  81. try {
  82. url = new URL(baseURL + "submitConnectionIndoor");
  83. } catch (MalformedURLException e) {
  84. e.printStackTrace();
  85. }
  86. try {
  87. con = (HttpURLConnection) url.openConnection();
  88. con.setRequestMethod("POST");
  89. con.setRequestProperty("Content-Type", "application/json; utf-8");
  90. con.setDoOutput(true);
  91. try(OutputStream os = con.getOutputStream()) {
  92. byte[] input = gson.toJson(user).getBytes(StandardCharsets.UTF_8);
  93. os.write(input, 0, input.length);
  94. }
  95. int status = con.getResponseCode();
  96. System.out.println("Error code HTTP request submit indoor = " + status);
  97. } catch (IOException e) {
  98. e.printStackTrace();
  99. }
  100. }
  101. public void submitConnectionOutdoor(Utilisateurs user) {
  102. URL url = null;
  103. HttpURLConnection con = null;
  104. try {
  105. url = new URL(baseURL + "submitConnectionOutdoor");
  106. } catch (MalformedURLException e) {
  107. e.printStackTrace();
  108. }
  109. try {
  110. con = (HttpURLConnection) url.openConnection();
  111. con.setRequestMethod("POST");
  112. con.setRequestProperty("Content-Type", "application/json; utf-8");
  113. con.setDoOutput(true);
  114. try(OutputStream os = con.getOutputStream()) {
  115. byte[] input = gson.toJson(user).getBytes(StandardCharsets.UTF_8);
  116. os.write(input, 0, input.length);
  117. }
  118. int status = con.getResponseCode();
  119. System.out.println("Error code HTTP request submit outdoor = " + status);
  120. } catch (IOException e) {
  121. e.printStackTrace();
  122. }
  123. }
  124. public ArrayList<Utilisateurs> getAllActiveUsers() {
  125. URL url = null;
  126. HttpURLConnection con = null;
  127. ArrayList<Utilisateurs> resList = null;
  128. try {
  129. url = new URL(baseURL + "getAllUsers");
  130. } catch (MalformedURLException e) {
  131. e.printStackTrace();
  132. }
  133. try {
  134. con = (HttpURLConnection) url.openConnection();
  135. con.setRequestMethod("GET");
  136. int status = con.getResponseCode();
  137. System.out.println("Error code HTTP request = " + status);
  138. BufferedReader in = new BufferedReader(
  139. new InputStreamReader(con.getInputStream()));
  140. String inputLine;
  141. StringBuffer content = new StringBuffer();
  142. while ((inputLine = in.readLine()) != null) {
  143. content.append(inputLine);
  144. }
  145. in.close();
  146. System.out.println("Response : " + content);
  147. Type listType = new TypeToken<ArrayList<Utilisateurs>>(){}.getType();
  148. resList = gson.fromJson(content.toString(),listType);
  149. } catch (IOException e) {
  150. e.printStackTrace();
  151. }
  152. return resList;
  153. }
  154. public void submitDeconnectionOutdoor(Utilisateurs user){
  155. URL url = null;
  156. HttpURLConnection con = null;
  157. try {
  158. url = new URL(baseURL + "submitDeconnectionOutdoor");
  159. } catch (MalformedURLException e) {
  160. e.printStackTrace();
  161. }
  162. try {
  163. con = (HttpURLConnection) url.openConnection();
  164. con.setRequestMethod("POST");
  165. con.setRequestProperty("Content-Type", "application/json; utf-8");
  166. con.setDoOutput(true);
  167. try(OutputStream os = con.getOutputStream()) {
  168. byte[] input = gson.toJson(user).getBytes(StandardCharsets.UTF_8);
  169. os.write(input, 0, input.length);
  170. }
  171. int status = con.getResponseCode();
  172. System.out.println("Error code HTTP request submit deco outdoor = " + status);
  173. } catch (IOException e) {
  174. e.printStackTrace();
  175. }
  176. }
  177. public void submitDeconnectionIndoor(Utilisateurs user){
  178. URL url = null;
  179. HttpURLConnection con = null;
  180. try {
  181. url = new URL(baseURL + "submitDeconnectionIndoor");
  182. } catch (MalformedURLException e) {
  183. e.printStackTrace();
  184. }
  185. try {
  186. con = (HttpURLConnection) url.openConnection();
  187. con.setRequestMethod("POST");
  188. con.setRequestProperty("Content-Type", "application/json; utf-8");
  189. con.setDoOutput(true);
  190. try(OutputStream os = con.getOutputStream()) {
  191. byte[] input = gson.toJson(user).getBytes(StandardCharsets.UTF_8);
  192. os.write(input, 0, input.length);
  193. }
  194. int status = con.getResponseCode();
  195. System.out.println("Error code HTTP request submit deco indoor = " + status);
  196. } catch (IOException e) {
  197. e.printStackTrace();
  198. }
  199. }
  200. }