149 lines
4.6 KiB
Java
149 lines
4.6 KiB
Java
package tsdemo;
|
|
// Class to alow Access manager to interface with the timestamp classes.
|
|
import oracle.sql.BLOB;
|
|
//import oracle.sql.*;
|
|
import oracle.jdbc.*;
|
|
import java.sql.*;
|
|
import java.io.*;
|
|
|
|
//
|
|
public class amTimestamp{
|
|
//
|
|
public static byte[] hash;
|
|
//
|
|
public static String fileName;
|
|
//
|
|
public static String timestamp;
|
|
//
|
|
public static oracle.sql.BLOB blobResponse;
|
|
//
|
|
public static String getHash() {
|
|
//
|
|
String stringHash;
|
|
//System.out.println("Class Hash: " + hash);
|
|
stringHash = hash.toString();
|
|
//
|
|
return stringHash;
|
|
}
|
|
//
|
|
/* Private Declaration */
|
|
private static Connection conn = null;
|
|
|
|
/* Procedural Declaration */
|
|
public static Connection getConnection() throws SQLException {
|
|
if ( conn == null ) {
|
|
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
|
|
try {
|
|
conn = DriverManager.getConnection("jdbc:default:connection:");
|
|
System.out.println("connection=default");
|
|
} catch ( Exception e ){}
|
|
}
|
|
if ( conn == null ) {
|
|
conn = DriverManager.getConnection("jdbc:oracle:thin:@loordv01:1521:dev10g","eft_nom","eft_nom");
|
|
System.out.println("connection=thin");
|
|
}
|
|
conn.setAutoCommit(false); // this is needed to write in update mode to BLOB
|
|
return conn;
|
|
}
|
|
//
|
|
public static oracle.sql.BLOB getRespBlob() throws SQLException,IOException {
|
|
//
|
|
// Returning a BLOB seems to be more tricky than expected
|
|
// If you return the original BLOB you will get an error
|
|
// from oracle, however loading it as a binary stream
|
|
// returns the value rather than a reference so works.
|
|
//
|
|
// Connection os required to create a temporary BLOB.
|
|
//
|
|
conn = getConnection();
|
|
BLOB tmpBlob = BLOB.createTemporary(conn, false,BLOB.DURATION_SESSION);
|
|
//
|
|
// Parameter to tmpBlob.setBinaryStream() is odd, got from a example and
|
|
// as it works I've left it as is.
|
|
//
|
|
OutputStream os = tmpBlob.setBinaryStream(0L);
|
|
//
|
|
os.write(blobResponse.getBytes());
|
|
os.flush();
|
|
os.close();
|
|
//
|
|
return tmpBlob;
|
|
}
|
|
//
|
|
public static String amTimestamp(oracle.sql.BLOB blobFile,
|
|
String URL,
|
|
String password,
|
|
String keystore) throws Exception {
|
|
//
|
|
//System.out.println(" URL: " + URL + " password: " + password + "keystore: " + keystore);
|
|
//
|
|
// Declare an Oracle BLOB type so we can pass this back to the PLSQL to allow the entire
|
|
// timestamp response to be stored in the database (using the getRespBlob method)
|
|
//
|
|
blobResponse = oracle.sql.BLOB.empty_lob();
|
|
timestamp = null;
|
|
//
|
|
// Get the hash value for the entered file
|
|
//
|
|
hash = amHashFile.getHash(blobFile, "SHA-1");
|
|
//System.out.println("Hash: " + hash);
|
|
//
|
|
// Instantiate a TimeStamp instance
|
|
//
|
|
TimeStamp ts = new TimeStamp();
|
|
//
|
|
//set the variables via the set methods
|
|
//
|
|
ts.setKeystoreFile(keystore);
|
|
ts.setKeystorePassword(password);
|
|
ts.setURL(URL);
|
|
// generate the timestamp
|
|
try {
|
|
//
|
|
timestamp = ts.generateTimestamp(hash,1318);
|
|
//
|
|
//System.out.println("Got Timestamp : " + timestamp);
|
|
//
|
|
} catch (Exception e) {
|
|
//
|
|
throw new TSException(e.getMessage());
|
|
//System.out.println("Exception occured.\n" + e.getMessage());
|
|
}
|
|
//
|
|
byte[] TSResp = ts.getResp();
|
|
//
|
|
// Retrieve the timesatmp response array from the timestamp class
|
|
//
|
|
blobResponse.setBytes(TSResp);
|
|
//
|
|
//System.out.println("BLOB length is : " + blobResponse.getLength());
|
|
//
|
|
return timestamp;
|
|
}
|
|
/**
|
|
*
|
|
* @param args
|
|
*/
|
|
public static void main(String[] args)
|
|
{
|
|
// Created for testing from JAVA rather than for actual use.
|
|
//System.out.println("Got here 1");
|
|
//String errMsg = "We";
|
|
amTimestamp theTimestamp = new amTimestamp();
|
|
//
|
|
oracle.sql.BLOB emptyBlob = null;
|
|
//
|
|
try {
|
|
//
|
|
String timeVal = theTimestamp.amTimestamp(emptyBlob,"https://tsa.trust-sign.hu:1318/","almafa","C:\\EFT_Noms\\keystore.jks");
|
|
//
|
|
System.out.println(" TimeVal: " + timeVal);
|
|
}
|
|
catch (Exception e) {
|
|
//
|
|
//if (e.getMessage() != null)
|
|
System.out.println(e.getMessage());
|
|
//
|
|
}
|
|
}
|
|
} |