//© Copyright 2017 DXC Technology Company. All rights reserved. package it.mctc.qp.se.server.test.gui; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.ProtocolException; import java.net.URL; import java.util.Collection; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.Map; import java.util.StringTokenizer; import java.util.Vector; import it.mctc.tms.domain.bean.tms.AssignedQuestion; import it.mctc.tms.domain.bean.tms.Candidate; import it.mctc.tms.domain.bean.tms.Question; import it.mctc.tms.domain.bean.tms.QuestionResult; import it.mctc.tms.domain.bean.tms.Test; import it.mctc.tms.domain.bean.tms.TestResult; /** * The Class Runner. */ public class Runner implements Runnable { /** The contatore. */ int contatore = 0; /** The cf. */ String cf = ""; /** The sessione. */ String sessione = ""; /** * Instantiates a new runner. * * @param contatore * the contatore * @param cf * the cf */ public Runner(int contatore, String cf) { this.contatore = contatore; this.cf = cf; } /* * (non-Javadoc) s * * @see java.lang.Runnable#run() */ @Override public void run() { try { Candidate ricevuto = (Candidate) login(cf); // sessione(ricevuto, sessione); if (ricevuto != null) { printSize(ricevuto, "Candidate"); Test test = caricaEsame(sessione, ricevuto); printSize(test, "Test"); // Thread.sleep(1000); System.out.println("itrazione candidato login: " + contatore + " --- " + ricevuto.getId()); for (int i = 0; i < 10; i++) { // Thread.sleep(1500); System.out.println(">>risposte: " + contatore + " --" + i); risposte(sessione, test); System.out.println("< 0) { String nuova = el.substring(ind, el.length()); return nuova; } } } } return null; } private void chiudiSessione(String sessione) throws MalformedURLException, IOException, ProtocolException, ClassNotFoundException { URL url = new URL("http://sistema.a1.local:8080/candidato/ServletChiusuraSessione;" + sessione); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestProperty("Content-Type", "application/octet-stream"); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Length", Integer.toString(calcolaLunghezzaOggetto(new Boolean(false)))); ObjectOutputStream objectOutputStream = new ObjectOutputStream(conn.getOutputStream()); objectOutputStream.writeObject(new Boolean(false)); // gzipp.finish(); objectOutputStream.close(); ObjectInputStream ois = new ObjectInputStream(conn.getInputStream()); // pis.getProgressMonitor().setMaximum(servletConnection.getContentLength()); Object ricevuto = ois.readObject(); ois.close(); // pis.close(); // gzip.close(); conn.disconnect(); } private Test caricaEsame(String sessione, Candidate candidate) throws MalformedURLException, IOException, ProtocolException, ClassNotFoundException { URL url = new URL("http://sistema.a1.local:8080/candidato/ServletCaricaEsame;" + sessione); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestProperty("Content-Type", "application/octet-stream"); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Length", Integer.toString(calcolaLunghezzaOggetto(candidate))); ObjectOutputStream objectOutputStream = new ObjectOutputStream(conn.getOutputStream()); objectOutputStream.writeObject(candidate); // gzipp.finish(); objectOutputStream.close(); ObjectInputStream ois = new ObjectInputStream(conn.getInputStream()); // pis.getProgressMonitor().setMaximum(servletConnection.getContentLength()); Object ricevuto = ois.readObject(); ois.close(); // pis.close(); // gzip.close(); conn.disconnect(); return (Test) ricevuto; } /** * Risposte. * * @param sessione * the sessione * @param candidate * the candidate * @throws MalformedURLException * the malformed URL exception * @throws IOException * Signals that an I/O exception has occurred. * @throws ProtocolException * the protocol exception * @throws ClassNotFoundException * the class not found exception */ private void risposte(String sessione, Test test) throws MalformedURLException, IOException, ProtocolException, ClassNotFoundException { TestResult testResult = calcola(test); printSize(testResult, "TestResult"); // // Asincrona asincrona = new Asincrona(Esame.getInstance(), Esame.SERVLETRISPOSTE, testResult); // asincrona.setServlet(new URL("http://sistema.a1.local:8080/candidato/")); // asincrona.addObserver(Esame.getInstance()); // asincrona.setOutput(testResult); // // new Thread(asincrona).start(); URL url = new URL("http://ucqzpc02.sidt.local:8080/candidato/ServletRisposte;" + sessione); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestProperty("Content-Type", "application/octet-stream"); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Length", Integer.toString(calcolaLunghezzaOggetto(testResult))); ObjectOutputStream objectOutputStream = new ObjectOutputStream(conn.getOutputStream()); objectOutputStream.writeObject(testResult); // gzipp.finish(); objectOutputStream.close(); ObjectInputStream ois = new ObjectInputStream(conn.getInputStream()); // pis.getProgressMonitor().setMaximum(servletConnection.getContentLength()); System.out.println(ois.readObject()); ois.close(); // pis.close(); // gzip.close(); conn.disconnect(); } /** * Calcola lunghezza oggetto. * * @param oggetto * the oggetto * @return the int * @throws IOException * Signals that an I/O exception has occurred. */ public int calcolaLunghezzaOggetto(Object oggetto) throws IOException { ByteArrayOutputStream outb = new ByteArrayOutputStream(); ObjectOutputStream pout = new ObjectOutputStream(outb); // GZIPOutputStream zip = new GZIPOutputStream(pout); pout.writeObject(oggetto); // zip.finish(); int lenZippata = outb.toByteArray().length; // zip.close(); pout.close(); outb.close(); return lenZippata; } private TestResult calcola(Test test) { Test t = new Test(); // test t.setId(test.getId()); Candidate c = new Candidate(); // candidato c.setId(test.getCandidate().getId()); t.setCandidate(c); TestResult tr = new TestResult(); // TestResultTO tr.setId(t.getId()); Question question; tr.setTest(t); Collection qresults = new Vector (); // question results Iterator it = test.getAssignedquestions().iterator(); Collection domande = new Vector (); while (it.hasNext()) { AssignedQuestion currentAssignedQuestion = (AssignedQuestion) it.next(); AssignedQuestion assignedQuestion = new AssignedQuestion(); // domanda assignedQuestion.setId(currentAssignedQuestion.getId()); question = new Question(); question.setId(currentAssignedQuestion.getQuestion().getId()); assignedQuestion.setQuestion(question); QuestionResult questionResult = new QuestionResult(); // risposta questionResult.setAssignedQuestion(assignedQuestion); questionResult.setId(currentAssignedQuestion.getId()); questionResult.setGivenanswer(currentAssignedQuestion.getQuestionResult().getGivenanswer()); // per ora fissa TestResult trs = new TestResult(); trs.setId(test.getId()); questionResult.setTestResult(trs); domande.add(assignedQuestion); qresults.add(questionResult); } t.setAssignedquestions(new LinkedHashSet (domande)); tr.setQuestionresults(new LinkedHashSet (qresults)); tr.setTest(t); tr.setUserData("esitoEsame", "ID"); tr.setUserData("risultatoEsame", "ID"); tr.setUserData("ChiusuraEffettuataDa", "me"); return tr; } private void printSize(Object object, String type) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(baos); out.writeObject(object); out.flush(); int objectsize = baos.toByteArray().length - 4; System.out.println("MyObject " + type + " occupies " + objectsize + " byte when serialized"); } catch (Exception e) { System.out.println("error " + e); } } }