JSON (ajax-)request to component serveResourcePath

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

JSON (ajax-)request to component serveResourcePath

Auke
Hello,

I'm writing a component that renders some kind of form based on data from an external SOAP service. Part of this data is loaded dynamically, i.e. I retrieve some data in the doBeforeRender, but after some user interaction a bit more needs to be loaded from the webservice using an AJAX request that returns JSON.

I was expecting the doBeforeServeResource to help me out, but was suprised to find out that I had to set the serveResourcePath to a blank JSP for it to work correctly. So:

        @Override
        public void doBeforeServeResource(HstRequest request, HstResponse response) {
                response.setServeResourcePath("jsp/blank.jsp"); // huh?

                // <snip> retrieve data

                try {
                        response.setContentType("application/json");
                        new Gson().toJson(tree.getFirstQuestion(), response.getWriter());
                } catch (IOException e) {
                        throw new HstComponentException("Unable to render question tree!", e);
                }
        }

This sort of feels that I'm not supposed to do it this way? Is there a cleaner way of doing this?

--
Auke
Ard
Reply | Threaded
Open this post in threaded view
|

Re: JSON (ajax-)request to component serveResourcePath

Ard
On Wed, Feb 27, 2013 at 10:42 AM, Auke <[hidden email]> wrote:

> Hello,
>
> I'm writing a component that renders some kind of form based on data from an
> external SOAP service. Part of this data is loaded dynamically, i.e. I
> retrieve some data in the doBeforeRender, but after some user interaction a
> bit more needs to be loaded from the webservice using an AJAX request that
> returns JSON.
>
> I was expecting the doBeforeServeResource to help me out, but was suprised
> to find out that I had to set the serveResourcePath to a blank JSP for it to
> work correctly. So:
>
>         @Override
>         public void doBeforeServeResource(HstRequest request, HstResponse response)
> {
>                 response.setServeResourcePath("jsp/blank.jsp"); // huh?

Good question....I think there is always a dispatcher needed for the
doBeforeServeResource.
@Woonsan, do you know this from the top of your head?

Did you consider using the jaxrs rest integration in the hst for what
your need, as that seems a much more natural approach to me

Regards Ard

>
>                 // <snip> retrieve data
>
>                 try {
>                         response.setContentType("application/json");
>                         new Gson().toJson(tree.getFirstQuestion(), response.getWriter());
>                 } catch (IOException e) {
>                         throw new HstComponentException("Unable to render question tree!", e);
>                 }
>         }
>
> This sort of feels that I'm not supposed to do it this way? Is there a
> cleaner way of doing this?
>
> --
> Auke
>
>
>
> --
> View this message in context: http://hippo.2275632.n2.nabble.com/JSON-ajax-request-to-component-serveResourcePath-tp7580512.html
> Sent from the Hippo CMS 7 mailing list archive at Nabble.com.
> _______________________________________________
> Hippo-cms7-user mailing list and forums
> http://www.onehippo.org/cms7/support/forums.html



--
Amsterdam - Oosteinde 11, 1017 WT Amsterdam
Boston - 1 Broadway, Cambridge, MA 02142

US +1 877 414 4776 (toll free)
Europe +31(0)20 522 4466
www.onehippo.com
_______________________________________________
Hippo-cms7-user mailing list and forums
http://www.onehippo.org/cms7/support/forums.html
Reply | Threaded
Open this post in threaded view
|

Re: JSON (ajax-)request to component serveResourcePath

Aharpour
Hi Ard

I had the same issue as Auke having last week. 
Using jaxrs rest integration is not a very good idea for this use case, because when you make a component you want it to be encapsulated. So that you can drop it wherever you want or even reusing in other projects without having to worry about a whole bunch of configurations.

Best regards
Ebrahim Aharpour 


