Wiki source code of ERRest Framework

Version 66.1 by Pascal Robert on 2011/09/23 16:39

Show last authors
1 {{toc}}{{/toc}}
2
3 = Presentations =
4
5 * A presentation made by Mike Schrag on February 16th 2010 about ERREST. [[http://webobjects.mdimension.com/wonder/screencasts/ERRest-2010-02-16.mov]]
6 It's also listed on the [[Screencasts Page>>http://www.wocommunity.org/webobjects_screencasts.html]] on wocommunity.org, in [[iTunes>>http://itunes.apple.com/us/podcast/webobjects-podcasts/id270165303]] and in the [[Podcasts RSS Feed>>http://www.wocommunity.org/podcasts/wopodcasts.xml]].
7
8 * Mike also did a session about it at WOWODC West 2009 (see below).
9
10 * A [[session>>http://www.wocommunity.org/webobjects_screencasts.html]] on ERRest integration with Dojo was made by Pascal Robert at WOWODC 2010.
11
12 * You can also find the slides from Pascal's two WOWODC 2011 about ERRest on [[SlideShare>>http://www.slideshare.net/wocommunity/]].
13
14 Current and past WOWODC Sessions are available for purchase on the WebObjects Community Association website.
15
16 1. Log in, or create an account here: [[http://www.wocommunity.org/account]]
17 1. Go to the Community Store where there will be options for purchasing memberships
18
19 = Class names vs Entity names =
20
21 Everything internally in ERRest is based on entity names, as it is throughout WebObjects and EOF.
22
23 Most times your Entity Name will be the same as your Class name, but because Entity names must be unique there are situations where you will need to change the Entity name i.e., if you have two Classes named the same thing but are in different packages.
24
25 Lets say you have a class named "School" in your model, but have defined the Entity name as "ERPSchool" you will always refer to it as "ERPSchool" when passing it in as a parameter. For example:
26
27 {{noformat}}
28 routeRequestHandler.addDefaultRoutes("ERPSchool"); // School.ENTITY_NAME
29 {{/noformat}}
30
31 Also, when naming your controller classes, you should use the Entity name.
32
33 {{noformat}}
34
35 public class ERPSchoolController extends ERXDefaultRouteController {
36 ...
37 School school = (School) routeObjectForKey("ERPSchool");
38
39 {{/noformat}}
40
41 If you want to call it "School" to the outside, add this before you register the default routes for "ERPSchool":
42
43 {{noformat}}
44
45 ERXRestNameRegistry.registry().setExternalNameForInternalName("School", "ERPSchool"); // "School", School.ENTITY_NAME
46
47 {{/noformat}}
48
49 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".
50
51 = WO HTTP adaptor =
52
53 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.
54
55 = To-many relationships =
56
57 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.
58
59 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 :
60
61 {{code}}
62
63 GET /cgi-bin/WebObjects/ra/Organization/1.json
64
65 {{/code}}
66
67 and after, you create a new Member :
68
69 {{code}}
70
71 POST /cgi-bin/WebObjects/ra/Organization/1/addMember.json
72
73 {{/code}}
74
75 = Same Origin policy =
76
77 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.
78
79 To get around that problem, many solutions has been found, but two of them are more accepted than the others : [[JSONP>>http://en.wikipedia.org/wiki/JSON#JSONP]] and [[HTTP//access//control>>https://developer.mozilla.org/en/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.
80
81 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. Support for those headers was added in ERRest somewhere in october 2010.
82
83 To enable the headers and support for the OPTIONS method, add this property:
84
85 {{code}}
86 ERXRest.accessControlAllowOrigin=*
87 {{/code}}
88
89 If you want to allow origin for only a specific host, change the value of the property to the IP or DNS name of the requester.
90
91 Now, sadly only newer (2008 or later) browsers support the HTTP Access Control standard. For older browsers, you have to support JSONP, or the [[window.name transport>>http://www.sitepen.com/blog/2008/07/22/windowname-transport/]]. JSONP is not integrated in ERRest, but you can use the window.name transport by enabling support with the following property.
92
93 {{code}}
94 ERXRest.allowWindowNameCrossDomainTransport=true
95 {{/code}}
96
97 = Dates =
98
99 The default formatter for dates is :
100
101 {{code}}
102
103 %Y-%m-%dT%H:%M:%SZ
104
105 {{/code}}
106
107 If you want to work with the the GMT offset, you have to use this instead :
108
109 {{code}}
110
111 %Y-%m-%dT%H:%M:%S%z
112
113 {{/code}}
114
115 To change it, you have to set the "er.rest.timestampFormat" property :
116
117 {{code}}
118
119 er.rest.timestampFormat = %Y-%m-%dT%H:%M:%S%z
120
121 {{/code}}
122
123 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.
124
125 = JSON Schema support =
126
127 ERRest have support for [[JSON Schema>>http://www.sitepen.com/blog/2008/10/31/json-schema/]] since october 2010. JSON Schema is like a mini-WSDL, where it describe which properties JSON objects have. Dojo use JSON Schema to perform client-side validation, which is cool because you can have some automatic validation based on your model, and validation is done even before the data is sent back to your REST service.
128
129 To get the schema, you need to check in your route action. For example, for the "index" action, you do:
130
131 {{code}}
132
133 public WOActionResults indexAction() throws Throwable {
134 if (isSchemaRequest()) {
135 return schemaResponse(yourERXKeyFilter());
136 }
137 ... do your normal work here
138 }
139
140 {{/code}}
141
142 And you call your REST route, but by adding "?schema=true" at the end of the URL. For example, ///ra/events.json?schema=true//. When you do that, a JSON Schema will be returned. Sample:
143
144 {{code}}
145
146 {
147 "name":"Event",
148 "properties":{
149 "isFullDay":{
150 "optional":true,
151 "type":"string",
152 "maxLength":5
153 },
154 "startDate":{
155 "optional":false,
156 "type":"string",
157 "format":"date-time"
158 },
159 "realDuration":{
160 "optional":true,
161 "type":"integer"
162 }
163 }
164 }
165
166 {{/code}}
167
168 So when you create a new Event with Dojo, if you try to put something else than a date in startDate, or you put a string in realDuration, it will fail because the schema says that startDate is using the date-time format, and that realDuration is a integer.
169
170 = Getting the URI of a REST route =
171
172 For whatever reason, you might need to know the URI for a REST route. To get that information, use the ERXRouteController.//controllersForRequest(WORequest) method, and get the value of request().uri(). Sample~://
173
174 {{code}}
175
176 NSMutableArray<ERXRouteController> routeControllers = ERXRouteController._controllersForRequest(this.context().request());
177 if (routeControllers.count() > 0) {
178 return routeControllers.objectAtIndex(0).request().uri()
179 } else {
180 return this.context().request().uri()
181 }
182
183 {{/code}}
184
185 = Automatic HTML routing =
186
187 Not only you can get XML and JSON out of ERRest, but you can also get rendered components. This give you "clean" URLs without having to write a bunch of Direct Actions.
188
189 To enable the automatic HTML routing, you need to override the isAutomaticHtmlRoutingEnabled method in your controller to return true:
190
191 {{code}}
192
193 public class EventsController extends ERXDefaultRouteController {
194 ...
195 protected boolean isAutomaticHtmlRoutingEnabled() {
196 return true;
197 }
198
199 {{/code}}
200
201 When do, you can now add .html when you reach a REST route with a browser, for example /ra/events.html instead of /ra/events.json. But you will notice that you will get an exception because it cannot find a component. You need to create a component with the following pattern:
202
203 EntityActionPage
204
205 For example, if you have a Event entity that is controlled by EventsController, and you call the "index" REST route to get a list of events, the component name have to be:
206
207 EventIndexPage
208
209 Also, for all actions except "index", your component have to implement IERXRouteComponent, and you have to add setter methods to get the data that the REST route is sending. For example, if you create a EventShowPage to show the details of a event, EventShowPage.java need the following method:
210
211 {{code}}
212
213 @ERXRouteParameter
214 public void setEvent(Event event) {
215 ...
216 }
217
218 {{/code}}
219
220 The @ERXRouteParameter annotation will make the method suitable for "accepting" objects from the controller, if the annotation is not there, you won't be able to receive the object from the controller.