Testing Custom Functions Before Adding to Ebase

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

PaulCooper
Ebase User
Posts: 27
Joined: Tue Mar 12, 2013 9:49 am

Testing Custom Functions Before Adding to Ebase

#1

Postby PaulCooper » Tue Apr 02, 2013 2:46 pm

I am trying to get my head around creating custom functions in Ebase v3.4.

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");
		}
	}
}
This compiles ok.

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());
  }
}
Please note passwords and aliases have been redacted

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.)
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#2

Postby Jon » Tue Apr 02, 2013 4:02 pm

Paul,

It's failing because the \\ before keystore needs to be escaped - I think i.e. it should be d:\\\\keystore. Also I think you will need to pass your boolean as a String e.g. as "true", then check this in the function code.

Regards
Jon
0 x

Steve
Moderator
Moderator
Posts: 421
Joined: Fri Sep 07, 2007 3:44 pm
Location: Sandy, UK
Contact:

#3

Postby Steve » Wed Apr 03, 2013 7:54 am

Hi Paul,

I believe you are popping the params off in the wrong order within your custom function.

Code: Select all

// 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(); 
Needs to be:

Code: Select all

// get the parameter from the stack 
      Object param6 = inStack.pop(); 
      Object param5 = inStack.pop(); 
      Object param4 = inStack.pop(); 
      Object param3 = inStack.pop(); 
      Object param2 = inStack.pop(); 
      Object param1 = inStack.pop(); 
For your test code, I would use forward slashes for path names... try this:

Code: Select all

// define an expression to test the encryptCredentials function... 
      // 
      String expr = "encryptCredentials(\"RSA/None/OAEPWithSHA1AndMGF1Padding\",\"d:/keystore.jks/",\"Password Redacted\",\"Key Alias Redacted\",\"hjgfhjasdgfhsdg\",\"true\")"; 
Steve
0 x

PaulCooper
Ebase User
Posts: 27
Joined: Tue Mar 12, 2013 9:49 am

#4

Postby PaulCooper » Wed Apr 03, 2013 8:48 am

Jon wrote:Paul,

It's failing because the \\ before keystore needs to be escaped - I think i.e. it should be d:\\\\keystore. Also I think you will need to pass your boolean as a String e.g. as "true", then check this in the function code.

Regards
Jon
It was \\ because it was a single \ escaped :-)

I wasn't sure about the true bit but I was going to sort that (if needed) once I got past the syntax issue which I had already deduced wasn't the cause of that error.
Last edited by PaulCooper on Wed Apr 03, 2013 8:56 am, edited 1 time in total.
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#5

Postby Jon » Wed Apr 03, 2013 8:56 am

I ran it through a debugger and the error was at the point d:\\k. Maybe try it with forward slashes as Steve suggests. When I fixed this, it also failed with the boolean parameter until I changed this to String "true".
0 x

PaulCooper
Ebase User
Posts: 27
Joined: Tue Mar 12, 2013 9:49 am

#6

Postby PaulCooper » Wed Apr 03, 2013 8:57 am

Thanks, Jon. :-)
0 x

PaulCooper
Ebase User
Posts: 27
Joined: Tue Mar 12, 2013 9:49 am

#7

Postby PaulCooper » Wed Apr 03, 2013 8:57 am

Steve wrote:Hi Paul,

I believe you are popping the params off in the wrong order within your custom function.
Yeah, that was daft of me! :-)

I will make the changes both you and Jon suggest and give that a go. Thanks for all the help.
0 x

Steve
Moderator
Moderator
Posts: 421
Joined: Fri Sep 07, 2007 3:44 pm
Location: Sandy, UK
Contact:

#8

Postby Steve » Wed Apr 03, 2013 9:00 am

I'll give that a try, though it would be ...,\"d:/keystore.jks\"..., thanks.
You are right ... sorry I did not notice that :oops:

Steve
0 x

PaulCooper
Ebase User
Posts: 27
Joined: Tue Mar 12, 2013 9:49 am

#9

Postby PaulCooper » Wed Apr 03, 2013 9:02 am

Steve wrote:
I'll give that a try, though it would be ...,"d:/keystore.jks"..., thanks.
You are right ... sorry I did not notice that :oops:

Steve
I hoped I had cut that out before you read it, ah well :oops:
0 x

PaulCooper
Ebase User
Posts: 27
Joined: Tue Mar 12, 2013 9:49 am

#10

Postby PaulCooper » Wed Apr 03, 2013 9:28 am

I tried out d:/ instead of d:\ and it didn't complain about that. The "true" bit complained though:

Error while parsing
Unrecognized symbol "true"

I might switch to using "Y" string instead. Not ideal but it would more likely get me where I want to go.
0 x


Who is online

Users browsing this forum: No registered users and 4 guests