From 41a5656cf8ca6d710f574e8dc67339db8321f79b Mon Sep 17 00:00:00 2001
From: Dominique Blouin <dominique.blouin@telecom-paristech.fr>
Date: Mon, 1 Jun 2015 18:30:41 +0000
Subject: [PATCH] saving address of database into config.xml, check length
 input of query

---
 bin/config.xml                        |  2 +-
 src/myutil/externalSearch/Client.java | 31 +++++++++++------
 src/ui/ConfigurationTTool.java        | 46 +++++++++++++++++++++++++
 src/ui/TDiagramMouseManager.java      | 48 ++++++++++++++-------------
 src/ui/window/JDialogSearchBox.java   | 45 +++++++++++++++----------
 5 files changed, 121 insertions(+), 51 deletions(-)

diff --git a/bin/config.xml b/bin/config.xml
index 169b6f84d7..6cc77d7a23 100755
--- a/bin/config.xml
+++ b/bin/config.xml
@@ -63,7 +63,7 @@
 
 <LastOpenFile data="/home/trhuy/TURTLE/stuxnet.xml"/>
 
-
+<ExternalServer data="localhost:1587"/>
 
 <LastWindowAttributes x="-1" y="25" width="1368" height="743" max="true" />
 
diff --git a/src/myutil/externalSearch/Client.java b/src/myutil/externalSearch/Client.java
index 82c279cec3..71501b3b0f 100644
--- a/src/myutil/externalSearch/Client.java
+++ b/src/myutil/externalSearch/Client.java
@@ -14,6 +14,8 @@ import java.io.ObjectOutputStream;
 import java.util.ArrayList;
 import java.util.logging.Level;
 import java.util.logging.Logger;
+import javax.net.ssl.SSLSocket;
+import javax.net.ssl.SSLSocketFactory;
 
 
 
