1 / 34

Java and Web Services

Java and Web Services. CS409 Application Services. Even Semester 2007. Why Java for Web Service?. One of web service principles: “loose coupling” Minimize dependencies. But retain awareness of each other. An SOA must be platform and language independent . So, why Java then???.

maalik
Download Presentation

Java and Web Services

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java and Web Services CS409 Application Services Even Semester 2007

  2. Why Java for Web Service? • One of web service principles: “loose coupling” • Minimize dependencies. • But retain awareness of each other. • An SOA must be platform and language independent. • So, why Java then???

  3. Why Java for Web Service? (2) • Ensures portability and interoperability. • applications written in Java are well integrated with the objective of web service. • Java EE platform provides industry standard mechanisms to access legacy systems. • e.g.: JDBC, JMS, etc. • Java EE provides the APIs needed to create and deploy interoperable Web Services.

  4. WS Components: Revisited WSDL UDDI WSDL XML XML Fig 1. Web Service Components

  5. Why Java for Web Service? (3) • Lots of API implementations, for example: • Java API for XML-based RPC (JAX-RPC). • Java API for XML Processing (JAXP). • Java API for XML Registries (JAXR). • SOAP with Attachments API for Java (SAAJ). • Java Web Services Developer Pack (WSDP) provides additional web services technologies (e.g. XML Digital Signature).

  6. Java Web Services Fig 2. Java EE Web Services Tools & Technologies

  7. J2EE: An Integrated WS Platform • Started from J2EE 1.4, the main focus is on Web Services. • Existing Java-XML technologies are integrated into a consolidated platform in a standard way, to allow applications exposed as Web services. • The APIs are very well integrated into development platforms and IDEs.

  8. JAXP • Java API for XML Processing (JAXP). • Is a vendor-neutral set of lightweight APIs for parsing & processing XML documents. • An XML parser is a must to process the XML documents exchanged among Web services. • JAXP API will abstract the parser implementations from the user application.

  9. JAXP (2) Fig 3. JAXP API

  10. JAXP (3) • JAXP has its own implementation, but allows JAXP specification-conforming parsers from other vendors to be plugged in. • It processes XML documents using the SAX or DOM models. • It permits use of XSLT engines during the document processing. • The main JAXP APIs are available through the javax.xml.parsers package.

  11. JAXP (4) SAX = Simple API for XML  push parsing model. DOM = Document Object Model  one-step parsing model. Fig 4. SAX and DOM Parser in JAXP API

  12. JAXP (5) • Sample code using JAXP with SAX mode public class AnAppThatUsesSAXForXMLProcessing extends DefaultHandler { public void someMethodWhichReadsXMLDocument() { // Get a SAX PArser Factory and set validation to true SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setValidating(true); // Create a JAXP SAXParser SAXParser saxParser = spf.newSAXParser(); // Get the encapsulated SAX XMLReader xmlReader = saxParser.getXMLReader(); // Set the ContentHandler of the XMLReader xmlReader.setContentHandler(this); // Tell the XMLReader to parse the XML document xmlReader.parse(XMLDocumentName); } }

  13. JAXP (6) • Sample code using JAXP with DOM mode public class AnAppThatUsesDOMForXMLProcessing { public void someMethodWhichReadsXMLDocument() { // Step 1: create a DocumentBuilderFactory DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(true); // Step 2: create a DocumentBuilder that satisfies // the constraints specified by the DocumentBuilderFactory db = dbf.newDocumentBuilder(); // Step 3: parse the input file Document doc = db.parse(XMLDocumentFile); // Parse the tree created - node by node } }

  14. JAX-RPC • API to enable a traditional client-server remote procedure call (RPC) mechanism using an XML-based protocol. • Allow developers to build SOAP-based Web service endpoints, along with their corresponding WSDL descriptions, and clients.

  15. JAX-RPC (2) • Advantages of JAX-RPC: • Standardization: • the creation of SOAP requests and responses. • marshalling and unmarshalling of parameters and other runtime and deployment-specific details. • different mapping scenarios, including XML to Java, Java to XML, WSDL-to-Java, and Java-to-WSDL mappings. • Removing SOAP creation and marshalling/ unmarshalling tasks from a developer's responsibilities by providing these functions in the library. • Defines standard mappings between WSDL or XML & Java. • Developers need only deal with JAX-RPC APIs; all the details for handling SOAP happen under the hood.

  16. JAX-RPC (3) Fig 5. Architecture of JAX-RPC Implementation

  17. JAX-RPC (4) • How JAX-RPC works • Client side • the client application's request passes through the client-side JAX-RPC runtime. • The runtime maps the request's Java types to XML and forms a corresponding SOAP message for the request. • It then sends the SOAP message across the network to the server.

  18. JAX-RPC (5) • How JAX-RPC works • Server side • the JAX-RPC runtime receives the SOAP message for the request. • The server-side runtime applies the XML to Java mappings. • The request then mapped to the corresponding Java method call, along with its parameters.

  19. JAX-RPC (6) • Modes of operation: • Synchronous request–response modeAfter a remote method is invoked, the service client's thread blocks until a return value or exception is returned. • One-way RPC modeAfter a remote method is invoked, the client's thread is not blocked and continues processing. No return value or exception is expected on this call. • Non-blocking RPC invocation modeA client invokes a remote procedure and continues in its thread without blocking. Later, the client processes the remote method return by performing a blocked receive call or by polling for the return value.

  20. JAX-RPC (7) • Sample code for JAX-RPC //Service endpoint interface public interface WeatherService extends Remote { public String getWeather(String city) throws RemoteException; } //Service implementation public class WeatherServiceImpl implements WeatherService, ServiceLifecycle { public void init(Object context) throws JAXRPCException {} public String getWeather(String city) { return ("Early morning fog clearing midday; " + "over all great day expected in " + city); } public void destroy() {} }

  21. JAX-RPC (8) • Sample code for JAX-RPC //Client accessing the weather service ..... Context ic = new InitialContext(); Service svc = (Service) ic.lookup("java:comp/env/service/WeatherService"); WeatherSvcIntf port = (WeatherSvcIntf) svc.getPort(WeatherSvcIntf.class); String info = port.getWeather("New York"); .....

  22. JAXR • Is Java API for XML Registries for accessing business registries. • Has a flexible architecture that supports UDDI, and other registry specifications (e.g. ebXML).

  23. JAXR (2) • How JAXR works: • Registry provider provides implementation of JAXR API that consists of two parts: • a registry-specific JAXR provider, which provides a registry-specific implementation of the API. • a JAXR pluggable provider, which implements those features of the API that are independent of the type of registry (e.g. UDDI, ebXML, etc). • A JAXR client uses JAXR API implementation to access business registries.

  24. JAXR (3) • How JAXR works: • The registry-specific provider acts on requests and responses between the client and the target registry. • The registry-specific provider converts client requests into a form understood by the target registry and sends the requests to the registry provider using registry-specific protocols. • It converts responses from the registry provider from a registry-specific format to a JAXR response, then passes the response to the client.

  25. JAXR (4) Fig 5. Architecture of JAXR Implementation

  26. SAAJ • Is SOAP with Attachments API for Java. • Enables developers to produce and consume messages conforming to the SOAP 1.1 specification. • Provides an abstraction for handling SOAP messages with attachments, such as XML documents, XML fragments, or MIME-type.

  27. SAAJ (2) • Developers can use SAAJ to have their applications operate directly with SOAP messages. • SAAJ allows the following modes of message exchanges: • Synchronous request-response messaging, the client sends a message and then waits for the response. • One-way asynchronous messaging (fire and forget), the client sends a message and continues with its processing without waiting for a response.

  28. SAAJ (3) Fig 6. SAAJ Major Components

  29. SAAJ (4) • The main SAAJ APIs are available through the javax.xml.soap package. • Main abstract class: MessageFactory public abstract class MessageFactory { // To create a new instance of the MessageFactory. public static MessageFactory newInstance() throws SOAPException { // Implementation } // Used to create SOAPMessage object, usually used to create new SOAP request messages. public abstract SOAPMessage createMessage() throws SOAPException; // To create a SAAJ SOAPMessage model (DOM) from an existing SOAP message. public abstract SOAPMessage createMessage( MimeHeaders mimeheaders, InputStream inputstream) throws IOException, SOAPException; }

  30. JAXB • Is Java Architecture for XML Binding, essentially an XML data-binding facility for the J2EE platform. • It maps the various parts of the XML documents to in-memory objects that truly represent the document's intended meaning, as per the schema definition. • Simplify the process of XML documents interpretation between communicating parties.

  31. JAXB (2) • Components of JAXB: • A binding compiler that creates Java classes Developers use get and set methods to access and modify the object contents. • A binding framework that provides runtime services (e.g. marshalling, unmarshalling, validation) that can be performed on the contents classes. • A binding language that describes binding of the schema to Java classes.

  32. JAXB (3) Fig 7. JAXB Architecture

  33. Summary: WS Integration in J2EE

  34. Thank You Doddy Lukito dlukito@infinitechnology.com dlukito@alumni.carnegiemellon.edu

More Related