On Wed, Feb 27, 2013 at 12:29 PM, Ard Schrijvers <[hidden email]> wrote:
On Wed, Feb 27, 2013 at 10:42 AM, Auke <[hidden email]> wrote:
> Hello,
>
> I'm writing a component that renders some kind of form based on data from an
> external SOAP service. Part of this data is loaded dynamically, i.e. I
> retrieve some data in the doBeforeRender, but after some user interaction a
> bit more needs to be loaded from the webservice using an AJAX request that
> returns JSON.
>
> I was expecting the doBeforeServeResource to help me out, but was suprised
> to find out that I had to set the serveResourcePath to a blank JSP for it to
> work correctly. So:
>
>         @Override
>         public void doBeforeServeResource(HstRequest request, HstResponse response)
> {
>                 response.setServeResourcePath("jsp/blank.jsp"); // huh?

Good question....I think there is always a dispatcher needed for the
doBeforeServeResource.
@Woonsan, do you know this from the top of your head?

Did you consider using the jaxrs rest integration in the hst for what
your need, as that seems a much more natural approach to me

Regards Ard

>
>                 // <snip> retrieve data
>
>                 try {
>                         response.setContentType("application/json");
>                         new Gson().toJson(tree.getFirstQuestion(), response.getWriter());
>                 } catch (IOException e) {
>                         throw new HstComponentException("Unable to render question tree!", e);
>                 }
>         }
>
> This sort of feels that I'm not supposed to do it this way? Is there a
> cleaner way of doing this?
>
> --
> Auke
>
>
>
> --
> View this message in context: http://hippo.2275632.n2.nabble.com/JSON-ajax-request-to-component-serveResourcePath-tp7580512.html
> Sent from the Hippo CMS 7 mailing list archive at Nabble.com.
> _______________________________________________
> Hippo-cms7-user mailing list and forums
> http://www.onehippo.org/cms7/support/forums.html



--
Amsterdam - Oosteinde 11, 1017 WT Amsterdam
Boston - 1 Broadway, Cambridge, MA 02142

US <a href="tel:%2B1%20877%20414%204776" value="+18774144776">+1 877 414 4776 (toll free)
Europe <a href="tel:%2B31%280%2920%20522%204466" value="+31205224466">+31(0)20 522 4466
www.onehippo.com
_______________________________________________
Hippo-cms7-user mailing list and forums
http://www.onehippo.org/cms7/support/forums.html


_______________________________________________
Hippo-cms7-user mailing list and forums
http://www.onehippo.org/cms7/support/forums.html
Ard
Reply | Threaded
Open this post in threaded view
|

Re: JSON (ajax-)request to component serveResourcePath

Ard
Hello Ebrahim,

On Wed, Feb 27, 2013 at 1:21 PM, Ebrahim Aharpour
<[hidden email]> wrote:
> Hi Ard
>
> I had the same issue as Auke having last week.
> Using jaxrs rest integration is not a very good idea for this use case,
> because when you make a component you want it to be encapsulated. So that
> you can drop it wherever you want or even reusing in other projects without
> having to worry about a whole bunch of configurations.

Thanks for chiming in! I understand your use case.

Before diving into the source code I hope Woonsan can shed some light
on this. Perhaps we should adapt the doBeforeServeResource handling
for your use cases.

Regards Ard

