You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by dunnlow <du...@yahoo.com> on 2012/11/26 21:21:56 UTC

Set body to length of header string in Spring DSL?

(a simplification of an earlier question - I'm using camel 10.0, spring 3.1)

Can someone tell me how,  *using Spring DSL*, to set the message body to the
size of the message header named "name"?  I don't care if it's simple, or a
different script language (I can't get any to work).  

Something like this -- but this doesn't work:

<camel:setBody>
       
<camel:javaScript>request.headers.get('name').length</camel:javaScript>
</camel:setBody>

I am able to verify the header exists and stream it to standard out just
fine, but trying to get the length throws a variety of exceptions depending
on the method/language I try.

Thanks very much!
-J



--
View this message in context: http://camel.465427.n5.nabble.com/Set-body-to-length-of-header-string-in-Spring-DSL-tp5723258.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Set body to length of header string in Spring DSL?

Posted by Claus Ibsen <cl...@gmail.com>.
On Wed, Nov 28, 2012 at 4:43 PM, dunnlow <du...@yahoo.com> wrote:
> Claus, I understand that is what the exception indicates; However, as you can
> see from my code above (which is the actual code from the test) that I have
> declared the header a Map<String,Object> and am setting the value to the
> string "John."  Thus, I'm confused where the hbase class is being referenced
> at all.  The only classes I've imported in my test are:
>
> java.util.HashMap
> java.util.Map
> org.apache.camel.test.junit4.CamSpringTestSupport,
> org.junit.Ignore
> org.junit.Test
> org.springframework.context.support.AbstractApplicationContext
> org.springframework.context.support.ClassPathXmlApplicationContext
>

Yeah but check your classpath. Your sample project has dependency on
other JARs, and these JARS has dependency on yet other JARs.
So it's what you have on the classpath though.


And btw it was my mistake headerAs does not support OGNL.

See this page. And look for the OGNL's
http://camel.apache.org/simple




>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Set-body-to-length-of-header-string-in-Spring-DSL-tp5723258p5723354.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: Set body to length of header string in Spring DSL?

Posted by dunnlow <du...@yahoo.com>.
Claus, I understand that is what the exception indicates; However, as you can
see from my code above (which is the actual code from the test) that I have
declared the header a Map<String,Object> and am setting the value to the
string "John."  Thus, I'm confused where the hbase class is being referenced
at all.  The only classes I've imported in my test are:

java.util.HashMap
java.util.Map
org.apache.camel.test.junit4.CamSpringTestSupport,
org.junit.Ignore
org.junit.Test
org.springframework.context.support.AbstractApplicationContext
org.springframework.context.support.ClassPathXmlApplicationContext



--
View this message in context: http://camel.465427.n5.nabble.com/Set-body-to-length-of-header-string-in-Spring-DSL-tp5723258p5723354.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Set body to length of header string in Spring DSL?

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

Your header is some sort of hbase type.
So you need to check its API how to convert that type to a String, or
some other way of getting the length of it.

As every Object in Java, has a .toString() method due its on
java.lang.Object, then invoking .toString() may
not in this case return the content of the hbase type, but maybe just
the standard toString with the classname
and an hashcode of the instance, something a like "HBase@4534f3"



On Tue, Nov 27, 2012 at 6:33 PM, dunnlow <du...@yahoo.com> wrote:
> Thanks, I have some reading up to do on OGNL (and file language).
>
> To my initial question, sorry, I'm still not able to get my test working.
> Basically, nothing is output if I put a value in the name with the
> expression you provided.  When I put a value in the name, I get the
> following output depending on the contents of the Otherwise statement
> (below):
>
> Do you have any more insight as to the issue?  Here are some of the
> expressions I tested and their results:
>
> 1) <camel:simple>${headerAs("name",String)}</camel:simple>
>        and also
>      <camel:simple>${headerAs("name",String).length()}</camel:simple>
>       both output:  John
>
> 2) <camel:simple>${header("name").toString()}</camel:simple>
>         and also
>     <camel:simple>${header("name").toString().length()}</camel:simple>
>      both output:  (nothing)
>
> 3) <camel:simple>${header("name")}</camel:simple>
>     outputs: (nothing)
>
> 4) <camel:simple>${in.headers.name}</camel:simple>
>    outputs:  John
>
> 5)  <camel:javaScript>(request.headers.get('name')).length.toString()
>    outputs:  function length() {/* int length() */}
>
> 6) <camel:groovy>'hello'.toString().length()</camel:groovy>
>    outputs:  TypeConversionException (..."Error during type conversion from
> type: java.lang.Integer to the requried typoe: byte[] with value 4 due to
> java.lang.NoClassDefFoundError: org/apache/hadoop/hbase/util/Bytes....  at
> org.apache.camel.imple.converter.BaseTypeConverterRegistry.convertTo(BaseTypeConverterRegistry.java:126)
>
> 7) <camel:groovy>'hello'.toString().length().toString()</camel:groovy>
>     outputs: 5
>
> my route:
>
>    <camel:route>
>        <camel:from uri="direct:t1">
>            <camel:choice>
>                 <camel:when>
>
> <camel:javaScript>request.headers.get('name')==null</camel:javaScript>
>                     <camel:setBody>
>                          <camel:constant>Is Null</camel:constant>
>                     </camel:setBody>
>                 </camel:when>
>                 <camel:otherwise>
>                        <camel:setBody>
>
> <camel:simple>${header("name").toString().length()}</camel:simple>
>                        </camel:setBody>
>                 </camel:otherwise>
>            </camel:choice>
>            <camel:to uri="stream:out"/>
>    </camel:route>
>
> my test code:
>
> public class CamelStringTest extends CamelStringTestSupport {
>    @Override
>    protected AbstractApplicationContext createApplicationContext() {
>        return new ClassPathXmlApplicationContext("context.xml");
>    }
>
> @Test
>    public void testName() throws Exception {
>     Map<String,Object> headers = new HashMap<String,Object>();
>     headers.put("name","John");
>     template.sendBodyAndHeaders("direct:t1","Hello there",headers);
>     Thread.sleep(3000);
>     assertNotNull(context.getEndpoint("direct:t1"));
>   }
> }
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Set-body-to-length-of-header-string-in-Spring-DSL-tp5723258p5723312.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: Set body to length of header string in Spring DSL?

