ServiceStack Request and Response Filters  Topic is solved

Discussions relating to the REST API of Jiwa 7.

ServiceStack Request and Response Filters

Postby SBarnes » Fri Jun 22, 2018 4:15 pm

Hi Mike,

Is there a way with ServiceStack to make a request and/or response filter fire ahead of everything else, I am looking at the concept of a licensing model for a possible client that I would want to control from the server based upon certain criteria?
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: ServiceStack Request and Response Filters

Postby Mike.Sheen » Sat Jun 23, 2018 2:27 pm

Hi Stuart,

After looking at the Order of Operations for ServiceStack, adding a request filter with a Priority of < 0, then that would be invoked before our built-in global request filter.

So, my understanding (and I've not proven this!) is that if you create a class which inherits from ServiceStack's RequestFilterAttribute, and set the Priority property to something < 0 - then any service classes decorated with an attribute of your custom class will cause that service to execute your custom request filter before our global request filter.

But - that would mean you'd have to decorate every service with the attribute - if you are wanting to apply this to our built-in services then that's not ideal - but if it's for your own custom service classes you've added, then it's not a problem.

If you do need a way of executing your custom request filter for our built-in classes before our global filter (without adding the attribute to our classes), then we'd have to build in some functionality to make that possible. That should be fairly simple - we just expose a list of delegate functions you can add to, and our global filter invokes those in order before anything else goes on in our global request filter.

There are also prerequest filters in ServiceStack - but those get invoked before the DTO is deserialised, and before our global request filter - that may or may not suit your needs.

I've added DEV-6678 to look into plugins adding request filters which are executed before our global filters.

Mike
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: ServiceStack Request and Response Filters

Postby SBarnes » Sat Jun 23, 2018 7:01 pm

Thanks Mike,

I had read the order of operations stuff but hadn't gotten much further than that.

But I like the idea of what you are suggesting which should work for what I need.
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: ServiceStack Request and Response Filters  Topic is solved

Postby Mike.Sheen » Mon Jun 25, 2018 1:02 pm

Hi Stuart,

Can you take a look at the attached and let me know if it suits your needs? It's simply a plugin (should work with any version 7.1.0 or later) which adds a global request filter to the start of the GlobalRequestFilters of ServiceStack's appHost. In this example we add a request filter looking for a request type of CarrierGETRequest (defined in our standard REST API Plugin) - and if the type matches it throws an exception - this is executed before the global filters defined in the REST API plugin.

I didn't need to modify our standard REST API plugin, or anything else - just a simple .Insert into the GlobalRequestFilters list at index 0 was what was needed. The only trick was dealing with helping fusion resolve any needed assemblies if the filter provided refers to any types defined in another plugin.

Mike
Attachments
Plugin REST API Global Request Filter Example.xml
(26.6 KiB) Downloaded 182 times
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: ServiceStack Request and Response Filters

Postby SBarnes » Mon Jun 25, 2018 1:07 pm

Thanks Mike,

I'll take a look and come back to you on this when I can.
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: ServiceStack Request and Response Filters

Postby SBarnes » Tue Jun 26, 2018 3:21 pm

Hi Mike,

It looks like it will do what I want, just one further question if I add a header to the response do you know how to read that out of the JsonServiceClient?
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: ServiceStack Request and Response Filters

Postby Mike.Sheen » Tue Jun 26, 2018 5:33 pm

Hi Stuart,

The ServiceStack JsonServiceClient has a ResponseFilter and GlobalResponseFilter property you can use to examine the response (including headers) before it's deserialised.

Code: Select all
string myHeader = null;
var client = new JsonServiceClient(baseUrl) {
    ResponseFilter = res => myHeader = res.Headers["X-MyHeader"],
};

var response = client.Post(request);


With the above, once the client.Post call returns, the response variable will contain the deserialised DTO, and the myHeader variable will contain the contents of the "X-MyHeader" response header.

Mike
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756


Return to REST API

Who is online

Users browsing this forum: No registered users and 4 guests