RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

54
1 UKLUG 2012 – Cardiff, Wales REST services and IBM Domino/XWork A presentation for ICON UK 2015 21-22 September 2015 by John Dalsgaard

Transcript of RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

Page 1: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

1

UKLUG 2012 – Cardiff, Wales

REST services and

IBM Domino/XWork

A presentation for ICON UK 201521-22 September 2015

by John Dalsgaard

Page 2: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

2

UKLUG 2012 – Cardiff, Wales

Agenda

● About me● Webservices● REST & JSON● Domino/XWork – out of the box...

– Domino Access Service (DAS)– Extension Library controls– Build your own– Demos

● Round up

Page 3: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

3

UKLUG 2012 – Cardiff, Wales

About me● Worked with Notes since 1995

version 4.5● Java since Notes 5.0.7 (2000)● Large web-apps. (40.000+ users)● Object Oriented approach since 1999 (yes, in

LotusScript...)● XPages & mobile apps (Appcelerator Titanium)....● Certified Principal/advanced administrator and

developer – all versions 4.6 → 9.0● Developer, project manager, IT manager – own

company (Dalsgaard Data A/S) since 1998.● IBM Champion for 2015

Page 4: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

4

UKLUG 2012 – Cardiff, Wales

About me

Page 5: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

5

UKLUG 2012 – Cardiff, Wales

Webservices

● What is a webservice?– Program to program communication– Implemementation independent– ”Contract” about interface

● Traditionally SOAP & XML...– Very ”verbose” (=not ”light”)– Needs pre-/post processing to ”extract” data

→ Meet the ”new kid on the block”:

Page 6: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

6

UKLUG 2012 – Cardiff, Wales

REST services using JSON

● REST = REpresentational State Transfer● JSON = JavaScript Object Notation● Why?? → Loose coupling...

– Angular, Ext.js, etc.– Mobile apps/web apps

● Let's take a quick look at these terms:

Page 7: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

7

UKLUG 2012 – Cardiff, Wales

REST

● Wikipedia:

Representational state transfer (REST) is an abstraction of the architecture of the World Wide Web; more precisely, REST is an architectural style consisting of a coordinated set of architectural constraints applied to components, connectors, and data elements, within a distributed hypermedia system. REST ignores the details of component implementation and protocol syntax in order to focus on the roles of components, the constraints upon their interaction with other components, and their interpretation of significant data elements.....

WHAT?????

Page 8: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

8

UKLUG 2012 – Cardiff, Wales

REST● Client-server architecture

– Uniform interface separates client from server● Stateless

– All info in request● Cacheable communications protocol

– Almost always HTTP● Uniform interface...

– HTML, URIs, XML, JSON, MIME, meta-data....● Actually, WWW via HTTP can also be viewed as a REST-based

architecture – so nothing new here that you did not know... :-)

Page 9: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

9

UKLUG 2012 – Cardiff, Wales

”RESTful” web-service

● Architectural style:– URI structure (base URI)– Internet media type. JSON – or: XML, Atom, …– Standard HTTP methods:

● GET● POST● PUT● DELETE

… also known as: CRUD (Create, Read, Update, Delete) methods

Page 10: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

10

UKLUG 2012 – Cardiff, Wales

”RESTful” web-service

● Designed for networked applications● Using HTTP as a simple alternative to more

complex mechanisms to connect between machines:– WebServices (SOAP, WSDLs etc.)– CORBA– RPC

Page 11: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

11

UKLUG 2012 – Cardiff, Wales

”RESTful” web-service● Example – SOAP:

<?xml version="1.0"?><soap:Envelopexmlns:soap="http://www.w3.org/2001/12/soap-envelope"soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <soap:body pb="http://www.acme.com/phonebook"> <pb:GetUserDetails> <pb:UserID>12345</pb:UserID> </pb:GetUserDetails> </soap:Body></soap:Envelope>

– … must be sent via a request (HTTP POST)● Example – RESTful web-service:

http://www.acme.com/phonebook/UserDetails/12345

– … just a URL!! (HTTP GET) – simple....

Page 12: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

12

UKLUG 2012 – Cardiff, Wales

JSON

● Wikipedia:JSON (/ d e sən/ jay-sən), or ˈ ʒ ɪ JavaScript Object Notation, is an open standard format that uses human-readable text to transmit data objects consisting of attribute–value pairs. It is used primarily to transmit data between a server and web application, as an alternative to XML.

Although originally derived from the JavaScript scripting language, JSON is a language-independent data format. Code for parsing and generating JSON data is readily available in a large variety of programming languages.

