Skip to content

Commit

Permalink
Eriku tehtud täiendused seoses bdoc'ga
Browse files Browse the repository at this point in the history
git-svn-id: https://arendus.sk.ee/svn/svnpki/Tempelplus/trunk@3130 984e8997-5708-0410-b877-c265616a6196
  • Loading branch information
jalukse committed Jul 10, 2014
1 parent 13af1bd commit b15bbb1
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 65 deletions.
5 changes: 4 additions & 1 deletion TestData/config/tempelplus-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
####TEMPELPLUSWRAPPER PROPERTIES####
####TEMPELPLUSWRAPPER PROPERTIES####
#DDOC CONFIG FILE
JDDOC_CONFIG=/src/repo/Tempelplus/JDigiDoc/jdigidoc.cfg
#TempelPlus folder path
Expand All @@ -9,6 +9,9 @@ TESTDATA_PATH=/tmp/TestData/
RECIPIENT="KnowIT test SK1"
#additional parameters of signing
ADDITIONAL_PARAMETERS= -role SKtest -country Eesti -state Harjumaa -city Tallinn -postcode 54321
#ddoc/bdoc
TEST_MODE=ddoc
TEMPELPLUS_LINUX_BIT=64

#Where are certificates in TestData folder?
#digitempel certificate
Expand Down
124 changes: 100 additions & 24 deletions src/ee/sk/automatedtests/TempelPlusWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import javax.activation.MimetypesFileTypeMap;

import org.objenesis.strategy.StdInstantiatorStrategy;

