xmlOperation es=new xmlOperation();
File car=new File(global.listaEsercizi);
if (car.exists()) {
- dbConnect db=new dbConnect();
try {
- ArrayList<String> esercizi=suString.stringToArrayList(db.dbQuery("4"));
+ ArrayList<String> esercizi=suString.stringToArrayList(global.request.dbQuery("4"));
boolean add=true;
for(String esercizio : esercizi)
{
}
}
}
- 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)
{
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()
}
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);
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/";
public static final String clEsercizio = "esercizio";
public static final String clVoto = "voto";
}
-
}
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);
}
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();
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<String, String> 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();
}
}
{
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)
try
{
e.printStackTrace();
- dbConnect request=new dbConnect();
- request.dbQuery("7/"+key);
}
catch (Exception k)
{
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);
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!");
}
}
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