json.java 7.0 KB

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