DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Silverlight Clients Playing Nice With Java: REST with JAX-RS

Silverlight Clients Playing Nice With Java: REST with JAX-RS

Arnon Rotem-gal-oz user avatar by
Arnon Rotem-gal-oz
·
Jun. 15, 12 · Java Zone · Interview
Like (0)
Save
Tweet
9.60K Views

Join the DZone community and get the full member experience.

Join For Free
A while back I worked on a RESTful  service using Jersey (not my first choice, but a reasonable compromise). It is hosted inside FuseESB and all is well. Here’s an overly simplified version of the resource that reports status:
@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 domain to allow cross-domain access. Here’s a simple all-permissive version of it:
@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.)

@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.

REST Web Protocols Java (programming language)

Published at DZone with permission of Arnon Rotem-gal-oz, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Modernize Legacy Code in Production: Rebuild Your Airplane Midflight Without Crashing
  • OPC-UA, MQTT, and Apache Kafka: The Trinity of Data Streaming in IoT
  • Open Source Security Risks
  • Top Soft Skills to Identify a Great Software Engineer

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo