json.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package captiveportal;
  2. import java.text.DateFormat;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Calendar;
  5. import java.util.Random;
  6. import java.util.Date;
  7. import java.io.BufferedReader;
  8. import java.io.InputStreamReader;
  9. import java.io.OutputStreamWriter;
  10. import java.io.StringWriter;
  11. import java.net.HttpURLConnection;
  12. import java.net.URL;
  13. import org.json.simple.JSONObject;
  14. import org.json.simple.JSONArray;
  15. import org.json.simple.parser.ParseException;
  16. import org.json.simple.parser.JSONParser;
  17. public class json {
  18. private final String teamKey = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
  19. private final String teamId = "INXXX-X";
  20. private String[] listTickets(String url, String json) throws Exception {
  21. //The url to the JSON listener
  22. URL http = new URL(url);
  23. //Make the connection to the listener
  24. HttpURLConnection con = (HttpURLConnection) http.openConnection();
  25. //add reuqest headers
  26. con.setRequestMethod("POST");
  27. con.setRequestProperty("User-Agent", "Mozilla/5.0");
  28. con.setRequestProperty("Content-Type", "application/json");
  29. con.setRequestProperty("Accept", "application/json");
  30. con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
  31. // Send post request
  32. //Do some fancy shizzle
  33. con.setDoOutput(true);
  34. OutputStreamWriter data= new OutputStreamWriter(con.getOutputStream());
  35. //Send the JSON request
  36. data.write(json);
  37. data.flush();
  38. //Get the JSON response
  39. BufferedReader in = new BufferedReader(
  40. new InputStreamReader(con.getInputStream()));
  41. String inputLine;
  42. StringBuffer response = new StringBuffer();
  43. //Put the response in a string (not in the underpants)
  44. while ((inputLine = in.readLine()) != null) {
  45. response.append(inputLine);
  46. }
  47. in.close();
  48. //Parse to JSON
  49. JSONParser parser=new JSONParser();
  50. //Make it a string
  51. String s = response.toString();
  52. //Just give it a shot
  53. try{
  54. //Create JSON object
  55. Object obj = parser.parse(s);
  56. //Get the ticket variable from the full JSON object
  57. JSONObject tickets = (JSONObject)obj;
  58. //Make from the value of tickets an array (because we can and it simply is)
  59. JSONArray array = (JSONArray)tickets.get("tickets");
  60. //Create an array from every ticketNumber in the array (with maximum as amount if tickets)
  61. String[] ticketNumbers = new String[(int) array.size()];
  62. //Let loop it
  63. for(int i = 0; i < (int) array.size(); i++)
  64. {
  65. //Get the ticket number from each ticket
  66. JSONObject ticketNumber = (JSONObject)array.get(i);
  67. //Make from the number a string and put in our newly created array
  68. ticketNumbers[i] = (String) ticketNumber.get("ticketNumber");
  69. //Not at the end? Lets do it again!
  70. }
  71. //Now the purpose of this function Return all the ticket number in a singel array
  72. return ticketNumbers;
  73. //If you found le wild Charizard catch it.
  74. }catch(ParseException pe){
  75. System.out.println("position: " + pe.getPosition());
  76. System.out.println(pe);
  77. /*
  78. I wanna be the very best,
  79. Like no one ever was.
  80. To catch them is my real test,
  81. To train them is my cause.
  82. */
  83. }
  84. //If error return it
  85. String[] returnArray = {"ERROR", "BOEHOEHOE"};
  86. return returnArray;
  87. }
  88. private String grandAccessRequest(String json) throws Exception {
  89. //The url to the JSON listener
  90. URL http = new URL("http://webapi.implementation.computerscience.international:9323/CFIS/Ticket");
  91. //Make the connection to the listener
  92. HttpURLConnection con = (HttpURLConnection) http.openConnection();
  93. //add reuqest headers
  94. con.setRequestMethod("POST");
  95. con.setRequestProperty("User-Agent", "Mozilla/5.0");
  96. con.setRequestProperty("Content-Type", "application/json");
  97. con.setRequestProperty("Accept", "application/json");
  98. con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
  99. // Send post request
  100. con.setDoOutput(true);
  101. OutputStreamWriter data= new OutputStreamWriter(con.getOutputStream());
  102. data.write(json);
  103. //Send JSON request
  104. data.flush();
  105. //Read JSON response
  106. BufferedReader in = new BufferedReader(
  107. new InputStreamReader(con.getInputStream()));
  108. String inputLine;
  109. StringBuffer response = new StringBuffer();
  110. //Get all the output, and place it in one string
  111. while ((inputLine = in.readLine()) != null) {
  112. response.append(inputLine);
  113. }
  114. in.close();
  115. //Parse result to JSON
  116. JSONParser parser=new JSONParser();
  117. //Make from a raw JSON object an string
  118. String s = response.toString();
  119. //Create JSON object
  120. Object obj = parser.parse(s);
  121. //Get the respond code
  122. JSONObject resultCode = (JSONObject)obj;
  123. //Make the code a string
  124. return (String) resultCode.get("result");
  125. }
  126. @SuppressWarnings("unchecked")
  127. //Deprecated since version 04-01-2015. So no small talk
  128. public boolean checkTicket(String ticketNumber) throws Exception
  129. {
  130. JSONObject json=new JSONObject();
  131. Random random = new Random();
  132. int requestId = random.nextInt(9999) + 1000;
  133. json.put("function", "List");
  134. json.put("teamId", teamId);
  135. json.put("teamKey", teamKey);
  136. json.put("requestId", new Integer(requestId));
  137. StringWriter out = new StringWriter();
  138. json.writeJSONString(out);
  139. String jsonText = out.toString();
  140. String[] ticketNumbers = listTickets("http://webapi.implementation.computerscience.international:9323/CFIS/Ticket", jsonText);
  141. for(int i = 0; i < ticketNumbers.length; i++)
  142. {
  143. if(new String(ticketNumbers[i]).equals(ticketNumber))
  144. {
  145. return true;
  146. }
  147. }
  148. return false;
  149. }
  150. @SuppressWarnings("unchecked")
  151. public boolean grandAccess(String ticketNumber) throws Exception
  152. {
  153. //Create JSON object
  154. JSONObject json=new JSONObject();
  155. //Our unique ID will be a timestamp
  156. //Set the stamp format
  157. DateFormat TimeStamp = new SimpleDateFormat("MMddyyyyHHmmssSSS");
  158. //Get the current date
  159. Date today = Calendar.getInstance().getTime();
  160. //Make it a string
  161. String CurrentTimeStamp = TimeStamp.format(today);
  162. //Put variables in our JSON object
  163. json.put("function", "RegisterInternetAccess");
  164. json.put("teamId", teamId);
  165. json.put("teamKey", teamKey);
  166. json.put("requestId", CurrentTimeStamp);
  167. json.put("ticketNumber", ticketNumber);
  168. //Write it to a string
  169. StringWriter out = new StringWriter();
  170. json.writeJSONString(out);
  171. String jsonText = out.toString();
  172. //Call the function grandAccessRequest
  173. String grandResult = grandAccessRequest(jsonText);
  174. //If login succesfull or if user already logged in. Then you may pass
  175. if(grandResult.equals("0") || grandResult.equals("8"))
  176. {
  177. //Congrats. Now you can log in!
  178. return true;
  179. }
  180. //Else..
  181. else
  182. {
  183. //Not
  184. //Print to the system why not
  185. System.out.println("Suddenly le wild error code (" +grandResult+ ") appeared!!");
  186. //Say it fails
  187. return false;
  188. }
  189. }
  190. }