You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by Bertrand Delacretaz <bd...@apache.org> on 2020/06/10 14:07:43 UTC

FYI, fluent helper for internal requests (was: Intra Sling Calls)

Hi,

On Wed, Jun 3, 2020 at 11:03 AM Bertrand Delacretaz
<bd...@apache.org> wrote:
> ...for my use cases a fluent interface to the SlingRequestProcessor would be nice...

I have now implemented a fluent helper for internal requests, using
either the SlingRequestProcessor (for full fidelity compared to HTTP
requests) or the ServletResolver (faster, no Servlet Filters - thanks
Radu for mentioning that option), at SLING-9512 and [1].

Here's a usage example, the tests [1] have more:

OutputStream os = InternalRequest
  .servletRequest(resourceResolver, servletResolver, "/some/path")
  .withResourceType("website/article/news")
  .withResourceSuperType("website/article")
  .withSelectors("print", "a4")
  .withExtension("pdf")
  .execute()
  .checkStatus(200)
  .checkResponseContentType("application/pdf")
  .getResponse()
  .getOutputStream();

Feedback is welcome! I'm using this in the GraphQL core that I'd like
to release soon, will need to release the servlet-helpers module as
well.

-Bertrand

[1] https://github.com/apache/sling-org-apache-sling-servlet-helpers

Re: FYI, fluent helper for internal requests (was: Intra Sling Calls)

Posted by Bertrand Delacretaz <bd...@apache.org>.
On Thu, Jun 11, 2020 at 8:20 PM Andreas Schaefer
<sc...@me.com.invalid> wrote:

>... Nevermind. I just realized that getResponse().getStatus() gives me access to the status code...

Yes, but you have to ignore the status check explicitly by calling
checkStatus() with no argument, meaning "I want to ignore the status
value".

I think that's a reasonable API to avoid forgetting to check the
status, while allowing for special cases where you don't care.

Note that I had to rework the APIs to make sure an actual resource is
provided for the "direct servlet execution" mode - no big differences
and that's clearer now IMO, just committed those changes about half an
hour ago.

-Bertrand

Re: FYI, fluent helper for internal requests (was: Intra Sling Calls)

Posted by Andreas Schaefer <sc...@me.com.INVALID>.

> On Jun 11, 2020, at 11:03 AM, Andreas Schaefer <sc...@me.com.INVALID> wrote:
> 
> Hi
> 
> 
>> On Jun 11, 2020, at 1:38 AM, Bertrand Delacretaz <bd...@apache.org> wrote:
>> 
>> Hi Andy,
>> 
>> On Wed, Jun 10, 2020 at 7:14 PM Andreas Schaefer
>> <sc...@me.com.invalid> wrote:
>>> 
>>> I will try to fit into my use case....I am thinking of having big files or images than then later are streamed
>>> to the caller outside of Sling...
>> 
>> My current use cases are about small String inputs and outputs, so I
>> haven't addressed the multi-part and large data thing so far.
>> 
>> I'm planning to make a release of that servlet helpers module in a few
>> days along with the GraphQL core that uses it with small amounts of
>> input and output, but it's fine to add what you need later, I think
>> that would be useful!
> 
> That is fine with me. Dealing with output streaming as well as multipart forms need some changes in the Mock Request / Response and that will take some time. Right now I do not have a use case for it.
> 
>> 
>>> ...Also the current code from GitHub is having the checkStatus() on the execute(). I think that having a separate
>>> method (checkStatus()) is better than having on the execute()...
>> 
>> I had that first and then moved to checking status as part of
>> execute() to make sure people don't forget to do that.
>> 
>> However I agree that a separate checkStatus method makes things more
>> obvious. I'll re-add that method but add an automatic check for 200
>> status before providing any response data to the client, unless status
>> has been checked already, to make sure status is indeed checked.
> 
> I am not sure if we should make the check mandatory. If the response is returning a stack trace it might help to understand what is going on and so the response is still valid even if the call failed.
> So for that I would suggest the following signatures:
> 
> 	InternalRequest checkStatus(String … expectedStatusCodes)
> 	InternalRequest checkStatus(Integer currentStatusCode, String … expectedStatusCodes)
> 
> The ‘currentStatusCode’ is set in the checkStatus() with the statusCode from the call. The checkStatus() is accepting a status code when one of the ‘expectedStatusCodes’ matches the current.
> 
> This way the caller can respond depending on the status code.

