dev@glassfish.java.net

Re: How to handle callbacks in jsr311

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 18 Mar 2009 15:55:26 +0100

Hi Farjola,

I think i understand now: you want to utilize JSON with padding. There
is nothing in JAX-RS that specifies this. But Jersey does provide
support using the JAX-RS plugin mechanism.

See the JSONWithPadding class:

   https://jersey.dev.java.net/source/browse/*checkout*/jersey/tags/jersey-1.0.2/api/jersey/com/sun/jersey/api/json/JSONWithPadding.html

You essentially wrap your Asset instance around a JSONWithPadding
instance.

Some comments follow in line in the code


> @Provider

You do not require the above. You are defining a root resource class
and not a provider.


>
> @Produces( { "text/xml", "application/json" })
> @Consumes( { "text/xml", "application/json" })
> @Path("/epg")
> public class WebBean {
>
> @Context
> protected UriInfo uriInfo;
> private IWebEjb webEjb;
>
> public WebBean() {
>
> try {
> InitialContext ctx = new InitialContext();
> webEjb = (IWebEjb) ctx.lookup("ejb/WebEjb");
> } catch (NamingException e) {
> logger.log(Level.SEVERE,"Failed to initate EPGWebBean",e);
> }

By the time Java EE 6 is ready you will no longer require the above
functionality and you will be avle to use injection.

Note that the default life-cycle for root resource classes is per-
request so you will be performing this work for each request. If you
do not want to do this you can use the Jersey annotation @Singleton to
declare only one instance per Web application.

Alternatively you can extend Jersey to inject the EJB instance. See
the following email for an attached NetBeans project that contains
classes to support this functionality:

http://markmail.org/search/?q=list%3Anet.java.dev.jersey.users
+EJB#query:list%3Anet.java.dev.jersey.users%20EJB%20order%3Adate-
backward+page:1+mid:catyvvrocdk2h4gx+state:results


>
> }
>
>
> @GET
> @Path("nowandnext")
> public Asset shownowandnext() {
> String startTime =
> uriInfo.getQueryParameters().get("starttime").get(0);
> String stopTime =
> uriInfo.getQueryParameters().get("stoptime").get(0);

You can use @QueryParam

     public Asset shownowandnext(
         @QueryParam("starttime") long startTime,
         @QueryParam("stoptime") long stopTime) {
         return webEjb.showNowAndNext(startTime, stopTime);
     }

In general see the JAX-RS overview here:

   http://wikis.sun.com/display/Jersey/Overview+of+JAX-RS+1.0+Features

Paul.

>
> return (webEjb.showNowAndNext(Long.valueOf(startTime),
> Long.valueOf(stopTime)));
>
> }
>
> When an Ajax/javascript client sends an HTTP GET and the response is
> a json object ( Asset is an entity, POJO decorated with the
> necessary annotations), the client request is to have a callback on
> this object, and by callback means that the json object looks like:
> ( {"ids": "id" : {type: x} } );
> As I was told this is a cross domain security issue in javascript
> which is solvable by callbacks. (I am not a client ajax/javascript
> developer, far from it) but I am asked to handle this on the server,
> and I have never done such a thing, so I was curious if anyone has
> tried and there are some guidelines on how to handle this in the
> resource classes.
>
> Thank you once again, hope it's clearer,
>
> /Farjola
>
>
>
>
>
> On Wed, Mar 18, 2009 at 10:26 AM, Paul Sandoz <Paul.Sandoz_at_sun.com>
> wrote:
> Hi Farjola,
>
>
> On Mar 17, 2009, at 10:05 PM, Farjola Zaloshnja wrote:
>
> Hi,
>
> I am trying to support callback functionality in my web bean, which
> consumes and produces application/json in my server all complaint
> with java ee technologies, has anyone around a simple example or
> information if it's possible and how please ?
>
> Could you provide a concrete Java example of what you would like to
> do? I do not understand your question. For example, i do not know
> what you mean by "web bean" and "callback".
>
> Do you want to create a resource class that works correctly within a
> Java EE 5 compliant container like GF v2.x? Do you want to use a
> session bean as a resource class?
>
> In general it is very easy to use JAX-RS/Jersey in any compliant Web
> container. But i need more information on what EE technologies you
> want to use and integrate with to help you.
>
> Note that JAX-RS does not specify support for JSON, so you will need
> to utilize additional features of a JAX-RS implementation for that.
>
> Paul.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: dev-help_at_glassfish.dev.java.net
>
>