You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Ralph Cook <ra...@accesspointinc.com> on 2017/06/11 13:37:43 UTC

converting XML DSL example to Java

The camel example for using Spring and XML DSL routing has the following config file:

        <beans
            xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:camel="http://camel.apache.org/schema/spring"
            xsi:schemaLocation="
                    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                    http://camel.apache.org/schema/spring       http://camel.apache.org/schema/spring/camel-spring.xsd"
        >

          <camelContext xmlns="http://camel.apache.org/schema/spring">

            <route id="helloRoute">
              <!-- incoming requests from the servlet is routed -->
              <from uri="servlet:hello"/>
              <choice>
                <when>
                  <!-- is there a header with the key name? -->
                  <header>name</header>
                  <!-- yes so return back a message to the user -->
                  <transform>
                    <simple>Hi I am ${sysenv.HOSTNAME}. Hello ${header.name} how are you today?</simple>
                  </transform>
                </when>
                <otherwise>
                  <!-- if no name parameter then output a syntax to the user -->
                  <transform>
                    <constant>Add a name parameter to uri, eg ?name=foo</constant>
                  </transform>
                </otherwise>
              </choice>
            </route>

          </camelContext>

        </beans>

I have this running; it puts the designated string on the browser page as a result after the user enters the correct URL in the browser address bar.

As an exercise, I'm attempting to do this in Java.  I have the following configuration:

<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:camel="http://camel.apache.org/schema/spring"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://camel.apache.org/schema/spring       http://camel.apache.org/schema/spring/camel-spring.xsd"
>

  <camelContext xmlns="http://camel.apache.org/schema/spring">
      <packageScan>
          <package>com.api.cameltwo.camelroutes</package>
      </packageScan>
  </camelContext>

</beans>

and the following Java file:

package com.api.cameltwo.camelroutes;

import org.apache.camel.Expression;
import org.apache.camel.spring.SpringRouteBuilder;

public class IncomingUrlRouteBuilder extends SpringRouteBuilder {
    Expression e;

    public void configure() {
        from("servlet:hello")
          .choice()
             .when(header("name"))
                .transform().simple("Hi ${header.name}")
                .to("file:target/messages?fileName=messageOne.txt")

             .otherwise()
                .transform().constant("no name")
                .to("file:target/messages?fileName=messageEmpty.txt")
;
    }
}

This works; the indicated string goes back to the browser screen, and gets written to the file. The thing I don't understand yet is the requirement for the '.to("file...")' clauses/calls in the Java version. I put in "file..." as an experiment; it seems that something is required after '.transform().simple()', but I don't know what. If I don't put that there, the compiler gives the error:

cannot find symbol
  symbol:   method otherwise()
  location: class ProcessorDefinition<ChoiceDefinition>

So what is it I need to put after the '.when()' to indicate that this route is done? I gather the .transform().simple() already puts the required output where it needs to go to get back to the browser result page.



The contents of this email and any attachments to it may contain Customer Proprietary Network Information (CPNI) as defined by the FCC and/or privileged and confidential information from Access Point, Inc. Any and all of this information is only for the viewing or use of the intended recipient. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution or use of, or the taking of any action in reliance upon, the information contained in this e-mail, or any of the attachments to this e-mail, is strictly prohibited and that this e-mail and all of the attachments to this e-mail, if any, must be immediately returned to Access Point, Inc. or destroyed and, in either case, this e-mail and all attachments to this e-mail must be immediately deleted from your computer without making any copies hereof. If you have received this e-mail in error, please notify Access Point, Inc. by e-mail immediately.

Re: converting XML DSL example to Java

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

Or inline the expressions as a parameter argument

transform( xxxx )

transform(simple("Hi ...."))

Then you don't need any kind of endChoice etc.