Nevermind. I just realized that getResponse().getStatus() gives me access to the status code.

> 
>> 
>>> ...I think we also should support multiple Status to be accepted...
>> 
>> -Bertrand


Re: FYI, fluent helper for internal requests (was: Intra Sling Calls)

Posted by Andreas Schaefer <sc...@me.com.INVALID>.
Hi


> On Jun 11, 2020, at 1:38 AM, Bertrand Delacretaz <bd...@apache.org> wrote:
> 
> Hi Andy,
> 
> On Wed, Jun 10, 2020 at 7:14 PM Andreas Schaefer
> <sc...@me.com.invalid> wrote:
>> 
>> I will try to fit into my use case....I am thinking of having big files or images than then later are streamed
>> to the caller outside of Sling...
> 
> My current use cases are about small String inputs and outputs, so I
> haven't addressed the multi-part and large data thing so far.
> 
> I'm planning to make a release of that servlet helpers module in a few
> days along with the GraphQL core that uses it with small amounts of
> input and output, but it's fine to add what you need later, I think
> that would be useful!

That is fine with me. Dealing with output streaming as well as multipart forms need some changes in the Mock Request / Response and that will take some time. Right now I do not have a use case for it.

> 
>> ...Also the current code from GitHub is having the checkStatus() on the execute(). I think that having a separate
>> method (checkStatus()) is better than having on the execute()...
> 
> I had that first and then moved to checking status as part of
> execute() to make sure people don't forget to do that.
> 
> However I agree that a separate checkStatus method makes things more
> obvious. I'll re-add that method but add an automatic check for 200
> status before providing any response data to the client, unless status
> has been checked already, to make sure status is indeed checked.

I am not sure if we should make the check mandatory. If the response is returning a stack trace it might help to understand what is going on and so the response is still valid even if the call failed.
So for that I would suggest the following signatures:

	InternalRequest checkStatus(String … expectedStatusCodes)
	InternalRequest checkStatus(Integer currentStatusCode, String … expectedStatusCodes)

The ‘currentStatusCode’ is set in the checkStatus() with the statusCode from the call. The checkStatus() is accepting a status code when one of the ‘expectedStatusCodes’ matches the current.

This way the caller can respond depending on the status code.

> 
>> ...I think we also should support multiple Status to be accepted...
> 
> -Bertrand


Re: FYI, fluent helper for internal requests (was: Intra Sling Calls)

Posted by Bertrand Delacretaz <bd...@apache.org>.
Hi Andy,

On Wed, Jun 10, 2020 at 7:14 PM Andreas Schaefer
<sc...@me.com.invalid> wrote:
>
> I will try to fit into my use case....I am thinking of having big files or images than then later are streamed
> to the caller outside of Sling...

My current use cases are about small String inputs and outputs, so I
haven't addressed the multi-part and large data thing so far.

I'm planning to make a release of that servlet helpers module in a few
days along with the GraphQL core that uses it with small amounts of
input and output, but it's fine to add what you need later, I think
that would be useful!

> ...Also the current code from GitHub is having the checkStatus() on the execute(). I think that having a separate
> method (checkStatus()) is better than having on the execute()...

I had that first and then moved to checking status as part of
execute() to make sure people don't forget to do that.

However I agree that a separate checkStatus method makes things more
obvious. I'll re-add that method but add an automatic check for 200
status before providing any response data to the client, unless status
has been checked already, to make sure status is indeed checked.

> ...I think we also should support multiple Status to be accepted...

-Bertrand

Re: FYI, fluent helper for internal requests (was: Intra Sling Calls)

Posted by Andreas Schaefer <sc...@me.com.INVALID>.
I tested the InternalRequest in my use cases and its works just fine.

