ソースを参照

Bugs eliminated. Rise of the commentary

Fixed some bugs. Added some commentary
Deben Oldert 11 年 前
コミット
ca59272fc9
1 ファイル変更62 行追加13 行削除
  1. 62 13
      captiveportal/json.java

+ 62 - 13
captiveportal/json.java

@@ -1,6 +1,10 @@
 package captiveportal;
 
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
 import java.util.Random;
+import java.util.Date;
 import java.io.BufferedReader;
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
@@ -18,11 +22,12 @@ public class json {
 	private final String teamId = "INXXX-X";
 	private String[] listTickets(String url, String json) throws Exception {
 		 
-		
+		//The url to the JSON listener
 		URL http = new URL(url);
+		//Make the connection to the listener
 		HttpURLConnection con = (HttpURLConnection) http.openConnection();
  
-		//add reuqest header
+		//add reuqest headers
 		con.setRequestMethod("POST");
 		con.setRequestProperty("User-Agent", "Mozilla/5.0");
 		con.setRequestProperty("Content-Type", "application/json");
@@ -30,51 +35,73 @@ public class json {
 		con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
  
 		// Send post request
+		//Do some fancy shizzle
 		con.setDoOutput(true);
 		OutputStreamWriter data= new OutputStreamWriter(con.getOutputStream());
+		//Send the JSON request
 		data.write(json);
 		data.flush();
+		//Get the JSON response
 		BufferedReader in = new BufferedReader(
 		        new InputStreamReader(con.getInputStream()));
 		String inputLine;
 		StringBuffer response = new StringBuffer();
- 
+		//Put the response in a string (not in the underpants)
 		while ((inputLine = in.readLine()) != null) {
 			response.append(inputLine);
 		}
 		in.close();
  
-		//print result
+		//Parse to JSON
 		JSONParser parser=new JSONParser();
+		  //Make it a string
 	      String s = response.toString();
-	      
+	      //Just give it a shot
 	      try{
+	    	 //Create JSON object
 	         Object obj = parser.parse(s);
+	         //Get the ticket variable from the full JSON object
 	         JSONObject tickets = (JSONObject)obj;
+	         //Make from the value of tickets an array (because we can and it simply is)
 	         JSONArray array = (JSONArray)tickets.get("tickets");
+	         //Create an array from every ticketNumber in the array (with maximum as amount if tickets)
 	         String[] ticketNumbers = new String[(int) array.size()];
+	         //Let loop it
 	         for(int i = 0; i < (int) array.size(); i++)
 	         	{
+	        	 //Get the ticket number from each ticket
 	        	 JSONObject ticketNumber = (JSONObject)array.get(i);
+	        	 //Make from the number a string and put in our newly created array
 	        	 ticketNumbers[i] = (String) ticketNumber.get("ticketNumber");
+	        	 //Not at the end? Lets do it again!
 	         	}
+	         //Now the purpose of this function Return all the ticket number in a singel array
 	         return ticketNumbers;
+	         //If you found le wild Charizard catch it.
 	      }catch(ParseException pe){
 	         System.out.println("position: " + pe.getPosition());
 	         System.out.println(pe);
+	         /*
+	          	I wanna be the very best,
+				Like no one ever was.
+				To catch them is my real test,
+				To train them is my cause.
+	          */
 	      }
-	     String[] returnArray = {"ERROR", "FOUTMELDING"};
+	      //If error return it
+	     String[] returnArray = {"ERROR", "BOEHOEHOE"};
 		return returnArray;
 	      
  
 	}
 	private String grandAccessRequest(String json) throws Exception {
 		 
-		
+		//The url to the JSON listener
 		URL http = new URL("http://webapi.implementation.computerscience.international:9323/CFIS/Ticket");
+		//Make the connection to the listener
 		HttpURLConnection con = (HttpURLConnection) http.openConnection();
  
-		//add reuqest header
+		//add reuqest headers
 		con.setRequestMethod("POST");
 		con.setRequestProperty("User-Agent", "Mozilla/5.0");
 		con.setRequestProperty("Content-Type", "application/json");
@@ -85,25 +112,32 @@ public class json {
 		con.setDoOutput(true);
 		OutputStreamWriter data= new OutputStreamWriter(con.getOutputStream());
 		data.write(json);
+		//Send JSON request
 		data.flush();
+		//Read JSON response
 		BufferedReader in = new BufferedReader(
 		        new InputStreamReader(con.getInputStream()));
 		String inputLine;
 		StringBuffer response = new StringBuffer();
- 
+		//Get all the output, and place it in one string
 		while ((inputLine = in.readLine()) != null) {
 			response.append(inputLine);
 		}
 		in.close();
  
-		//print result
+		//Parse result to JSON
 		JSONParser parser=new JSONParser();
+		  //Make from a raw JSON object an string
 	      String s = response.toString();
+	      //Create JSON object
 	      Object obj = parser.parse(s);
+	      //Get the respond code 
 	      JSONObject resultCode = (JSONObject)obj;
+	      //Make the code a string
 	      return (String) resultCode.get("result");  
 	}
 	@SuppressWarnings("unchecked")
+	//Deprecated since version 04-01-2015. So no small talk
 	public boolean checkTicket(String ticketNumber) throws Exception
 		{
 		JSONObject json=new JSONObject();
@@ -130,25 +164,40 @@ public class json {
 	@SuppressWarnings("unchecked")
 	public boolean grandAccess(String ticketNumber) throws Exception 
 		{
+		//Create JSON object
 		JSONObject json=new JSONObject();
-		Random random = new Random();
-		int  requestId = random.nextInt(9999) + 1000;
+		//Our unique ID will be a timestamp
+		//Set the stamp format
+		DateFormat TimeStamp = new SimpleDateFormat("MMddyyyyHHmmssSSS");
+		//Get the current date
+		Date today = Calendar.getInstance().getTime();
+		//Make it a string
+		String CurrentTimeStamp = TimeStamp.format(today);
+		//Put variables in our JSON object
 		json.put("function", "RegisterInternetAccess");
 		json.put("teamId", teamId);
 		json.put("teamKey", teamKey);
-		json.put("requestId", new Integer(requestId));
+		json.put("requestId", CurrentTimeStamp);
 		json.put("ticketNumber", ticketNumber);
+		//Write it to a string
 		StringWriter out = new StringWriter();
 		  json.writeJSONString(out);
 		  String jsonText = out.toString();
+		//Call the function grandAccessRequest
 		String grandResult = grandAccessRequest(jsonText);
+		//If login succesfull or if user already logged in. Then you may pass
 		if(grandResult.equals("0") || grandResult.equals("8"))
 		{
+			//Congrats. Now you can log in!
 			return true;
 		}
+		//Else..
 		else
 		{
+			//Not
+			//Print to the system why not
 			System.out.println("Suddenly le wild error code (" +grandResult+ ") appeared!!");
+			//Say it fails
 			return false;
 		}
 	}