From 8dbc676367c5e63c1ebcabd00556497cf6f04fd7 Mon Sep 17 00:00:00 2001 From: Claudio Sacerdoti Coen Date: Tue, 9 Dec 2014 15:12:12 +0100 Subject: [PATCH] New version --- server/com/company/DirectoryWhatcer.java | 6 +-- server/com/company/dbConnect.java | 19 +++++-- server/com/company/global.java | 5 +- server/com/company/guestServer.java | 14 +---- server/com/company/httpget.java | 34 +++++------- server/com/company/personalServer.java | 7 +-- server/com/company/superServer.java | 67 ++++++++++++++---------- server/com/company/xmlOperation.java | 11 ++-- 8 files changed, 83 insertions(+), 80 deletions(-) diff --git a/server/com/company/DirectoryWhatcer.java b/server/com/company/DirectoryWhatcer.java index 1d219b2..630c908 100644 --- a/server/com/company/DirectoryWhatcer.java +++ b/server/com/company/DirectoryWhatcer.java @@ -39,9 +39,8 @@ class DirectoryWatcher implements Runnable { xmlOperation es=new xmlOperation(); File car=new File(global.listaEsercizi); if (car.exists()) { - dbConnect db=new dbConnect(); try { - ArrayList esercizi=suString.stringToArrayList(db.dbQuery("4")); + ArrayList esercizi=suString.stringToArrayList(global.request.dbQuery("4")); boolean add=true; for(String esercizio : esercizi) { @@ -66,9 +65,8 @@ class DirectoryWatcher implements Runnable { } } } - dbConnect db=new dbConnect(); try { - String[] esercizi=suString.stringToVectorString(db.dbQuery("4")); + String[] esercizi=suString.stringToVectorString(global.request.dbQuery("4")); boolean delete=true; for(String esercizio : esercizi) { diff --git a/server/com/company/dbConnect.java b/server/com/company/dbConnect.java index e7c7e20..c89c18e 100644 --- a/server/com/company/dbConnect.java +++ b/server/com/company/dbConnect.java @@ -18,7 +18,21 @@ public class dbConnect private static Connection db; private static Statement stmt; dbConnect() - {} + { + try { + Class.forName("com.mysql.jdbc.Driver").newInstance(); + db = DriverManager.getConnection(global.dbConstant.dbLink + global.dbConstant.dbName, global.dbConstant.dbAdmin, global.dbConstant.dbPassword); + stmt = db.createStatement(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (SQLException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } public static Long time() @@ -319,9 +333,6 @@ public class dbConnect } public String dbQuery(String input) throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException, IOException, NoSuchAlgorithmException { - Class.forName("com.mysql.jdbc.Driver").newInstance(); - db = DriverManager.getConnection(global.dbConstant.dbLink+global.dbConstant.dbName, global.dbConstant.dbAdmin, global.dbConstant.dbPassword); - stmt = db.createStatement(); deletetmp(); if (input.charAt(0) == '0') return splitUsrPass(input.substring(2), 0); diff --git a/server/com/company/global.java b/server/com/company/global.java index 304f970..7c82d27 100644 --- a/server/com/company/global.java +++ b/server/com/company/global.java @@ -2,6 +2,10 @@ package com.company; public class global { + //variabili globali per utilizzare la dbConnect + public static dbConnect request; + //********************************************* + public static final String listaEsercizi = "/projects/logicplayer/server/com/company/esercizi.xml"; public static final String locationEsercizi = "/projects/logicplayer/public_html/esercizi/"; public static String home = "/projects/logicplayer/server/com/company/"; @@ -38,5 +42,4 @@ public class global { public static final String clEsercizio = "esercizio"; public static final String clVoto = "voto"; } - } diff --git a/server/com/company/guestServer.java b/server/com/company/guestServer.java index 1fb6ce9..ee7f79e 100644 --- a/server/com/company/guestServer.java +++ b/server/com/company/guestServer.java @@ -24,24 +24,14 @@ class guestServer implements Runnable BufferedReader br = new BufferedReader(isr); String requestMessage = br.readLine(); System.out.println("ho ricevuto: "+requestMessage); - /*System.out.println("Message received from client is "+returnMessage); - dbConnect request=new dbConnect(); - String returnQuery=request.dbQuery(returnMessage); - OutputStream os = socket.getOutputStream(); - OutputStreamWriter osw = new OutputStreamWriter(os); - BufferedWriter bw = new BufferedWriter(osw); - System.out.println(returnQuery); - bw.write(returnQuery+"\n"); - bw.flush();*/ if(requestMessage!=null) { - dbConnect request = new dbConnect(); String returnQuery; if ( requestMessage.charAt(0) == '0' || requestMessage.charAt(0) == '1' || requestMessage.charAt(0) == '2' || (requestMessage.charAt(0) == 'a' && requestMessage.charAt(1) == '0')) { - returnQuery = request.dbQuery(requestMessage); + returnQuery = global.request.dbQuery(requestMessage); OutputStream os = socket.getOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(os); BufferedWriter bw = new BufferedWriter(osw); @@ -51,7 +41,7 @@ class guestServer implements Runnable } else { - returnQuery = request.dbQuery("5/" + requestMessage); + returnQuery =global.request.dbQuery("5/" + requestMessage); if (returnQuery.contains("request-login") || returnQuery.contains("request-error")) { OutputStream os = socket.getOutputStream(); diff --git a/server/com/company/httpget.java b/server/com/company/httpget.java index 079cc01..fa3b431 100644 --- a/server/com/company/httpget.java +++ b/server/com/company/httpget.java @@ -33,44 +33,36 @@ public class httpget implements Runnable { server.createContext("/test", new MyHandler()); server.setExecutor(null); // creates a default executor server.start(); - }catch (Exception e) - { + } catch (IOException e) { e.printStackTrace(); } - } + } static class MyHandler implements HttpHandler { public void handle(HttpExchange t) throws IOException { - - /*String response = "This is the response"; - System.out.println("mannaggia santa"); - t.sendResponseHeaders(200, response.length()); - OutputStream os = t.getResponseBody(); - os.write(response.getBytes());*/ URI req=t.getRequestURI(); String query=req.getQuery(); Map result=splitQuery(query); String id=result.get("id"); - //System.out.println(id); try { - dbConnect request=new dbConnect(); - String returnQuery=request.dbQuery("3"+id); + + String returnQuery=global.request.dbQuery("3"+id); t.sendResponseHeaders(200, returnQuery.length()); OutputStream os = t.getResponseBody(); os.write(returnQuery.getBytes()); os.close(); - } - catch(SQLException e) - {} - catch(ClassNotFoundException c) - {} - catch(IllegalAccessException a) - {} - catch(InstantiationException i) - {} catch (NoSuchAlgorithmException e) { + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (SQLException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { e.printStackTrace(); } } diff --git a/server/com/company/personalServer.java b/server/com/company/personalServer.java index 1d338eb..5dfccf0 100644 --- a/server/com/company/personalServer.java +++ b/server/com/company/personalServer.java @@ -27,15 +27,14 @@ class personalServer implements Runnable { try { - dbConnect request=new dbConnect(); - String returnQuery=request.dbQuery(requestMessage); + String returnQuery=global.request.dbQuery(requestMessage); OutputStream os = socket.getOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(os); BufferedWriter bw = new BufferedWriter(osw); bw.write(returnQuery+"\n"); bw.flush(); System.out.println("ho inviato: "+returnQuery); - request.dbQuery("6/"+key); + global.request.dbQuery("6/"+key); socket.close(); } catch (Exception e) @@ -43,8 +42,6 @@ class personalServer implements Runnable try { e.printStackTrace(); - dbConnect request=new dbConnect(); - request.dbQuery("7/"+key); } catch (Exception k) { diff --git a/server/com/company/superServer.java b/server/com/company/superServer.java index bac54fd..0889ed4 100644 --- a/server/com/company/superServer.java +++ b/server/com/company/superServer.java @@ -11,12 +11,11 @@ import java.nio.file.Path; class superServer { - public static void main(String[] args) - { - String ksName = global.home+global.keyStoreName; + public static void main(String[] args) { + String ksName = global.home + global.keyStoreName; char ksPass[] = global.keyStorePass.toCharArray(); char ctPass[] = global.keyStorePass.toCharArray(); - + global.request=new dbConnect(); //thread di controllo directory esercizi Path pathToWatch = FileSystems.getDefault().getPath(global.locationEsercizi); DirectoryWatcher dirWatcher = new DirectoryWatcher(pathToWatch); @@ -24,35 +23,47 @@ class superServer dirWatcherThread.start(); //thread che si occupa del lato http - httpget regServer=new httpget(); - Thread httpServer=new Thread(regServer); + httpget regServer = new httpget(); + Thread httpServer = new Thread(regServer); httpServer.start(); - - while(true) - { - try - { - KeyStore ks = KeyStore.getInstance("JKS"); - ks.load(new FileInputStream(ksName), ksPass); - KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); - kmf.init(ks, ctPass); - SSLContext sc = SSLContext.getInstance("TLS"); - sc.init(kmf.getKeyManagers(), null, null); - SSLServerSocketFactory ssf = sc.getServerSocketFactory(); - SSLServerSocket s= (SSLServerSocket) ssf.createServerSocket(global.portaServer); - System.out.println("Server started:"); - while(true) - { + SSLServerSocket s = null; + try { + KeyStore ks = KeyStore.getInstance("JKS"); + ks.load(new FileInputStream(ksName), ksPass); + KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); + kmf.init(ks, ctPass); + SSLContext sc = SSLContext.getInstance("TLS"); + sc.init(kmf.getKeyManagers(), null, null); + SSLServerSocketFactory ssf = sc.getServerSocketFactory(); + s = (SSLServerSocket) ssf.createServerSocket(global.portaServer); + } catch (CertificateException e) { + e.printStackTrace(); + } catch (UnrecoverableKeyException e) { + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (KeyStoreException e) { + e.printStackTrace(); + } catch (KeyManagementException e) { + e.printStackTrace(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + if (s != null) { + System.out.println("Server started:"); + while (true) { + try { SSLSocket c = (SSLSocket) s.accept(); - guestServer guest=new guestServer(c); - Thread t=new Thread(guest); + guestServer guest = new guestServer(c); + Thread t = new Thread(guest); t.start(); + } catch (IOException e) { + e.printStackTrace(); } } - catch (Exception e) - { - e.printStackTrace(); - } } + System.out.println("Server die!"); } } diff --git a/server/com/company/xmlOperation.java b/server/com/company/xmlOperation.java index f15cd79..7a07f93 100644 --- a/server/com/company/xmlOperation.java +++ b/server/com/company/xmlOperation.java @@ -105,16 +105,17 @@ public class xmlOperation { Document doc = (Document) builder.build(xmlFile); Element rootNode = doc.getRootElement(); - - for(Element esercizio:rootNode.getChildren()) + Element tmp = null; + for(Element esercizio : rootNode.getChildren()) { if(esercizio.getText().contentEquals(name)) { - System.out.println("trovato"); - rootNode.removeContent(esercizio); + tmp=esercizio; + break; } } - + if(tmp!=null) + rootNode.removeContent(tmp); XMLOutputter xmlOutput = new XMLOutputter(); // display nice nice -- 2.39.2