SCDJWS 6. REST JAX-P

1,084 views

Published on

Refers

Published in: Technology, Education
1 Comment
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total views
1,084
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
67
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

SCDJWS 6. REST JAX-P

  1. 1. REST, JSON, SOAP and XML Processing <ul>Francesco Ierna </ul>
  2. 2. <ul>Rest </ul><ul><li>Rest : REST defines a set of architectural principles by which you can design Web services that focus on a system's resources , including how resource states are addressed and transferred over HTTP by a wide range of clients written in different languages . A REST web service provides a way to retrieve a representation of such a resource and to manipulate resources using a limited set of verbs . M ain interest are the URI and the HTTP method </li><ul><li>The information that selects which resource to operate on is kept in an URI.
  3. 3. Every request to a REST web service happens in complete isolation
  4. 4. Resources are manipulated by exchanging representations of the resources
  5. 5. REST web services typically allow for the resource representation to be in a format selected by the client at the time of the request
  6. 6. Using : </li></ul></ul><ul><ul><ul><li>explicits use of HTTP methods in a way that follows the protocol as defined by RFC 2616. with HTTP, these operations are the HTTP operations GET, POST, PUT, DELETE </li></ul></ul></ul><ul><ul><li>Problems  : </li><ul><li>Using GET is not very sure. </li><ul><li>to move the parameter names and values on the request URI into XML tags and to use POST in which we put the xml as body </li></ul></ul></ul></ul>
  7. 7. <ul>Rest </ul><ul><li>Rest : REST defines a set of architectural principles by which you can design Web services that focus on a system's resources, including how resource states are addressed and transferred over HTTP by a wide range of clients written in different languages </li><ul><li>mapping between create, read, update, and delete (CRUD) operations and HTTP methods. </li><ul><li>To create a resource on the server, use POST .
  8. 8. To retrieve a resource, use GET .
  9. 9. To change the state of a resource or to update it, use PUT .
  10. 10. To remove or delete a resource, use DELETE . </li></ul></ul></ul><ul><ul><li>Stateless : are less complicated to design, write, and distribute across load-balanced servers </li><ul><li>Server : Generates responses that include links to other resources to allow applications to navigate between related ressources
  11. 11. Client application : Uses the Cache-Control response header to determine whether to cache the resource (make a local copy of it) or not. A 304 HTTP response code means the client can safely use a cached </li></ul><li>Expose directory structure-like URIs : should be intuitive to the point where they are easy to guess </li><ul><li>This type of URI is hierarchical, rooted at a single path, and branching from it are subpaths that expose the service's main areas : http://www.myservice.org/discussion/2008/12/10/ {topic}
  12. 12. Hide the server-side scripting technology file extensions, Substitute spaces with hyphens or underscores, Avoid query strings as much as you can, Instead of using the 404 code always provide a default page or resource as a response </li></ul></ul></ul>
  13. 13. <ul>Rest Overview </ul>
  14. 14. <ul>JSON </ul><ul><li>JSON support is implemented as a custom binding. So just like there are SOAP/HTTP binding or Plain Old XML binding, you can specify JSON binding to expose a
  15. 15. Such web services can be JAX-WS web services or REST web services
  16. 16. JSON text can be dynamically executed as JavaScript
  17. 17. JSON is not as verbose as XML while retaining some of the flexibility and extensibility </li></ul>
  18. 18. <ul>JSON </ul>
  19. 19. <ul>JSON Types </ul>
  20. 20. <ul>Pro REST/SOAP </ul><ul><li>REST OK when: </li></ul><ul><ul><li>Are completely stateless
  21. 21. A caching infrastructure can be leveraged for performance
  22. 22. The service producer and service consumer have a mutual understanding of the context and content being passed along
  23. 23. Bandwidth is particularly important and needs to be limited. PDAs and mobile phones
  24. 24. Web service delivery or aggregation into existing web sites can be enabled easily with a RESTful style. Asynchronous JavaScript with XML (Ajax) and toolkits such as Direct Web Remoting (DWR) to consume the services in their web applications </li></ul></ul><ul><li>SOAP OK when : </li></ul><ul><ul><li>A formal contract must be established to describe the interface that the web service offers
  25. 25. The architecture must address complex nonfunctional requirements
  26. 26. The architecture needs to handle asynchronous processing and invocation </li></ul></ul>
  27. 27. <ul>Pro REST/SOAP </ul>
  28. 28. <ul>Why REST/SOAP </ul><ul><li>REST OK when: </li></ul><ul><ul><li>Serializing simpler data structures, as opposed to documents like web pages
  29. 29. Clients are AJAX clients.
  30. 30. Bandwidth usage needs to be minimized
  31. 31. Validation of the format and contents of the data is not required. </li></ul></ul><ul><li>SOAP OK when : </li><ul><li>You want to ensure that the data returned by the web service can not be evaluated </li></ul></ul><ul><ul><li>Format, and to some extent the contents, of the data sent to and received from the web service needs to be validated.
  32. 32. Better tooling is required
  33. 33. Reuse of existing data structure definitions is desired </li></ul></ul>
  34. 34. JAXP <ul><li>JAXP : The Java API for XML Processing (JAXP) is for processing XML data using applications written in the Java programming language </li><ul><ul><li>SAX, DOM, XML, StAX, XSLT, Xpath, XInclude
  35. 35. supports the Extensible Stylesheet Language Transformations (XSLT) standard
  36. 36. JAXP also provides namespace support, allowing you to work with DTDs that might otherwise have naming conflicts </li></ul></ul></ul>
  37. 37. SAX <ul><li>SAX: provides an event-driven, serial access mechanism that processes XML data one element at a time. SAX is a push API, which means that the client implements callbacks that are invoked as certain data, such as elements, are encountered in the document being parsed </li><ul><li>Objects </li></ul></ul><ul><ul><li>SAXParserFactory  : creates an instance of the parser determined by the system property
  38. 38. SAXParser  : The SAXParser interface defines several kinds of parse() methods
  39. 39. SAXReaderIt is the SAXReader that carries on the conversation with the SAX event handlers you define
  40. 40. Events : </li><ul><li>DefaultHandler  : you can override only the ones you're interested in
  41. 41. ContentHandler  : are invoked when an XML tag is recognized
  42. 42. ErrorHandler  : are invoked in response to various parsing errors
  43. 43. DTDHandler  : Defines methods you will generally never be called upon to use
  44. 44. EntityResolver  : The resolveEntity method is invoked when the parser must identify data identified by a URI. In most cases </li></ul></ul></ul><ul><ul><li>When we should use SAX : </li><ul><li>fast and efficient, but its event model makes it most useful for such state-independent filtering
  45. 45. With a pull parser, you get the next node, whatever it happens to be, at any point in the code that you ask for it
  46. 46. SAX requires much less memory than DOM, because SAX does not construct an internal representation </li></ul></ul></ul>
  47. 47. SAX Overview
  48. 48. SAX Example public static void main(String argv[]) { // Use an instance of ourselves as the SAX event handler DefaultHandler handler = new Echo(); // Use the default (non-validating) parser SAXParserFactory factory = SAXParserFactory.newInstance(); try { // Set up output stream out = new OutputStreamWriter(System.out, &quot;UTF8&quot;); // Parse the input SAXParser saxParser = factory.newSAXParser(); saxParser.parse(new File(argv[0]), handler); } catch (Throwable t) { t.printStackTrace(); } }
  49. 49. DOM <ul><li>SAX: </li><ul><li>Objects </li></ul></ul><ul><ul><li>javax.xml.parsers.DocumentBuilderFactory: selects the factory implementation that is used to produce the builder
  50. 50. DocumentBuilder.newDocument() : method to create an empty Document that implements the org.w3c.dom.Document interface
  51. 51. Parse : to create a Document from existing XML data </li></ul></ul><ul><ul><li>When we should use DOM: </li><ul><li>is platform-neutral and language-neutral API that enables programs to dynamically update the contents of XML documents
  52. 52. allows extreme flexibility in parsing, navigating, and updating the contents of a document
  53. 53. high memory requirements and the need for more powerful processing capabilities </li></ul></ul></ul>
  54. 54. DOM Overview
  55. 55. DOM Example public static void buildDom() { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.newDocument(); Element root = (Element) document.createElement(&quot;rootElement&quot;); document.appendChild(root); root.appendChild(document.createTextNode(&quot;Some&quot;)); root.appendChild(document.createTextNode(&quot; &quot;)); root.appendChild( document.createTextNode(&quot;text&quot;) ); } catch (ParserConfigurationException pce) { // Parser with specified options can't be built pce.printStackTrace();}}
  56. 56. XSLT <ul><li>SAX: Objects : </li></ul><ul><ul><li>TransformerFactory : used to create a Transformer
  57. 57. javax.xml.transform  : This package defines the factory class you use to get a Transformer object </li></ul></ul><ul><ul><ul><li>Input/ouput
  58. 58. transform() method to make the transformation happen </li></ul></ul></ul><ul><ul><li>javax.xml.transform.dom  : DOMSource and DOMResult classes, which let you use a DOM as an input to or output from a transformation
  59. 59. javax.xml.transform.sax  : Defines the SAXSource and SAXResult classes, which let you use a SAX event generator as input to a transformation, or deliver SAX events as output to a SAX event processor
  60. 60. javax.xml.transform.stream  : Defines the StreamSource and StreamResult classes, which let you use an I/O stream as an input to or output from a transformation </li></ul></ul><ul><ul><li>CURSOR API  : cursor API represents a cursor with which you can walk an XML document from beginning to end </li><ul><li>XMLStreamReader ; XML Information model, including document encoding, element names, attributes, namespaces, text nodes, start tags, comments, processing instructions, document boundaries </li></ul><li>ITERATOR API  : iterator API represents an XML document stream as a set of discrete event objects. These events are pulled by the application and provided by the parser in the order in which they are read in the source XML document </li></ul></ul>
  61. 61. XSLT Overview
  62. 62. XSLT Example Example : j avax.xml.transform.dom Document document; try { File f = new File(argv[0]); DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.parse(f); // Use a Transformer for output TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(System.out); transformer.transform(source, result); }
  63. 63. StAX API <ul><li>SAX: exposes methods for iterative, event-based processing of XML documents. XML documents are treated as a filtered series of events, and infoset states can be stored in a procedural fashion. API is bidirectional , meaning that it can both read and write XML documents </li></ul><ul><ul><li>CURSOR API  : cursor API represents a cursor with which you can walk an XML document from beginning to end </li><ul><li>XMLStreamReader ; XML Information model, including document encoding, element names, attributes, namespaces, text nodes, start tags, comments, processing instructions, document boundaries </li></ul><li>ITERATOR API  : iterator API represents an XML document stream as a set of discrete event objects. These events are pulled by the application and provided by the parser in the order in which they are read in the source XML document
  64. 64. CURSOR vs ITERATOR : </li><ul><li>you can make smaller, more efficient code with the cursor API
  65. 65. the cursor API is more efficient.
  66. 66. XML processing pipelines, use the iterator API
  67. 67. modify the event stream, use the iterator API
  68. 68. to be able to handle pluggable processing of the event stream, use the iterator API </li></ul><li>When we should use StAX : </li><ul><li>bidirectional features, small memory footprint, and low processor requirements
  69. 69. is that StAX is difficult to use if you return XMLdocuments that follow complex schema </li></ul></ul></ul>

×