>
> Best regards
> Ebrahim Aharpour
>
>
> On Wed, Feb 27, 2013 at 12:29 PM, Ard Schrijvers <[hidden email]>
> wrote:
>>
>> On Wed, Feb 27, 2013 at 10:42 AM, Auke <[hidden email]> wrote:
>> > Hello,
>> >
>> > I'm writing a component that renders some kind of form based on data
>> > from an
>> > external SOAP service. Part of this data is loaded dynamically, i.e. I
>> > retrieve some data in the doBeforeRender, but after some user
>> > interaction a
>> > bit more needs to be loaded from the webservice using an AJAX request
>> > that
>> > returns JSON.
>> >
>> > I was expecting the doBeforeServeResource to help me out, but was
>> > suprised
>> > to find out that I had to set the serveResourcePath to a blank JSP for
>> > it to
>> > work correctly. So:
>> >
>> >         @Override
>> >         public void doBeforeServeResource(HstRequest request,
>> > HstResponse response)
>> > {
>> >                 response.setServeResourcePath("jsp/blank.jsp"); // huh?
>>
>> Good question....I think there is always a dispatcher needed for the
>> doBeforeServeResource.
>> @Woonsan, do you know this from the top of your head?
>>
>> Did you consider using the jaxrs rest integration in the hst for what
>> your need, as that seems a much more natural approach to me
>>
>> Regards Ard
>>
>> >
>> >                 // <snip> retrieve data
>> >
>> >                 try {
>> >                         response.setContentType("application/json");
>> >                         new Gson().toJson(tree.getFirstQuestion(),
>> > response.getWriter());
>> >                 } catch (IOException e) {
>> >                         throw new HstComponentException("Unable to
>> > render question tree!", e);
>> >                 }
>> >         }
>> >
>> > This sort of feels that I'm not supposed to do it this way? Is there a
>> > cleaner way of doing this?
>> >
>> > --
>> > Auke
>> >
>> >
>> >
>> > --
>> > View this message in context:
>> > http://hippo.2275632.n2.nabble.com/JSON-ajax-request-to-component-serveResourcePath-tp7580512.html
>> > Sent from the Hippo CMS 7 mailing list archive at Nabble.com.
>> > _______________________________________________
>> > Hippo-cms7-user mailing list and forums
>> > http://www.onehippo.org/cms7/support/forums.html
>>
>>
>>
>> --
>> Amsterdam - Oosteinde 11, 1017 WT Amsterdam
>> Boston - 1 Broadway, Cambridge, MA 02142
>>
>> US +1 877 414 4776 (toll free)
>> Europe +31(0)20 522 4466
>> www.onehippo.com
>> _______________________________________________
>> Hippo-cms7-user mailing list and forums
>> http://www.onehippo.org/cms7/support/forums.html
>
>
>
> _______________________________________________
> Hippo-cms7-user mailing list and forums
> http://www.onehippo.org/cms7/support/forums.html



--
Amsterdam - Oosteinde 11, 1017 WT Amsterdam
Boston - 1 Broadway, Cambridge, MA 02142

US +1 877 414 4776 (toll free)
Europe +31(0)20 522 4466
www.onehippo.com
_______________________________________________
Hippo-cms7-user mailing list and forums
http://www.onehippo.org/cms7/support/forums.html
Reply | Threaded
Open this post in threaded view
|

Re: JSON (ajax-)request to component serveResourcePath

Auke
What Ebrahim is saying was indeed my consideration.

Thanks for looking into this.

--
Auke
Reply | Threaded
Open this post in threaded view
|

Re: JSON (ajax-)request to component serveResourcePath

Woonsan Ko-3
In reply to this post by Ard
Hi Auke / Ebrahim / Ard,

In the beginning, we designed HST Component API to be as simple as
possible and to let a component use the existing servlet technology as
much as possible.
So, we didn't add #doRender() or #doServeResource() methods, but we
added only #doBeforeRender() and #doBeforeServeResource() methods, which
means any rendering and resource serving processes should be done
through the configured templates or resource templates.
That's why we haven't recommended to render or serve resources directly
in the component codes and we have had APIs only to set render path or
serve resource path as servlet dispatch path in the before-phase methods.

Therefore, based on the current best practices of HST-2 so far, it's not
a good idea to have content writing codes in your before-phase methods.
One way to avoid this might be to use freemarker templates in either
your jar files or repository, currently. If the template path starts
with "jcr:", then it will be loaded from the repository. If starting
with "classpath:", then it will be loaded from the classpath. [1]

Now, we can still discuss how we can improve this.
Even though we started the assumption that we'd better not make it
complex in the beginning, I think the things are changing now.
So, I think we can consider supporting two more operations,
#doRender(HstRequest, HstResponse) and #doServeResource(HstRequest,
HstResponse), by improving the API (e.g., by adding
RenderableHstComponent interface and add it to GenericHstComponent).
Then, it is possible for a component to render or serve resources
directly, as needed. I don't think it's a good idea to allow this in the
existing before-phase methods (as we designed not to do so in the
beginning), so I opt for adding new methods.

