Reputation: 840
I've a variable defined in nifi.properties
file in my system which I'm trying to read inside a ExecuteScript
processor using Groovy
. Below is I've tried:
def flowFile = session.get()
if (!flowFile) return
try
{
def newFile = new File(${config.password.file}).getText()
flowFile = session.putAttribute(flowFile, "passwordFile", newFile.toString())
session.transfer(flowFile, REL_SUCCESS)
}
catch(Exception e)
{
flowFile = session.putAttribute(flowFile, "errorStack",e.toString())
session.transfer(flowFile, REL_FAILURE)
}
The value of config.password.file
is absolute path to a file which contains the password which I need to use.
Not sure but this approach doesn't work. This is the error stack I'm getting:
groovy.lang.MissingPropertyException: No such property: config for class: Script90
I tried to use the groovy functionality of reading the password from a file on my local machine via the code below and it works fine.
def filex = "C:\\Users\\myUser\\Desktop\\passwordFile.pass"
String passFile = new File(filex).getText()
Any idea what I'm missing/doing wrong?
Also, based on the error stack I mentioned above, it doesn't mentions exactly what property is missing. Does anyone have any idea on how to customize the code as such it generates the error exactly which property is missing or something like it?
Upvotes: 0
Views: 2107
Reputation: 12083
The quick answer is that NiFi Expression Language is not supported in the script body or within a script file provided by an ExecuteScript property, but what you want can still definitely be done.
How is config.password.file
defined? Is it a user-defined property on the ExecuteScript processor? If so, the problem is getting access to a variable that has Groovy-specific characters such as period. Instead of being able to refer to them by name, you'll have to use the script's Binding, as follows:
def flowFile = session.get()
if (!flowFile) return
try {
def newFile = new File(binding.getVariable('config.password.file').value).text
flowFile = session.putAttribute(flowFile, "passwordFile", newFile.toString())
session.transfer(flowFile, REL_SUCCESS)
}
catch(e) {
flowFile = session.putAttribute(flowFile, "errorStack",e.toString())
session.transfer(flowFile, REL_FAILURE)
}
If it is not a property but a flow file attribute, try the following:
def flowFile = session.get()
if (!flowFile) return
try {
def newFile = new File(flowFile.getAttribute('config.password.file')).text
flowFile = session.putAttribute(flowFile, "passwordFile", newFile.toString())
session.transfer(flowFile, REL_SUCCESS)
}
catch(e) {
flowFile = session.putAttribute(flowFile, "errorStack",e.toString())
session.transfer(flowFile, REL_FAILURE)
}
The two scripts differ only in the newFile line, the former gets the property as a PropertyValue object from the Binding associated with the Script, the latter gets the value of the flow file attribute.
Upvotes: 5