import ee.sk.digidoc.factory.DigiDocFactory;
import ee.sk.digidoc.DataFile;
import ee.sk.digidoc.DigiDocException;
Expand All @@ -39,10 +41,22 @@ public class TempelPlusWrapper {
private static SignedDoc sdoc = null;
private static BufferedReader stdout, stderr;
static Vector<String> outputLines;

String config_file, slash, tempelPlusPath, testDataPath, recipient, cert1, cert2, cert3, bits, cn;

boolean printChecksumData, windows;

public enum TestMode {
ddoc(".ddoc"), bdoc(".bdoc");

private String extension;
private TestMode(String extension){
this.extension = extension;
}
public String getExtension(){
return this.extension;
}
};
private TestMode testMode;

// Synchronize exceptions between main and sub-threads
private volatile Exception exc = null;
Expand All @@ -51,6 +65,10 @@ public class TempelPlusWrapper {

public void main(String[] args) throws IOException {
}

public TestMode getTestMode(){
return this.testMode;
}

/** Wrapper constructor - loads configuration from properties file **/
public TempelPlusWrapper() {
Expand Down Expand Up @@ -91,6 +109,17 @@ public TempelPlusWrapper() {
bits = tpwConfigFile.getProperty("TEMPELPLUS_LINUX_BIT");

printChecksumData = Boolean.valueOf(tpwConfigFile.getProperty("PRINT_CHECKSUMDATA"));

String testModeConfValue = tpwConfigFile.getProperty("TEST_MODE");
if(testModeConfValue == null){
throw new RuntimeException("Property \"TEST_MODE\" is missing in Configuration file: " + configFile);
}else if(testModeConfValue.equals("ddoc")){
testMode = TestMode.ddoc;
}else if(testModeConfValue.equals("bdoc")){
testMode = TestMode.bdoc;
}else{
throw new RuntimeException("Property \"TEST_MODE\" has unsupported value in Configuration file: " + configFile);
}
}

private boolean tempelPlusLines = false;
Expand Down Expand Up @@ -172,7 +201,7 @@ private void tempelplusExecution(String parameter[], String dataFolder) throws E
// InputStream in = process.getInputStream();
OutputStream out = process.getOutputStream();

new BufferedWriter(new OutputStreamWriter(out));
BufferedWriter bwo = new BufferedWriter(new OutputStreamWriter(out));

Thread readingOutput = executeReadTPLinesThread();

Expand All @@ -192,7 +221,7 @@ private void tempelplusExecution(String parameter[], String dataFolder) throws E
dataFolder = null;
readingOutput.interrupt();

executeReadErrorsThread();
Thread startedStdErrThread = executeReadErrorsThread();


OutputStream os = process.getOutputStream();
Expand All @@ -202,9 +231,16 @@ private void tempelplusExecution(String parameter[], String dataFolder) throws E
Log.write("Waiting for process...");
Log.nextLine();
if(dataFolder == null){
process.waitFor();
process.waitFor();
}
out.flush();
out.close();
bwo.flush();
bwo.close();
is.close();
process.destroy();
startedStdErrThread.interrupt();
r.gc();
Log.write("Done.");
} catch (Exception e) {
Log.write("Exception: " + e, true);
Expand All @@ -226,7 +262,7 @@ private String[] createExecutableStringArray(String[] exec, String[] params) {
return executableStringArray;
}

private void executeReadErrorsThread() {
private Thread executeReadErrorsThread() {
// Error messages
Thread stderrThread = new Thread() {
public void run() {
Expand All @@ -249,6 +285,7 @@ public void run() {
}
}; // End of error messages
stderrThread.start();
return stderrThread;
}

private void checkThreadException(Thread thread, Integer secondsToKillThread) throws Exception {
Expand Down Expand Up @@ -374,10 +411,15 @@ public boolean containerRemoveFile(String container, String fileOut) throws Digi
DigiDocFactory digFac = ConfigManager.instance().getDigiDocFactory();
sdoc = digFac.readSignedDoc(container);
FileOutputStream fos = new FileOutputStream(fileOut);
byte[] xml = sdoc.getDataFile(0).getBodyAsData();
fos.write(xml);
InputStream is = sdoc.getDataFile(0).getBodyAsStream();
byte[] data = new byte[4096];
int n = 0;
while((n = is.read(data)) > 0) {
fos.write(data, 0, n);
}
fos.flush();
fos.close();
is.close();
System.out.println("File extracted from container!");
} catch (DigiDocException dde) {
System.out.println("DigiDoc error: " + dde);
Expand All @@ -398,7 +440,7 @@ boolean containerRemoveFiles(String folder) {
File filesFolder = new File(folder);
String[] files = filesFolder.list();
for (int i = 0; i < files.length; i++) {
if (files[i].endsWith(".ddoc")) {
if (files[i].endsWith(testMode.getExtension())) {
sdoc = digFac.readSignedDoc(folder + files[i]);
String fileName = files[i];
// get file name without extension
Expand All @@ -408,11 +450,20 @@ boolean containerRemoveFiles(String folder) {
String newExtension = containerFileName.substring(fileName.lastIndexOf("."));
// get file from container and give new name
FileOutputStream fos = new FileOutputStream(folder + fileNameExtensionless + "_out" + newExtension);
byte[] xml = sdoc.getDataFile(0).getBodyAsData();
// taking file from container
fos.write(xml);
// byte[] xml = sdoc.getDataFile(0).getBodyAsData();
// // taking file from container
// fos.write(xml);
// fos.flush();
// fos.close();
InputStream is = sdoc.getDataFile(0).getBodyAsStream();
byte[] data = new byte[4096];
int n = 0;
while((n = is.read(data)) > 0) {
fos.write(data, 0, n);
}
fos.flush();
fos.close();
is.close();
}
}
System.out.println("Files extracted from container!");
Expand Down Expand Up @@ -440,7 +491,7 @@ boolean containerRemoveFiles(String folder, String addedFolder) {
File addedFilesFolder = new File(addedFolder);
String[] addedFiles = addedFilesFolder.list();
for (int i = 0; i < files.length; i++) {
if (files[i].endsWith(".ddoc")) {
if (files[i].endsWith(testMode.getExtension())) {
for (int j = 0; j <= addedFiles.length; j++) {
sdoc = digFac.readSignedDoc(folder + files[i]);
// container name
Expand All @@ -450,11 +501,16 @@ boolean containerRemoveFiles(String folder, String addedFolder) {
String oldExtension = fileName.substring(fileName.lastIndexOf("."), fileName.length());
// get file from container and give new name
FileOutputStream fos = new FileOutputStream(folder + fileNameNoExt + "_out" + oldExtension);
byte[] xml = sdoc.getDataFile(j).getBodyAsData();
// extract file from container
fos.write(xml);
// // extract file from container
InputStream is = sdoc.getDataFile(j).getBodyAsStream();
byte[] data = new byte[4096];
int n = 0;
while((n = is.read(data)) > 0) {
fos.write(data, 0, n);
}
fos.flush();
fos.close();
is.close();
}
}
}
Expand Down Expand Up @@ -501,7 +557,7 @@ boolean validateFolder(String folder) throws DigiDocException {
String[] files = filesFolder.list();

for (int i = 0; i <= files.length - 1; i++) {
if (files[i].endsWith(".ddoc")) {
if (files[i].endsWith(testMode.getExtension())) {
sdoc = digiFac.readSignedDoc(folder + files[i]);
// if no errors occur via JDigiDoc
ArrayList<?> errs = sdoc.validate(true);
Expand Down Expand Up @@ -793,6 +849,14 @@ boolean checksumFolder(String[] inputFiles, String[] outputFiles) throws IOExcep
/** Delete file
* @throws IOException **/
public boolean deleteFile(String sFilePath) throws IOException {

try {
System.gc();
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File oFile = new File(sFilePath);

if (oFile.isDirectory()) {
Expand All @@ -808,21 +872,33 @@ public boolean deleteFile(String sFilePath) throws IOException {
// out.flush();
// out.close();
// out = null;
System.gc();

return oFile.delete();
}

/** Get file from folder **/

public String getInputFile(String folder, String extension) {
return getInputFileFromFolder(folder, extension);
}

public String getInputFile(String folder) {
return getInputFileFromFolder(folder, null);
}

/** Get file from folder **/
private String getInputFileFromFolder(String folder, String extension) {
File filesFolder = new File(folder);
String[] files = filesFolder.list();
String fileIn = null;
for (int i = 0; i < files.length; i++) {
if (new File(folder + files[i]).isDirectory() && !new File(folder + files[i]).getName().contains(".cdoc"))
if (new File(folder + files[i]).isDirectory() && !new File(folder + files[i]).getName().contains(".cdoc")) {
continue;
else
fileIn = folder + files[i];
} else {
if(extension == null || (extension != null && files[i].endsWith(extension))){
fileIn = folder + files[i];
}else{
continue;
}
}

}
return fileIn;
}
Expand Down Expand Up @@ -919,7 +995,7 @@ String[] getOutputFiles(String[] inputFiles) {

/** Get container name from inputFile **/
public String getContainer(String inputFile) {
String container = getNameOnly(inputFile) + ".ddoc";
String container = getNameOnly(inputFile) + testMode.getExtension();
return container;
}

Expand Down
Loading

0 comments on commit b15bbb1

Please sign in to comment.