What do you think?

Regards,

Woonsan


[1]
http://www.onehippo.org/7_7/library/concepts/web-application/hst-2-freemarker-support-for-repository-and-classpath-located-freemarker-templates.html

On 2/27/13 6:29 AM, Ard Schrijvers wrote:

> On Wed, Feb 27, 2013 at 10:42 AM, Auke <[hidden email]> wrote:
>> Hello,
>>
>> I'm writing a component that renders some kind of form based on data from an
>> external SOAP service. Part of this data is loaded dynamically, i.e. I
>> retrieve some data in the doBeforeRender, but after some user interaction a
>> bit more needs to be loaded from the webservice using an AJAX request that
>> returns JSON.
>>
>> I was expecting the doBeforeServeResource to help me out, but was suprised
>> to find out that I had to set the serveResourcePath to a blank JSP for it to
>> work correctly. So:
>>
>>          @Override
>>          public void doBeforeServeResource(HstRequest request, HstResponse response)
>> {
>>                  response.setServeResourcePath("jsp/blank.jsp"); // huh?
>
> Good question....I think there is always a dispatcher needed for the
> doBeforeServeResource.
> @Woonsan, do you know this from the top of your head?
>
> Did you consider using the jaxrs rest integration in the hst for what
> your need, as that seems a much more natural approach to me
>
> Regards Ard
>
>>
>>                  // <snip> retrieve data
>>
>>                  try {
>>                          response.setContentType("application/json");
>>                          new Gson().toJson(tree.getFirstQuestion(), response.getWriter());
>>                  } catch (IOException e) {
>>                          throw new HstComponentException("Unable to render question tree!", e);
>>                  }
>>          }
>>
>> This sort of feels that I'm not supposed to do it this way? Is there a
>> cleaner way of doing this?
>>
>> --
>> Auke
>>
>>
>>
>> --
>> View this message in context: http://hippo.2275632.n2.nabble.com/JSON-ajax-request-to-component-serveResourcePath-tp7580512.html
>> Sent from the Hippo CMS 7 mailing list archive at Nabble.com.
>> _______________________________________________
>> Hippo-cms7-user mailing list and forums
>> http://www.onehippo.org/cms7/support/forums.html
>
>
>


--
[hidden email]     www.onehippo.com
Boston - 1 Broadway, Cambridge, MA 02142
Amsterdam - Oosteinde 11, 1017 WT Amsterdam
US +1 877 414 4776 (toll free)
Europe +31(0)20 522 4466
_______________________________________________
Hippo-cms7-user mailing list and forums
http://www.onehippo.org/cms7/support/forums.html
Ard
Reply | Threaded
Open this post in threaded view
|

Re: JSON (ajax-)request to component serveResourcePath

Ard
On Wed, Feb 27, 2013 at 3:45 PM, Woonsan Ko <[hidden email]> wrote:

> Hi Auke / Ebrahim / Ard,
>
> In the beginning, we designed HST Component API to be as simple as possible
> and to let a component use the existing servlet technology as much as
> possible.
> So, we didn't add #doRender() or #doServeResource() methods, but we added
> only #doBeforeRender() and #doBeforeServeResource() methods, which means any
> rendering and resource serving processes should be done through the
> configured templates or resource templates.
> That's why we haven't recommended to render or serve resources directly in
> the component codes and we have had APIs only to set render path or serve
> resource path as servlet dispatch path in the before-phase methods.
>
> Therefore, based on the current best practices of HST-2 so far, it's not a
> good idea to have content writing codes in your before-phase methods. One
> way to avoid this might be to use freemarker templates in either your jar
> files or repository, currently. If the template path starts with "jcr:",
> then it will be loaded from the repository. If starting with "classpath:",
> then it will be loaded from the classpath. [1]
>
> Now, we can still discuss how we can improve this.
> Even though we started the assumption that we'd better not make it complex
> in the beginning, I think the things are changing now.
> So, I think we can consider supporting two more operations,
> #doRender(HstRequest, HstResponse) and #doServeResource(HstRequest,
> HstResponse), by improving the API (e.g., by adding RenderableHstComponent
> interface and add it to GenericHstComponent).
> Then, it is possible for a component to render or serve resources directly,
> as needed. I don't think it's a good idea to allow this in the existing
> before-phase methods (as we designed not to do so in the beginning), so I
> opt for adding new methods.
>
> What do you think?

