<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
  ~  Copyright (c) 2009, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
  ~
  ~  WSO2 Inc. 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.
  -->

<!DOCTYPE html
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta content="text/html; charset=iso-8859-1" http-equiv="content-type"/>
  <title>WSO2 ESB - Templates Samples </title>
</head>

<body>
<h1>Running Templates with WSO2 Enterprise Service Bus (ESB)</h1>

    <h2><a name="Sample750">Sample 750: Stereotyping XSLT Transformations with Templates </a> </h2>
    <pre xml:space="preserve">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
    &lt;definitions xmlns=&quot;http://ws.apache.org/ns/synapse&quot;&gt;

        &lt;proxy name=&quot;StockQuoteProxy&quot;&gt;
            &lt;target&gt;
            &lt;inSequence&gt;
            &lt;!--use sequence template to trasnform incoming request--&gt;
            &lt;call-template target=&quot;xslt_func&quot;&gt;
                            &lt;with-param name=&quot;xslt_key&quot; value=&quot;xslt-key-req&quot;/&gt;
                    &lt;/call-template&gt;
            &lt;send&gt;
                &lt;endpoint&gt;
                                  &lt;address uri=&quot;http://localhost:9000/services/SimpleStockQuoteService&quot;/&gt;
                            &lt;/endpoint&gt;
                    &lt;/send&gt;
                &lt;/inSequence&gt;

                &lt;outSequence&gt;
            &lt;!--use sequence template to trasnform incoming response--&gt;
            &lt;call-template target=&quot;xslt_func&quot;&gt;
                            &lt;with-param name=&quot;xslt_key&quot; value=&quot;xslt-key-back&quot;/&gt;
                    &lt;/call-template&gt;
                    &lt;send/&gt;
                &lt;/outSequence&gt;
            &lt;/target&gt;
        &lt;/proxy&gt;

        &lt;!--this sequence template will trasnform requests with the given xslt local entry key And will log
        the message before and after. Takes Iterate local entry key as an argument--&gt;
        &lt;template xmlns=&quot;http://ws.apache.org/ns/synapse&quot; name=&quot;xslt_func&quot;&gt;
            &lt;parameter name=&quot;xslt_key&quot;/&gt;
            &lt;sequence&gt;
                    &lt;log level=&quot;full&quot;&gt;
                &lt;property name=&quot;BEFORE_TRANSFORM&quot; value=&quot;true&quot; /&gt;
            &lt;/log&gt;
                 &lt;xslt key=&quot;{$func:xslt_key}&quot;/&gt;
            &lt;log level=&quot;full&quot;&gt;
                &lt;property name=&quot;AFTER_TRANSFORM&quot; value=&quot;true&quot; /&gt;
            &lt;/log&gt;
            &lt;/sequence&gt;
        &lt;/template&gt;

        &lt;localEntry key=&quot;xslt-key-req&quot; src=&quot;file:repository/samples/resources/transform/transform.xslt&quot;/&gt;
        &lt;localEntry key=&quot;xslt-key-back&quot; src=&quot;file:repository/samples/resources/transform/transform_back.xslt&quot;/&gt;
    &lt;/definitions&gt;</pre>

    <p><strong>Objective: Introduction to ESB Sequence Templates</strong> </p>

    <p><strong>Prerequisites:</strong><br />
    Start the Synapse configuration numbered 750: i.e. wso2esb-samples -sn 750<br />
    Start the Axis2 server and deploy the SimpleStockQuoteService if not already
    done </p>

    <p>Execute the stock quote client by requesting for a stock quote on the
    proxy service as follows: </p>
    <pre xml:space="preserve">ant stockquote -Daddurl=http://localhost:8280/services/StockQuoteProxy  -Dmode=customquote</pre>

    <p> ESB Sequence Template can act a reusable function. Here proxy service reuses template xslt_func which will transform requests with the given xslt local entry key And will log
        the message before and after. It takes xslt transformation corresponding to local entry key as an
        argument (for insequence this key is xslt-key-req and out sequence it is xslt-key-back). We use
        call-template mediator for passing the xslt key parameter to a sequence template. ESB console
        will display how the custom stockquote is transformed and the transformed response from the service.   </p>


