Forráskód Böngészése

Updated

Fully updated the request handling
Deben Oldert 10 éve
szülő
commit
cfcd8eaba1

+ 0 - 13
Java/JSON.java

@@ -1,13 +0,0 @@
-/*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
-
-/**
- *
- * @author Deben
- */
-public class JSON {
-    
-}

+ 9 - 4
Java/APS.java → SAS:APS Server/APS.java

@@ -1,7 +1,7 @@
 /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
+ * Feel free to copy/use it for your own project.
+ * Keep in mind that it took me several days/weeks, beers and asperines to make this.
+ * So be nice, and give me some credit, I won't bite and it won't hurt you.
  */
 
 import java.io.IOException;
@@ -20,7 +20,7 @@ import org.json.simple.parser.ParseException;
 
 /**
  *
- * @author Deben
+ * @author Deben Oldert
  */
 @WebServlet(urlPatterns = {"/APS"})
 public class APS extends HttpServlet {
@@ -142,9 +142,14 @@ public class APS extends HttpServlet {
                         String[] keys = {"serviceType",
                                          "serviceNumber",
                                          "notificationId",
+                                         "apiKey",
                                          "deviceId"};
                         boolean state;
                         for(String key : keys) {
+                            if(!json.containsKey(key)) {
+                                outputResult(response, 560, requestId, null);
+                                return;
+                            }
                             state = ldap.writeInfo(key, (String) json.get(key));
                             if(!state) {
                                 outputResult(response, 82, requestId, null);

+ 10 - 10
Java/ErrorCode.java → SAS:APS Server/ErrorCode.java

@@ -1,17 +1,14 @@
-
-import java.util.HashMap;
-
-
-
 /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
+ * Feel free to copy/use it for your own project.
+ * Keep in mind that it took me several days/weeks, beers and asperines to make this.
+ * So be nice, and give me some credit, I won't bite and it won't hurt you.
  */
 
+import java.util.HashMap;
+
 /**
  *
- * @author Deben
+ * @author Deben Oldert
  */
 public class ErrorCode {
     public String getCodeText(int id) {
@@ -20,12 +17,15 @@ public class ErrorCode {
         list.put(-1, "Unknown error code");
         list.put(0, "OK");
         list.put(1, "Refused");
+        list.put(2, "User needs device registration");
+        list.put(5, "Invalid code");
         list.put(50, "Failed to send mail");
         list.put(51, "Incorrect mail adress");
         list.put(52, "Mail not equal");
         list.put(80, "Failed to connect to LDAP server");
         list.put(81, "Error reading attribute");
         list.put(82, "Error writing attribute");
+        list.put(83, "Failed to register device");
         
         // Data errors
         
@@ -39,7 +39,7 @@ public class ErrorCode {
         list.put(602, "Failed to read from DB");
         
         //LDAP errors
-        
+        list.put(699, "User not found");
         list.put(700, "OK");
         list.put(749, "Username / password incorrect");
         

+ 105 - 10
Java/Function.java → SAS:APS Server/Function.java

@@ -1,3 +1,8 @@
+/*
+ * Feel free to copy/use it for your own project.
+ * Keep in mind that it took me several days/weeks, beers and asperines to make this.
+ * So be nice, and give me some credit, I won't bite and it won't hurt you.
+ */
 
 import java.io.BufferedReader;
 import java.io.DataOutputStream;
@@ -11,15 +16,9 @@ import org.json.simple.JSONObject;
 import org.json.simple.parser.JSONParser;
 import org.json.simple.parser.ParseException;
 
-/*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
-
 /**
  *
- * @author Deben
+ * @author Deben Oldert
  */
 public class Function {
     
@@ -36,6 +35,7 @@ public class Function {
                               "apiKey",
                               "deviceId",
                               "notificationId",
+                              "registerCode",
                               "serverURL",
                               "appURL",
                               "storeURL",
@@ -64,7 +64,6 @@ public class Function {
             }
             if(jsonObject.get(key) != null && !jsonObject.get(key).getClass().getName().equals("org.json.simple.JSONObject")) {
                 array.put(key, (String) jsonObject.get(key).toString());
-                //System.out.println(key+", "+jsonObject.get(key));
             }
             if(userInfo) {
                 JSONObject userObject = (JSONObject) jsonObject.get("userInfo");
@@ -75,7 +74,6 @@ public class Function {
                         continue;
                     }
                     array.put(key, (String) userObject.get(key).toString());
-                    //System.out.println("userInfo: "+key+", "+userObject.get(key));
                 }
             }
         }
@@ -103,7 +101,7 @@ public class Function {
             case "STORE":
                 return "http://google.com";
             case "GCM":
-                return "https://android.googleapis.com/gcm/send";
+                return "https://gcm-http.googleapis.com/gcm/send";
             case "APNS":
                 return "http://apple.com";
             default:
@@ -113,6 +111,7 @@ public class Function {
     public String makeRequest(String type, String url, String body) throws MalformedURLException, IOException {
         URL obj = new URL(url);
         HttpURLConnection con = (HttpURLConnection) obj.openConnection();
+        System.out.println(body);
         
         
         switch(type) {
@@ -121,12 +120,19 @@ public class Function {
             case "POST":
                 con.setRequestMethod("POST");
 		con.setRequestProperty("content-type", "application/json");
+                if(url.contains("gcm")) {
+                    con.setRequestProperty("Authorization", "key=AIzaSyB67KpF-KSuZoPdnuy03TEIKRjHkBLEPpM");
+                }
                 
                 con.setDoOutput(true);
 		DataOutputStream wr = new DataOutputStream(con.getOutputStream());
                 wr.writeBytes(body);
 		wr.flush();
 		wr.close();
+                if(con.getResponseCode() != 200) {
+                    System.err.println(con.getResponseCode());
+                    return String.valueOf(con.getResponseCode());
+                }
                 
                 BufferedReader in = new BufferedReader(
 		        new InputStreamReader(con.getInputStream()));
@@ -147,5 +153,94 @@ public class Function {
                 return null;
         }
     }
+    public String genRegCode(String str) {
+        String first = str.substring(0, 1);
+        String last = str.substring(str.length()-1);
+        int firstCode = getCharCode(first);
+        int lastCode = getCharCode(last);
+        if(firstCode < 27 && firstCode > 0) {
+            first = ""+firstCode;
+        }
+        else {
+            return null;
+        }
+        if(lastCode < 27 && lastCode > 0) {
+            last = ""+lastCode;
+        }
+        else {
+            return null;
+        }
+        if(first.length() < 2) {
+            first = "0"+first;
+        }
+        if(last.length() < 2) {
+            last = "0"+last;
+        }
+        return first+last;
+    }
+    public boolean checkRegCode(String code, String str) {
+        String newCode = genRegCode(str);
+        return code.equals(newCode);
+    }
+    
+    private int getCharCode(String letter) {
+        letter = letter.toLowerCase();
+        switch(letter) {
+            case "a":
+                return 1;
+            case "b":
+                return 2;
+            case "c":
+                return 3;
+            case "d":
+                return 4;
+            case "e":
+                return 5;
+            case "f":
+                return 6;
+            case "g":
+                return 7;
+            case "h":
+                return 8;
+            case "i":
+                return 9;
+            case "j":
+                return 10;
+            case "k":
+                return 11;
+            case "l":
+                return 12;
+            case "m":
+                return 13;
+            case "n":
+                return 14;
+            case "o":
+                return 15;
+            case "p":
+                return 16;
+            case "q":
+                return 17;
+            case "r":
+                return 18;
+            case "s":
+                return 19;
+            case "t":
+                return 20;
+            case "u":
+                return 21;
+            case "v":
+                return 22;
+            case "w":
+                return 23;
+            case "x":
+                return 24;
+            case "y":
+                return 25;
+            case "z":
+                return 26;
+            default:
+                return 0;
+        }
+    }
     
 }

+ 28 - 9
Java/LDAP.java → SAS:APS Server/LDAP.java

@@ -1,7 +1,7 @@
 /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
+ * Feel free to copy/use it for your own project.
+ * Keep in mind that it took me several days/weeks, beers and asperines to make this.
+ * So be nice, and give me some credit, I won't bite and it won't hurt you.
  */
 
 import java.util.HashMap;
@@ -23,12 +23,13 @@ import javax.naming.directory.SearchResult;
 
 /**
  *
- * @author Deben
+ * @author Deben Oldert
  */
 public class LDAP {
     private static DirContext ctx;
     private static String userName;
     private static String passWord;
+    private static String base = "dc=vpn,dc=local";
 
     LDAP(String username, String password) throws NamingException {
         Hashtable<String, String> env = new Hashtable();
@@ -52,7 +53,7 @@ public class LDAP {
     }
     
     public int userCheck() throws NamingException {
-        Hashtable tmpEnv = (Hashtable) ctx.getEnvironment().clone();
+        /*Hashtable tmpEnv = (Hashtable) ctx.getEnvironment().clone();
         
         tmpEnv.put(Context.SECURITY_PRINCIPAL, userName);
         tmpEnv.put(Context.SECURITY_CREDENTIALS, passWord);
@@ -63,7 +64,25 @@ public class LDAP {
         }
         catch(NamingException e) {
             return getErrorCode(e.toString()) + 700;
+        }*/
+        try {
+            String[] key = {"cn"};
+
+            SearchControls cons = new SearchControls();
+            cons.setSearchScope(SearchControls.SUBTREE_SCOPE);
+            cons.setReturningAttributes(key);
+            NamingEnumeration<SearchResult> answer = ctx.search(base, "cn=" + userName, cons);
+            if(answer.hasMore()) {
+                return 0;
+            }
+            else {
+                return 730;
+            }
         }
+        catch(NamingException e) {
+                return getErrorCode(e.toString()) + 700;
+                }
+        
     }
     
     public HashMap getUserInfo(String[] keys) throws NamingException {
@@ -72,12 +91,10 @@ public class LDAP {
         cons.setReturningAttributes(keys);
         
         HashMap<String, String> array = new HashMap();
-        NamingEnumeration<SearchResult> answer = ctx.search("dc=vpn,dc=local", "sAMAccountName=" + userName, cons);
+        NamingEnumeration<SearchResult> answer = ctx.search(base, "cn=" + userName, cons);
         
         if(answer.hasMore()) {
             Attributes attrs = answer.next().getAttributes();
-            //System.out.println(attrs.get("serviceType").toString());
-            //array.put("serviceType", attrs.get("serviceType").toString());
             
             String tmp;
             for(String key : keys) {
@@ -95,8 +112,10 @@ public class LDAP {
         
     }
     public boolean writeInfo(String key, String value) {
+        System.out.println("Modding: "+key+", "+value);
         ModificationItem[] mods = new ModificationItem[1];
-        String name = "cn="+userName+",cn=Users,dc=vpn,dc=local";
+        String name = "CN="+userName+",CN=Users,DC=vpn,DC=local";
+        System.out.println(name);
         
         mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
           new BasicAttribute(key, value));

+ 4 - 4
Java/MAIL.java → SAS:APS Server/MAIL.java

@@ -9,14 +9,14 @@ import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeMessage;
 
 /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
+ * Feel free to copy/use it for your own project.
+ * Keep in mind that it took me several days/weeks, beers and asperines to make this.
+ * So be nice, and give me some credit, I won't bite and it won't hurt you.
  */
 
 /**
  *
- * @author Deben
+ * @author Deben Oldert
  */
 public class MAIL {
     public boolean send(String mail, String subject, String body){

+ 119 - 47
Java/SAS.java → SAS:APS Server/SAS.java

@@ -1,7 +1,7 @@
 /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
+ * Feel free to copy/use it for your own project.
+ * Keep in mind that it took me several days/weeks, beers and asperines to make this.
+ * So be nice, and give me some credit, I won't bite and it won't hurt you.
  */
 
 import java.io.IOException;
@@ -22,7 +22,7 @@ import org.json.simple.parser.ParseException;
 
 /**
  *
- * @author Deben
+ * @author Deben Oldert
  */
 @WebServlet(urlPatterns = {"/SAS"})
 public class SAS extends HttpServlet {
@@ -32,13 +32,13 @@ public class SAS extends HttpServlet {
     int ldapError;
     SQL sql;
     ResultSet result;
-    final Object T3 = new Object();
-    final Object T2 = new Object();
-    final Object T1 = new Object();
+    //final Object T3 = new Object();
+    //final Object T2 = new Object();
+    //final Object T1 = new Object();
     String req;
     String reqBody;
     int i;
-    int timeout = 60;
+    int timeout = 30;
     boolean finished = false;
 
     public SAS() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
@@ -123,35 +123,44 @@ public class SAS extends HttpServlet {
                     tmpBody = tmpJSON.toJSONString();
 
                     HashMap jsonAnswer = function.defragJSON(function.makeRequest("POST", function.getURL("APS"), tmpBody));
-                    //System.out.print(jsonAnswer);
                     if(jsonAnswer.get("result").equals("0")) {
                         if(jsonAnswer.containsKey("serviceType")) {
                             System.out.println("SENDING PUSH NOTIFICATION");
-                            tmpJSON = new JSONObject();
-                                tmpJSON.put("function", "notify");
-                                tmpJSON.put("requestId", requestId);
-                                tmpJSON.put("serviceType", (String) jsonAnswer.get("serviceType"));
-                                tmpJSON.put("serviceNumber", (String) jsonAnswer.get("serviceNumber"));
-                                tmpJSON.put("apiKey", (String) jsonAnswer.get("apiKey"));
-                                tmpJSON.put("deviceId", (String) jsonAnswer.get("deviceId"));
-                            tmpBody = tmpJSON.toJSONString();
-                            System.out.println("%%%%%%%%%%%%%%%");
-                            System.out.println((String) jsonAnswer.get("serviceType"));
-                            //function.defragJSON(function.makeRequest("POST", function.getURL((String) jsonAnswer.get("serviceType")), tmpBody));
-                            sql.threadUpdate(requestId, "request", null);
+                            String[] info = {
+                                            "deviceId",
+                                            "serviceNumber",
+                                            "serviceType",
+                                            "notificationId",
+                                            "apiKey"};
+                            HashMap userInfo = ldap.getUserInfo(info);
+                            
+                            JSONObject exData = new JSONObject();
+                            exData.put("requestId", requestId);
+                            
+                            JSONObject data = new JSONObject();
+                            data.put("data", exData);
+                            data.put("to", userInfo.get("notificationId"));
+                                                      
+                            answer = function.makeRequest("POST", function.getURL(jsonAnswer.get("serviceType").toString()), data.toJSONString());
+                            
+                            sql.threadUpdate(requestId, "request", answer);
                             for(i = 1; i<=timeout && !finished; i++) {
                                 result = sql.stmt.executeQuery("SELECT `state`, `data` FROM `thread` WHERE `threadId`='"+requestId+"'");
-                                //System.out.println("GOT RESULT");
                                 if(result.first()) {
-                                    //System.out.println("GOT VALID RESULT state: "+result.getString("state")+" || data: "+result.getString("data"));
-                                    if(result.getString("state").equals("repley")) {
-                                        //System.out.println("GOT REPLEY");
+                                    if(result.getString("state").equals("reply")) {
                                         
                                         if(result.getString("data") == null || result.getString("data").equals("") || result.getString("data").equals("null")) {
                                             outputResult(response, 601, requestId, null, true);
                                             finished = true;
                                         } else {
-                                            outputResult(response, Integer.parseInt((String) jsonAnswer.get("result")), requestId, function.defragJSON(result.getString("data")), true);
+                                            HashMap sqlResult = function.defragJSON(result.getString("data"));
+                                            if(sqlResult.get("confirmation").equals("approved")) {
+                                                outputResult(response, 0, requestId, function.defragJSON(result.getString("data")), false);
+                                            }
+                                            else {
+                                                outputResult(response, 1, requestId, function.defragJSON(result.getString("data")), false);
+                                            }
+                                            sql.threadUpdate(requestId, "done", "{\"result\":0}");
                                             finished = true;
                                             break;
                                         }
@@ -162,7 +171,6 @@ public class SAS extends HttpServlet {
                                     }
                                 }
                                 else {
-                                    //System.out.println("GOT INVALID RESULT");
                                     outputResult(response, 602, requestId, null, true);
                                     finished = true;
                                     break;
@@ -175,6 +183,7 @@ public class SAS extends HttpServlet {
                             }
                             
                         } else {
+                            outputResult(response, 2, requestId, null, false);
                             System.out.println("SENDING EMAIL");
                             tmpJSON = new JSONObject();
                                 tmpJSON.put("function", "sendmail");
@@ -188,22 +197,24 @@ public class SAS extends HttpServlet {
                                     + "<li>Download and install our app at: "+function.getURL("STORE")+"</li>"
                                     + "<li>Start it: "+function.getURL("APP")+"</li></ol>"
                                     + "We currently don't support iPhone. Sorry for the inconvience.<br><br>"
+                                    + "Your registration code is: <b>"+function.genRegCode(json.get("username").toString())+"</b>.<br><br>"
                                     + "Regards,<br>"
                                     + "The IT Security department");
                             tmpBody = tmpJSON.toJSONString();
                             jsonAnswer = function.defragJSON(function.makeRequest("POST", function.getURL("APS"), tmpBody));
+                            System.out.println(jsonAnswer);
                             if(jsonAnswer.get("result").equals("0")) {
                                     String state;
                                     String data;
                                     
-                                    for(i=1; i<=timeout && !finished; i++) {
+                                    for(i=1; i<=300 && !finished; i++) {
                                         result = sql.stmt.executeQuery("SELECT state,data FROM thread WHERE threadId='"+requestId+"'");
                                         if(result.first()) {
                                             state = result.getString("state");
                                             data = result.getString("data");
-                                            if(state.equals("repley")) {
-                                                answer = function.makeRequest("POST", function.getURL("APS"), data);
-                                                outputResult(response, 0, requestId, null, true);
+                                            if(state.equals("request")) {
+                                                System.out.println("Got response");
+                                                sql.threadUpdate(requestId, "reply", function.makeRequest("POST", function.getURL("APS"), data));
                                                 finished = true;
                                             } else {
                                                 System.out.println("WAITING "+i+" SECONDS");
@@ -216,7 +227,7 @@ public class SAS extends HttpServlet {
                                         }
                                     }
                                     System.out.println("WAITED "+i+" SECONDS");
-                                    if(i > timeout) {
+                                    if(i > 300) {
                                         outputResult(response, 950, requestId, null, true);
                                     }
                             } else {
@@ -225,34 +236,98 @@ public class SAS extends HttpServlet {
                             }
                         }
                     } else {
-                        outputResult(response, 1, requestId, null, true);
+                        outputResult(response, Integer.parseInt((String)jsonAnswer.get("result")), requestId, null, true);
                     }
                     
                     break;
                 case "register":
                     System.out.println("##### SAS >> REGISTER");
                     if((ldapError = ldap.userCheck()) == 0) {
-                        sql.threadUpdate(requestId, "repley", reqBody);
-                        outputResult(response, 0, requestId, null, false);
-                        /*result = sql.stmt.executeQuery("SELECT state,data FROM thread WHERE threadId='"+requestId+"'");
-                            if(result.first()) {
-                                int resultCode = result.getInt("result");
-                                outputResult(response, resultCode, requestId, null, false); 
+                        String username = json.get("username").toString();
+                        String regCode = json.get("registerCode").toString();
+                        if(username != null && regCode != null) {
+                            if(function.checkRegCode(regCode, username)) {
+                                System.out.println("REGCODE PASSED");
+                                sql.threadUpdate(req, "request", reqBody);
+                                String state;
+                                HashMap data;
+                                for(i=1; i<=10; i++) {
+                                    System.out.println("LOOP STARTED");
+                                        result = sql.stmt.executeQuery("SELECT state,data FROM thread WHERE threadId='"+requestId+"'");
+                                        if(result.first()) {
+                                            System.out.println("GOT SQL");
+                                            state = result.getString("state");
+                                            System.out.println(state);
+                                            if(state.equals("reply")) {
+                                                System.out.println("Got final response");
+                                                data = function.defragJSON(result.getString("data"));
+                                                outputResult(response, Integer.parseInt(data.get("result").toString()), requestId, null, true);
+                                                System.out.println("OUTPUT");
+                                                return;
+                                            } else {
+                                                System.out.println("REG WAITING "+i+" SECONDS");
+                                                Thread.sleep(1000);
+                                            }
+                                            
+                                        } else {
+                                            outputResult(response, 602, requestId, null, true);
+                                        }
+                                    }
+                                if(i > 10) {
+                                    outputResult(response, 950, requestId, null, true);
+                                }
                             }
                             else {
-                                outputResult(response, 602, requestId, null, true);
-                            }*/
+                                outputResult(response, 5, requestId, null, true);
+                            }
+                        }
+                        else {
+                            outputResult(response, 560, requestId, null, true);
+                        }
                     } else {
                         outputResult(response, ldapError, requestId, null, true);
                     }
                     break;
                 case "confirm":
                     System.out.println("##### SAS >> CONFIRM");
-                    //System.out.println("Body: "+reqBody);
-                    sql.threadUpdate(requestId, "repley", reqBody);
-                    //sql.stmt.execute("UPDATE thread set `state`='repley',`data`="+function.getBody(request.getReader())+" WHERE `threadId`='"+requestId+"'");
+                    sql.threadUpdate(requestId, "reply", reqBody);
+                    String state;
+                    HashMap data;
+                    for(i=1; i<=10; i++){
+                        result = sql.stmt.executeQuery("SELECT state,data FROM thread WHERE threadId='"+requestId+"'");
+                        if(result.first()) {
+                            System.out.println("GOT SQL");
+                            state = result.getString("state");
+                            System.out.println(state);
+                            if(state.equals("done")) {
+                                System.out.println("Got final response");
+                                data = function.defragJSON(result.getString("data"));
+                                System.out.println(data);
+                                outputResult(response, Integer.parseInt(data.get("result").toString()), requestId, null, true);
+                                System.out.println("OUTPUT");
+                                return;
+                            } else {
+                                System.out.println("REG WAITING "+i+" SECONDS");
+                                Thread.sleep(1000);
+                            }
+                                            
+                        } else {
+                            outputResult(response, 602, requestId, null, true);
+                        }
+                    }
+                    if(i > 10) {
+                        outputResult(response, 950, requestId, null, true);
+                    }
+                    
+                    
+                    outputResult(response, 0, requestId, null, false);
+                    break;
+                case "clean":
+                    sql.clean();
                     outputResult(response, 0, requestId, null, false);
                     break;
+                default:
+                    outputResult(response, 555, requestId, null, true);
             }
         
             
@@ -269,8 +344,6 @@ public class SAS extends HttpServlet {
         array.put("resultText", code.getCodeText(errorCode));
         array.put("requestId", requestId);
         
-
-        //extra.forEach((k, v) -> System.out.println("key: "+k+" value:"+v));
         if(extra != null) {
             if(extra.containsKey("confirmation")) {
                 array.put("confirmation", extra.get("confirmation"));
@@ -287,7 +360,6 @@ public class SAS extends HttpServlet {
             sql.threadUpdate(requestId, "terminate", null);
         }
          try (PrintWriter out = response.getWriter()) {
-            /* TODO output your page here. You may use following sample code. */
             out.println(array);
         } 
     }

+ 12 - 4
Java/SQL.java → SAS:APS Server/SQL.java

@@ -1,7 +1,16 @@
+/*
+ * Feel free to copy/use it for your own project.
+ * Keep in mind that it took me several days/weeks, beers and asperines to make this.
+ * So be nice, and give me some credit, I won't bite and it won't hurt you.
+ */
 import java.sql.*;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
+/**
+ *
+ * @author Deben Oldert
+ */
 
 public class SQL
 {
@@ -10,7 +19,6 @@ public class SQL
   SQL() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException
   {
       try {
-          // load the sqlite-JDBC driver using the current class loader
           Class.forName("com.mysql.jdbc.Driver");
           conn = DriverManager.getConnection("jdbc:mysql://192.168.2.240:3306/sas?user=Implementation&password=Test123456");
           stmt = conn.createStatement();
@@ -24,20 +32,20 @@ public class SQL
   public void threadUpdate(String threadId, String state, String data) throws SQLException, ClassNotFoundException {
       switch (state) {
           case "terminate":
-              
               stmt.execute("DELETE FROM thread WHERE threadId='"+threadId+"'");
               System.out.println("SQL ==> TERMINATE");
               break;
           case "birth":
-              
               stmt.execute("INSERT INTO thread (threadId,state,`data`) VALUES ('"+threadId+"','"+state+"','"+data+"')");
               System.out.println("SQL ==> BIRTH");
               break;
           default:
-              
               stmt.execute("UPDATE thread set `state`='"+state+"',`data`='"+data+"' WHERE `threadId`='"+threadId+"'");
               System.out.println("SQL ==> UPDATE");
               break;
       }
   }
+  public void clean() throws SQLException {
+      stmt.execute("TRUNCATE TABLE thread");
+  }
 }

BIN
SAS:APS Server/exports/Implementation.war