Blogs

VCD #16 - The REST Bot: Behind the scenes
Uwe Fetzer
Business Card
Company: SE|38 IT-Engineering & Consulting
Posted on Sep. 29, 2009 12:22 AM in ABAP, Application Server, Beyond SAP, Business Process Expert, Business Process Modeling, Business Server Pages, Python

URL: http://wiki.sdn.sap.com/wiki/display/events/2009+Community+Day

Subscribe.Subscribe
Print. Print
Permalink Permalink

Abstract

In VCD #16 you have seen, amongst others, the REST bot. This short blog wants to explain how he/it works.

The blog is devided into the ABAP part and the Google wave part. You may read both or just one of these parts, according your preferences.

Motivation

In the last couple of weeks you surely have followed the new trending topics on twitter and SDN: REST and Googlewave.

My timeline in these exciting weeks

  • 08/21 Daniel Graversen published his Blog "SAP Enterprise Service and Google Wave"
  • 08/25 Fortuna or the Google God (or who else) had a good day and invited me to join the developers preview of Googlewave
  • 08/27 eGeeks Podcast "SOAP vs. REST"
  • 09/03 DJ Adams found me on Googlewave: "I've been hacking around this morning with a connection to my SAP system" -> Demo of a wave robot based on an old rest dashboard application he wrote in 2004 (see his blog "Forget SOAP - build real web services with the ICF")

What else motivation an ABAP enthusiast needs to jump into a new adventure?

REST

As you can see in Daniels blog, SOAP isn't the easiest way of communication between Googlewave and SAP. Therefore my first question was: how did DJ do this? But I didn't ask him, I did ask myself.

Last time I've developed a HTTP handler in the SAP ICF was some years ago, so I had to experiment a little bit. But the first handler works quite fast, so my next question was: can be build an universal handler that is able to handle all kind of REST requests? The answer is: yes we can ;)

The result you can find in the ABAP part of this blog.

REST extreme

Brian McKellar 06/27/2004: "I like your idea of addressable data. It will not take long until someone has the handler for /name_of_table/row_key!" (as a comment on DJ's above mentioned Blog)

He was quite right...

Googlewave

If you are not familiar with Googlewave yet, you may have a look on Daniels Blog "Starting on Google Wave"

About Robots

Wave robots are tiny programs written in Java or Python. They interact with the wave nearly as they were human participants. The code is stored as an Google App Engine application on the google server (appspot.com).

Advantages:

  • robots have the full access to the wave (all information, all participants, all change possibilities).
  • They can add information as plain text, form fields and images (but no file attachments yet. Maybe later, the Wave API is still under construction)

Disadvantages:

  • see "Advantage": They ONLY can add information as plain text, form fields and images.
  • Robots cannot interact with gadgets
About Gadgets

Gadgets are XML files which you can embedded into a blib of a wave.

Advantages:

  • within the XML file you can include (nearly) all HTML-Tags and Scripts (Javascript, Actionscript,...).
  • the XML file can be stored everywhere on the web

Disadvantage:

  • Gadgets don't have access to the wave and cannot interact with robots

The ABAP Part

The main goal for our new universal handler class is to handle REST HTTP requests regardless which object type we request and which content-type we expect as response.

HTTP request should look like http://host:port/rest/noun/key1/key2/../keyn/verb

  • Noun: Object type, i.e. “customer”
  • Key1 – Key n: the object key, i.e. the customer number
  • Verb: method, i.e. “getname”
The REST handler Class

The new handler class must implement the interface IF_HTTP_EXTENSION.

Method IF_HTTP_EXTENSION~HANDLE_REQUEST:

In the ICF tree (transaction SICF) we only have to create a new entry “REST” under /sap/bc, enter the logon data and the new created handler class into the handler list:

(In a productive environment you better create an alias to this service)

The REST resolver classes

After implementing the handler class, you only have to create a class for each object type you want to serve, i.e. the Customer-Class with a method “resolve” and the following interface:

The import parameter “request” contains the object key(s) and the desired method (verb) as last entry.

Exporting parameters:

  • data: content as byte-stream (i.e. a PDF document or an image)
  • cdata: content as plain text
  • content_type: i.e. “text/plain” or “application/pdf”

The method “resolve” could looks like:

 

If you want to serve many objects, you should create an Interface for this method (not part of this blog).

The “REST extreme” class

Just the code, no comments. There is really no use case for productive usage of this kind of stuff. Just a proof of concept and the right motivation (see above).

 

The Wave Part

The Google wave part was (for me) the more difficult one: I didn’t want (and still don’t want) to learn Java therefore I had to learn Python. But this was a nice experience!

The Robot

Creating the robot to answer the simple questions (plain text response) was quite simple, it (or he?) contains just two sections:

  • Create an Hello message and a How to use guide
  • Concatenate the rest command to a URL and get the content
The Gadget

But one issue was still open: I have created a resolving class which delivers a PDF response (rest/spool/xyz/getpdf). As I mentioned above, the robot only can handle plain-text responses. “Make a gadget”, a twitter follower advised me. Nice try, but how can I pass parameters to a gadget. I mentioned also, that the robot has no access to the gadget and the gadget cannot read the requesting blip for the parameters.

Sleepless nights.

One day, I was at my current customer, I’ve got the answer: do we really need a static XML file for the gadget? Quit work, took my motorbike and flew to my home office and tried my idea: it works :)

