json.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package captiveportal;
  2. import java.util.Random;
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStreamWriter;
  6. import java.io.StringWriter;
  7. import java.net.HttpURLConnection;
  8. import java.net.URL;
  9. import org.json.simple.JSONObject;
  10. import org.json.simple.JSONArray;
  11. import org.json.simple.parser.ParseException;
  12. import org.json.simple.parser.JSONParser;
  13. public class json {
  14. private String[] listTickets(String url, String json) throws Exception {
  15. URL http = new URL(url);
  16. HttpURLConnection con = (HttpURLConnection) http.openConnection();
  17. //add reuqest header
  18. con.setRequestMethod("POST");
  19. con.setRequestProperty("User-Agent", "Mozilla/5.0");
  20. con.setRequestProperty("Content-Type", "application/json");
  21. con.setRequestProperty("Accept", "application/json");
  22. con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
  23. // Send post request
  24. con.setDoOutput(true);
  25. OutputStreamWriter data= new OutputStreamWriter(con.getOutputStream());
  26. data.write(json);
  27. data.flush();
  28. BufferedReader in = new BufferedReader(
  29. new InputStreamReader(con.getInputStream()));
  30. String inputLine;
  31. StringBuffer response = new StringBuffer();
  32. while ((inputLine = in.readLine()) != null) {
  33. response.append(inputLine);
  34. }
  35. in.close();
  36. //print result
  37. JSONParser parser=new JSONParser();
  38. String s = response.toString();
  39. try{
  40. Object obj = parser.parse(s);
  41. JSONObject tickets = (JSONObject)obj;
  42. JSONArray array = (JSONArray)tickets.get("tickets");
  43. String[] ticketNumbers = new String[(int) array.size()];
  44. for(int i = 0; i < (int) array.size(); i++)
  45. {
  46. JSONObject ticketNumber = (JSONObject)array.get(i);
  47. ticketNumbers[i] = (String) ticketNumber.get("ticketNumber");
  48. }
  49. return ticketNumbers;
  50. }catch(ParseException pe){
  51. System.out.println("position: " + pe.getPosition());
  52. System.out.println(pe);
  53. }
  54. String[] returnArray = {"ERROR", "FOUTMELDING"};
  55. return returnArray;
  56. }
  57. private String grandAccessRequest(String json) throws Exception {
  58. URL http = new URL("http://webapi.implementation.computerscience.international:9323/CFIS/Ticket");
  59. HttpURLConnection con = (HttpURLConnection) http.openConnection();
  60. //add reuqest header
  61. con.setRequestMethod("POST");
  62. con.setRequestProperty("User-Agent", "Mozilla/5.0");
  63. con.setRequestProperty("Content-Type", "application/json");
  64. con.setRequestProperty("Accept", "application/json");
  65. con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
  66. // Send post request
  67. con.setDoOutput(true);
  68. OutputStreamWriter data= new OutputStreamWriter(con.getOutputStream());
  69. data.write(json);
  70. data.flush();
  71. BufferedReader in = new BufferedReader(
  72. new InputStreamReader(con.getInputStream()));
  73. String inputLine;
  74. StringBuffer response = new StringBuffer();
  75. while ((inputLine = in.readLine()) != null) {
  76. response.append(inputLine);
  77. }
  78. in.close();
  79. //print result
  80. JSONParser parser=new JSONParser();
  81. String s = response.toString();
  82. Object obj = parser.parse(s);
  83. JSONObject resultCode = (JSONObject)obj;
  84. return (String) resultCode.get("result");
  85. }
  86. @SuppressWarnings("unchecked")
  87. public boolean checkUser(String ticketNumber) throws Exception
  88. {
  89. JSONObject json=new JSONObject();
  90. Random random = new Random();
  91. int requestId = random.nextInt(9999) + 1000;
  92. json.put("function", "List");
  93. json.put("teamId", "IN103-3");
  94. json.put("teamKey", "8b846f08-caca-4aab-b254-0109c5997053");
  95. json.put("requestId", new Integer(requestId));
  96. StringWriter out = new StringWriter();
  97. json.writeJSONString(out);
  98. String jsonText = out.toString();
  99. String[] ticketNumbers = listTickets("http://webapi.implementation.computerscience.international:9323/CFIS/Ticket", jsonText);
  100. for(int i = 0; i < ticketNumbers.length; i++)
  101. {
  102. if(new String(ticketNumbers[i]).equals(ticketNumber))
  103. {
  104. return true;
  105. }
  106. }
  107. return false;
  108. }
  109. @SuppressWarnings("unchecked")
  110. public boolean grandAccess(String ticketNumber) throws Exception
  111. {
  112. JSONObject json=new JSONObject();
  113. Random random = new Random();
  114. int requestId = random.nextInt(9999) + 1000;
  115. json.put("function", "RegisterInternetAccess");
  116. json.put("teamId", "IN103-3");
  117. json.put("teamKey", "8b846f08-caca-4aab-b254-0109c5997053");
  118. json.put("requestId", new Integer(requestId));
  119. json.put("ticketNumber", ticketNumber);
  120. StringWriter out = new StringWriter();
  121. json.writeJSONString(out);
  122. //String jsonText = out.toString();
  123. //String grandResult = grandAccessRequest(jsonText);
  124. return true;
  125. /*if(grandResult == "8" || grandResult == "0")
  126. {
  127. return true;
  128. }
  129. else
  130. {
  131. System.out.println("Suddenly le wild error code (" +grandResult+ ") appeared!!");
  132. return false;
  133. }*/
  134. }
  135. }