Posted by dunnlow <du...@yahoo.com>.
Thanks, I have some reading up to do on OGNL (and file language).

To my initial question, sorry, I'm still not able to get my test working. 
Basically, nothing is output if I put a value in the name with the
expression you provided.  When I put a value in the name, I get the
following output depending on the contents of the Otherwise statement
(below):

Do you have any more insight as to the issue?  Here are some of the
expressions I tested and their results:

1) <camel:simple>${headerAs("name",String)}</camel:simple> 
       and also
     <camel:simple>${headerAs("name",String).length()}</camel:simple> 
      both output:  John

2) <camel:simple>${header("name").toString()}</camel:simple> 
        and also
    <camel:simple>${header("name").toString().length()}</camel:simple> 
     both output:  (nothing)

3) <camel:simple>${header("name")}</camel:simple>
    outputs: (nothing)

4) <camel:simple>${in.headers.name}</camel:simple>
   outputs:  John  

5)  <camel:javaScript>(request.headers.get('name')).length.toString()
   outputs:  function length() {/* int length() */}

6) <camel:groovy>'hello'.toString().length()</camel:groovy>
   outputs:  TypeConversionException (..."Error during type conversion from
type: java.lang.Integer to the requried typoe: byte[] with value 4 due to
java.lang.NoClassDefFoundError: org/apache/hadoop/hbase/util/Bytes....  at
org.apache.camel.imple.converter.BaseTypeConverterRegistry.convertTo(BaseTypeConverterRegistry.java:126)

7) <camel:groovy>'hello'.toString().length().toString()</camel:groovy>
    outputs: 5

my route:

   <camel:route>
       <camel:from uri="direct:t1">
           <camel:choice>
                <camel:when>
                   
<camel:javaScript>request.headers.get('name')==null</camel:javaScript>
                    <camel:setBody>
                         <camel:constant>Is Null</camel:constant>
                    </camel:setBody>
                </camel:when>
                <camel:otherwise>
                       <camel:setBody>
                            
<camel:simple>${header("name").toString().length()}</camel:simple>
                       </camel:setBody>
                </camel:otherwise>
           </camel:choice>
           <camel:to uri="stream:out"/>
   </camel:route>

my test code:

public class CamelStringTest extends CamelStringTestSupport {
   @Override
   protected AbstractApplicationContext createApplicationContext() {
       return new ClassPathXmlApplicationContext("context.xml");
   }

@Test
   public void testName() throws Exception {
    Map<String,Object> headers = new HashMap<String,Object>();
    headers.put("name","John");
    template.sendBodyAndHeaders("direct:t1","Hello there",headers);
    Thread.sleep(3000);
    assertNotNull(context.getEndpoint("direct:t1"));
  }
}



--
View this message in context: http://camel.465427.n5.nabble.com/Set-body-to-length-of-header-string-in-Spring-DSL-tp5723258p5723312.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Set body to length of header string in Spring DSL?

