29.07.2014 Views

Introducing: REST and JSON in Caché - InterSystems Benelux

Introducing: REST and JSON in Caché - InterSystems Benelux

Introducing: REST and JSON in Caché - InterSystems Benelux

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Introduc<strong>in</strong>g</strong>: <strong>REST</strong><br />

<strong>and</strong> <strong>JSON</strong> <strong>in</strong> Caché<br />

Olivier Caudron<br />

Senior Sales Eng<strong>in</strong>eer<br />

<strong>InterSystems</strong> <strong>Benelux</strong>


What is <strong>REST</strong>?<br />

• <strong>REST</strong> ("Representational State Transfer") is<br />

NOT a technology or "st<strong>and</strong>ard"<br />

• <strong>REST</strong> is a series of patterns, a set of pr<strong>in</strong>ciples,<br />

us<strong>in</strong>g the exist<strong>in</strong>g services of HTTP to express<br />

client queries to a server<br />

• It is a reaction to the complexity of RPC<br />

protocols like SOAP that defer functionalities to<br />

a higher layer (mostly <strong>in</strong> the SOAP "envelope")<br />

• http://en.wikipedia.org/wiki/<br />

Representational_state_transfer


Typical <strong>REST</strong>Ful Patterns<br />

• Use the HTTP "verbs" for CRUD operations:<br />

• HTTP POST == CREATE record(s)<br />

• HTTP GET == READ record(s)<br />

• HTTP PUT == UPDATE record(s)<br />

• HTTP DELETE == DELETE record(s)<br />

• Use the URL for identify<strong>in</strong>g resources<br />

• E.g. GET http://localhost/restapp/employee/1<br />

• The structure of the URL is not st<strong>and</strong>ardized <strong>in</strong> <strong>REST</strong>;<br />

it might also be:<br />

http://localthost/restapp?resource=employee&id=1<br />

or some other variant.


Uses of <strong>REST</strong><br />

• <strong>REST</strong> is ma<strong>in</strong>ly an architecture style for web<br />

*applications* (not relevant for *static* websites)<br />

• Can use <strong>REST</strong> as an alternative for SOAP/WS:<br />

• "<strong>REST</strong>Ful web services"<br />

• JUST an architecture style: NOTHING is st<strong>and</strong>ardized!!!<br />

• Object serialization is NOT def<strong>in</strong>ed (often use <strong>JSON</strong>)<br />

• Parameter pass<strong>in</strong>g is NOT st<strong>and</strong>ardized<br />

• Security is NOT def<strong>in</strong>ed <strong>in</strong> <strong>REST</strong><br />

• Transaction h<strong>and</strong>l<strong>in</strong>g, etc… ?<br />

• As long as it's not better st<strong>and</strong>ardized, not suitable<br />

for WS <strong>in</strong>teroperability IMHO


What is <strong>JSON</strong>?<br />

• <strong>JSON</strong> ("JavaScript Object Notation") is an<br />

object serialization system based on the<br />

JavaScript syntax<br />

• !!! Albeit very close, <strong>JSON</strong>'s notation is NOT identical<br />

to the JS syntax, there are significant differences !!!<br />

• However the contract is that the JS "eval" comm<strong>and</strong><br />

will be able to deserialize any <strong>JSON</strong> stream<br />

• <strong>JSON</strong>'s specification is very short (a onepager)<br />

<strong>and</strong> easy to underst<strong>and</strong><br />

• See http://www.ietf.org/rfc/rfc4627.txt or<br />

http://www.json.org/


<strong>JSON</strong> <strong>in</strong> a Nutshell<br />

• 2 base conta<strong>in</strong>ers: object <strong>and</strong> array<br />

• Array: [1,"foo","bar",3.141592]<br />

• Object: {foo:"bar",val:10}<br />

• Can be nested: arrays may conta<strong>in</strong> objects or arrays,<br />

<strong>and</strong> vice-versa, down several levels<br />

