You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by st...@gmx.de on 2011/03/11 20:27:15 UTC

Dynamic URI in Java DSL

Hi all,

I'm new to Camel and already searched the mailling list for this issue but couldn't find a solution for JavaDSL.

I'd like to build a "to" URI dynamically from a value coming from the body. More concrete: I'd like to load a FreeMarker template depending on a body value. I've tried this:

to("freemarker://templateHome/${body.templateName}.ftl")

It seems that the URI is not evaluated.
I also tried this:

to("freemarker://templateHome" + simple("${body.templateName}) + ".ftl")

But both seems not to work. Am I missing something or did I something get wrong with the simple syntax usage?

Thanks in advance.

Best regards
Stephan
-- 
Schon gehört? GMX hat einen genialen Phishing-Filter in die
Toolbar eingebaut! http://www.gmx.net/de/go/toolbar

Re: Dynamic URI in Java DSL

Posted by Claus Ibsen <cl...@gmail.com>.
On Sat, Mar 12, 2011 at 10:12 AM, Stephan <st...@gmx.de> wrote:
> Hi Claus,
>
> ok, so in order to use dynamic URIs I always have to use the recipientList.
> Thanks for clearifying.
>

Well thats the solution according to the EIP patterns.


> But wouldn't it be easier to allow the simple language in all uris? In order
> to better understand Camel, I'm would be interested in the reason against
> it. Are there any?
>

Well we have to be careful to not add to much into the DSL.

Also if we just let the String uri be parsed by the simple language,
you may end up with exceptions due invalid syntax, and the fact that
the uri was supposed to be used as is, eg it could be part of the uri
to send to a remote server, for example over HTTP.

If we allow you to specify using an expression then the XML DSL may
become verbose as you would have to do
<to>
   <simple>xxxxx<simple>
</to>

And back in the old days the simple language wasn't as versatile as it
is today. So maybe for Camel 3.0 we can consider some sort of dynamic
to in the DSL.


> Thanks in advance.
>
> Regards
> Stephan
>



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

Re: Dynamic URI in Java DSL

Posted by Stephan <st...@gmx.de>.
Hi Claus,

ok, so in order to use dynamic URIs I always have to use the recipientList.
Thanks for clearifying.

But wouldn't it be easier to allow the simple language in all uris? In order
to better understand Camel, I'm would be interested in the reason against
it. Are there any?

Thanks in advance.

Regards
Stephan

Re: Dynamic URI in Java DSL

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

Welcome to the Camel community.

The EIP patterns has the recipient list as the solution for "dynamic to"
http://camel.apache.org/recipient-list.html

Then you can use any Camel expression to compute the dynamic uri such
as the Simple language
http://camel.apache.org/simple

.recipientList(simple("freemarker://templateHome/${body.templateName}.ftl"));

But you can also use Groovy or whatever you like
http://camel.apache.org/languages.html


On Fri, Mar 11, 2011 at 8:27 PM,  <st...@gmx.de> wrote:
> Hi all,
>
> I'm new to Camel and already searched the mailling list for this issue but couldn't find a solution for JavaDSL.
>
> I'd like to build a "to" URI dynamically from a value coming from the body. More concrete: I'd like to load a FreeMarker template depending on a body value. I've tried this:
>
> to("freemarker://templateHome/${body.templateName}.ftl")
>
> It seems that the URI is not evaluated.
> I also tried this:
>
> to("freemarker://templateHome" + simple("${body.templateName}) + ".ftl")
>
> But both seems not to work. Am I missing something or did I something get wrong with the simple syntax usage?
>
> Thanks in advance.
>
> Best regards
> Stephan
> --
> Schon gehört? GMX hat einen genialen Phishing-Filter in die
> Toolbar eingebaut! http://www.gmx.net/de/go/toolbar
>



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

Re: Dynamic URI in Java DSL

Posted by Stephan <st...@gmx.de>.
Hi Jon,

thanks for your reply. Yes, I already found the
"CamelFreemarkerResourceUri", but I'm more interested in, where the
expression language can be used in general. I couldn't find any meaningful
rule for that.

Claus Ibsen gave an good example in his book (p 464), on how to use the
simple syntax in the file: protocol:

.to("file:backup/?fileName=${date:now:yyyy-MM-dd}/${exchangeId}.txt").

So I'm wondering, why this isn't possible in other URIs? Wouldn't it be
easier to understand, if one can say, that every string/uri in my route gets
evaluated, so I can use the simple expression everywhere? Whats the reason
against it? For me it sounds a bit inconsequent to allow expression in the
file: protocol, but not in others.

Thanks for clearifying and bringing me to the light ;-)

Regards
Stephan






<http://s%2Eniedermeier@ftapi.com/>
   <http://www.ftapi.com/>

Re: Dynamic URI in Java DSL

Posted by Jon Anstey <ja...@gmail.com>.
>From the compoment page http://camel.apache.org/freemarker.html you can use
the CamelFreemarkerResourceUri header for this:

from("direct:in").
  setHeader("CamelFreemarkerResourceUri").constant("path/to/my/template.ftl").
  to("freemarker:dummy");

Just use a simple expression instead of constant.

Cheers,
Jon

On Fri, Mar 11, 2011 at 3:57 PM, <st...@gmx.de> wrote:

> Hi all,
>
> I'm new to Camel and already searched the mailling list for this issue but
> couldn't find a solution for JavaDSL.
>
> I'd like to build a "to" URI dynamically from a value coming from the body.
> More concrete: I'd like to load a FreeMarker template depending on a body
> value. I've tried this:
>
> to("freemarker://templateHome/${body.templateName}.ftl")
>
> It seems that the URI is not evaluated.
> I also tried this:
>
> to("freemarker://templateHome" + simple("${body.templateName}) + ".ftl")
>
> But both seems not to work. Am I missing something or did I something get
> wrong with the simple syntax usage?
>
> Thanks in advance.
>
> Best regards
> Stephan
> --
> Schon gehört? GMX hat einen genialen Phishing-Filter in die
> Toolbar eingebaut! http://www.gmx.net/de/go/toolbar
>



-- 
Cheers,
Jon
---------------
FuseSource
Email: jon@fusesource.com
Web: fusesource.com
Twitter: jon_anstey
Blog: http://janstey.blogspot.com
Author of Camel in Action: http://manning.com/ibsen