Layer7 API Management

Expand all | Collapse all

How can I create a simple arithmetic operation (a   b, for example)?

Jump to Best Answer
  • 1.  How can I create a simple arithmetic operation (a   b, for example)?

    Posted 01-30-2015 12:14 PM

    I have to do some logic like "if x <= a + b { //do something }", and I can't find an easy way to do that?

     

    Does anyone have a clue?



  • 2.  Re: How can I create a simple arithmetic operation (a   b, for example)?
    Best Answer

    Posted 01-30-2015 12:22 PM

    This can be done using a few assertion to calculate the 2 numbers and then compare if the result is less than another number. Below you will find a policy fragment on how to do this.

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

    <wsp:Policy xmlns:L7p="http://www.layer7tech.com/ws/policy" xmlns:wsp="http://schemas.xmlsoap.org/ws/2002/12/policy">

        <wsp:All wsp:Usage="Required">

            <L7p:SetVariable>

                <L7p:Base64Expression stringValue="PGEvPg=="/>

                <L7p:ContentType stringValue="text/xml; charset=utf-8"/>

                <L7p:DataType variableDataType="message"/>

                <L7p:VariableToSet stringValue="math"/>

            </L7p:SetVariable>

            <L7p:SetVariable>

                <L7p:Base64Expression stringValue="MTU="/>

                <L7p:VariableToSet stringValue="a"/>

            </L7p:SetVariable>

            <L7p:SetVariable>

                <L7p:Base64Expression stringValue="MTI="/>

                <L7p:VariableToSet stringValue="b"/>

            </L7p:SetVariable>

            <L7p:SetVariable>

                <L7p:Base64Expression stringValue="MzA="/>

                <L7p:VariableToSet stringValue="result"/>

            </L7p:SetVariable>

            <L7p:ResponseXpathAssertion>

                <L7p:VariablePrefix stringValue="simpleMath"/>

                <L7p:XmlMsgSrc stringValue="math"/>

                <L7p:XpathExpression xpathExpressionValue="included">

                    <L7p:Expression stringValue="$a + $b"/>

                    <L7p:Namespaces mapValue="included">

                        <L7p:entry>

                            <L7p:key stringValue="s"/>

                            <L7p:value stringValue="http://schemas.xmlsoap.org/soap/envelope/"/>

                        </L7p:entry>

                    </L7p:Namespaces>

                    <L7p:XpathVersion xpathVersion="XPATH_1_0"/>

                </L7p:XpathExpression>

            </L7p:ResponseXpathAssertion>

            <L7p:ComparisonAssertion>

                <L7p:Expression1 stringValue="${simpleMath.results}"/>

                <L7p:Expression2 stringValue="${result}"/>

                <L7p:FailIfVariableNotFound booleanValue="false"/>

                <L7p:Operator operator="LE"/>

                <L7p:Predicates predicates="included">

                    <L7p:item binary="included">

                        <L7p:Operator operator="LE"/>

                        <L7p:RightValue stringValue="${result}"/>

                    </L7p:item>

                </L7p:Predicates>

            </L7p:ComparisonAssertion>

        </wsp:All>

    </wsp:Policy>



  • 3.  Re: How can I create a simple arithmetic operation (a   b, for example)?

    Posted 02-02-2015 03:36 AM

    Its works perfectly!

     

    Thank you very much!

     

    Best regards.



  • 4.  Re: How can I create a simple arithmetic operation (a   b, for example)?

    Posted 02-02-2015 09:56 AM

    We do have a custom assertion that does this for us!

     

    Sent from my iPhone



  • 5.  Re: How can I create a simple arithmetic operation (a   b, for example)?

    Posted 02-05-2015 12:31 PM

    The XPath assertion should cover your basic math needs.

     

    If you're doing anything more sophisticated that XPath can't do - e.g. mathematical functions like SIN and LOG -  there's a Tactical "Evaluate Math Expression" assertion that might be of value. Talk to Support if you need it.