Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Understanding the Role of Media Types in RESTful applications

1,848 views

Published on

A Presentation explaining which role media types play in achieving evolvability in networked, decentralized systems.

Published in: Technology
  • Be the first to comment

Understanding the Role of Media Types in RESTful applications

  1. 1. Understanding the Role of Media Types in RESTful Applications Jan Algermissen / @algermissen GOTO Copenhagen 2011
  2. 2. Application
  3. 3. ApplicationUser
  4. 4. Application Use Case
  5. 5. Application Application
  6. 6. Monolithic
  7. 7. User Monolithic
  8. 8. Monolithic Use Case
  9. 9. MonolithicApplication
  10. 10. MonolithicComponents
  11. 11. Monolithic Connectors
  12. 12. Unix Command Line
  13. 13. User Unix Command Line
  14. 14. Unix Command Line Use Case
  15. 15. Unix Command Line Application
  16. 16. Unix Command Line Components
  17. 17. Unix Command Line Connectors
  18. 18. Network-Based (ODBC)
  19. 19. User Network-Based (ODBC)
  20. 20. Network-Based (ODBC)Use Case
  21. 21. Network-Based (ODBC) Application
  22. 22. Network-Based (ODBC) Components
  23. 23. Network-Based (ODBC) Connector
  24. 24. Network-Based (ODBC)
  25. 25. Network-Based (ODBC) User
  26. 26. Network-Based (ODBC) Use Case
  27. 27. Network-Based (ODBC) Application
  28. 28. Network-Based (ODBC) Components
  29. 29. Network-Based (ODBC) Connector
  30. 30. Network-Based (HTTP)
  31. 31. User Network-Based (HTTP)
  32. 32. Network-Based (HTTP) Use Case
  33. 33. Network-Based (HTTP) Application
  34. 34. Network-Based (HTTP)User Agent Component Intermediary Component Server Component
  35. 35. Network-Based (HTTP) Connectors
  36. 36. Network-Based (HTTP)
  37. 37. User Network-Based (HTTP)
  38. 38. Network-Based (HTTP) Use Case
  39. 39. Network-Based (HTTP) Application
  40. 40. Network-Based (HTTP)User Agent Component Intermediary Component Server Component
  41. 41. Network-Based (HTTP) Connectors
  42. 42. Network-Based (HTTP) Administrative Domain
  43. 43. Issues ofnetwork-based, decentralized systems
  44. 44. Network Issues ofnetwork-based, decentralized systems
  45. 45. Network Unpredictable Use Issues ofnetwork-based, decentralized systems
  46. 46. Network Unpredictable Use Issues ofnetwork-based, decentralized systems Complexity
  47. 47. Network Unpredictable Use Issues of network-based, decentralized systemsTrust Complexity
  48. 48. NetworkFederated Unpredictable Change Use Issues of network-based, decentralized systems Trust Complexity
  49. 49. Performance NetworkFederated Unpredictable Change Use Issues of network-based, decentralized systems Trust Complexity
  50. 50. Performance Network ScalabilityFederated Unpredictable Change Use Issues of network-based, decentralized systems Trust Complexity
  51. 51. Performance Network ScalabilityFederated Unpredictable Change Use Issues of network-based, decentralized systems Trust Complexity Simplicity
  52. 52. Performance Network ScalabilityFederated Unpredictable Change Use Issues of network-based, decentralized systems Trust Complexity Visibility Simplicity
  53. 53. Performance NetworkEvolvability Scalability Federated Unpredictable Change Use Issues of network-based, decentralized systems Trust Complexity Visibility Simplicity
  54. 54. Performance Scalability Simplicity VisibilityEvolvability
  55. 55. Properties of Key Interest Performance Scalability Simplicity Visibility Evolvability
  56. 56. Evolvability
  57. 57. GOALServer owner can implement any change without asking existing clients.
  58. 58. Client Contract Server
  59. 59. ContractClient Server
  60. 60. Contract Media TypeClient Server
  61. 61. Media Type
  62. 62. Media Type1.What is a media type?
  63. 63. Media Type1.What is a media type?2.How do I design one?
  64. 64. Media Type1.What is a media type?2.How do I design one?3.How do media types enable evolvability?
  65. 65. 1. What is a media type?
  66. 66. 1. What is a media type?•A name (identifier)
  67. 67. 1. What is a media type?•A name (identifier)•A family of compatible schemas
  68. 68. 1. What is a media type?•A name (identifier)•A family of compatible schemas•Hypermedia semantics
  69. 69. 1. What is a media type?•A name (identifier)•A family of compatible schemas•Hypermedia semantics•Processing rules
  70. 70. 1. What is a media type?•A name (identifier)•A family of compatible schemas•Hypermedia semantics•Processing rules•Extension rules
  71. 71. 2. How do I design a media type?
  72. 72. Automatically check prices and place order if price is below threshold User Shop AUser Agent(Crawler) Shop B
  73. 73. UC 01: Automatically check prices and...Primary Actor: User (Buyer)Precondition: Crawler knows catalog entry URIsMain Success Scenario:1. User starts crawler2. Crawler requests catalogs of known shops3. Crawler compares prices of items to threshold4. Crawler selects item if price below threshold5. Crawler creates order for item6. Crawler submits order to order-processor7. Crawler archives order response8. User reviews archived order responses
  74. 74. UC 01: Automatically check prices and...Primary Actor: User (Buyer)Precondition: Crawler knows catalog entry URIsMain Success Scenario:1. User starts crawler2. Crawler requests catalogs of known shops3. Crawler compares prices of items to threshold4. Crawler selects item if price below threshold5. Crawler creates order for item6. Crawler submits order to order-processor7. Crawler archives order response8. User reviews archived order responses
  75. 75. Elements of Example Media Type Data: Catalog (with items) Order (with order-items) Order Response Hypermedia controls: Order-Processor (Catalog)
  76. 76. Catalog Example<catalog>  <title>Shoe Catalog</title>  <order-processor href="/orders"/>  <item id=”123” href=”/items/23”>    <name>Shiny Boots</name>    <price currency="EUR" amount="27.99">  </item>  <item id=”456” href=”/items/388”>    <name>Fancy Sneakers</name>    <price currency="EUR" amount="12.95">  </item></catalog>
  77. 77. Order Example<order> <buyer>...</buyer> <line-items> <line-item id=”123”> <price currency=”EUR”>27.99</price> <quantity>4</quantity> </line-item> <line-item id=”456”> <price currency=”EUR”>12.95</price> <quantity>4</quantity> </line-item> </line-items></order>
  78. 78. application/procurement•Three schemas <catalog>, <order> and<order-response>•No mandatory elements•Extensible•Specified as version 1•Processing Rules If an <order-processor> element is present an order can be POSTed to that resource. Such an oder accepting resource SHOULD accept <order>-documents. ...
  79. 79. 3.How does a media type enable evolvability?
  80. 80. 3.How does a media type enable evolvability? Contract Media Type Client Server
  81. 81. Shop Service@GET@Produces("application/procurement")public String makeProcurementCatalog() { return "<catalog/>";}
  82. 82. Catalog Example<catalog>  <title>Shoe Catalog</title>  <order-processor href="/orders"/>  <item id=”123” href=”/items/23”>    <name>Shiny Boots</name>    <price currency="EUR" amount="27.99">  </item>  <item id=”456” href=”/items/388”>    <name>Fancy Sneakers</name>    <price currency="EUR" amount="12.95">  </item></catalog>
  83. 83. Shop Service (improved)@GET@Produces("application/procurement")public Catalog makeProcurementCatalog() { Catalog catalog = new Catalog(“Shoe Catalog”); catalog.setOrderProcessor("/orders"); catalog.addItem("/items/23","Shiny Boots","27.99"); catalog.addItem("/items/388","Fancy Sneakers","12.95"); return catalog;}
  84. 84. Server Development Principle Design by purpose
  85. 85. Crawler Implementationpublic void handleCatalog(catalogUri) { WebResource resource = getClient().resource(catalogUri); ClientResponse response = resource.get(ClientResponse.class); Catalog catalog = response.getEntity(Catalog.class); Item item = catalog.getItemById(“123”); if(item != null && item.getPrice() != null && item.getPrice().getAmount(“EUR”) < THRESHOLD) { placeOrder(catalog,item); } }
  86. 86. Crawler Impl. (cont.)public void placeOrder(Catalog catalog, Item item) { String orderProcessorUri = catalog.getOrderProcessorUri(); Order order = new Order(item); WebResource resource = getClient().resource(orderProcessorUri); ClientResponse response = resource.post(ClientResponse.class,order); // handle order placement response }
  87. 87. Client Development Principle Design by intent
  88. 88. ObservationREST limits the possible contractchanges to just two kinds: Addingdata or controls and removing data orcontrols from a message.
  89. 89. New RequirementAdd in-stock quantity
  90. 90. Extended Catalog ExampleGET /catalog200 OkContent-Type: application/procurement<catalog>  <title>Shoe Catalog</title>  <order-processor href="/orders"/>  <item id=”123” href=”/items/23”>    <name>Shiny Boots</name>    <price currency="EUR" amount="27.99"> <in-stock>5</in-stock>  </item>  <item id=”456” href=”/items/388”>    <name>Fancy Sneakers</name>    <price currency="EUR" amount="12.95"> <in-stock>2</in-stock>  </item></catalog>
  91. 91. Media Type Principle Extensible schemas
  92. 92. Client Development Principle “Must Ignore”
  93. 93. Share Useful ExtensionsFeed extensions back intomedia type, increase schemaversion.
  94. 94. Shop Server ChangeNo order processor available
  95. 95. Catalog ExampleGET /catalog200 OkContent-Type: application/procurement<catalog>  <title>Shoe Catalog</title>  <item id=”123” href=”/items/23”>    <name>Shiny Boots</name>    <price currency="EUR" amount="27.99">  </item>  <item id=”456” href=”/items/388”>    <name>Fancy Sneakers</name>    <price currency="EUR" amount="12.95">  </item></catalog>
  96. 96. Crawler Impl. (fails)public void placeOrder(Catalog catalog, Item item) { String orderProcessorUri = catalog.getOrderProcessorUri(); Order order = new Order(item); WebResource resource = getClient().resource(orderProcessorUri); ClientResponse response = resource.post(ClientResponse.class,order); // handle order placement response }
  97. 97. Crawler Impl. (improved)public void placeOrder(Client Response response, Catalog catalog, Item item) { String orderProcessorUri = catalog.getOrderProcessorUri(); if(orderProcessorUri == null) { throw new FailedExpectationException( “Expected order processor not found”,response); } Order order = new Order(item); WebResource resource = getClient().resource(orderProcessorUri); ClientResponse response = resource.post(ClientResponse.class,order); // handle order placement response }
  98. 98. Crawler Impl. (improved)try { handleCatalog(catalogUri);} catch(FailedExpectationException e) { String message = e.getMessage(); ClientResponse response = e.getClientResponse(); LOG.error( ... ); reportIncident( “Failed expectation when processing catalog ” + catalogUri, message, response, PRIORITY_CRITICAL); // ...}
  99. 99. Client Development Principle “What if” programming style
  100. 100. Media Type PrincipleAvoid mandatory elements
  101. 101. Server Development Principle Be responsible
  102. 102. If you are a media type designer...
  103. 103. If you are a media type designer... •Define extensible schemas
  104. 104. If you are a media type designer... •Define extensible schemas •Avoid mandatory elements
  105. 105. If you are a server developer...
  106. 106. If you are a server developer...•Use from the media type what suits yourpossibilities
  107. 107. If you are a server developer...•Use from the media type what suits yourpossibilities•Be responsible and maintain things
  108. 108. If you are a client developer...
  109. 109. If you are a client developer...•Use from the media type what suits yourintended application
  110. 110. If you are a client developer...•Use from the media type what suits yourintended application•Ignore unknown data and controls
  111. 111. If you are a client developer...•Use from the media type what suits yourintended application•Ignore unknown data and controls•“What if” programming style
  112. 112. Server does not own the contract
  113. 113. Flexible Contract
  114. 114. Source of information
  115. 115. Q&A

×