<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
    this work for additional information regarding copyright ownership.
    The ASF licenses this file to You under the Apache License, Version 2.0
    (the "License"); you may not use this file except in compliance with
    the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:broker="http://activemq.apache.org/schema/core"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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
         http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.5.0.xsd">

  <!-- create an ActiveMQ broker -->
  <!-- do not use the shutdown hook as it would cause the broker to shutdown when you press ctrl + c -->
  <broker:broker id="broker" useJmx="true" persistent="false" brokerName="myBroker" useShutdownHook="false">
    <broker:transportConnectors>
      <broker:transportConnector name="vm" uri="vm://myBroker"/>
    </broker:transportConnectors>
  </broker:broker>

  <!-- camel jms client -->
  <bean class="org.apache.activemq.camel.component.ActiveMQComponent" id="jms">
    <property name="brokerURL" value="vm://myBroker"/>
  </bean>

  <!-- Camel with the routes -->
  <camelContext xmlns="http://camel.apache.org/schema/spring">

    <route id="FileToJMS">
      <from uri="file:target/placeorder"/>
      <log message="Received file ${file:name}"/>
      <to uri="jms:incomingOrders"/>
    </route>

    <route id="HTTPtoJMS">
      <from uri="jetty:http://0.0.0.0:8888/placeorder"/>
      <convertBodyTo type="java.lang.String"/>
      <log message="Received HTTP order ${body}"/>
      <inOnly uri="jms:incomingOrders"/>
      <transform>
        <constant>OK</constant>
      </transform>
      <log message="Replying HTTP order with OK"/>
    </route>

    <route id="NormalizeMessageData">
      <from uri="jms:incomingOrders"/>
      <convertBodyTo type="java.lang.String"/>
      <choice>
        <when>
          <simple>${body} contains '?xml'</simple>
          <log message="Transforming from XML to Order objects"/>
          <split>
            <xpath>/orders/order</xpath>
            <unmarshal>
              <jaxb contextPath="org.fusesource.camel"/>
            </unmarshal>
            <to uri="jms:orders"/>
          </split>
        </when>
        <otherwise>
          <log message="Transforming from CSV to Order objects"/>
          <split>
            <tokenize token="\n"/>
            <unmarshal>
              <bindy packages="org.fusesource.camel" type="Csv"/>
            </unmarshal>
            <to uri="jms:orders"/>
          </split>
        </otherwise>
      </choice>
    </route>

  </camelContext>

</beans>
