You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@sling.apache.org by Andreas Schaefer <sc...@me.com> on 2010/01/26 18:07:34 UTC

Client JSon Calls

Hi

To see if Sling would fit our project I would need to be able to handle calls from a client (Ext/JS) sending JSon to server and expecting a JSon response back. In the samples I read that it is possible to register a servlet inside Sling but I am not quite sure how to do it. The only place where I found a servlet used is in 'org.apache.sling.sample.slingshot.impl.ResourceTypeConversionServlet' or the 'org.apache.sling.launchpad.testservices.war.servlets.TestServlet'.

Would that be the way to handle JSon calls from a client inside Sling?

Could that be done with an ESP file instead?

Cheers - Andy Schaefer

Re: Client JSon Calls

Posted by Alexander Klimetschek <ak...@day.com>.
On Wed, Jan 27, 2010 at 10:21, Vidar Ramdal <vi...@idium.no> wrote:
> It would be nice if there was a way to get JSON output from the
> standard SlngPostServlet, so that way we could leverage current
> SlingPostOperations.  I have looked into it briefly. The HtmlResponse
> object that is passed to SlingPostOperations is a bit of a turn-off in
> this sense, but AFAICT only HtmlResponse.send() actually produces
> HTML.
> We could abstract HtmlResponse to some output-agnostic Response
> object, and then let SlingPostServlet decide (how?) which output
> format to return.

That would be a good idea. The return format should be selected via
the extension (POST foobar.html vs. POST foobar.json).

But some background on the html response: The reason why it was chosen
is because it is the only response that works, ie. that is parseable
by a javascript browser client, when you post a form to the
SlingPostServlet via a hidden iframe (to post forms in an ajax style
_and_ support file uploads). With an iframe, the response cannot be
json, but only html. The structured html of the sling html response
allows you to parse out the message, status, etc.

So in the end, the usable combinations for the SlingPostServlet will
probably be:
form post => html response
json post => json response

Regards,
Alex

-- 
Alexander Klimetschek
alexander.klimetschek@day.com

Re: Client JSon Calls

Posted by Vidar Ramdal <vi...@idium.no>.
>> On Tue, Jan 26, 2010 at 6:07 PM, Andreas Schaefer <sc...@me.com> wrote:
>>> To see if Sling would fit our project I would need to be able to handle calls from a client (Ext/JS)
>>> sending JSon to server and expecting a JSon response back....

Ah, sorry, I missed the *sending* JSON part ... which was kind of important.

>>> ... In the samples I read that it is possible to register a servlet inside Sling but I am not quite sure
>>> how to do it....

> On 27.01.2010 09:22, Bertrand Delacretaz wrote:
>> That would be a way, you could register a servlet to handle POST
>> methods [...]

The problem with this is that your SlingPostOperations and
SlingPostProcessors won't be executed - those are handled by the
default SlingPostServlet, which returns HTML. If you don't need
postoperations and postprocessors in your case, then this is not an
issue.

It would be nice if there was a way to get JSON output from the
standard SlngPostServlet, so that way we could leverage current
SlingPostOperations.  I have looked into it briefly. The HtmlResponse
object that is passed to SlingPostOperations is a bit of a turn-off in
this sense, but AFAICT only HtmlResponse.send() actually produces
HTML.
We could abstract HtmlResponse to some output-agnostic Response
object, and then let SlingPostServlet decide (how?) which output
format to return.

-- 
Vidar S. Ramdal <vi...@idium.no> - http://www.idium.no
Sommerrogata 13-15, N-0255 Oslo, Norway
+ 47 22 00 84 00 / +47 21 531941, ext 2070

Re: Client JSon Calls

Posted by Felix Meschberger <fm...@gmail.com>.
Hi,

On 27.01.2010 09:22, Bertrand Delacretaz wrote:
> Hi Andy,
> 
> On Tue, Jan 26, 2010 at 6:07 PM, Andreas Schaefer <sc...@me.com> wrote:
>> To see if Sling would fit our project I would need to be able to handle calls from a client (Ext/JS)
>> sending JSon to server and expecting a JSon response back....


Actually, there is a FAQ entry for this problem [1].

> 
> Ok - as standard the SlingPostServlet expects incoming values as
> request parameters, see
> http://sling.apache.org/site/manipulating-content-the-slingpostservlet-servletspost.html
> for docs.
> 
>> ... In the samples I read that it is possible to register a servlet inside Sling but I am not quite sure
>> how to do it....
> 
> That would be a way, you could register a servlet to handle POST
> methods which contain JSON data, and use a JSON parser in there to get
> to the data. We have a simple parser in the commons/json bundle, [1].
> 
> To register servlets see http://sling.apache.org/site/servlets.html
> 
>> ...Could that be done with an ESP file instead?..

I would suggest for this to be an extension to the SlingPostServlet (if
at all possible). There is an issue for this: SLING-1172 [2]

Any patches welcome ...

Regards
Felix

> 
> Yes, scripts and servlets are equivalent.
> 
> -Bertrand
> 
> [1] https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/JSONObject.java
> 

[1]
http://cwiki.apache.org/SLING/faq.html#FAQ-HowdoIcreateanodebypostingajsondocumenttoaURL%3F
[2] https://issues.apache.org/jira/browse/SLING-1172

Re: Client JSon Calls

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

On Tue, Jan 26, 2010 at 6:07 PM, Andreas Schaefer <sc...@me.com> wrote:
> To see if Sling would fit our project I would need to be able to handle calls from a client (Ext/JS)
> sending JSon to server and expecting a JSon response back....

Ok - as standard the SlingPostServlet expects incoming values as
request parameters, see
http://sling.apache.org/site/manipulating-content-the-slingpostservlet-servletspost.html
for docs.