<h2><a name="Sample751">Sample 751: Message Split Aggregate Using Templates </a> </h2>
<pre xml:space="preserve">&lt;definitions xmlns=&quot;http://ws.apache.org/ns/synapse&quot;&gt;

    &lt;proxy name=&quot;SplitAggregateProxy&quot;&gt;
        &lt;target&gt;
            &lt;inSequence&gt;
		&lt;!--use iterate sequence template to split incoming request and send--&gt;
                &lt;call-template target=&quot;iter_func&quot;&gt;
                        &lt;with-param xmlns:m0=&quot;http://services.samples&quot; name=&quot;iter_expr&quot; value=&quot;{{//m0:getQuote/m0:request}}&quot;/&gt;
 			&lt;with-param xmlns:m0=&quot;http://services.samples&quot; name=&quot;attach_path&quot; value=&quot;{{//m0:getQuote}}&quot;/&gt;
                &lt;/call-template&gt;
            &lt;/inSequence&gt;
            &lt;outSequence&gt;
		&lt;!--use aggregate sequence template to combine the responses and send back--&gt;
                &lt;call-template target=&quot;aggr_func&quot;&gt;
                        &lt;with-param xmlns:m0=&quot;http://services.samples&quot; name=&quot;aggr_expr&quot; value=&quot;{{//m0:getQuoteResponse}}&quot;/&gt;
                &lt;/call-template&gt;
            &lt;/outSequence&gt;
        &lt;/target&gt;
    &lt;/proxy&gt;

    &lt;!--this sequence template will aggregate the responses , merge and send back to the client. This takes aggregate
	expression as an argument--&gt;
    &lt;template xmlns=&quot;http://ws.apache.org/ns/synapse&quot; name=&quot;aggr_func&quot;&gt;
        &lt;parameter name=&quot;aggr_expr&quot;/&gt;
        &lt;sequence&gt;
            &lt;log level=&quot;full&quot;/&gt;
            &lt;aggregate&gt;
                &lt;completeCondition&gt;
                    &lt;messageCount min=&quot;-1&quot; max=&quot;-1&quot;/&gt;
                &lt;/completeCondition&gt;
                &lt;onComplete expression=&quot;$func:aggr_expr&quot;&gt;
                    &lt;log level=&quot;full&quot; /&gt;
                    &lt;send/&gt;
                &lt;/onComplete&gt;
            &lt;/aggregate&gt;
        &lt;/sequence&gt;
    &lt;/template&gt;

    &lt;!--this sequence template will iterate through stock quote symbols ,split and send them to endpoints. Takes Iterate
	expression and soap attaach path as arguments --&gt;
    &lt;template xmlns=&quot;http://ws.apache.org/ns/synapse&quot; name=&quot;iter_func&quot;&gt;
        &lt;parameter name=&quot;iter_expr&quot;/&gt;
        &lt;parameter name=&quot;attach_path&quot;/&gt;
        &lt;sequence&gt;

            &lt;iterate  xmlns:m0=&quot;http://services.samples&quot; preservePayload=&quot;true&quot; attachPath=&quot;$func:attach_path&quot; expression=&quot;$func:iter_expr&quot;&gt;
                &lt;target&gt;
                    &lt;sequence&gt;
                        &lt;send&gt;
                            &lt;endpoint&gt;
                                &lt;address uri=&quot;http://localhost:9000/services/SimpleStockQuoteService&quot;/&gt;
                            &lt;/endpoint&gt;
                        &lt;/send&gt;
                    &lt;/sequence&gt;
                &lt;/target&gt;
            &lt;/iterate&gt;
        &lt;/sequence&gt;
    &lt;/template&gt;
&lt;/definitions&gt;</pre>

    <p><strong>Objective: Demonstrate the use of tempaltes in Split/Aggregate scenario . </strong></p>

    <p><strong>Prerequisites:</strong>Deploy the SimpleStockQuoteService in sample
    Axis2 server and start it on port 9000. </p>

    <p>Start ESB with the sample configuration 751 (i.e. wso2esb-samples -sn 751).
    </p>

    <p>This will be the same sample demonstrated in advanced mediation with Iterate mediator used to split the
    messages in to parts and process them asynchronously and then aggregate the
    responses coming in to ESB. Only addition will be two sequence  templates 'iter_func' and
    'aggr_func' which will split the message and aggregate the responses back , respectively.</p>

    <p>Invoke the client as follows. </p>
    <pre xml:space="preserve">ant stockquote -Daddurl=http://localhost:8280/services/SplitAggregateProxy -Ditr=4</pre>

    <p>Sequence Template 'iter_func' takes iterate expressions and attach path as arguments.</p>

    <p>Sequence Template 'aggr_func' takes aggregate expression as argument.</p>

    <p>In this sample we are using special dynamic xpath expressions(in double curly braces {{expr}}) in call-template mediator so that
    they are evaluated only when iteration and aggregation is done. Note that function scope (ie:-$func) is used to access template 
    parameters in 'iter_func' and 'aggr_func'.
    </p>

    <h2><a name="Sample752">Sample 752: Load balancing Between three Endpoints With Endpoint Templates </a> </h2>