@@ -38,7 +40,11 @@ public class Client {
         } else if (cmd.equals(Message.RESULT_SEARCH)) {
             ArrayList<Record> lrecord = new ArrayList<>();
             try {
-                byte[] encoded = Files.readAllBytes(Paths.get("/home/trhuy/Downloads/02-51-34.xml"));
+                //byte[] encoded = Files.readAllBytes(Paths.get("/home/trhuy/Downloads/02-51-34.xml"));
+
+
+                byte[] encoded = (byte[])answerMsg.getContent().get(0);
+
                 String resultxml = new String(encoded, "UTF-8");
 
                 Document doc = Jsoup.parse(resultxml);
@@ -61,7 +67,8 @@ public class Client {
         } else if (cmd.equals(Message.RESULT_DETAIL)) {
             Record r = new Record();
             try {
-                byte[] encoded = Files.readAllBytes(Paths.get("/home/trhuy/Downloads/02-40-06.xml"));
+                //byte[] encoded = Files.readAllBytes(Paths.get("/home/trhuy/Downloads/02-40-06.xml"));
+                byte[] encoded = (byte[])answerMsg.getContent().get(0);
                 String resultxml = new String(encoded, "UTF-8");
 
                 Document doc = Jsoup.parse(resultxml);
@@ -114,10 +121,9 @@ public class Client {
         } else if (cmd.equals(Message.RESULT_STATISTIC)) {
             //show picture
             //Use a function to convert binary to image
-            ArrayList<Object> resultContent = new ArrayList();
-            resultContent = answerMsg.getContent();
-            byte[] imgByte = (byte[]) resultContent.get(0);
-            Message.convertByteToImage(imgByte);
+            byte[] encoded = (byte[]) answerMsg.getContent().get(0);
+
+            return (Object)encoded;
 
             //Call Huy's function to load Image
         } else System.out.print(Message.ERR_CMD2);
@@ -134,12 +140,17 @@ public class Client {
     }
 
     public Message send(Message msg){
+        SSLSocket sslClient = null;
         try {
-            Socket client = new Socket(dbaddr,dpport);
+           // Socket client = new Socket(dbaddr,dpport);
+            SSLSocketFactory sslSocketFactory = (SSLSocketFactory)SSLSocketFactory.getDefault();
+            sslClient = (SSLSocket)sslSocketFactory.createSocket("LocalHost",12345);
+
+            sslClient.setEnabledCipherSuites(sslClient.getSupportedCipherSuites());
             System.out.println("Client has been created successfully!");
 
-            ObjectOutputStream outputStream = new ObjectOutputStream(client.getOutputStream());
-            ObjectInputStream inputStream = new ObjectInputStream(client.getInputStream());
+            ObjectOutputStream outputStream = new ObjectOutputStream(sslClient.getOutputStream());
+            ObjectInputStream inputStream = new ObjectInputStream(sslClient.getInputStream());
 
             outputStream.writeObject(msg);
 
@@ -154,7 +165,7 @@ public class Client {
 
             outputStream.close();
             inputStream.close();
-            client.close();
+            sslClient.close();
             return answerMsg;
 
         } catch (UnknownHostException e) {
diff --git a/src/ui/ConfigurationTTool.java b/src/ui/ConfigurationTTool.java
index 12e9b3a124..df62481dd6 100755
--- a/src/ui/ConfigurationTTool.java
+++ b/src/ui/ConfigurationTTool.java
@@ -145,6 +145,8 @@ public class ConfigurationTTool {
 	public static String LastWindowAttributesMax="";
     
     public static String fileName = "";
+
+	public static String ExternalServer="";
     
     public static void makeDefaultConfiguration() {
     	//System.out.println(Paths.get("").toAbsolutePath().toString());
@@ -214,6 +216,31 @@ public class ConfigurationTTool {
                 }
             }
         }
+        //---------------------------------------------
+        index0 = data.indexOf("ExternalServer");
+
+        //sb.append("data = " + data + " ConfigurationTTool.LastOpenFile=" + ConfigurationTTool.LastOpenFile);
+
+        if (index0 > -1) {
+            index1 = data.indexOf('"', index0);
+            if (index1 > -1) {
+                index2 = data.indexOf('"', index1 + 1);
+                if (index2 > -1) {
+                    tmp = data.substring(index2, data.length());
+                    data = data.substring(0, index1+1) + ConfigurationTTool.ExternalServer+ tmp;
+                    //sb.append("data = " + data);
+                    write = true;
+					/*try {
+					FileOutputStream fos = new FileOutputStream(f);
+					fos.write(data.getBytes());
+					fos.close();
+					} catch (Exception e) {
+					throw new  MalformedConfigurationException("Saving file failed");
+					}*/
+                }
+            }
+        }
+        //--------------------------
 		
 		index0 = data.indexOf("LastWindowAttributes");
 		if (index0 > -1) {
@@ -401,6 +428,9 @@ public class ConfigurationTTool {
 		sb.append("LastWindowAttributesHeight: " + LastWindowAttributesHeight + "\n");
 		sb.append("LastWindowAttributesMax: " + LastWindowAttributesMax + "\n");
 		
+		//Huy Truong
+		sb.append("ExternalServer " + ExternalServer + "\n"); 
+		
 		return sb.toString();
         
     }
@@ -650,6 +680,10 @@ public class ConfigurationTTool {
 			if (nl.getLength() > 0)
 				LastWindowAttributes(nl);
 			
+			nl = doc.getElementsByTagName("ExternalServer");
+			if (nl.getLength() >0)
+				ExternalServer(nl);
+			
 			
         } catch (Exception e) {
             throw new MalformedConfigurationException(e.getMessage());
@@ -1287,6 +1321,18 @@ public class ConfigurationTTool {
             throw new MalformedConfigurationException(e.getMessage());
         }
     }
+	
+
+    private static void ExternalServer(NodeList nl) throws MalformedConfigurationException {
+        try {
+            Element elt = (Element) (nl.item(0));
+            ExternalServer = elt.getAttribute("data");
+            if (ExternalServer == "")
+                ExternalServer = "localhost:9999";
+        } catch (Exception e) {
+            throw new MalformedConfigurationException(e.getMessage());
+        }
+    }
     
     public static boolean  isConfigured(String s) {
     	return ((s != null) && (s.trim().length() > 0));
diff --git a/src/ui/TDiagramMouseManager.java b/src/ui/TDiagramMouseManager.java
index 9732205ca9..6db772510b 100755
--- a/src/ui/TDiagramMouseManager.java
+++ b/src/ui/TDiagramMouseManager.java
@@ -344,6 +344,31 @@ public class TDiagramMouseManager implements MouseListener, MouseMotionListener
 	public void mouseClicked(MouseEvent e) {
 		//System.out.println("MouseClick: " + e.getClickCount());
 
+		//open a Search Dialog with seleted component's value
+		if ( (e.getButton() == MouseEvent.BUTTON1) && e.isAltDown()) {
+			byte info = tdp.hoveredComponent(e.getX(), e.getY());
+			System.out.print (info);
+			if (info > 1) {
+				tgc = tdp.componentHovered();
+				String search = tgc.getValue();
+
+				if (tdp.mgui.searchBox==null ) {
+					tdp.mgui.searchBox = new JDialogSearchBox(tdp.getGUI().getFrame(), "Search Box", selectedMultiComponents, this);
+				}
+				else {
+					if (tdp.mgui.searchBox.isShowing()) {
+						tdp.mgui.searchBox.addValueListKeyword(search);
+						tdp.mgui.searchBox.show();
+					} else{
+						tdp.mgui.searchBox=null;
+						tdp.mgui.searchBox = new JDialogSearchBox(tdp.getGUI().getFrame(), "Search Box", selectedMultiComponents, this);
+
+					}
+				}
+
+			}
+		}
+
 
 		//author:Huy TRUONG
 		//add component's value into list by clicking on component and holding Ctrl 
@@ -372,32 +397,9 @@ public class TDiagramMouseManager implements MouseListener, MouseMotionListener
 			selectedMultiComponents.clear();
 			// clear the list when release Ctrl and click
 			
-		
-
-		//open a Search Dialog with seleted component's value
-		if (tdp.mode == TDiagramPanel.NORMAL && e.isAltDown()) {
-			byte info = tdp.hoveredComponent(e.getX(), e.getY());
-			
-			if (info > 1) {
-				tgc = tdp.componentHovered();
-				String search = tgc.getValue();
 
-                if (tdp.mgui.searchBox==null ) {
-                    tdp.mgui.searchBox = new JDialogSearchBox(tdp.getGUI().getFrame(), "Search Box", selectedMultiComponents, this);
-                }
-                else {
-                    if (tdp.mgui.searchBox.isShowing()) {
-                        tdp.mgui.searchBox.addValueListKeyword(search);
-                        tdp.mgui.searchBox.show();
-                    } else{
-                        tdp.mgui.searchBox=null;
-                        tdp.mgui.searchBox = new JDialogSearchBox(tdp.getGUI().getFrame(), "Search Box", selectedMultiComponents, this);
 
-                    }
-                }
 
-			}
-		}
 		//--
 		
 
diff --git a/src/ui/window/JDialogSearchBox.java b/src/ui/window/JDialogSearchBox.java
index 5bb9935ad3..c191db2eaa 100644
--- a/src/ui/window/JDialogSearchBox.java
+++ b/src/ui/window/JDialogSearchBox.java
@@ -76,6 +76,8 @@ import myutil.CheckConnection;
 //import myutil.Message;
 //import myutil.Client;
 import myutil.externalSearch.Record;
+import ui.ConfigurationTTool;
+import ui.MalformedConfigurationException;
 import ui.TDiagramMouseManager;
 import myutil.externalSearch.Message;
 import myutil.externalSearch.Client;
@@ -180,6 +182,7 @@ public class JDialogSearchBox extends javax.swing.JFrame  {
         removeBt = new javax.swing.JButton();
         searchBt = new javax.swing.JButton();
         searchBox = new javax.swing.JTextField();
+        final int maxlength = 100;
         googleCb = new javax.swing.JCheckBox();
         googleScholarCb = new javax.swing.JCheckBox();
         databaseCb = new javax.swing.JCheckBox();
@@ -224,8 +227,8 @@ public class JDialogSearchBox extends javax.swing.JFrame  {
         dbport = default_port;
 
 
-        jTextaddressDB.setText(dbaddress + ":" + dbport);
-
+        //jTextaddressDB.setText(dbaddress + ":" + dbport);
+        jTextaddressDB.setText(ConfigurationTTool.ExternalServer);
 
         setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
         setResizable(false);
@@ -252,6 +255,7 @@ public class JDialogSearchBox extends javax.swing.JFrame  {
 
         searchBox.setText("Key words");
 
+
         googleCb.setText("Google");
 
         googleScholarCb.setText("Google Scholar");
@@ -508,6 +512,7 @@ public class JDialogSearchBox extends javax.swing.JFrame  {
             }
         });
 
+
         searchBox.getDocument().addDocumentListener(new DocumentListener() {
             @Override
             public void insertUpdate(DocumentEvent documentEvent) {
@@ -525,7 +530,10 @@ public class JDialogSearchBox extends javax.swing.JFrame  {
             }
 
             public void checkandsetSearchBt() {
-                if (searchBox.getText().length() <= 0) {
+                if (searchBox.getText().length() <= 0 || searchBox.getText().length() > maxlength ) {
+                    if (searchBox.getText().length() > maxlength )
+                        JOptionPane.showMessageDialog(null, "The input is too long.", "Warning",
+                                JOptionPane.WARNING_MESSAGE);
                     searchBt.setEnabled(false);
                 } else
                     searchBt.setEnabled(true);
@@ -633,8 +641,9 @@ public class JDialogSearchBox extends javax.swing.JFrame  {
                     msg.addKeywordMessage(st);
 
                     Client cl = new Client();
-                    //Message returnMsg = cl.send(msg);
-                    Message returnMsg  = new Message(Message.RESULT_DETAIL);
+                    Message returnMsg = cl.send(msg);
+                    //Message returnMsg  = new Message(Message.RESULT_DETAIL);
+
                     Record r = (Record)cl.parserAnswerMessage(returnMsg);
                     printDetailRecord(r);
                 }
@@ -675,14 +684,21 @@ public class JDialogSearchBox extends javax.swing.JFrame  {
               JOptionPane joptionpane = new JOptionPane();
               int i = joptionpane.showOptionDialog(null, inputs, "Setup the address of database",
                       joptionpane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
-                      null, new Object[]{"OK", "Cancel"}, "OK");
+                      null, new Object[]{"Save", "Cancel"}, "OK");
               if (i == joptionpane.OK_OPTION) {
                   while (! (jTextaddressDB.getText().contains(":") && jTextaddressDB.getText().split(":").length == 2)) {
                       JOptionPane.showMessageDialog(null, "Address:Port", "Wrong format",
                               JOptionPane.WARNING_MESSAGE);
                       i = joptionpane.showOptionDialog(null, inputs, "Setup the address of database",
                               joptionpane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
-                              null, new Object[]{"OK", "Cancel"}, "OK");
+                              null, new Object[]{"Save", "Cancel"}, "OK");
+                  }
+                  ConfigurationTTool.ExternalServer=jTextaddressDB.getText();
+
+                  try {
+                      ConfigurationTTool.saveConfiguration();
+                  } catch (MalformedConfigurationException e) {
+                      e.printStackTrace();
                   }
               } else if (i == joptionpane.CLOSED_OPTION) {
               }
@@ -910,13 +926,12 @@ public class JDialogSearchBox extends javax.swing.JFrame  {
                     //msg.addKeywordMessage(searchBox.getText());
                     //create client
                     Client client = new Client();
-                    Message msg1 = new Message(Message.RESULT_SEARCH);
-                    //Message returnMsg = client.send(msg1);
+                    //Message msg1 = new Message(Message.RESULT_SEARCH);
+                    Message returnMsg = client.send(msg);
 
                     //Message msg1 = new Message(Message.RESULT_SEARCH);
 
-                    //System.out.print("truoc khi vao parser");
-                    ArrayList<Record> re =  (ArrayList<Record>)client.parserAnswerMessage(msg1);
+                    ArrayList<Record> re =  (ArrayList<Record>)client.parserAnswerMessage(returnMsg);
                     putDBToTable(re);
                     showtable(rowsDB, modelDB,2);
 
@@ -1041,21 +1056,17 @@ public class JDialogSearchBox extends javax.swing.JFrame  {
     public void addValueListKeyword(String st){
         if  (! this.listModel.contains(st)){
         	this.listModel.addElement(st);
-        	
+
         	String query = "";
-        	
+
         	if (this.listModel.getSize()>0)
                 query = splitAndConcat((String) this.listModel.elementAt(0));
-                System.out.println(query);
     	    	for (int i=1; i< this.listModel.getSize(); i++ ){
                     if (query != "")
     	    		    query= query + " + " + splitAndConcat((String) this.listModel.elementAt(i));
                     else
                         query = splitAndConcat((String) this.listModel.elementAt(i));
     	    	}
-
-            System.out.println(query);
-    	    	
         	this.searchBox.setText(query);
         }
     	
-- 
GitLab