I think it makes sense.

Regards Ard

>
> Regards,
>
> Woonsan
>
>
> [1]
> http://www.onehippo.org/7_7/library/concepts/web-application/hst-2-freemarker-support-for-repository-and-classpath-located-freemarker-templates.html
>
>
> On 2/27/13 6:29 AM, Ard Schrijvers wrote:
>>
>> On Wed, Feb 27, 2013 at 10:42 AM, Auke <[hidden email]> wrote:
>>>
>>> Hello,
>>>
>>> I'm writing a component that renders some kind of form based on data from
>>> an
>>> external SOAP service. Part of this data is loaded dynamically, i.e. I
>>> retrieve some data in the doBeforeRender, but after some user interaction
>>> a
>>> bit more needs to be loaded from the webservice using an AJAX request
>>> that
>>> returns JSON.
>>>
>>> I was expecting the doBeforeServeResource to help me out, but was
>>> suprised
>>> to find out that I had to set the serveResourcePath to a blank JSP for it
>>> to
>>> work correctly. So:
>>>
>>>          @Override
>>>          public void doBeforeServeResource(HstRequest request,
>>> HstResponse response)
>>> {
>>>                  response.setServeResourcePath("jsp/blank.jsp"); // huh?
>>
>>
>> Good question....I think there is always a dispatcher needed for the
>> doBeforeServeResource.
>> @Woonsan, do you know this from the top of your head?
>>
>> Did you consider using the jaxrs rest integration in the hst for what
>> your need, as that seems a much more natural approach to me
>>
>> Regards Ard
>>
>>>
>>>                  // <snip> retrieve data
>>>
>>>                  try {
>>>                          response.setContentType("application/json");
>>>                          new Gson().toJson(tree.getFirstQuestion(),
>>> response.getWriter());
>>>                  } catch (IOException e) {
>>>                          throw new HstComponentException("Unable to
>>> render question tree!", e);
>>>                  }
>>>          }
>>>
>>> This sort of feels that I'm not supposed to do it this way? Is there a
>>> cleaner way of doing this?
>>>
>>> --
>>> Auke
>>>
>>>
>>>
>>> --
>>> View this message in context:
>>> http://hippo.2275632.n2.nabble.com/JSON-ajax-request-to-component-serveResourcePath-tp7580512.html
>>> Sent from the Hippo CMS 7 mailing list archive at Nabble.com.
>>> _______________________________________________
>>> Hippo-cms7-user mailing list and forums
>>> http://www.onehippo.org/cms7/support/forums.html
>>
>>
>>
>>
>
>
> --
> [hidden email]     www.onehippo.com
>
> Boston - 1 Broadway, Cambridge, MA 02142
> Amsterdam - Oosteinde 11, 1017 WT Amsterdam
> US +1 877 414 4776 (toll free)
> Europe +31(0)20 522 4466
> _______________________________________________
> Hippo-cms7-user mailing list and forums
> http://www.onehippo.org/cms7/support/forums.html



--
Amsterdam - Oosteinde 11, 1017 WT Amsterdam
Boston - 1 Broadway, Cambridge, MA 02142

US +1 877 414 4776 (toll free)
Europe +31(0)20 522 4466
www.onehippo.com
_______________________________________________
Hippo-cms7-user mailing list and forums
http://www.onehippo.org/cms7/support/forums.html