>... In the samples I read that it is possible to register a servlet inside Sling but I am not quite sure
> how to do it....

That would be a way, you could register a servlet to handle POST
methods which contain JSON data, and use a JSON parser in there to get
to the data. We have a simple parser in the commons/json bundle, [1].

To register servlets see http://sling.apache.org/site/servlets.html

> ...Could that be done with an ESP file instead?..

Yes, scripts and servlets are equivalent.

-Bertrand

[1] https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/JSONObject.java

Re: Client JSon Calls

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

Thanks for you quick support. I already discovered that I can return JSon through Sling but I was more wondering about a caller that would send in JSon.

As far as I can see now this would require an servlet implementation like the JsonQueryServlet where I would deserialize a JSon string into a Java object from a given parameter in order to process the request.

I will see how I can create a test application with such a servlet to process my business rules.

-Andy

On Jan 26, 2010, at 9:38 AM, Vidar Ramdal wrote:

> On Tue, Jan 26, 2010 at 6:07 PM, Andreas Schaefer <sc...@me.com> wrote:
>> Hi
>> 
>> To see if Sling would fit our project I would need to be able to handle calls from a client (Ext/JS) sending JSon to server and expecting a JSon response back. In the samples I read that it is possible to register a servlet inside Sling but I am not quite sure how to do it. The only place where I found a servlet used is in 'org.apache.sling.sample.slingshot.impl.ResourceTypeConversionServlet' or the 'org.apache.sling.launchpad.testservices.war.servlets.TestServlet'.
> 
> Also look in the Sling codebase. SlingInfoServlet [1]  is a simple
> example that registers a servlet to handle requests to
> /system/sling/info. Or look at JsonQueryServlet [2], which registers
> for all requests ending with ".query.json".
> 
>> Would that be the way to handle JSon calls from a client inside Sling?
> 
> Sling comes with a default JSON renderer. Just add ".json" to any
> resource URI, and you'll get a JSON representation, e.g.:
> http://localhost:8080/your/node.json
> 
> If you need a custom JSON format, you could write your own servlet, or ...
> 
>> Could that be done with an ESP file instead?
> 
> Absolutely. If you're not happy with the default JSON representation
> of a resource, you could easily implement your own in ESP:
> <%
>  response.setContentType("application/json");
> %>
> {
>  your_custom_json_property: <%=currentNode.something%>
> }
> Saving this script as /apps/servlet/default/customjson.esp in your
> repository, and then doing a request to
> http://localhost:8080/your/node.customjson should invoke your script
> and return the JSON.
> 
> Also see the JSONQueryServlet for how to return query results in JSON:
> http://in-the-sling.blogspot.com/2008/09/how-to-use-json-query-servlet.html
> 
> If you haven't done so already, check out the Sling in 15 minutes
> guide: http://sling.apache.org/site/discover-sling-in-15-minutes.html
> 
> Hope this gets you started - if you run into any trouble, don't
> hesitate to ask on this list.
> 
> [1] http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/impl/info/SlingInfoServlet.java?view=markup
> [2] http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/JsonQueryServlet.java?view=markup
> 
> -- 
> Vidar S. Ramdal <vi...@idium.no> - http://www.idium.no
> Sommerrogata 13-15, N-0255 Oslo, Norway
> + 47 22 00 84 00 / +47 21 531941, ext 2070


Re: Client JSon Calls

Posted by Vidar Ramdal <vi...@idium.no>.
On Tue, Jan 26, 2010 at 6:07 PM, Andreas Schaefer <sc...@me.com> wrote:
> Hi
>
> To see if Sling would fit our project I would need to be able to handle calls from a client (Ext/JS) sending JSon to server and expecting a JSon response back. In the samples I read that it is possible to register a servlet inside Sling but I am not quite sure how to do it. The only place where I found a servlet used is in 'org.apache.sling.sample.slingshot.impl.ResourceTypeConversionServlet' or the 'org.apache.sling.launchpad.testservices.war.servlets.TestServlet'.

Also look in the Sling codebase. SlingInfoServlet [1]  is a simple
example that registers a servlet to handle requests to
/system/sling/info. Or look at JsonQueryServlet [2], which registers
for all requests ending with ".query.json".

> Would that be the way to handle JSon calls from a client inside Sling?

Sling comes with a default JSON renderer. Just add ".json" to any
resource URI, and you'll get a JSON representation, e.g.:
http://localhost:8080/your/node.json

If you need a custom JSON format, you could write your own servlet, or ...

> Could that be done with an ESP file instead?

Absolutely. If you're not happy with the default JSON representation
of a resource, you could easily implement your own in ESP:
<%
  response.setContentType("application/json");
%>
{
  your_custom_json_property: <%=currentNode.something%>
}
Saving this script as /apps/servlet/default/customjson.esp in your
repository, and then doing a request to
http://localhost:8080/your/node.customjson should invoke your script
and return the JSON.

Also see the JSONQueryServlet for how to return query results in JSON:
http://in-the-sling.blogspot.com/2008/09/how-to-use-json-query-servlet.html

If you haven't done so already, check out the Sling in 15 minutes
guide: http://sling.apache.org/site/discover-sling-in-15-minutes.html

Hope this gets you started - if you run into any trouble, don't
hesitate to ask on this list.

[1] http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/impl/info/SlingInfoServlet.java?view=markup
[2] http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/JsonQueryServlet.java?view=markup

-- 
Vidar S. Ramdal <vi...@idium.no> - http://www.idium.no
Sommerrogata 13-15, N-0255 Oslo, Norway
+ 47 22 00 84 00 / +47 21 531941, ext 2070