Transcript Class 3.3

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???
2
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.
3
WS Components: Revisited
UDDI
WSDL
WSDL
XML
XML
Fig 1. Web Service Components
4
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).
5
Java Web Services
6
Fig 2. Java EE Web Services Tools & Technologies
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.
7
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.
8
JAXP (2)
Fig 3. JAXP API
9
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.
10
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
11
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);
}
}
12
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
}
}
13
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.
14
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.
15
JAX-RPC (3)
Fig 5. Architecture of JAX-RPC Implementation
16
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.
17
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.
18
JAX-RPC (6)
• Modes of operation:
– Synchronous request–response mode
After a remote method is invoked, the service client's
thread blocks until a return value or exception is
returned.
– One-way RPC mode
After 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 mode
A 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.
19
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() {}
}
20
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");
.....
21
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).
22
JAXR (2)
•
How JAXR works:
1. 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).
2. A JAXR client uses JAXR API
implementation to access business registries.
23
JAXR (3)
• How JAXR works:
3. The registry-specific provider acts on requests and
responses between the client and the target registry.
4. 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.
5. It converts responses from the registry provider from
a registry-specific format to a JAXR response, then
passes the response to the client.
24
JAXR (4)
Fig 5. Architecture of JAXR Implementation
25
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.
26
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.
27
SAAJ (3)
Fig 6. SAAJ Major Components
28
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;
}
29
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.
30
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.
31
JAXB (3)
Fig 7. JAXB Architecture
32
Summary: WS Integration in J2EE
33
Technology
Name
Supporting
Standard
Purpose
JAXP
XML schema
JAX-RPC
SOAP
Enables exchange of SOAP requests and responses through an API
that hides the complex SOAP details from the developers.
JAXR
UDDI,
ebXML
Enables accessing business registries with an API that supports any
type of registry specification.
SAAJ
SOAP with
Attachments
Enables exchange of document-oriented XML messages using Java
APIs.
JAXB
XML
document
Enables processing of XML documents in a vendor neutral way;
supports SAX and DOM models.
Standard in-memory representation of an XML document.
Provides an XML data-binding facility for the J2EE platform.
Thank You
Doddy Lukito
[email protected]
[email protected]