HATEOAS:TheConfusingPartofRESTDr.JimWebberWeb:@jimwebberRoadmap•TheRichardsonMaturityModel•HypermediaFormats•HATEOAS–HypermediaAsTheEngineOfApplicationState•RESTfulbusinessprotocolsTheRichardsonMaturityModel•Level0–SOAP,XMLRPC,POX–SingleURI•Level1–URITunnelling–ManyURIs,Singleverb•Level2–ManyURIs,manyverbs–CRUDservices(e.g.AmazonS3)•Level3–Level2+Hypermedia–RESTfulServicesHypermediaHTTPURIWhytheWeb?WhybeRESTful?•Scalable•Fault-tolerant•Recoverable•Secure•Looselycoupled•Preciselythesamecharacteristicswewantinbusinesssoftwaresystems!MediaTypesRule!•TheWeb’scontractsareexpressedintermsofmediatypesandlinkrelations–Ifyouknowthetype,youcanprocessthecontent•SometypesarespecialbecausetheyworkinharmonywiththeWeb–Wecallthese“hypermediaformats”(Hyper)mediatypesProcessingmodelHypermediacontrols(linksandforms)Supportedoperations(methods,headersandstatuscodes)StandardisedmediatypeRepresentationformats(mayincludeschemas)Composeapplication-specificbehavioursontopofthehandlingofstandardisedmediatypesGeneralSpecificPlainOldXMLisnotHypermediaFriendlyHTTP/1.1200OKContent-Length:227Content-Type:application/xmlDate:Wed,19Nov200821:48:10GMTorderxmlns=’stheprotocol?Sowhat?•Howdoyouknowthenextthingtodo?•Howdoyouknowtheresourcesyou’remeanttointeractwithnext?•Inshort,howdoyouknowtheservice’sprotocol?–TurntoWADL?Yuck!–Readthedocumentation?Comeon!–URITemplates?TightCoupling!URITemplatesareNOTaHypermediaSubstitute•OftenURItemplatesareusedtoadvertiseallresourcesaservicehosts–Dowereallyneedtoadvertisethemall?•Thisisverbose•Thisisout-of-bandcommunication•Thisencouragestight-couplingtoresourcesthroughtheirURItemplate•Thishastheopportunitytocausetrouble!–Knowledgeof“deep”URIsisbakedintoconsumingprograms–Servicesencapsulationisweakandconsumerswillprogramtoit–ServicewillchangeitsimplementationandbreakconsumersBadIdeaswithURITemplates•Imaginewe’recreatedanorder,whatnext?•WecouldsharethisURItemplate:–{order_id}•Theorder_idfieldshouldmatchtheorderIDthatcamefromtherestbucksservice–Soundsgreat!•ButwhatifRestbucksoutsourcespayment?–ChangetheURIforpayments,breakthetemplate,breakconsumers!–D’oh!•Becarefulwhatyoushare!TwoCommonHypermediaFormats:XHTMLandATOM•Botharecommonplacetoday•Botharehypermediaformats–Theycontainlinks•Bothhaveaprocessingmodelthatexplicitlysupportslinks•Whichmeansbothcandescribeprotocols…What’sthebigdealwithXHTML?•Itdoestwointerestingthings:1.Itgivesusdocumentstructure2.Itgivesuslinks•So?1.Wecanunderstandtheformatofthoseresources2.Wecandiscoverotherresources!•How?1.Followthelinks!2.Encodetheresourcerepresentationas“normal”XMLinyourXHTMLdocuments•ContrastthiswiththeAtomandAPPapproach...similar!XHTMLinActionhtmlxmlns==orderpclass=locationtakeAway/pulclass=itemsliclass=itempclass=namelatte/ppclass=quantity1/ppclass=milkwhole/ppclass=sizesmall/p/li/ulahref==paymentpayment/a/div/body/htmlBusinessdata“HypermediaControl”AtomSyndicationFormat•Theapplication/atom+xmlmediatypeishypermediaaware•Youshouldexpectlinkswhenprocessingsuchrepresentations•Andbepreparedtodothingswiththem!HTTP/1.1200OKContent-Length:342Content-Type:application/atom+xmlDate:Sun,22Mar200917:04:10GMTentryxmlns==paymenthref==special-offerhref=://restbucks.com/order/1234/idupdated2009-03-22T16:57:02Z/updatedsummary1xCafeLatte/summary/entryapplication/atom+xml•Therelcarriessemantics•Atomisahypermediaformat–Bothfeedsandentriescontainshypermediacontrolsthatcandescribeprotocols•Andithasreachapplication/vnd.restbucks+xml•Whatamouthful!•Thevndnamespaceisforproprietarymediatypes–AsopposedtotheIANA-registeredones•RestbucksownXMLisahybrid–WeuseplainoldXMLtoconveyinformation–Andlinkelementstoconveyprotocol•Thisisimportant,sinceitallowsustocreateRESTful,hypermediaawareservicesLinks•ConnectednessisgoodinWeb-basedsystems•ResourcerepresentationscancontainotherURIs•Linksactasstatetransitions•Application(conversation)stateiscapturedintermsofthesestatesdrinknamelatte/namemilkwhole/milksizesmall/sizelinkhref===application/vnd.restbucks+xml//drinkAnatomyofalinkInformationprocessingWHATformdoesthelinkedresourcetake?AddressWHEREdoesthelinkedresourcereside?SemanticcontextWHYaccessthelinkedresource?ProjectingContractswithLinks•ThevalueoftheWebisits“linked-ness”–LinksonaWebpageconstituteacontractforpagetraversals•UseLinkstodescribestatetransitionsinprogrammaticWebservices•Hypermediasupportlinks–Allowustodescribehigher-orderprotocolswhichsitcomfortab