*/
private static void encryptSEL(String userId, String filePath, String users, String tempFileName, boolean firstFile){
if(ServerConfiguration.debug)
JOptionPane.showMessageDialog(null, "ENCRYPT SEL PROCESS - da controllare- users: " + users);
UploadManager uploadManager = null;
try {
SessionManager.saveSession(userId, new UploadManager(users, "0", false));
uploadManager = SessionManager.getUploadManager(userId);
if(ServerConfiguration.debug)
JOptionPane.showMessageDialog(null, "Ora dovrebbe salvare il nuovo ID in IDS");
String newID = uploadManager.prepareUpload();
String ACL = uploadManager.getACL();
if(ServerConfiguration.debug)
JOptionPane.showMessageDialog(null, "ACL: " + ACL);
String nextToken = "";
boolean firstStep = true;
boolean firstAesKey = true;
int aclType = 0;
String jsonoutPath = ServerConfiguration.getTempFilePath() + "/" + tempFileName + "_jsonout.txt";
String jsonPath = ServerConfiguration.getTempFilePath() + "/" + tempFileName + "_json.txt";
String nextPath = ServerConfiguration.getTempFilePath() + "/" + tempFileName + "_next.txt";
String AesKeyPath = ServerConfiguration.getTempFilePath() + "/" + tempFileName + "_AESKey.txt";
String AesKeyFinalPath = ServerConfiguration.getTempFilePath() + "/" + tempFileName + "_AESKeyFinale.txt";
if(firstFile){ //If first file calculate new aesKey final
do {
aclType = uploadManager.firstStepCreateJson();
if(ServerConfiguration.debug)
JOptionPane.showMessageDialog(null, "Tipo di ACL: " + aclType);
switch (aclType)
{
case 1:
if(ServerConfiguration.debug)
JOptionPane.showMessageDialog(null, "CASO 1");
LOGGER.debug("Do cicle: case 1");
boolean moreUsers;
do{
firstStep = true;
boolean destinationACLReached;
do{
String jsonapp = uploadManager.createJsonUpload(nextToken,firstStep);
if(ServerConfiguration.debug)
JOptionPane.showMessageDialog(null, "JSON App: " + jsonapp);
LOGGER.debug("jsonapp: " + jsonapp);
if(jsonapp.compareTo("{\"values\":[]}") != 0) {
//write json.txt
FileUtility.writeFile(jsonPath, jsonapp);
if(firstStep) {
if(firstAesKey) {
if(ServerConfiguration.debug)
JOptionPane.showMessageDialog(null, "firstAESKey");
//run wpes1
String command[] = {
ServerConfiguration.getEXECUTABLES_PATH() + "/wpes1_linux",
"new1",
jsonPath,
PIN,
AesKeyPath,
newID,
nextPath,
ACL
};
RunnerExecutables.execute(command);
//close if firstAesKey
} else {
if(ServerConfiguration.debug)
JOptionPane.showMessageDialog(null, "NOT firstAESKey");
//run wpes1
String command[] = {
ServerConfiguration.getEXECUTABLES_PATH() + "/wpes1_linux",
"new1",
jsonPath,
AesKeyFinalPath,
PIN,
AesKeyPath,
newID,
nextPath,
ACL
};
RunnerExecutables.execute(command);
} //close else firstAesKey
firstStep = false;
// close if firstStep
} else {
if(ServerConfiguration.debug)
JOptionPane.showMessageDialog(null, "non è il primo passo");
if(firstAesKey) {
if(ServerConfiguration.debug)
JOptionPane.showMessageDialog(null, "firstAESKey");
//run wpes1
String command[] = {
ServerConfiguration.getEXECUTABLES_PATH() + "/wpes1_linux",
"newn",
jsonPath,
AesKeyPath,
AesKeyPath,
newID,
nextPath,
ACL
};
RunnerExecutables.execute(command);
// close if firstAesKey
} else {
if(ServerConfiguration.debug)
JOptionPane.showMessageDialog(null, "NOT firstAESKey");
//run wpes1
String command[] = {
ServerConfiguration.getEXECUTABLES_PATH() + "/wpes1_linux",
"newn",
jsonPath,
AesKeyFinalPath,
AesKeyPath,
AesKeyPath,
newID,
nextPath,
ACL
};
RunnerExecutables.execute(command);
} // close else firstAesKey
} //close else firstStep
if(ServerConfiguration.debug)
JOptionPane.showMessageDialog(null, "Leggi il file nextToken.txt");
//read next.txt
nextToken = FileSystemUtils.readFile(nextPath);
//nextToken = FileUtility.readFile(nextPath);
destinationACLReached = uploadManager.finished(nextToken);
if(ServerConfiguration.debug)
JOptionPane.showMessageDialog(null, "destinationACLReached: " +destinationACLReached);
//read jsonout.txt
String jsonout;
jsonout = FileSystemUtils.readFile(jsonoutPath);
//jsonout = FileUtility.readFile(jsonoutPath);
if(ServerConfiguration.debug)
JOptionPane.showMessageDialog(null, "Leggi jsonout");
uploadManager.putTokensInDBWPES(jsonout, "SEL");
if(ServerConfiguration.debug)
JOptionPane.showMessageDialog(null, "HA INSERITO I TOKEN in TOKENTABLEENCRYPT?");
//remove Json.txt file from temp because the file is used only for the executable and it will be rewritten
FileUtility.deleteTempFile(jsonoutPath);
FileUtility.deleteTempFile(jsonPath);
// end if json != val
} else {
if(ServerConfiguration.debug)
JOptionPane.showMessageDialog(null, "DESTINATION ACL Reached,jsonapp.compareTo(\"{\"values\":[]}\") == 0");
destinationACLReached=true;
}
}while(!destinationACLReached);
if(firstAesKey) {
if(ServerConfiguration.debug)
JOptionPane.showMessageDialog(null, "firstAESKEY 4 ");
File aesKeyFile = new File(AesKeyPath);
aesKeyFile.createNewFile();
if(aesKeyFile.exists()) {
//rename AesKey.txt in AesKeyFin.txt ***************************************************
FileUtility.renameFile(AesKeyPath, AesKeyFinalPath);
firstAesKey = false;
if(ServerConfiguration.debug)
JOptionPane.showMessageDialog(null, "Ha appena scritto AesKEyFinal");
}
}
//retrieve if there are more user than owner in current ACL
moreUsers = uploadManager.MoreUsersInCurrentACL();
if(ServerConfiguration.debug)
JOptionPane.showMessageDialog(null, "more users? " + moreUsers);
}while(moreUsers);
break;//END CASE 1
case 3:
if(ServerConfiguration.debug)
JOptionPane.showMessageDialog(null, "CASE 3");
LOGGER.debug("Do cicle: case 3");
boolean Step = true;
nextToken = "";
String json = uploadManager.createJsonUpload(nextToken, Step);
FileSystemUtils.writeFile(jsonPath, json);
//FileUtility.writeFile(jsonPath, json);
if(ServerConfiguration.debug)
JOptionPane.showMessageDialog(null, "json generated: " + json);
if(firstAesKey) {
if(ServerConfiguration.debug)
JOptionPane.showMessageDialog(null, "firstAESKEY 5");
//run wpes1
String command[] = {
ServerConfiguration.getEXECUTABLES_PATH() + "/wpes1_linux",
"new1",
jsonPath,
PIN,
AesKeyPath,
newID,
nextPath,
ACL
};
RunnerExecutables.execute(command);
firstAesKey = false;
//rename AesKey.txt on AesKeyFin.txt ***************************************************
FileUtility.renameFile(AesKeyPath, AesKeyFinalPath);
//end if firstAesKey
} else {
if(ServerConfiguration.debug)
JOptionPane.showMessageDialog(null, "NOT firstAESKEY 5");
//run wpes1
String command[] = {
ServerConfiguration.getEXECUTABLES_PATH() + "/wpes1_linux",
"new1",
jsonPath,
AesKeyFinalPath,
PIN,
AesKeyPath,
newID,
nextPath,
ACL
};
RunnerExecutables.execute(command);
}//end else firstAesKey
//read jsonout.txt
String jsonout = FileSystemUtils.readFile(jsonoutPath);
//String jsonout = FileUtility.readFile(jsonoutPath);
if(ServerConfiguration.debug)
JOptionPane.showMessageDialog(null, "Jsonout 6: " + jsonout);
uploadManager.putTokensInDBWPES(jsonout, "SEL");
if(ServerConfiguration.debug)
JOptionPane.showMessageDialog(null, "HA INSERITO I TOKEN in TOKENTABLEENCRYPT??");
//remove Json.txt file from temp because the file is used only for the executable and it will be rewritten
FileUtility.deleteTempFile(jsonPath);
LOGGER.debug("End case 3");
break;//END CASE 3
default:
LOGGER.warn("Do cicle: case not 1 or 3. It's: " + aclType);
if(ServerConfiguration.debug)
JOptionPane.showMessageDialog(null, "no 1 or 3 case. it's " + aclType + " case");
break;
}//end switch
}while(aclType != 0);
}
if(ServerConfiguration.debug)
JOptionPane.showMessageDialog(null, "Fine di tutti i cicli");
if(ServerConfiguration.debug)
JOptionPane.showMessageDialog(null, "Ora cancello il file dov'era prima");
//Delete old file, if exists
File fileToEncrypt = new File(filePath);
if(fileToEncrypt.exists())
fileToEncrypt.delete();
if(ServerConfiguration.debug)
JOptionPane.showMessageDialog(null, "Ora cripto SEL il file e lo rimetto in webdav.\n" +
"Salva la chiave SEL di cifratura");
//run wpes1
String command[] = {
ServerConfiguration.getEXECUTABLES_PATH() + "/wpes2_linux",
"eSEL",
AesKeyFinalPath,
ServerConfiguration.getTempFilePath() + "/" + tempFileName,
filePath
};
RunnerExecutables.execute(command);
uploadManager.commit();
} catch (Exception e) {
e.printStackTrace();
if(uploadManager != null){
try{
uploadManager.rollBack();
} catch (SQLException e1){
LOGGER.error("SQL Exception during SEL encryption",e1);
}
}else{
LOGGER.error("Exception SEL encryption",e);