12

Am I trying to split a message on a newline character and use the following script:

    def mesType = "";
def lines = message.getPayload().split("\n");

if ( lines[0][0..6] == '123456' ||  lines[1][0..6] == '123456') {
    mesType = "MES1";
}

if ( lines[0][0..7] == '654321' ||  lines[1][0..7] == '654321') {
    mesType = "MES2";
}

if ( lines[0][0..7] == '234561' ||  lines[1][0..7] == '234561') {
    mesType = "MES3";
}

message.setProperty('mesType', mesType);

return message.getPayload();

But when I use this I got the following error in my log file:

groovy.lang.MissingMethodException: No signature of method: [B.split() is applicable for argument types: (java.lang.String) values: {"\n"} (javax.script.ScriptException)

When I changes the split line to the following:

def lines = message.getPayload().toString().split("\n");

I get an error that the array is OutOfBound, so it looks like it is still not doing anything on the newline character.

The message that comes in (message.getPayload) is a message from the file system, and does contain newline characters. It looks like this:

ABCDFERGDSFF
123456SDFDSFDSFDSF
JGHJGHFHFH

What am I doing wrong? Message is collected using Mule 2.X

Mark Veenstra
  • 4,691
  • 6
  • 35
  • 66

2 Answers2

16

looks like message.payload returns a byte[] which you need to get back into a String:

def lines = new String( message.payload, 'UTF-8' ).split( '\n' )

Should get it :-)

Also, I tend to prefer writing things like this as:

def mesType = new String( message.payload, 'US-ASCII' ).split( '\n' ).take( 2 ).with { lines ->
    switch( lines ) {
        case { it.any { line -> line.startsWith( '123456' ) } } : 'MES1' ; break
        case { it.any { line -> line.startsWith( '654321' ) } } : 'MES2' ; break
        case { it.any { line -> line.startsWith( '234561' ) } } : 'MES3' ; break
        default :
          ''
    }
}

Rather than lots of if...else blocks with ranged string access (ie: yours will fail if you get a line with only 3 chars, or only 1 line in the payload)

With Groovy 1.5.6, you're stuck with:

def mesType = new String( message.payload, 'US-ASCII' ).split( '\n' )[ 0..1 ].with { lines ->

And keep your fingers crossed it has at least 2 lines in the payload

Or you're going to need to introduce a method to take up to 2 elements from an array

Can you try:

It might be the with that's breaking in 1.5.6 (not sure)... Try unrolling it back to what you had originally:

def lines = new String( message.payload, 'US-ASCII' ).split( '\n' )[ 0..1 ]
def mesType = 'empty'
if(      lines.any { line -> line.startsWith( '123456' ) } ) mesType = 'MES1'
else if( lines.any { line -> line.startsWith( '654321' ) } ) mesType = 'MES2'
else if( lines.any { line -> line.startsWith( '234561' ) } ) mesType = 'MES3'
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • Is the encoding necessary? It is not a XML, but an ASCII file that is received and encoded in other encoding then UTF-8 – Mark Veenstra Sep 19 '13 at 15:03
  • It's usually good practice to specify an encoding. Try `US-ASCII` – tim_yates Sep 19 '13 at 15:05
  • When I use your prefered solution it says: `[Ljava.lang.String;.take() is applicable for argument types: (java.lang.Integer) values: {2} (groovy.lang.MissingMethodException)` – Mark Veenstra Sep 19 '13 at 15:17
  • Ahhh...what version of groovy are you using? `take` was only added to arrays in Groovy 1.8.1 – tim_yates Sep 19 '13 at 15:17
  • Don't really know. We use Mule 2.X any idea which Groovy version is included in that one? – Mark Veenstra Sep 19 '13 at 15:18
  • Looks like it might be Groovy v1.5.6 from April 2008... *sigh*... Hang on, I'll come up with an alternative... – tim_yates Sep 19 '13 at 15:23
  • I wonder if the problem is that `take()` can't be used, because when I use `get()`, which works in other Groovy scripts on same Mule instances. I also get the error `[Ljava.lang.String;.get() is applicable for argument types: (java.lang.Integer) values: {0} (groovy.lang.MissingMethodException)` – Mark Veenstra Sep 19 '13 at 15:26
  • Yeah, there's no `get` method for arrays... There's `getAt`, is that what you meant? – tim_yates Sep 19 '13 at 15:27
  • the variable `mesType` keeps empty (I filled the default with the text 'emtpy'). looks like it is not looping over all lines – Mark Veenstra Sep 19 '13 at 15:33
  • Or you don't have matching patterns in the payload – tim_yates Sep 19 '13 at 15:35
  • I do have a match I am pretty sure. Strange thing is that the variable is not showing with text 'empty', but it is even NULL. So it does not do the loop – Mark Veenstra Sep 19 '13 at 15:40
  • @markdark added another version to the end of my answer that does away with any fancyness that might be broken or non-existant in groovy 1.5.6 – tim_yates Sep 19 '13 at 15:43
  • Damn... that was it... Now it extracts the message type checked on 1st and 2nd line. Thanks very much – Mark Veenstra Sep 19 '13 at 17:08
3
def lines = "${message.getPayload()}".split('\n');

This approach should also work