Page 13: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

13

UKLUG 2012 – Cardiff, Wales

JSON

● A syntax for storing & exchanging data● An easier to use alternative to XML● Is a lightweight data interchange format● Is language independant● Is ”self-describing” and easy to understand

JSON uses JavaScript syntax, but the JSON format is text only, just like XML. Text can be read and used as a data format by any programming language...

Page 14: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

14

UKLUG 2012 – Cardiff, Wales

JSON vs. XML● XML

<employees> <employee> <firstName>John</firstName> <lastName>Doe</lastName> </employee> <employee> <firstName>Anna</firstName> <lastName>Smith</lastName> </employee> <employee> <firstName>Peter</firstName> <lastName>Jones</lastName> </employee></employees>

● JSON{"employees":[

{"firstName":"John", "lastName":"Doe"}, {"firstName":"Anna", "lastName":"Smith"}, {"firstName":"Peter", "lastName":"Jones"}]}

Page 15: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

15

UKLUG 2012 – Cardiff, Wales

JSON syntax

● Object: {}{ 'text' : 'Hello world!', 'number' : 123, 'valid' : true }

● Array: []{ 'numbers' : [ 1, 2, 3 ] }

{ 'objects' : [ {'a':1}, {'b':2}, {'c':3} ] }● Value:

– string, number, object, array, boolean, null● Please note: NO date/time type...!!! Grrrrr.....

Page 16: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

16

UKLUG 2012 – Cardiff, Wales

JSON and JavaScript

Sample data: var text = ”{ 'name' : 'ICON UK', 'current' : 2015 }”;

● Create an object:– var iconUK = JSON.parse(text);

● Create text representation of an object:– var iconUKText = JSON.stringify(iconUK);

● Refer to attributes:– var name = iconUK.name;

● Add another attribute:– iconUK['venue'] = 'London'

Page 17: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

17

UKLUG 2012 – Cardiff, Wales

Domino/XWork - out of the box

● Webservices (SOAP, XML, etc...)– Provider (server) – since 7.0– Consumer (client) – since 8.0– Written in LotusScript/Java

RESTful service using JSON →

● Domino Access Services (DAS)– core service - since 9.0.1– data service - since 8.5.3 UP1 (~DDS)– calendar service - since 9.0.1

Page 18: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

18

UKLUG 2012 – Cardiff, Wales

Domino Access Services

● Implemented as OSGi plugins/servlets● Based on Apache Wink

● How to enable & configure– Web access– Enable Domino Access Service (DAS)– Enable for database– Enable for specific elements

Page 19: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

19

UKLUG 2012 – Cardiff, Wales

Enable web access● HTTP Server must be started.

– Check console: show tasksHTTP Server Listen for connect requests on TCP Port:80

● Use internet sites – just do it!– Activate in server document– Create Internet site document for domain

● After changes: restart task http● Check:

– Open the server on the port you saw on the console– http://server.dom.dk:80/ (leave out port if 80)

Page 20: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

20

UKLUG 2012 – Cardiff, Wales

Check DAS

Open: server.dom.dk/api– lists services and

their state

Page 21: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

21

UKLUG 2012 – Cardiff, Wales

Enable data service

● On Internet site document (configuration tab):

● Need to refresh http to take effect– tell http refresh

Page 22: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

22

UKLUG 2012 – Cardiff, Wales