> On Jun 10, 2020, at 10:14 AM, Andreas Schaefer <sc...@me.com.INVALID> wrote:
> 
> I will try to fit into my use case. That said I am sort of confused with the OutputStream. As far as I understand it the output stream is when I write to a sink but not read from a source. If we want to support streaming in the InternalRequest it needs to provide an InputStream and the ResponseBondySupports needs to be using a Buffered Output Stream to facilitates that. I am thinking of having big files or images than then later are streamed to the caller outside of Sling.
> 
> Also the current code from GitHub is having the checkStatus() on the execute(). I think that having a separate method (checkStatus()) is better than having on the execute(). That said I think we also should support multiple Status to be accepted.
> 
> Cheers - Andy
> 
>> On Jun 10, 2020, at 7:07 AM, Bertrand Delacretaz <bd...@apache.org> wrote:
>> 
>> Hi,
>> 
>> On Wed, Jun 3, 2020 at 11:03 AM Bertrand Delacretaz
>> <bd...@apache.org> wrote:
>>> ...for my use cases a fluent interface to the SlingRequestProcessor would be nice...
>> 
>> I have now implemented a fluent helper for internal requests, using
>> either the SlingRequestProcessor (for full fidelity compared to HTTP
>> requests) or the ServletResolver (faster, no Servlet Filters - thanks
>> Radu for mentioning that option), at SLING-9512 and [1].
>> 
>> Here's a usage example, the tests [1] have more:
>> 
>> OutputStream os = InternalRequest
>> .servletRequest(resourceResolver, servletResolver, "/some/path")
>> .withResourceType("website/article/news")
>> .withResourceSuperType("website/article")
>> .withSelectors("print", "a4")
>> .withExtension("pdf")
>> .execute()
>> .checkStatus(200)
>> .checkResponseContentType("application/pdf")
>> .getResponse()
>> .getOutputStream();
>> 
>> Feedback is welcome! I'm using this in the GraphQL core that I'd like
>> to release soon, will need to release the servlet-helpers module as
>> well.
>> 
>> -Bertrand
>> 
>> [1] https://github.com/apache/sling-org-apache-sling-servlet-helpers
> 


Re: FYI, fluent helper for internal requests (was: Intra Sling Calls)

Posted by Andreas Schaefer <sc...@me.com.INVALID>.
I will try to fit into my use case. That said I am sort of confused with the OutputStream. As far as I understand it the output stream is when I write to a sink but not read from a source. If we want to support streaming in the InternalRequest it needs to provide an InputStream and the ResponseBondySupports needs to be using a Buffered Output Stream to facilitates that. I am thinking of having big files or images than then later are streamed to the caller outside of Sling.

Also the current code from GitHub is having the checkStatus() on the execute(). I think that having a separate method (checkStatus()) is better than having on the execute(). That said I think we also should support multiple Status to be accepted.

Cheers - Andy

> On Jun 10, 2020, at 7:07 AM, Bertrand Delacretaz <bd...@apache.org> wrote:
> 
> Hi,
> 
> On Wed, Jun 3, 2020 at 11:03 AM Bertrand Delacretaz
> <bd...@apache.org> wrote:
>> ...for my use cases a fluent interface to the SlingRequestProcessor would be nice...
> 
> I have now implemented a fluent helper for internal requests, using
> either the SlingRequestProcessor (for full fidelity compared to HTTP
> requests) or the ServletResolver (faster, no Servlet Filters - thanks
> Radu for mentioning that option), at SLING-9512 and [1].
> 
> Here's a usage example, the tests [1] have more:
> 
> OutputStream os = InternalRequest
>  .servletRequest(resourceResolver, servletResolver, "/some/path")
>  .withResourceType("website/article/news")
>  .withResourceSuperType("website/article")
>  .withSelectors("print", "a4")
>  .withExtension("pdf")
>  .execute()
>  .checkStatus(200)
>  .checkResponseContentType("application/pdf")
>  .getResponse()
>  .getOutputStream();
> 
> Feedback is welcome! I'm using this in the GraphQL core that I'd like
> to release soon, will need to release the servlet-helpers module as
> well.
> 
> -Bertrand
> 
> [1] https://github.com/apache/sling-org-apache-sling-servlet-helpers