The solution:

I simply wrote a BSP (gadget.xml) which has one input parameter:

I change my robot a bit the gadget was correctly called by the robot as you can see in the replay of VCD #16.

Added part in the robot:

Conclusion

These where funny weeks. Have met some new and interesting people, had some great discussions, learned a lot. What’s the next topic? ;-)

More to read...

 

Uwe Fetzer is ABAP addicted since 1991 www.se38.de


Add to: del.icio.us | Digg | Reddit


Comment on this article
Comment on this weblog
Showing messages 1 through 8 of 8.

Titles Only Main Topics Oldest First

  • Bug in Gadget
    2009-09-29 14:25:48 Uwe Fetzer Business Card [Reply]

    Found a bug in the gadget, sorry.
    "REPLACE ' ' WITH '/' INTO rest_objects." does not work.


    Please change to:
    TRANSLATE rest_objects USING ' /'.


    Have fun, Uwe

  • thanks for sharing
    2009-09-29 02:33:35 Durairaj Athavan Raja Business Card [Reply]

    "I didn’t want (and still don’t want) to learn Java"


    I belong to your clan.


    And your mention about DJ Adams, Forget SOAP blog, oh man that was eye opener for me. From then(2004) and now i have really exploited that idea and did many apps.


    Though i was online during your VCD session, due to technical difficulties could hear you all (audio issue). Cant wait to try out your sample, eagerly looking forward to my googlewave invite.


    Regards
    Raja

  • Great blog post
    2009-09-29 01:29:48 DJ Adams Business Card [Reply]

    Hey Uwe


    I've been chatting to you about Wave, REST, ICF and ABAP so I'm a bit biased, but this is a great post. Nice work on the overview AND the detail.


    By the way, from 10,000 feet, looking at the local variable definitions in your code sample above, I had to laugh, because it reflects what I see a lot in my code these days ... most of the definitions are STRING or XSTRING. Who needs other types? ;-)


    Just a quick note - on the subject of robots and plain text, have a look at Annotations, I think they might be useful and what you're after.


    Cheers
    dj

    • Great blog post
      2009-09-29 01:51:46 Uwe Fetzer Business Card [Reply]

      Hi DJ,
      ... and sometimes "TYPE REF TO DATA", lol
      Annotations: good point. Had viewed some waves in Debug mode already to get some informations about annotations.
      I hope the Python API will get some methods to set annotations in the future more comfortable. Today the API seems to be a bit behind the JAVA API.
      CU, Uwe

Showing messages 1 through 8 of 8.