On Mon, Jun 12, 2017 at 11:07 AM, Alessandro Rontani
<fu...@gmail.com> wrote:
> I think you can use .endChoice() to end a  branch, so you can try this:
>
>  public void configure() {
>   from("servlet:hello")
>   .choice()
>     .when(header("name"))
>       .transform().simple("Hi ${header.name}")
>     .endChoice()
>     .otherwise()
>       .transform().constant("no name")
>     .endChoice()
>   .end();
>  }
>
> see
> http://camel.apache.org/why-can-i-not-use-when-or-otherwise-in-a-java-camel-route.html
> for some deeper explanation
>
> Best regards
> Alessandro
>
>
> 2017-06-11 14:37 GMT+01:00 Ralph Cook <ra...@accesspointinc.com>:
>
>> The camel example for using Spring and XML DSL routing has the following
>> config file:
>>
>>         <beans
>>             xmlns="http://www.springframework.org/schema/beans"
>>             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>>             xmlns:camel="http://camel.apache.org/schema/spring"
>>             xsi:schemaLocation="
>>                     http://www.springframework.org/schema/beans
>> http://www.springframework.org/schema/beans/spring-beans.xsd
>>                     http://camel.apache.org/schema/spring
>> http://camel.apache.org/schema/spring/camel-spring.xsd"
>>         >
>>
>>           <camelContext xmlns="http://camel.apache.org/schema/spring">
>>
>>             <route id="helloRoute">
>>               <!-- incoming requests from the servlet is routed -->
>>               <from uri="servlet:hello"/>
>>               <choice>
>>                 <when>
>>                   <!-- is there a header with the key name? -->
>>                   <header>name</header>
>>                   <!-- yes so return back a message to the user -->
>>                   <transform>
>>                     <simple>Hi I am ${sysenv.HOSTNAME}. Hello ${
>> header.name} how are you today?</simple>
>>                   </transform>
>>                 </when>
>>                 <otherwise>
>>                   <!-- if no name parameter then output a syntax to the
>> user -->
>>                   <transform>
>>                     <constant>Add a name parameter to uri, eg
>> ?name=foo</constant>
>>                   </transform>
>>                 </otherwise>
>>               </choice>
>>             </route>
>>
>>           </camelContext>
>>
>>         </beans>
>>
>> I have this running; it puts the designated string on the browser page as
>> a result after the user enters the correct URL in the browser address bar.
>>
>> As an exercise, I'm attempting to do this in Java.  I have the following
>> configuration:
>>
>> <beans
>>     xmlns="http://www.springframework.org/schema/beans"
>>     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>>     xmlns:camel="http://camel.apache.org/schema/spring"
>>     xsi:schemaLocation="
>>             http://www.springframework.org/schema/beans
>> http://www.springframework.org/schema/beans/spring-beans.xsd
>>             http://camel.apache.org/schema/spring
>> http://camel.apache.org/schema/spring/camel-spring.xsd"
>> >
>>
>>   <camelContext xmlns="http://camel.apache.org/schema/spring">
>>       <packageScan>
>>           <package>com.api.cameltwo.camelroutes</package>
>>       </packageScan>
>>   </camelContext>
>>
>> </beans>
>>
>> and the following Java file:
>>
>> package com.api.cameltwo.camelroutes;
>>
>> import org.apache.camel.Expression;
>> import org.apache.camel.spring.SpringRouteBuilder;
>>
>> public class IncomingUrlRouteBuilder extends SpringRouteBuilder {
>>     Expression e;
>>
>>     public void configure() {
>>         from("servlet:hello")
>>           .choice()
>>              .when(header("name"))
>>                 .transform().simple("Hi ${header.name}")
>>                 .to("file:target/messages?fileName=messageOne.txt")
>>
>>              .otherwise()
>>                 .transform().constant("no name")
>>                 .to("file:target/messages?fileName=messageEmpty.txt")
>> ;
>>     }
>> }
>>
>> This works; the indicated string goes back to the browser screen, and gets
>> written to the file. The thing I don't understand yet is the requirement
>> for the '.to("file...")' clauses/calls in the Java version. I put in
>> "file..." as an experiment; it seems that something is required after
>> '.transform().simple()', but I don't know what. If I don't put that there,
>> the compiler gives the error:
>>
>> cannot find symbol
>>   symbol:   method otherwise()
>>   location: class ProcessorDefinition<ChoiceDefinition>
>>
>> So what is it I need to put after the '.when()' to indicate that this
>> route is done? I gather the .transform().simple() already puts the required
>> output where it needs to go to get back to the browser result page.
>>
>>
>>
>> The contents of this email and any attachments to it may contain Customer
>> Proprietary Network Information (CPNI) as defined by the FCC and/or
>> privileged and confidential information from Access Point, Inc. Any and all
>> of this information is only for the viewing or use of the intended
>> recipient. If you are not the intended recipient, you are hereby notified
>> that any disclosure, copying, distribution or use of, or the taking of any
>> action in reliance upon, the information contained in this e-mail, or any
>> of the attachments to this e-mail, is strictly prohibited and that this
>> e-mail and all of the attachments to this e-mail, if any, must be
>> immediately returned to Access Point, Inc. or destroyed and, in either
>> case, this e-mail and all attachments to this e-mail must be immediately
>> deleted from your computer without making any copies hereof. If you have
>> received this e-mail in error, please notify Access Point, Inc. by e-mail
>> immediately.
>>
>
>
>
> --
> *Siamo quello che facciamo ripetutamente. L'eccellenza non e' un atto, ma
> abitudine - *Aristotele



-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2

Re: converting XML DSL example to Java

Posted by Alessandro Rontani <fu...@gmail.com>.
I think you can use .endChoice() to end a  branch, so you can try this:

 public void configure() {
  from("servlet:hello")
  .choice()
    .when(header("name"))
      .transform().simple("Hi ${header.name}")
    .endChoice()
    .otherwise()
      .transform().constant("no name")
    .endChoice()
  .end();
 }

