You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@synapse.apache.org by Florin Bejinaru <fl...@gmail.com> on 2009/10/01 13:36:02 UTC

Re: Custom request filtering

I finally managed to get synapse to behave the way I wanted, and for anyone
else who might want to use the clone mediator to send requests to multiple
services I am posting the configuration that worked for me.

<definitions xmlns="http://ws.apache.org/ns/synapse">

    <in>
        <!-- Log all messages passing through -->
        <log level="full"/>

        <!-- ensure that the default configuration only sends if it is one
of samples -->
        <!-- Otherwise Synapse would be an open proxy by default
(BAD!)               -->
        <filter source="get-property('To')" regex=".*/Event.*">
            <clone continueParent="true">
                <target to="
http://192.168.239.1:8080/EventService/services/Event/">
                    <sequence>
                        <property action="set" name="service-port"
value="8080"/>
                        <!--<class name="JavaMediator"/>-->
                        <sequence key="localSequence"/>
                        <!--<send/>-->
                    </sequence>

                    <!-- The endpoint for the clone mediator isn't needed
apparently -->
                    <!--<endpoint>
                        <address uri="
http://192.168.239.1:8080/EventService/services/Event"/>
                    </endpoint>-->
                </target>

            </clone>

            <property action="set" name="service-port" value="9000"/>
            <send>
                <endpoint>
                    <address uri="http://192.168.239.128:9000/services/Event
"/>
                </endpoint>
            </send>
            <drop/>

        </filter>
    </in>
    <out>
        <log level="full"/>


        <!-- The next filter drops any responses which are not coming from
the port 9000 endpoint-->

        <filter source="get-property('service-port')" regex=".*9000.*">
            <send/>
        </filter>
    </out>

    <!-- The next sequence is a simple content filtering sequence that only
applies to the cloned request -->

    <sequence name="localSequence">
        <switch source="//Event/EventHeader/name">
            <case regex=".*STREAM_TERMINATED.*">
                <drop/>
            </case>
            <default>
                <send/>
            </default>
        </switch>
    </sequence>
</definitions>

Thanks for being patient with me as I am just getting started with Synapse
Florin


2009/9/30 Florin Bejinaru <fl...@gmail.com>