DAS: List all ”services” (db's)

● Open: server.dom.dk/api/data

Page 23: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

23

UKLUG 2012 – Cardiff, Wales

DAS: Open a specific database

● Try: server.dom.dk/reports.nsf/api/data/collections

→ We need to enable DAS for the database

Page 24: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

24

UKLUG 2012 – Cardiff, Wales

DAS: Enable for database ● On the advanced properties of the database:

● Select level in ”Allow Domino Data Service”:

● Important decision: – Views only or views and documents

Page 25: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

25

UKLUG 2012 – Cardiff, Wales

DAS: Open database again

● Try: server.dom.dk/demo/json.nsf/api/data/collections

Page 26: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

26

UKLUG 2012 – Cardiff, Wales

DAS: Enable for view● We need to enable DAS for the view first● Open the view in Domino Designer● On the view properties – advanced tab

● Enable: ”Allow Domino Data Service operations”:

● Save the view, open it using the url returned

Page 27: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

27

UKLUG 2012 – Cardiff, Wales

DAS: Open a view● Try: server.dom.dk/.../collections/unid/A892133953...

● Heureka!! - we see a list of all documents!● Also try: server.dom.dk/.../collections/name/persons

Page 28: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

28

UKLUG 2012 – Cardiff, Wales

DAS: Open a document● Try: server.dom.dk/.../documents/unid/33735D0BC...

● Requires ”Views and documents” to be set in DB props.

Page 29: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

29

UKLUG 2012 – Cardiff, Wales

DAS: Writing back...● Remember content type MUST be:

– application/json– Set ”Content-type” in header of request

● If you get ”405 Method not allowed”– Enable method in internet site

● By default these are NOT enabled:–PUT–PATCH–DELETE

– Or override header in your request● ”X-HTTP-Method-Override” : ”POST”

Page 30: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

30

UKLUG 2012 – Cardiff, Wales

DAS: Save existing document

● Use ”PATCH” to change specific fields– url: …/documents/unid/33735D0BCE799....– updates only the fields in the request

● Use ”PUT” to change ALL fields– url: …/documents/unid/33735D0BCE799....– All fields are replaced with the fields from

request – fields not specified are blanked....

Page 31: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

31

UKLUG 2012 – Cardiff, Wales

DAS: Create / delete document

● Use ”POST” to create a document with specified fields– url: …/documents?form=Person– You MUST add form to url

● Use ”DELETE” to remove the document entirely– url: …/documents/unid/33735D0BCE799....

Page 32: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

32

UKLUG 2012 – Cardiff, Wales

DAS: Data service - more...

● See the design of a view:– //.../collections/name/persons/design

● Compute values on update of document– //.../documents/unid/33735D0BC...?computewithform=true

● Use ”normal” url actions to control view collection, e.g.– //.../collections/name/persons?start=1&count=2

Page 33: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

33

UKLUG 2012 – Cardiff, Wales

Live D

EMO

Page 34: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

34

UKLUG 2012 – Cardiff, Wales

Demo...● Server:

– Local VM with Domino 9.0.1FP2 on CentOS 6.7– Extension Library (from IBM)– OpenNTF Domino API installed– OpenNTF Essentials installed

● A demo database (download from Bitbucket.org)– Showing an MVC pattern I use– Added a number of JSON demos– Is available for download

● Tool for testing: – Google Chrome Postman

Page 35: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

35

UKLUG 2012 – Cardiff, Wales

DAS: Calendar service● There is a ”catch” to enabling this service...

– In the internet site document you have to type ”Calendar” as an option.... - it is not predefined

Page 36: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

36

UKLUG 2012 – Cardiff, Wales

DAS: Calendar service● Built on the new calendar backend classes in

Domino/XWork 9.0.1● Current user's calendars, email address, and

servicesserver.dom.dk/api/calendar

● Events from specific calendarserver.dom.dk/demo/cal.nsf/api/calendar/events

● Events from specific calendar (iCal format)server.dom.dk/.../events?format=iCalendar

● Only shows events that have NOT started yet

Page 37: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

37

UKLUG 2012 – Cardiff, Wales

DAS: Calendar service

● You can also CREATE new events!!!– Using POST and specifiying all fields under an

”events” object– Handles the various types: Meeting, appointment,

etc.– Will send invites to participants of meetings– Handles notifications– Actions for complete workflow: Accept, decline,

delegate, etc.– … and more!

Page 38: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

38

UKLUG 2012 – Cardiff, Wales

DAS: Calendar service

● You can also UPDATE existing events!!!– Using PUT and specifiying ALL fields under an

”events” object – as it is returned by creating or getting the event

● Use ”.../events/<exact id as from request>– including ”....-Lotus_Auto_Generated” !!!

● You should keep all fields – including system fields– If you don't → Defaults are applied...

● E.g. specifying only start time → end time set to same..!!!

● Failure to follow these guidelines will result in an error 400 ”Bad request”

Page 39: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

39

UKLUG 2012 – Cardiff, Wales

Live D

EMO

Page 40: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

40

UKLUG 2012 – Cardiff, Wales

Extension Library● Comes with the Domino 9.0.x server (and Domino

Designer)● Just needs to be enabled in XSP properties● Does NOT require DAS to be enabled● Provides easy to use controls:

– REST Service (data)– Remote Service (JSON-RPC)

● Allow you to run serverside code as a REST service...● Also provide support for:

– NSF and OSGi servlets... - advanced stuff!!

Page 41: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

41

UKLUG 2012 – Cardiff, Wales

Extension Library

● Why would you use it...????

● → Allows further customizations– Include/exclude certain data columns– Include/exclude system columns (@....)– Calculate contents of own columns– Run code before/after CRUD operations– Sort and search– Create ”handles” (variable) to use in XPage as

datasources

Page 42: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

42

UKLUG 2012 – Cardiff, Wales

Ext. Lib. REST Service● Create a new XPage● Drag a ”REST Service” component

to it:● Fill in ”the blanks”

– pathInfo → identifiesthe service

– Select service– Fill in the info

needed for thattype of service

Page 43: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

43

UKLUG 2012 – Cardiff, Wales

Ext. Lib. REST Service

● To call your service you open the XPage with the REST Service control(s) and add the pathInfo, e.g.:

server.dom.dk/db.nsf/yourpage.xsp/persons

– ...assuming you set pathInfo to ”persons” for one of the REST Services on the ”yourpage” XPage

Page 44: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

44

UKLUG 2012 – Cardiff, Wales

Ext. Lib. NSF servlet● You can register a servlet to e.g. give you a

JSON representation of a view● Extends DefaultServletFactory

– add a factory that maps to a service (e.g. view name)

– Register in Code/Java/META-INF/services● file: com.ibm.xsp.adapter.servletFactory→ Full name of servlet class

● Refer to using url, e.g.:server.dom.dk/db.nsf/xsp/services/Persons

● Does NOT require DAS to be enabled

Page 45: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

45

UKLUG 2012 – Cardiff, Wales

Live D

EMO

Page 46: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

46

UKLUG 2012 – Cardiff, Wales

Build your own...● Why??

– … using your own MVC – Java objects– Full control– Does NOT require DAS to be enabled

● Handy ”ingredients”– Java– XPages

● Use an ”XAgent” (or an NSF/OSGi servlet)● Select a JSON ”package”

– Built-in with XPages– Or others like GSON – (wrap as plugin!!)

Page 47: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

47

UKLUG 2012 – Cardiff, Wales

Build your own...

● Use cases:– Generate JSON directly from your Java class– Consume your JSON POSTs directly by parsing

them to the corresponding Java class

→ Ready to use in your logic– Control e.g. date formating generally for all Date

fields● Eg. use ISO datetime format

Page 48: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

48

UKLUG 2012 – Cardiff, Wales

Build your own...● LotusScript....

– You didn't expect me to say this!– An option if you have existing systems with

business logic written in LotusScript– Simple:

● print – correct content-type● print …. your JSON (as text)

– … but I would not advice to build new this way → you would like to use a library/package to build your JSON for you!

● Does NOT require DAS to be enabled

Page 49: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

49

UKLUG 2012 – Cardiff, Wales

Live D

EMO

Page 50: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

50

UKLUG 2012 – Cardiff, Wales

Round Up● What are REST and JSON● GET, POST, PUT, DELETE – ~CRUD● Domino Access Services – out of the box

– Data– Calendar

● Extension Library– REST Service– (Remote Service)

● Build own solution– Java & JSON ”package” - …. & LotusScript ;-)