• Datatypes: object, array, str<strong>in</strong>g, number, boolean<br />

(true, false), null<br />

• Unlike JS, str<strong>in</strong>gs can only be enclosed <strong>in</strong> double quotes<br />

• Str<strong>in</strong>gs are unicode only<br />

• Character escape rules limited compared to JS


Uses for <strong>JSON</strong><br />

• Orig<strong>in</strong>ally, used to (de)serialize JS structures<br />

• Now used <strong>in</strong> <strong>in</strong>teroperability contexts where<br />

<strong>JSON</strong> is used between diverse OO systems<br />

• E.g. Javascript <strong>and</strong> PHP<br />

• <strong>JSON</strong> reflects the fact that JS is a prototypebased<br />

OO language, not a class-based OO<br />

language like Java, C++, Caché…<br />

• No metadata (classes), structures are completely<br />

dynamic need to def<strong>in</strong>e conventions between<br />

communicants on how to use the data!


Why <strong>JSON</strong>?<br />

• <strong>JSON</strong> is becom<strong>in</strong>g <strong>in</strong>creas<strong>in</strong>gly popular<br />

• May eventually completely replace XML (AJAX) for<br />

browser/server communications<br />

• MUCH simpler than XML (or others like EDI)<br />

• More human-readable than XML (most of the time)<br />

• MUCH more efficient to parse <strong>and</strong> serialize<br />

• Much more size-efficient than XML (but not EDI)<br />

• Better <strong>in</strong>tegration with Javascript


Downsides of <strong>JSON</strong><br />

• XML has many advantages over <strong>JSON</strong>:<br />

• Semantics & validation (DTD, XSD, WSDL, Schematron…)<br />

• Clear support for encod<strong>in</strong>g (lack<strong>in</strong>g <strong>in</strong> <strong>JSON</strong> –<br />

completely relies on convention)<br />

• Integration with RPC protocols (SOAP & WS)<br />

• Integration with security protocols beyond po<strong>in</strong>t-to-po<strong>in</strong>t<br />

(WS-Security, WS-Policy) <strong>and</strong> other services<br />

(WS-Transaction etc.)<br />

• IMHO, <strong>JSON</strong> w<strong>in</strong>s h<strong>and</strong>s down <strong>in</strong> the browser<br />

context but XML w<strong>in</strong>s h<strong>and</strong>s down <strong>in</strong> the web<br />

services context!


3 rd Party Support<br />

• The <strong>in</strong>creas<strong>in</strong>g popularity of <strong>JSON</strong> makes that<br />

it is very well supported by all sorts of tools:<br />

• HTML frameworks: JQuery, AngularJS, etc.<br />

• Server-side products: PHP <strong>and</strong> others, even<br />

databases<br />

• Some frameworks rely entirely on <strong>JSON</strong> <strong>and</strong><br />

provide a completely transparent<br />

serialization/deserialization (AngularJS)


Why <strong>REST</strong> <strong>and</strong> <strong>JSON</strong> <strong>in</strong> one Presentation?<br />

• <strong>REST</strong> is unrelated to <strong>JSON</strong> but requires some<br />

serialization technique for any serious work<br />

(could be XML)<br />

• <strong>JSON</strong> is unrelated to <strong>REST</strong> but needs some<br />

<strong>in</strong>teroperability technique (transport)<br />

• Nowadays, most <strong>REST</strong> projects use <strong>JSON</strong> as<br />

serialization mechanism<br />

• On the other h<strong>and</strong>, <strong>JSON</strong> is used a lot outside of<br />

the <strong>REST</strong> context (e.g. replacement for AJAX)


<strong>REST</strong> <strong>in</strong> Caché<br />

• <strong>REST</strong>Ful patterns can be used <strong>in</strong> any version<br />

of Caché us<strong>in</strong>g CSP (with some work)<br />

• …with the exception of some <strong>REST</strong>Ful URLs<br />

e.g. http://myserver/myapp/resource/id<br />

