REST with JAX-RS and silverlight clients


Posted on December 5th, by Arnon Rotem-Gal-Oz in Blog. No Comments

I am Working on a RESTful  service using Jersey (not my first choice, but a reasonable compromise). It is hosted inside FuseESBand all is well. Here’s an overly simplified version of the resource that reports status:

1
2
3
4
5
6
7
8
9
10
11
@Path("/stat")
public class StatusResource {
    @GET
    @Produces(MediaType.TEXT_PLAIN)
      public String getStatus() {
        if (checkStatus())
            return "Ok";
         else
            return "Not so good";
    }
}

Then we tried to connect a silverlight client to this server (don’t ask me why).  The first hurdle was a security one. Silverlight doesn’t like to communicate with a server it didn’t launch from. To resolve that you need to add a ClientAccessPolicy.xml to the root of your domainto allow cross-domain access. Here’s a simple all-permissive version of it:

1
2
3
4
5
6
7
8
9
10
11
@Path("clientaccesspolicy.xml")
public class ClientAccessPolicyResource {
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public String getStatus() {
        return "<?xml version=\"1.0\" encoding=\"utf-8\"?><access-policy><cross-domain-access><policy> <allow-from http-request-headers=\"*\"><domain uri=\"*\"/></allow-from> <grant-to> <resource path=\"/\" include-subpaths=\"true\"/>"+
     " </grant-to></policy><policy ><allow-from http-methods=\"*\"><domain uri=\"*\"/></allow-from><grant-to>"+
      "<resource path=\"/\" include-subpaths=\"true\"/> </grant-to></policy></cross-domain-access></access-policy>";
    }
 
}

By the way – since silverlight is entirely a client technology you can also serve it from the Java web server (as long as you emit the right mime-type ) in which case you don’t need the cross-domain policy mentioned above.

Another interesting phenomena we saw was that the client only got the response once. no matter how many times we polled the server. We ran fiddler and saw that no request are in fact going out to the server – it was the browser’s (IE8) cache that was holding us back. It took me some head scratching as to how to solve this – but the solution is pretty simple – return an ‘expires” http header from the server to make sure the browser will only cache the result for a limited time. To do that with jersey you need to return a Response object instead of the text (or JSON/HTML etc.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Path("stat")
public class StatusResource {
 
    @GET @Produces(MediaType.TEXT_PLAIN)
    public Response getStatus() {
 
        Response.ResponseBuilder response = Response.ok(checkStatus);
 
    Date expirationDate = new Date(System.currentTimeMillis() + maxCacheInterval);
    response.expires(expirationDate);
     return response.build();
    }
 
}

While I think Silverlight is a technology on its way out. It is still around. It is nice to know that with a couple of simple moves you can get it to play nice even with a Java backend


Illustration by arinas74

Tweet about this on Twitter1Share on LinkedIn0Share on Facebook1Share on Google+1Buffer this pageShare on Reddit0Share on StumbleUpon0Email this to someone