<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesource.org/schema/mule/core/2.2"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:vm="http://www.mulesource.org/schema/mule/vm/2.2"
      xmlns:cxf="http://www.mulesource.org/schema/mule/cxf/2.2"
      xmlns:smtp="http://www.mulesource.org/schema/mule/smtp/2.2"
      xmlns:email="http://www.mulesource.org/schema/mule/email/2.2"
      xmlns:servlet="http://www.mulesource.org/schema/mule/servlet/2.2"
      xsi:schemaLocation="
               http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd
               http://www.mulesource.org/schema/mule/vm/2.2 http://www.mulesource.org/schema/mule/vm/2.2/mule-vm.xsd
               http://www.mulesource.org/schema/mule/cxf/2.2 http://www.mulesource.org/schema/mule/cxf/2.2/mule-cxf.xsd
               http://www.mulesource.org/schema/mule/smtp/2.2 http://www.mulesource.org/schema/mule/smtp/2.2/mule-smtp.xsd
               http://www.mulesource.org/schema/mule/email/2.2 http://www.mulesource.org/schema/mule/email/2.2/mule-email.xsd
               http://www.mulesource.org/schema/mule/servlet/2.2 http://www.mulesource.org/schema/mule/servlet/2.2/mule-servlet.xsd">
    
    <!-- Configure some properties to work with GMail's SMTP -->
    <smtp:gmail-connector name="emailConnector" />
 
    <!-- Use this as a poor man's message queue, in the real world we would use JMS -->
    <vm:connector name="vmQueues" queueEvents="true"/>
    
    <!-- This queue contains a feed of the latest statistics generated by the Data Warehouse (it should really be a LIFO queue) -->
    <vm:endpoint name="stats" path="statistics" />

    <message-properties-transformer name="setHtmlContentType" overwrite="true">
        <add-message-property key="Content-Type" value="text/html"/>
        <!-- Tomcat lowercases headers, need to drop this old one too -->
        <delete-message-property key="content-type"/>
    </message-properties-transformer>

    <model name="Bookstore">

          <service name="CatalogService">
               <inbound>
                    <!-- Public interface -->
                    <inbound-endpoint address="cxf:http://0.0.0.0:8777/services/catalog" />

                    <!-- Administration interface -->
                    <inbound-endpoint address="servlet://catalog">                                      
                        <transformers>                                  
                            <!-- Extract parameters from HTTP Request -->
                            <servlet:http-request-to-parameter-map />
                            <!-- Convert request parameters to Book object -->
                            <custom-transformer class="org.mule.example.bookstore.transformers.ParameterMapToBook"/>
                        </transformers>
                        <response-transformers>
                            <!-- Format response to be a nice HTML page -->
                            <custom-transformer class="org.mule.example.bookstore.transformers.AddBookResponse"/>
                            <!-- Force text/html, otherwise it falls back to request props, which have form-encoded one -->
                            <transformer ref="setHtmlContentType"/>
                        </response-transformers>
                    </inbound-endpoint>
               </inbound>
               <component>
                    <singleton-object class="org.mule.example.bookstore.CatalogServiceImpl" />
               </component>
          </service> 

          <service name="OrderService">
               <inbound>
                    <!-- Public interface -->
                    <inbound-endpoint address="cxf:http://0.0.0.0:8777/services/order" />
               </inbound>
               <component>
                    <singleton-object class="org.mule.example.bookstore.OrderServiceImpl" />
               </component>
               <outbound>
                    <multicasting-router>
                        <vm:outbound-endpoint path="emailNotification"/>
                        <vm:outbound-endpoint path="dataWarehouse"/>
                    </multicasting-router>
               </outbound>
          </service>
          
          <service name="EmailNotificationService">
               <inbound>
                    <vm:inbound-endpoint path="emailNotification" />
               </inbound>
               <outbound>
                    <pass-through-router>
                         <smtp:outbound-endpoint user="username" password="password" host="smtp.gmail.com" 
                             from="bookstore@mulesource.com" subject="Your order has been placed!">
                              <transformers>
                                    <custom-transformer class="org.mule.example.bookstore.transformers.OrderToEmailTransformer" />
                                    <email:string-to-email-transformer />
                              </transformers> 
                         </smtp:outbound-endpoint>
                    </pass-through-router>
               </outbound>
          </service>
          
          <service name="DataWarehouse">
               <inbound>
                    <vm:inbound-endpoint path="dataWarehouse"/>
               </inbound>
               <component>
                    <singleton-object class="org.mule.example.bookstore.DataWarehouse" />
               </component>
               <outbound>
                    <pass-through-router>
                        <outbound-endpoint ref="stats" transformer-refs="setHtmlContentType"/>
                    </pass-through-router>
               </outbound>
          </service>
     </model>
</mule>
