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

Torsten Mielke commented on CAMEL-24629:
----------------------------------------

Thanks. 
First of all, please raise a new ticket if you want to request that Camel Main 
can also parse ${HOST} in application.properties.
Given there is already a solution to use {{{{{}env:HOST{}}}}}, I am not sure if 
that gets accepted.

Then, I see the error here as well now and want to reiterate, the easiest 
solution is to switch to using {{{{{}env:HOST{}}}}} instead of ${HOST}. That 
works uniformly across all runtimes.

Now, what happens is, when running {{camel run ...}} , it parses the route 
definition to understand what dependencies are needed (among other things). 
The stack trace of the error {{"Unknown function: HOST"}} is from JBang’s 
silent Camel Main export (runExport → ExportQuarkus.export → KameletMain), 
which happens even before the Quarkus runtime starts.

{{to:}} vs {{toD:}} have different evaluators. {{{{{}my.host{}}}}} is a Camel 
property placeholder and is resolved for both. 
{{toD:}} then parses the result as Simple language expression, {{to:}} does not.

With your route, the chain is:
1. Route URI: https://{{{}my.host{}}}/posts?…
2. my.host = ${HOST} is still the literal property value at this point
3. After {{{{{}my.host{}}}}} resolution: [https://$|https://$/]{HOST}/posts?…
4. the Simple language sees ${HOST} and looks up a function named HOST
5. That function does not exist → error: Unknown function: HOST

Simple does support environment variables, but not as a bare name. It wants 
${env:HOST} or ${sysenv:HOST}. For the Simple language ${HOST} is not “read env 
var HOST”; it is “call Simple function HOST”.

Why {{to:}} works: 
Static to: never runs that Simple parse. The URI is only subject to 
{{{{{}…}}{}}} placeholders. Quarkus then interpolates ${HOST} in 
application.properties when the exported Quarkus app actually starts, so 
my.host becomes {{jsonplaceholder.typicode.com}} and the HTTP call succeeds.
Again, the "Unknown function: HOST" error happens even before the Quarkus 
runtime starts while Camel Main parses the route definition.

As a result, if you export your route definition to a Camel Quarkus app and run 
it from there, it works just fine.
The placeholder in
{code:java}
        - toD:
            uri: "https://{{my.host}}/posts/1";
{code}
is evaluated just fine.


To summarise this ticket, I would highly recommend to use {{{{{}env:HOST{}}}}} 
when evaluating environment variables. 
It works across all runtimes and avoids the parsing errors of the Camel Main 
export explained above.

> toD not taking into account substituted properties with env variables
> ---------------------------------------------------------------------
>
>                 Key: CAMEL-24629
>                 URL: https://issues.apache.org/jira/browse/CAMEL-24629
>             Project: Camel
>          Issue Type: Bug
>          Components: camel-core
>    Affects Versions: 4.22.0
>            Reporter: Cameron Fitzwater
>            Assignee: Torsten Mielke
>            Priority: Minor
>         Attachments: camel-24629.zip
>
>
> I have a .env file that has:
> MY_HOST=abc
>  
> My application.properties file has:
> my.host=${MY_HOST}
>  
> When using {*}toD{*}:
>  - toD:
>             uri: 
> "https://{{{{{}my.host{}}}}}.domain.com/${exchangeProperty.location}";
>             parameters:
>                 lean: 1
>                 bridgeEndpoint: true
>                 throwExceptionOnFailure: false
> This is failing when using *quarkus* runtime. I've sourced my .env before 
> running camel run/export, still fails.
>  
> It does NOT fail when using it in *to:* component.
>  
> I use this convention to wire the application so that it can more easily be 
> consumable by K8s to set the environment variables.
>  
>  
> Error:
> {code:java}
> org.apache.camel.FailedToCreateRouteException: Failed to create route: 
> get-location at: >>> 
> DynamicTo[toD[https://{{my.host}}.domain.com/${exchangeProperty.location}?bridgeEndpoint=true&lean=1&throwExceptionOnFailure=false]]
>  <<< in route: 
> Route(get-location)[From[rest:get:/:/locations/{id}?routeId=... because: 
> Unknown function: MY_HOST at location 8
> https://${MY_HOST}.domain.com/${exchangeProperty.location}?bridgeEndpoint=true&lean=1&throwExceptionOnFailure=false
>         *{code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to