[ 
https://issues.apache.org/jira/browse/NIFI-6145?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16801855#comment-16801855
 ] 

Matt Burgess commented on NIFI-6145:
------------------------------------

We won't be able to add the methods to the FlowFile interface proper, as NiFi 
is written in pure Java so it doesn't know about Closures nor does it have 
access to Groovy classes such as ExpandoMetaClass.  In your script you can add 
the following methods to the top and it should give you what you want:


{code:java}
FlowFile.metaClass.withInputStream  = { Closure c -> 
   def inStream = session.read(delegate)
   c.call(inStream)
   inStream.close()
}

FlowFile.metaClass.withOutputStream  = { Closure c -> 
   def outStream = session.write(delegate)
   c.call(outStream)
   outStream.close()
}

FlowFile.metaClass.withReader = { String charset, Closure c -> 
   def inStream = session.read(delegate)
   def br = new BufferedReader(new InputStreamReader(inStream, charset))
   c.call(br)
   br.close()
}

FlowFile.metaClass.withWriter = { String charset, Closure c -> 
   def outStream = session.write(delegate)
   def bw = new BufferedWriter(new OutputStreamWriter(outStream, charset))
   c.call(bw)
   bw.close()
}
{code}


We could also add this to the script automatically before evaluating it, but 
that might throw the line numbers way off (although they are already off 
because we add imports for convenience).

IMO this is a better addition to ExecuteGroovyScript (EGS) rather than 
ExecuteScript, as it is specific to the Groovy language/idiom, which is what 
ExecuteGroovyScript is meant to take advantage of. I suppose we could add it to 
both but I think EGS is a better spot. That's if we even need to add it (since 
you can add the snippet above to scripts and get the capability). Thoughts?

> Make groovy File functions available for FlowFile to simplify scripting
> -----------------------------------------------------------------------
>
>                 Key: NIFI-6145
>                 URL: https://issues.apache.org/jira/browse/NIFI-6145
>             Project: Apache NiFi
>          Issue Type: New Feature
>            Reporter: Dmitry Lukyanov
>            Priority: Minor
>
> I'd like to add groovy File functions to a FlowFile class insige groovy 
> script to make work with flowfie streams similar like we work with File in 
> groovy.
> Object withInputStream(Closure closure)
> Create a new InputStream for this file and passes it into the closure.
>  Object       withOutputStream(Closure closure)
> Creates a new OutputStream for this file and passes it into the closure. 
>  Object       withReader(String charset, Closure closure)
> Create a new BufferedReader for this file using the specified charset and 
> then passes it into the closure, ensuring the reader is closed after the 
> closure returns. 
>  Object       withWriter(String charset, Closure closure)
> Creates a new BufferedWriter for this file, passes it to the closure, and 
> ensures the stream is flushed and closed after the closure returns. 
> etc...
> ----
> ready to contribute



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to