<?xml version="1.0" encoding="UTF-8"?>
<!--

    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.
-->
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">

    <camelContext xmlns="http://camel.apache.org/schema/blueprint">
        <route id="example-http-inbound">
            <from uri="jetty:http://0.0.0.0:9090/example"/>
            <convertBodyTo type="java.lang.String"/>
            <log message="[EXAMPLE INBOUND] Received: ${body}"/>
            <choice>
                <when>
                    <simple>${headers.CamelHttpMethod} == 'POST'</simple>
                    <setHeader headerName="type">
                        <jsonpath>$.notification.type</jsonpath>
                    </setHeader>
                    <choice>
                        <when>
                            <simple>${header.type} == 'email'</simple>
                            <log message="[EXAMPLE INBOUND] Received email notification"/>
                            <to uri="direct:email"/>
                            <setHeader headerName="Exchange.HTTP_RESPONSE_CODE"><constant>200</constant></setHeader>
                        </when>
                        <when>
                            <simple>${header.type} == 'http'</simple>
                            <log message="[EXAMPLE INBOUND] Received http notification"/>
                            <to uri="direct:http"/>
                            <setHeader headerName="Exchange.HTTP_RESPONSE_CODE"><constant>200</constant></setHeader>
                        </when>
                        <otherwise>
                            <log message="[EXAMPLE INBOUND] Unknown notification"/>
                            <setBody><constant>{ "status": "reject", "type": "unknown" }</constant></setBody>
                            <setHeader headerName="Exchange.HTTP_RESPONSE_CODE"><constant>400</constant></setHeader>
                        </otherwise>
                    </choice>
                </when>
                <otherwise>
                    <log message="[EXAMPLE INBOUND] only POST is accepted (${headers.CamelHttpMethod})"/>
                    <setBody><constant>{ "error": "only POST is accepted" }</constant></setBody>
                    <setHeader headerName="Exchange.HTTP_RESPONSE_CODE"><constant>500</constant></setHeader>
                </otherwise>
            </choice>
        </route>
        <route id="example-email">
            <from uri="direct:email"/>
            <log message="[EXAMPLE EMAIL] Sending notification email"/>
            <setHeader headerName="to"><jsonpath>$.notification.to</jsonpath></setHeader>
            <setHeader headerName="subject"><constant>Notification</constant></setHeader>
            <setBody><jsonpath>$.notification.message</jsonpath></setBody>
            <!-- <to uri="smtp://localhost"/> -->
            <setBody><simple>{ "status": "email sent", "to": "${header.to}", "subject": "${header.subject}" }</simple></setBody>
        </route>
        <route id="example-http">
            <from uri="direct:http"/>
            <log message="[EXAMPLE HTTP] Sending http notification"/>
            <setHeader headerName="service"><jsonpath>$.notification.service</jsonpath></setHeader>
            <!-- <to uri="jetty:..." /> -->
            <setBody><simple>{ "status": "http requested", "service": "${header.service}" }</simple></setBody>
        </route>
    </camelContext>


</blueprint>