Page 51: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

51

UKLUG 2012 – Cardiff, Wales

Questions??

● Did you learn something?● Could you use it?

?

Page 52: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

52

UKLUG 2012 – Cardiff, Wales

Contact infoPlease feel free to contact me:

John DalsgaardDalsgaard Data A/SSolbjergvej 42SolbjergDK-4270 Høng

Phone: +45 4914-1271Email: [email protected]: www.dalsgaard-data.euTwitter: @john_dalsgaard, @DalsgaardDataASSkype: john_dalsgaard

Page 53: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

53

UKLUG 2012 – Cardiff, Wales

Sources & links● Wikipedia: Representational state transfer

● Learn REST: A Tutorial

● VIEW Tips: Brad Balassaitis on JSON-RPC

● IBM Domino Access Services 9.0.1

● Wikipedia: JSON / JavaScript Object Notation

● Introducing JSON

● JSON Tutorial

● REST services in Domino - Domino Access Services (PDF)

● Extension Library REST Services (PDF)

● Extension Library on OpenNTF (includes demo db)

● JSON test client: Chrome Postman

● Wrap an existing jar into a plugin

● Demo-DB on Bitbucket.org

Page 54: RESTful services on IBM Domino/XWork (ICON UK 21-22 Sept. 2015)

54

UKLUG 2012 – Cardiff, Wales

Sources & links● For the advanced – check these frameworks....

– Apache Wink (what DAS etc. is build on)

– … and Wink with OpenNTF Extension Library

– Jersey

– Specifikation: Java API for RESTful Services (JAX-RS)