<pre xml:space="preserve">&lt;definitions xmlns=&quot;http://ws.apache.org/ns/synapse&quot;&gt;

    &lt;proxy name=&quot;LBProxy&quot; transports=&quot;https http&quot; startOnLoad=&quot;true&quot;&gt;
        &lt;target faultSequence=&quot;errorHandler&quot;&gt;
            &lt;inSequence&gt;
                &lt;send&gt;
                    &lt;endpoint&gt;
                        &lt;session type=&quot;simpleClientSession&quot;/&gt;
                        &lt;loadbalance algorithm=&quot;org.apache.synapse.endpoints.algorithms.RoundRobin&quot;&gt;
                            &lt;endpoint name=&quot;templ_ep1&quot; template=&quot;endpoint_template&quot; uri=&quot;http://localhost:9001/services/LBService1&quot;&gt;
				&lt;parameter name=&quot;suspend_duration&quot; value=&quot;5&quot;/&gt;
                            &lt;/endpoint&gt;
			    &lt;endpoint name=&quot;templ_ep2&quot; template=&quot;endpoint_template&quot; uri=&quot;http://localhost:9002/services/LBService1&quot;&gt;
				&lt;parameter name=&quot;suspend_duration&quot; value=&quot;20&quot;/&gt;
                            &lt;/endpoint&gt;
			    &lt;endpoint name=&quot;templ_ep3&quot; template=&quot;endpoint_template&quot; uri=&quot;http://localhost:9003/services/LBService1&quot;&gt;
				&lt;parameter name=&quot;suspend_duration&quot; value=&quot;200&quot;/&gt;
                            &lt;/endpoint&gt;
                        &lt;/loadbalance&gt;
                    &lt;/endpoint&gt;
                &lt;/send&gt;
                &lt;drop/&gt;
            &lt;/inSequence&gt;
            &lt;outSequence&gt;
                &lt;send/&gt;
            &lt;/outSequence&gt;
        &lt;/target&gt;
        &lt;publishWSDL uri=&quot;file:repository/samples/resources/proxy/sample_proxy_2.wsdl&quot;/&gt;
    &lt;/proxy&gt;

    &lt;sequence name=&quot;errorHandler&quot;&gt;
        &lt;makefault&gt;
            &lt;code value=&quot;tns:Receiver&quot; xmlns:tns=&quot;http://www.w3.org/2003/05/soap-envelope&quot;/&gt;
            &lt;reason value=&quot;COULDN&#39;T SEND THE MESSAGE TO THE SERVER.&quot;/&gt;
        &lt;/makefault&gt;
        &lt;send/&gt;
    &lt;/sequence&gt;



    &lt;template name=&quot;endpoint_template&quot;&gt;
	    &lt;parameter name=&quot;suspend_duration&quot;/&gt;
	    &lt;endpoint name=&quot;annonymous&quot;&gt;
		&lt;address uri=&quot;$uri&quot;&gt;
		    &lt;enableAddressing/&gt;
                    &lt;suspendDurationOnFailure&gt;$suspend_duration&lt;/suspendDurationOnFailure&gt;
		&lt;/address&gt;
	    &lt;/endpoint&gt;
    &lt;/template&gt;

&lt;/definitions&gt;</pre>

    <p><strong>Objective: Demonstrate how to use endpoint templates in a load balance configuration . </strong></p>

    <p><strong>Prerequisites:</strong>Sample setup is same as LoadBalance endpoint scenario (#154).

Start the Synapse configuration numbered 752: i.e. wso2esb-samples -sn 752
Start the Axis2 server and deploy the LoadbalanceFailoverService if not already done

 </p>

    <p>Configuration demonstrates a single endpoint template named 'endpoint_template'. Three endpoint templates
    are created inside loadbalance endpoint which will point to this template. Note different parameters passed
    on to the same template ie:- service url and suspend duration as parameters</p>

    <p>Invoke the client as follows. </p>
    <pre xml:space="preserve"> ant loadbalancefailover -Dmode=session -Dtrpurl=http://localhost:8280/services/LBProxy</pre>


    <p>As in the previous loadbalance samples client console will show loadbalance requests going towards three endpoints
        created from the same template. Notice differnt suspension times  for three services in a failed
        endpoint scenario as well. (reflects different parameters passed onto the template)
    </p>

</body>
</html>
