I have been trying to follow the instructions for creating custom function in the help pages but I have become stuck.
Here is the function
Code: Select all
import java.util.*;
import com.ebasetech.ufs.function.*;
import com.ebasetech.ufs.kernel.*;
import org.nfunk.jep.ParseException;
import org.apache.commons.codec.binary.Base64;
public class encryptCredentials extends UFSCustomFunction
{
public encryptCredentials(UFSFormInterface formInterface)
{
super(formInterface);
numberOfParameters = 6;
}
public void run(Stack inStack) throws FormException, ParseException
{
checkStack(inStack);
// get the parameter from the stack
Object param1 = inStack.pop();
Object param2 = inStack.pop();
Object param3 = inStack.pop();
Object param4 = inStack.pop();
Object param5 = inStack.pop();
Object param6 = inStack.pop();
String encryptionMethod;
String keyStoreFile;
String keyStorePassword;
String KeyAlias;
String dataString;
boolean inBase64EncodeFlag;
if ( param1 instanceof String && param2 instanceof String && param3 instanceof String && param4 instanceof String && param5 instanceof String && param6 instanceof Boolean)
{
encryptionMethod = param1.toString();
keyStoreFile = param2.toString();
keyStorePassword = param3.toString();
KeyAlias = param4.toString();
dataString = param5.toString();
inBase64EncodeFlag = ((Boolean) param6).booleanValue();
PublicKey pubKey = null;
if (encryptionMethod.equals("PLAIN") || encryptionMethod.equals("NONE"))
{
inStack.push(encryptionMethod);
return;
}
try
{
//Get Public Key for Encryption
FileInputStream fis = new FileInputStream(keyStoreFile);
KeyStore keystore = KeyStore.getInstance("JKS");
keystore.load(fis, keyStorePassword.toCharArray());
Certificate cert = keystore.getCertificate(KeyAlias);
pubKey = cert.getPublicKey();
}
catch (Exception e)
{
inStack.push(new String("Error in extracting Public Key: " + e.getMessage()));
}
try
{
Security.addProvider(new BouncyCastleProvider());
Cipher cipher = Cipher.getInstance(encryptionMethod, "BC");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] cipherData = cipher.doFinal(dataString.getBytes());
if (inBase64EncodeFlag)
{
inStack.push(new String(Base64.encodeBase64(cipherData)));
}
else
{
inStack.push(new String(cipherData));
}
}
catch (Exception e)
{
inStack.push(new String("Error encrypting: " + e.getMessage()));
return;
}
}
else
{
throw new ParseException("Error in arguments: At least one of the first 5 parameters are not of type String or the last one isn't of type Boolean");
}
}
}
I then tried to test this function using the method also described in the ebase help pages:
Here is the test code:
Code: Select all
/**
* Class : encryptCredentialsTest
*
* Function : Tests the encryptCredentials custom function.
*
*/
import org.nfunk.jep.*;
import com.ebasetech.ufs.kernel.*;
import java.lang.Boolean;
class encryptCredentialsTest {
/**
* Constructor.
*/
public encryptCredentialsTest() {}
/**
* Main method. Test of the encryptCredentials custom function.
*/
public static void main(String args[])throws FormException{
double value;
// create a new parser...
//
JEP parser = new JEP();
// define an expression to test the encryptCredentials function...
//
String expr = "encryptCredentials(\"RSA/None/OAEPWithSHA1AndMGF1Padding\",\"d:\\keystore.jks\",\"Password Redacted\",\"Key Alias Redacted\",\"hjgfhjasdgfhsdg\",Boolean.TRUE)";
// add the function class to the expression evaluator...
//
parser.addFunction("encryptCredentials",
new encryptCredentials(null));
// now parse the expression...
//
parser.parseExpression(expr);
if (parser.hasError()) {
System.out.println("Error while parsing");
System.out.println(parser.getErrorInfo());
return;
}
// now retrieve the result from evaluating encryptCredentials function...
//
value = parser.getValue();
if (parser.hasError()) {
System.out.println("Error during evaluation");
System.out.println(parser.getErrorInfo());
return;
}
System.out.println(expr + " = " + parser.getValue());
}
}
This compiles ok too but when I run it I get the following error:
Error while parsing
Syntax error
I can not see why it is throwing this error the expression to be parsed looks perfectly ok. Is there anyone out there that can point me at a way forward or point out the obvious mistake?
Regards,
Paul Cooper
(Please note I have tried changing the Boolean last argument of the function to be a String argument and passed "Y" instead of true and this still had the same error.)