Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 8 Next »

Presentations

A presentation was made by Mike on February 16th 2010 about ERREST. The presentation was recorded and made available here :

http://webobjects.mdimension.com/wonder/screencasts/ERRest-2010-02-16.mov

It's also listed on the Screencasts page on wocommunity.org and in the podcasts RSS feed.

Mike also did a session about it at WOWODC West 2009, you can buy this session in the Community Store.

Class names vs entity names

A question was asked about situations where your EO class name is different from your entity name. Mike's answer was :

Everything internally is based on entity names. Your class name has very little to do with things other than your own source code. So for example:

routeRequestHandler.addDefaultRoutes("SamsSchool"); // School.ENTITY_NAME

Regardless, you will always have:
public class SamsSchoolController extends ERXDefaultRouteController {
...
School school = (School) routeObjectForKey("samsSchool");

When those slides say "[EntityName]", they mean it (smile)

If you want to call it "School" to the outside, add this before you register the default routes for "SamsSchool":

ERXRestNameRegistry.registry().setExternalNameForInternalName("School", "SamsSchool"); // "School", School.ENTITY_NAME

After adding this, no other code changes. All you're saying is that the routes and type names that send over the wire should all say "School".

WO HTTP adaptor

The WO HTTP adaptor coming with WO 5.3 doesn't support PUT and DELETE operations, so you won't be able to use those two HTTP methods. You need to use either the 5.4 adaptor or the Wonder version of the adaptor.

To-many relationships

ERRest won't let you add or remove objects in a to-many relationship, it can only update an existing object that is in the relationship. To add a new object to a relationship, you first need to fetch (or create) the parent object and make a second call to create the object and add it to the relationship.

So let's say you have an existing Organization object and you want to add a Member to it. First, you need to fetch the Organization :

GET /cgi-bin/WebObjects/ra/Organization/1.json

and after, you create a new Member :

POST /cgi-bin/WebObjects/ra/Organization/1/addMember.json

Same Origin policy

Look like the Same Origin Policy do not work in Safari 5, Safari doesn't send a OPTIONS call before the GET/POST/DELETE/PUT and it won't load up the JSON data in the JavaScript app.

If you are planning to offer your REST services to other people, they might run into Same Origin Policy problem. When using XMLHttpRequest on a page who is on a different domain than the REST service, XMLHttpRequest will tell you that it's not acceptable.

To get around that problem, many solutions has been found, but two of them are more accepted than the others : JSONP and HTTP_access_control. I didn't try the JSONP route, but I did try HTTP_access_control, and Dojo and Prototype are using this method to get around the Same Origin policy problem.

HTTP_access_control works by adding new headers in the request that says which HTTP methods the request wants to do, and it's send as a OPTIONS HTTP method. You MUST reply with some headers to this OPTIONS request, so you need to do two things :

1) Create a method to handle the request in your route controller :

  public WOActionResults optionsAction() throws Throwable {
    ERXResponse response = new ERXResponse();
    response.setHeader("*", "Access-Control-Allow-Origin");
    response.setHeaders(this.request().headersForKey("Access-Control-Request-Method"), "Access-Control-Allow-Methods");
    response.setHeaders(this.request().headersForKey("Access-Control-Request-Headers"), "Access-Control-Allow-Headers");
    response.setHeader("1728000", "Access-Control-Max-Age");
    return response;
  }

2) Add a new route to handle the OPTIONS request. The route URI should be the same as the GET/POST/PUT/DELETE routes that you have.

restRequestHandler.insertRoute(new ERXRoute(Member.ENTITY_NAME,"/members/index", ERXRoute.Method.Get,MembersController.class, "index"));
restRequestHandler.insertRoute(new ERXRoute(Member.ENTITY_NAME,"/members/index", ERXRoute.Method.Options,MembersController.class, "options"));

Now you also need to add the Origin header into the response of your GET/POST/PUT/DELETE, or else XMLHttpRequest will still complain. To do that, you need to cast the response as a WOResponse and add the header. So instead of doing :

  return response(editingContext(), Member.NomEntite, members, erxFilter());

You have to :

  WOResponse myResponse = response(editingContext(), Member.NomEntite, members, erxFilter()).generateResponse();
  myResponse.setHeaders(this.request().headersForKey("Origin"), "Access-Control-Allow-Origin");
  return myResponse;

Dates

The default formatter for dates is :

%Y-%m-%dT%H:%M:%SZ

If you want to work with the the GMT offset, you have to use this instead :

%Y-%m-%dT%H:%M:%S%z

To change it, you have to set the "er.rest.timestampFormat" property :

er.rest.timestampFormat = %Y-%m-%dT%H:%M:%S%z

If you are using Dojo, you can use dojo.date.stamp.toISOString and dojo.date.stamp.fromISOString to convert from or to a Java date object.

  • No labels