> Thank you for the swift response, the information you provided was indeed
> helpful.
>
> However, I am still having trouble. I as may have stated before, what I
> need to do is create a clone of the request filter it and send it to one
> endpoint and send the original request to another endpoint.
>
> I have tried this configuration : (for simplicity this is the <in> mediator
> only)
>
> <filter source="get-property('To')" regex=".*/Event.*">
>
>             *<clone continueParent="true">
>                 <target>
>                     <sequence>
>                         <property action="set" name="service-port"
> value="8080"/>
>                         <class name="JavaMediator"/>
>                         <!--<sequence key="localSequence"/>-->
>                         <send/>
>                     </sequence>
>                     <endpoint>
>                         <address uri="
> http://192.168.239.1:8080/EventService/services/Event"/>
>                     </endpoint>
>                 </target>
>
>             </clone>*
>
>             <property action="set" name="service-port" value="9000"/>
>             <send>
>                 <endpoint>
>                     <address uri="
> http://192.168.239.128:9000/services/Event"/>
>                 </endpoint>
>             </send>
>             <drop/>
>
>         </filter>
>     </in>
>
> but this raises a "system cannot infer the transport information from the
> /services/Event URL", which in my opinion is normal because the endpoint is
> not set before the sequence with the send is executed, but if I substitute
> the above clone element with :
>
>             <clone continueParent="true">
>                 <target>
>                     <sequence>
>                         <property action="set" name="service-port"
> value="8080"/>
>                         <class name="JavaMediator"/>
>                         <!--<sequence key="localSequence"/>-->
>                     </sequence>
>                     <endpoint>
>                         <address uri="
> http://192.168.239.1:8080/EventService/services/Event"/>
>                     </endpoint>
>                     <send/>
>                     <!--<outsequence>
>                         <send/>
>                     </outsequence>-->
>                 </target>
>
>             </clone>
>
> nothing gets sent to the :8080/EventService endpoint. Which also seems
> reasonable because there is no <send/> mediator in the sequence.
>
> So how do I get the clone mediator to filter and send to the specified
> endpoint , should I manually set the 'To' property inside the clone mediator
> sequence ?
>
> Thank you,
> Florin
>
>
> 2009/9/30 Hiranya Jayathilaka <hi...@gmail.com>
>
> Hi Florin,
>> Please find the answers to your questions and some additional comments
>> inline.
>>
>> On Tue, Sep 29, 2009 at 4:42 PM, Florin Bejinaru
>> <fl...@gmail.com>wrote:
>>
>> > Hello again,
>> >
>> > I want to do custom request filtering for just one of the endpoints
>> > mentioned in my previous mail (a port 9000 endpoint and a port 8080
>> > endpoint). So what I want to do is send every request to the 9000
>> endpoint
>> > and filter the requests for the 8080 endpoint based on the request
>> content.
>> >
>> > I need to do dynamic filtering (in that you should be able to change the
>> > filters at run time and these should be managed centrally).
>> >
>> > I figure I can do the filtering in two ways:
>> > One would be to filter based on a set of xslt files located on a remote
>> > registry, and the other to use a "class" mediator with a custom class
>> that
>> > filters based on a set of xslt files loaded through a web service,
>> hosted
>> > by
>> > synapse preferably.
>> >
>> > To save me from trying to do both of these, could you tell me if :
>> >
>> > 1) By using a remote registry can I set a filtering xslt, to filter
>> > requests
>> > for the 8080 endpoint?
>> >
>>
>> The most common way of implementing content based routing with Synapse is
>> by
>> using the filter mediator or by using the switch mediator. Both these
>> mediators take an XPath expression (not XSLT) based on which to perform
>> the
>> filtering. You can put these mediators into a sequence in a way so that
>> your
>> filtering requirements are properly handled and then save the entire
>> sequence in the remote registry. Then in the main sequence you will be
>> able
>> to refer to the sequence in the registry using a key. You can make
>> modifications to the sequence at runtime and Synapse will load the
>> sequence
>> in the registry from time to time. Thus any changes you have made will
>> take
>> effect at runtime.
>>
>>
>> > 2) By using a class mediator, can I host a web-service in synapse that
>> > saves
>> > files somewhere where they can be accessed by my class mediator ?
>> >
>>
>> You cannot host Web Services in Synapse. But with the class mediator you
>> can
>> write some custom code which interacts with the file system. Anyway I
>> don't
>> think it's a good idea to get your mediators read/write to the file
>> system.
>> That might have a significant performance hit. You should be using the
>> registry to store resource. Then you can benefit from our core API and the
>> caching features.
>>
>>
>> >
>> > 3) With the class mediator implementation, what are my xslt filtering
>> > options, what XSLT engine can I (should I) use from my Java mediator
>> class
>> > ?
>> >
>>
>> As I mentioned earlier with the class mediator you can get Synapse to
>> execute any piece of custom code. So you can use any third party libraries
>> you want when writing the custom code. But filtering is a basic feature of
>> Synapse and ideally you should be using the filter/switch mediators to get
>> this done. That is far more efficient and easier.
>>
>>
>> >
>> > Any help would be greatly appreciated !
>> >
>>
>> I think you can use a configuration similar to the following to implement
>> your scenario:
>>
>> <sequence name="main">
>>     <in>
>>           <sequence key="/key/to/dynamic/sequence"/>
>>      </in>
>>      <out>
>>           <send/>
>>      </out>
>> </sequence>
>>
>> Dynamic sequence in the registry:
>>
>> <sequence name="foo">
>>        <switch source="get-property('To')">
>>                <case regex="http://localhost:8080*">
>>                        <filter source="xpath to examine the content"
>> regex="regular expression">
>>                              <send/>
>>                         </filter>
>>                         <drop/>
>>                 </case>
>>                 <case regex="http://localhost:9000*">
>>                         <send/>
>>                 </case>
>>         </switch>
>> </sequence>
>>
>> This is just one way of doing it (may be not the most elegant way). Please
>> have a loot at sample 1 and 2 in our  sample guide for more info.
>>
>> Thanks,
>> Hiranya
>>
>>
>>
>> > Thank you,
>> > Florin
>> >
>>
>>
>>
>> --
>> Hiranya Jayathilaka
>> Software Engineer;
>> WSO2 Inc.;  http://wso2.org
>> E-mail: hiranya@wso2.com;  Mobile: +94 77 633 3491
>> Blog: http://techfeast-hiranya.blogspot.com
>>
>
>

Re: Custom request filtering

Posted by Hiranya Jayathilaka <hi...@gmail.com>.
Hi Florin,

On Thu, Oct 1, 2009 at 5:06 PM, Florin Bejinaru <fl...@gmail.com>wrote:

> I finally managed to get synapse to behave the way I wanted, and for anyone
> else who might want to use the clone mediator to send requests to multiple
> services I am posting the configuration that worked for me.
>

Good to see that you have sorted this out. Thanks a lot for posting your
configuration for our future reference. Please feel free to contact the
community if you encounter any issues in the future.

Thanks,
Hiranya


> <definitions xmlns="http://ws.apache.org/ns/synapse">
>
>    <in>
>        <!-- Log all messages passing through -->
>        <log level="full"/>
>
>        <!-- ensure that the default configuration only sends if it is one
> of samples -->
>        <!-- Otherwise Synapse would be an open proxy by default
> (BAD!)               -->
>         <filter source="get-property('To')" regex=".*/Event.*">
>            <clone continueParent="true">
>                 <target to="
> http://192.168.239.1:8080/EventService/services/Event/">
>                     <sequence>
>                        <property action="set" name="service-port"
> value="8080"/>
>                        <!--<class name="JavaMediator"/>-->
>                        <sequence key="localSequence"/>
>                        <!--<send/>-->
>                    </sequence>
>
>                     <!-- The endpoint for the clone mediator isn't needed
> apparently -->
>                    <!--<endpoint>
>                         <address uri="
> http://192.168.239.1:8080/EventService/services/Event"/>
>                    </endpoint>-->
>                </target>
>
>            </clone>
>
>            <property action="set" name="service-port" value="9000"/>
>            <send>
>                <endpoint>
>                    <address uri="
> http://192.168.239.128:9000/services/Event
> "/>
>                </endpoint>
>            </send>
>            <drop/>
>
>        </filter>
>    </in>
>     <out>
>        <log level="full"/>
>
>
>        <!-- The next filter drops any responses which are not coming from
> the port 9000 endpoint-->
>
>        <filter source="get-property('service-port')" regex=".*9000.*">
>            <send/>
>        </filter>
>    </out>
>
>    <!-- The next sequence is a simple content filtering sequence that only
> applies to the cloned request -->
>
>    <sequence name="localSequence">
>        <switch source="//Event/EventHeader/name">
>            <case regex=".*STREAM_TERMINATED.*">
>                <drop/>
>            </case>
>            <default>
>                <send/>
>            </default>
>        </switch>
>    </sequence>
> </definitions>
>
> Thanks for being patient with me as I am just getting started with Synapse
> Florin
>
>
> 2009/9/30 Florin Bejinaru <fl...@gmail.com>
>
> > Thank you for the swift response, the information you provided was indeed
> > helpful.
> >
> > However, I am still having trouble. I as may have stated before, what I
> > need to do is create a clone of the request filter it and send it to one
> > endpoint and send the original request to another endpoint.
> >
> > I have tried this configuration : (for simplicity this is the <in>
> mediator
> > only)
> >
> > <filter source="get-property('To')" regex=".*/Event.*">
> >
> >             *<clone continueParent="true">
> >                 <target>
> >                     <sequence>
> >                         <property action="set" name="service-port"
> > value="8080"/>
> >                         <class name="JavaMediator"/>
> >                         <!--<sequence key="localSequence"/>-->
> >                         <send/>
> >                     </sequence>
> >                     <endpoint>
> >                         <address uri="
> > http://192.168.239.1:8080/EventService/services/Event"/>
> >                     </endpoint>
> >                 </target>
> >
> >             </clone>*
> >
> >             <property action="set" name="service-port" value="9000"/>
> >             <send>
> >                 <endpoint>
> >                     <address uri="
> > http://192.168.239.128:9000/services/Event"/>
> >                 </endpoint>
> >             </send>
> >             <drop/>
> >
> >         </filter>
> >     </in>
> >
> > but this raises a "system cannot infer the transport information from the
> > /services/Event URL", which in my opinion is normal because the endpoint
> is
> > not set before the sequence with the send is executed, but if I
> substitute
> > the above clone element with :
> >
> >             <clone continueParent="true">
> >                 <target>
> >                     <sequence>
> >                         <property action="set" name="service-port"
> > value="8080"/>
> >                         <class name="JavaMediator"/>
> >                         <!--<sequence key="localSequence"/>-->
> >                     </sequence>
> >                     <endpoint>
> >                         <address uri="
> > http://192.168.239.1:8080/EventService/services/Event"/>
> >                     </endpoint>
> >                     <send/>
> >                     <!--<outsequence>
> >                         <send/>
> >                     </outsequence>-->
> >                 </target>
> >
> >             </clone>
> >
> > nothing gets sent to the :8080/EventService endpoint. Which also seems
> > reasonable because there is no <send/> mediator in the sequence.
> >
> > So how do I get the clone mediator to filter and send to the specified
> > endpoint , should I manually set the 'To' property inside the clone
> mediator
> > sequence ?
> >
> > Thank you,
> > Florin
> >
> >
> > 2009/9/30 Hiranya Jayathilaka <hi...@gmail.com>
> >
> > Hi Florin,
> >> Please find the answers to your questions and some additional comments
> >> inline.
> >>
> >> On Tue, Sep 29, 2009 at 4:42 PM, Florin Bejinaru
> >> <fl...@gmail.com>wrote:
> >>
> >> > Hello again,
> >> >
> >> > I want to do custom request filtering for just one of the endpoints
> >> > mentioned in my previous mail (a port 9000 endpoint and a port 8080
> >> > endpoint). So what I want to do is send every request to the 9000
> >> endpoint
> >> > and filter the requests for the 8080 endpoint based on the request
> >> content.
> >> >
> >> > I need to do dynamic filtering (in that you should be able to change
> the
> >> > filters at run time and these should be managed centrally).
> >> >
> >> > I figure I can do the filtering in two ways:
> >> > One would be to filter based on a set of xslt files located on a
> remote
> >> > registry, and the other to use a "class" mediator with a custom class
> >> that
> >> > filters based on a set of xslt files loaded through a web service,
> >> hosted
> >> > by
> >> > synapse preferably.
> >> >
> >> > To save me from trying to do both of these, could you tell me if :
> >> >
> >> > 1) By using a remote registry can I set a filtering xslt, to filter
> >> > requests
> >> > for the 8080 endpoint?
> >> >
> >>
> >> The most common way of implementing content based routing with Synapse
> is
> >> by
> >> using the filter mediator or by using the switch mediator. Both these
> >> mediators take an XPath expression (not XSLT) based on which to perform
> >> the
> >> filtering. You can put these mediators into a sequence in a way so that
> >> your
> >> filtering requirements are properly handled and then save the entire
> >> sequence in the remote registry. Then in the main sequence you will be
> >> able
> >> to refer to the sequence in the registry using a key. You can make
> >> modifications to the sequence at runtime and Synapse will load the
> >> sequence
> >> in the registry from time to time. Thus any changes you have made will
> >> take
> >> effect at runtime.
> >>
> >>
> >> > 2) By using a class mediator, can I host a web-service in synapse that
> >> > saves
> >> > files somewhere where they can be accessed by my class mediator ?
> >> >
> >>
> >> You cannot host Web Services in Synapse. But with the class mediator you
> >> can
> >> write some custom code which interacts with the file system. Anyway I
> >> don't
> >> think it's a good idea to get your mediators read/write to the file
> >> system.
> >> That might have a significant performance hit. You should be using the
> >> registry to store resource. Then you can benefit from our core API and
> the
> >> caching features.
> >>
> >>
> >> >
> >> > 3) With the class mediator implementation, what are my xslt filtering
> >> > options, what XSLT engine can I (should I) use from my Java mediator
> >> class
> >> > ?
> >> >
> >>
> >> As I mentioned earlier with the class mediator you can get Synapse to
> >> execute any piece of custom code. So you can use any third party
> libraries
> >> you want when writing the custom code. But filtering is a basic feature
> of
> >> Synapse and ideally you should be using the filter/switch mediators to
> get
> >> this done. That is far more efficient and easier.
> >>
> >>
> >> >
> >> > Any help would be greatly appreciated !
> >> >
> >>
> >> I think you can use a configuration similar to the following to
> implement
> >> your scenario:
> >>
> >> <sequence name="main">
> >>     <in>
> >>           <sequence key="/key/to/dynamic/sequence"/>
> >>      </in>
> >>      <out>
> >>           <send/>
> >>      </out>
> >> </sequence>
> >>
> >> Dynamic sequence in the registry:
> >>
> >> <sequence name="foo">
> >>        <switch source="get-property('To')">
> >>                <case regex="http://localhost:8080*">
> >>                        <filter source="xpath to examine the content"
> >> regex="regular expression">
> >>                              <send/>
> >>                         </filter>
> >>                         <drop/>
> >>                 </case>
> >>                 <case regex="http://localhost:9000*">
> >>                         <send/>
> >>                 </case>
> >>         </switch>
> >> </sequence>
> >>
> >> This is just one way of doing it (may be not the most elegant way).
> Please
> >> have a loot at sample 1 and 2 in our  sample guide for more info.
> >>
> >> Thanks,
> >> Hiranya
> >>
> >>
> >>
> >> > Thank you,
> >> > Florin
> >> >
> >>
> >>
> >>
> >> --
> >> Hiranya Jayathilaka
> >> Software Engineer;
> >> WSO2 Inc.;  http://wso2.org
> >> E-mail: hiranya@wso2.com;  Mobile: +94 77 633 3491
> >> Blog: http://techfeast-hiranya.blogspot.com
> >>
> >
> >
>



-- 
Hiranya Jayathilaka
Software Engineer;
WSO2 Inc.;  http://wso2.org
E-mail: hiranya@wso2.com;  Mobile: +94 77 633 3491
Blog: http://techfeast-hiranya.blogspot.com