[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[ecf-dev] News from the REST API

Hi @ all,
I recently have committed some bigger changes for the REST API to osuosl. You can find the source at [1]. There are three changes you may interested in:

1) REST services work now almost like OSGi services. You can simply register a POJO with one interface implemented and use this for a REST service. Imagine this for Twitter. How can such a szenario look like for Twitter's timeline. Maybe like this way:

a) Create a TwitterContainer which registers a service object and associate it with some IRestCalls:
public class TwitterContainer extends RestContainer {

public TwitterContainer(ID restId) {
Map twitterMethods = new HashMap();
try {
twitterMethods.put("getUserTimeline", new GetRestCall(new URI("/statuses/user_timeline.json"), "ecf.rest.resource.json.org", new Object[]{"count=2"}, null, 10000));
registerRestService(new String[] { ITwitter.class.getName() }, new TwitterService(), twitterMethods, null);
} catch (URISyntaxException e) {
} catch (ECFException e) {

    b) Create a service class which may implement some interfaces, i.e. IUserTimeline:
    public class TwitterService implements ITwitter, IUserTimeline, IRestResponseProcessor {

private JSONArray jsonArray;

public IUserTimeline getTimeline() {
return this;

public IUserStatus[] getUserStatuses() {
List statuses = new ArrayList();
for (int i = 0; i < jsonArray.length(); i++) {
try {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String source = jsonObject.getString("source");
String text = jsonObject.getString("text");
String createdString = jsonObject.getString("created_at");
IUserStatus status = new UserStatus(createdString, source, text);
} catch (JSONException e) {
IUserStatus[] result = new IUserStatus[statuses.size()];
return result;

public void processResource(Object response) {
if(response instanceof JSONArray) {
jsonArray = (JSONArray) response;

    As you can see, this service implments another Interface called IRestResponseProcessor. This is just for passing the parsed response from a REST call to this service object which is the proxy object for an IRemoteService, too. So there can be other interfaces for handling all the timeline stuff.

   c) Use all the stuff i.e. in a test:
public void testGetTimeline() {
IConnectContext context = ConnectContextFactory.createUsernamePasswordConnectContext("eclipsedummy", "eclipse");
remoteService = container.getRemoteService(RestService.class.getName());
try {
remoteService.callSync(new RestRemoteCall("getUserTimeline"));
TwitterService service = (TwitterService) remoteService.getProxy();
IUserTimeline timeline = service.getTimeline();
IUserStatus[] userStatuses = timeline.getUserStatuses();
assertEquals(2, userStatuses.length);
} catch (ECFException e) {

You can take a closer look to all of this at the tests in org.eclipse.ecf.tests.remoteservice.rest in the org.eclipse.ecf.tests.remoteservice.rest.scenario packages. I have created two scenarios which are placed in this package, one for twitter and one for googles search API within minutes. So the whole goal was to make it as simple as possible to use REST services. I think this is pretty simple ;)

2) A lot of service classes exists now to make the creation of IRestCalls much easier. Additional all HTTP methods are working now including HTTP.DELETE ;) 

3) A generic way exists now to use Parser for responses. In the REST world this is called a resource representation. So you can simply register an OSGi service for your resource i.e. XML, JSON, RSS and so on. I have implemented four examples for XML, JSON (jackson and json.org impl) and RSS. To associate such a resource representation with a REST call an identifier is used which can be passed i.e. to an GetRestCall in the construcor.
So, thats all for the moment. I hope you can provide feedback for this API because at the middle of August the deadline for this gsoc project is reached. Please feel free to post questions, too. 

Regards Holger

Innoopract Informationssysteme GmbH
Tel: 0721 - 66 47 33 - 0
Fax: 0721 - 66 47 33 29
========================= Legal Disclaimer =========================
According to Section 80 of the German Corporation Act Innoopract
Informationssysteme GmbH must indicate the following information:
Address: Stephanienstrasse 20, 76133 Karlsruhe Germany
General Manager: Jochen Krause
Registered Office: Karlsruhe, Commercial Register Mannheim HRB 107883