see
http://camel.apache.org/why-can-i-not-use-when-or-otherwise-in-a-java-camel-route.html
for some deeper explanation

Best regards
Alessandro


2017-06-11 14:37 GMT+01:00 Ralph Cook <ra...@accesspointinc.com>:

> The camel example for using Spring and XML DSL routing has the following
> config file:
>
>         <beans
>             xmlns="http://www.springframework.org/schema/beans"
>             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>             xmlns:camel="http://camel.apache.org/schema/spring"
>             xsi:schemaLocation="
>                     http://www.springframework.org/schema/beans
> http://www.springframework.org/schema/beans/spring-beans.xsd
>                     http://camel.apache.org/schema/spring
> http://camel.apache.org/schema/spring/camel-spring.xsd"
>         >
>
>           <camelContext xmlns="http://camel.apache.org/schema/spring">
>
>             <route id="helloRoute">
>               <!-- incoming requests from the servlet is routed -->
>               <from uri="servlet:hello"/>
>               <choice>
>                 <when>
>                   <!-- is there a header with the key name? -->
>                   <header>name</header>
>                   <!-- yes so return back a message to the user -->
>                   <transform>
>                     <simple>Hi I am ${sysenv.HOSTNAME}. Hello ${
> header.name} how are you today?</simple>
>                   </transform>
>                 </when>
>                 <otherwise>
>                   <!-- if no name parameter then output a syntax to the
> user -->
>                   <transform>
>                     <constant>Add a name parameter to uri, eg
> ?name=foo</constant>
>                   </transform>
>                 </otherwise>
>               </choice>
>             </route>
>
>           </camelContext>
>
>         </beans>
>
> I have this running; it puts the designated string on the browser page as
> a result after the user enters the correct URL in the browser address bar.
>
> As an exercise, I'm attempting to do this in Java.  I have the following
> configuration:
>
> <beans
>     xmlns="http://www.springframework.org/schema/beans"
>     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>     xmlns:camel="http://camel.apache.org/schema/spring"
>     xsi:schemaLocation="
>             http://www.springframework.org/schema/beans
> http://www.springframework.org/schema/beans/spring-beans.xsd
>             http://camel.apache.org/schema/spring
> http://camel.apache.org/schema/spring/camel-spring.xsd"
> >
>
>   <camelContext xmlns="http://camel.apache.org/schema/spring">
>       <packageScan>
>           <package>com.api.cameltwo.camelroutes</package>
>       </packageScan>
>   </camelContext>
>
> </beans>
>
> and the following Java file:
>
> package com.api.cameltwo.camelroutes;
>
> import org.apache.camel.Expression;
> import org.apache.camel.spring.SpringRouteBuilder;
>
> public class IncomingUrlRouteBuilder extends SpringRouteBuilder {
>     Expression e;
>
>     public void configure() {
>         from("servlet:hello")
>           .choice()
>              .when(header("name"))
>                 .transform().simple("Hi ${header.name}")
>                 .to("file:target/messages?fileName=messageOne.txt")
>
>              .otherwise()
>                 .transform().constant("no name")
>                 .to("file:target/messages?fileName=messageEmpty.txt")
> ;
>     }
> }
>
> This works; the indicated string goes back to the browser screen, and gets
> written to the file. The thing I don't understand yet is the requirement
> for the '.to("file...")' clauses/calls in the Java version. I put in
> "file..." as an experiment; it seems that something is required after
> '.transform().simple()', but I don't know what. If I don't put that there,
> the compiler gives the error:
>
> cannot find symbol
>   symbol:   method otherwise()
>   location: class ProcessorDefinition<ChoiceDefinition>
>
> So what is it I need to put after the '.when()' to indicate that this
> route is done? I gather the .transform().simple() already puts the required
> output where it needs to go to get back to the browser result page.
>
>
>
> The contents of this email and any attachments to it may contain Customer
> Proprietary Network Information (CPNI) as defined by the FCC and/or
> privileged and confidential information from Access Point, Inc. Any and all
> of this information is only for the viewing or use of the intended
> recipient. If you are not the intended recipient, you are hereby notified
> that any disclosure, copying, distribution or use of, or the taking of any
> action in reliance upon, the information contained in this e-mail, or any
> of the attachments to this e-mail, is strictly prohibited and that this
> e-mail and all of the attachments to this e-mail, if any, must be
> immediately returned to Access Point, Inc. or destroyed and, in either
> case, this e-mail and all attachments to this e-mail must be immediately
> deleted from your computer without making any copies hereof. If you have
> received this e-mail in error, please notify Access Point, Inc. by e-mail
> immediately.
>



-- 
*Siamo quello che facciamo ripetutamente. L'eccellenza non e' un atto, ma
abitudine - *Aristotele