Posted by Claus Ibsen <cl...@gmail.com>.
On Tue, Nov 27, 2012 at 4:22 PM, dunnlow <du...@yahoo.com> wrote:
> Thank you both, huge help.
>
> Claus, question for you in the, "teach a man to fish.." category.  What is
> the best way to see all of the methods that simple provides?  For example, I
> had no idea about the Length() method - is that method inherited?  Thank you
> again,  -J
>

Ah that's the OGNL support for file language. It's basically
introspecting the body type, and invoked the method you provided in
the syntax.
So in this case, because its a java.lang.String it invokes the
"length" method on the String type.

>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Set-body-to-length-of-header-string-in-Spring-DSL-tp5723258p5723308.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: Set body to length of header string in Spring DSL?

Posted by dunnlow <du...@yahoo.com>.
Thank you both, huge help.  

Claus, question for you in the, "teach a man to fish.." category.  What is
the best way to see all of the methods that simple provides?  For example, I
had no idea about the Length() method - is that method inherited?  Thank you
again,  -J



--
View this message in context: http://camel.465427.n5.nabble.com/Set-body-to-length-of-header-string-in-Spring-DSL-tp5723258p5723308.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Set body to length of header string in Spring DSL?

Posted by Claus Ibsen <cl...@gmail.com>.
On Tue, Nov 27, 2012 at 4:14 AM, Willem jiang <wi...@gmail.com> wrote:
> Hi,
>
> Does the header "name" value is String?
> If it is not , you'd better to convert it to a String.
>

Yeah it has to be a String so there is a length method. You can invoke
the toString method first.

<simple>${header("name").toString().length()}</simple>

Though there is also a headerAs function
<simple>${headerAs("name", String).length()}</simple>

The other scripting language should be able to do it as well. Though
you may have to invoke .toString() to ensure the value is a String
before calling the length method.



>
> --
> Willem Jiang
>
> Red Hat, Inc.
> FuseSource is now part of Red Hat
> Web: http://www.fusesource.com | http://www.redhat.com
> Blog: http://willemjiang.blogspot.com (http://willemjiang.blogspot.com/) (English)
>           http://jnn.javaeye.com (http://jnn.javaeye.com/) (Chinese)
> Twitter: willemjiang
> Weibo: willemjiang
>
>
>
>
>
> On Tuesday, November 27, 2012 at 4:21 AM, dunnlow wrote:
>
>> (a simplification of an earlier question - I'm using camel 10.0, spring 3.1)
>>
>> Can someone tell me how, *using Spring DSL*, to set the message body to the
>> size of the message header named "name"? I don't care if it's simple, or a
>> different script language (I can't get any to work).
>>
>> Something like this -- but this doesn't work:
>>
>> <camel:setBody>
>>
>> <camel:javaScript>request.headers.get('name').length</camel:javaScript>
>> </camel:setBody>
>>
>> I am able to verify the header exists and stream it to standard out just
>> fine, but trying to get the length throws a variety of exceptions depending
>> on the method/language I try.
>>
>> Thanks very much!
>> -J
>>
>>
>>
>> --
>> View this message in context: http://camel.465427.n5.nabble.com/Set-body-to-length-of-header-string-in-Spring-DSL-tp5723258.html
>> Sent from the Camel - Users mailing list archive at Nabble.com (http://Nabble.com).
>
>
>



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: Set body to length of header string in Spring DSL?

Posted by Willem jiang <wi...@gmail.com>.
Hi,

Does the header "name" value is String?
If it is not , you'd better to convert it to a String.


-- 
Willem Jiang

Red Hat, Inc.
FuseSource is now part of Red Hat
Web: http://www.fusesource.com | http://www.redhat.com
Blog: http://willemjiang.blogspot.com (http://willemjiang.blogspot.com/) (English)
          http://jnn.javaeye.com (http://jnn.javaeye.com/) (Chinese)
Twitter: willemjiang 
Weibo: willemjiang





On Tuesday, November 27, 2012 at 4:21 AM, dunnlow wrote:

> (a simplification of an earlier question - I'm using camel 10.0, spring 3.1)
> 
> Can someone tell me how, *using Spring DSL*, to set the message body to the
> size of the message header named "name"? I don't care if it's simple, or a
> different script language (I can't get any to work). 
> 
> Something like this -- but this doesn't work:
> 
> <camel:setBody>
> 
> <camel:javaScript>request.headers.get('name').length</camel:javaScript>
> </camel:setBody>
> 
> I am able to verify the header exists and stream it to standard out just
> fine, but trying to get the length throws a variety of exceptions depending
> on the method/language I try.
> 
> Thanks very much!
> -J
> 
> 
> 
> --
> View this message in context: http://camel.465427.n5.nabble.com/Set-body-to-length-of-header-string-in-Spring-DSL-tp5723258.html
> Sent from the Camel - Users mailing list archive at Nabble.com (http://Nabble.com).