• To support those URLs, a change <strong>in</strong> the CSP<br />

application def<strong>in</strong>ition was required<br />

• Will be available <strong>in</strong> 2013.2, already stable<br />

• Further, we implemented an elegant<br />

dispatch<strong>in</strong>g mechanism for <strong>REST</strong>Ful requests<br />

• Note that <strong>REST</strong> is irrelevant to ZEN


Us<strong>in</strong>g <strong>REST</strong> <strong>in</strong> Caché 2013.2<br />

• Typically, you will create a second (or more)<br />

CSP application for a given namespace<br />

• Primary CSP application used to serve static <strong>and</strong> non-<br />

<strong>REST</strong>Ful pages<br />

• On this application, def<strong>in</strong>e<br />

a "dispatch class"<br />

• New sett<strong>in</strong>g<br />

• CSP file sett<strong>in</strong>gs <strong>and</strong> custom<br />

pages disabled: not relevant<br />

to <strong>REST</strong>Ful operations


Us<strong>in</strong>g <strong>REST</strong> <strong>in</strong> Caché 2013.2<br />

• The dispatch class is a class that <strong>in</strong>herits from<br />

%CSP.<strong>REST</strong> <strong>and</strong> def<strong>in</strong>es an xdata block UrlMap<br />

• Routes <strong>REST</strong>Ful URLs to classmethods<br />

• (<strong>REST</strong> is a stateless API: no <strong>in</strong>stance methods)<br />

• Declare the HTTP verb<br />

• Can pass parameters by prefix<strong>in</strong>g them with a colon


<strong>REST</strong> <strong>in</strong> Ensemble<br />

• A support exists <strong>in</strong> Ensemble for <strong>REST</strong>Ful<br />

communications (services <strong>and</strong> operations)<br />

• See the EnsLib.<strong>REST</strong> package or the<br />

Demo.<strong>REST</strong> package <strong>in</strong> the EnsDemo<br />

namespace


<strong>JSON</strong> <strong>in</strong> ZEN<br />

• Because of <strong>JSON</strong>'s efficiency <strong>and</strong> practicality,<br />

it is <strong>in</strong>creas<strong>in</strong>gly used <strong>in</strong> ZEN<br />

• Javascript notations are also <strong>in</strong>creas<strong>in</strong>gly used to<br />

def<strong>in</strong>e contents or structure for components (e.g.<br />

navigator, f<strong>in</strong>derPane…)<br />

• To feed a <strong>JSON</strong>-aware component, use one<br />

of the json providers: jsonProvider,<br />

jsonSQLProvider or jsonArrayProvider<br />

• Invisible component added to the ZEN page def<strong>in</strong>ition<br />

• L<strong>in</strong>k via ControllerId attribute of component


<strong>JSON</strong> <strong>in</strong> CSP<br />

• In Caché 2013.1, <strong>JSON</strong> serialization/deserialization<br />

methods have been made public <strong>and</strong> can be used<br />

outside of ZEN<br />

• Can be an alternative to #SERVER()# <strong>and</strong><br />

#CALL()# <strong>in</strong> CSP, possibly with <strong>REST</strong> (2013.2)<br />

• <strong>JSON</strong> is not as well-<strong>in</strong>tegrated to CSP as hyperevents<br />

• …but can be <strong>in</strong>terest<strong>in</strong>g to use with <strong>JSON</strong>-enabled 3 rd<br />

party frameworks (e.g. JQuery, AngularJS)<br />

• Could possibly be used <strong>in</strong> other contexts, e.g. to<br />

communicate between PHP <strong>and</strong> Caché


DEMO


<strong>Introduc<strong>in</strong>g</strong>: <strong>REST</strong><br />

<strong>and</strong> <strong>JSON</strong> <strong>in</strong> Caché<br />

Olivier Caudron<br />

Senior Sales Eng<strong>in</strong>eer<br />

<strong>InterSystems</strong> <strong>Benelux</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!