You are on page 1of 22

Lesson 3

Web Services
Lesson 3: Web Services
_____________________________________________________________________________

3.1 Lesson Objectives


The term Web Services is used to describe a standardized way of integrating web applications
using the XML, SOAP, WSDL and UDDI standard over an internet protocol. This lesson provides
an explanation of web services, the web services stack and the concept of Service oriented
architecture. Important terms such as SOAP, WSDL, UDDI are also discussed in this lesson.
Finally, the lesson and provides an example that describes the steps involved in writing,
deploying and running web service.

Building Rich Internet Applications with Adobe Flex 3-2


_______________________________________________________________________
Lesson 3: Web Services
_____________________________________________________________________________

3.2 Web Services


Web Services is a technology that can change the future of computing and e-commerce. Web
Services is a distributed computing technology that offers interaction and collaboration among
vendors and customers, with the vision of providing ubiquitous computing.

One of key definitions of Web Services is: "Web Services are loosely coupled software
components delivered over Internet-standard technologies." Some of the early products in Web
Services started appearing in 1997 when Sun announced its Jini platform and Hewlett-Packard
its e-speak. After that, many big players such as IBM and Microsoft joined this race. The Web
Services arena picked up steam after the big players roped in and several small players also
joined hands for what was perceived as the next Internet wave.

Two of the key problems solved by Web Services over earlier distributed systems such as
CORBA, DCOM, RPC, and so forth were:

Interoperability: Earlier distributed systems suffered from interoperability issues because


each vendor implemented its own on-wire format for distributed object messaging. By using
XML as an on-wire standard, the two camps of Java/J2EE and .NET/C# now could speak to
each other.

Firewall traversal: Collaboration across corporations was an issue because distributed


systems such as CORBA and DCOM used non-standard ports. Web Services use HTTP as a
transport protocol and most of the firewalls allow access though port 80 (for HTTP), leading to
easier and dynamic collaboration. The dynamic nature of Web Services interaction offers several
exciting services for the users

3.2.1 The Web Services Stack


The Web Services model follows the publish, find, and bind paradigm. In the first step, a service
provider publishes a Web Service in a Web Service registry. Secondly, a client who is looking for
a service to meet their requirement searches in a registry. The client then chooses a service
based on its preferences. The client then downloads the service description and binds with that
to invoke and use the service.

Building Rich Internet Applications with Adobe Flex 3-3


_______________________________________________________________________
Lesson 3: Web Services
_____________________________________________________________________________

One of the primary concerns of Web-based programmers was how to transmit data in an
interoperable manner. At the bottom-most layer is the XML standard that addresses this. SOAP
(Simple Object Access Protocol) is an XML-based mechanism for messaging and RPC (Remote
Procedure Calls). It addresses the fundamental problem of firewall traversal in RPC systems by
using HTTP as the transport. SOAP is the protocol used for invoking the service.

3.2.2 What is a Web Service?


Web services constitute a distributed computer architecture made up of many different
computers trying to communicate over the network to form one system. They consist of a set of
standards that allow developers to implement distributed applications - using radically different

Building Rich Internet Applications with Adobe Flex 3-4


_______________________________________________________________________
Lesson 3: Web Services
_____________________________________________________________________________

tools provided by many different vendors - to create applications that use a combination of
software modules called from systems in disparate departments or from other companies.
A Web service contains some number of classes, interfaces, enumerations and structures that
provide black box functionality to remote clients. Web services typically define business objects
that execute a unit of work (e.g., perform a calculation, read a data source, etc.) for the
consumer and wait for the next request. Web service consumer does not necessarily need to be
a browser-based client. Console-based and Windows Forms-based clients can consume a Web
service. In each case, the client indirectly interacts with the Web service through an intervening
proxy. The proxy looks and feels like the real remote type and exposes the same set of
methods. Under the hood, the the proxy code really forwards the request to the Web service
using standard HTTP or optionally SOAP messages.

3.2.3 Why Web Services?


Here are the benefits of using Web Services:

Exposing the function on to network: A Web service is a unit of managed code that can be
remotely invoked using HTTP, that is, it can be activated using HTTP requests. So, Web
Services allows you to expose the functionality of your existing code over the network. Once it
is exposed on the network, other application can use the functionality of your program.

Connecting Different Applications: Web Services allows different applications to talk to each
other and share data and services among themselves. Other applications can also use the
services of the web services. For example VB or .NET application can talk to java web services
and vice versa. So, Web services is used to make the application platform and technology
independent.

Standardized Protocol: Web Services uses standardized industry standard protocol for the
communication. All the four layers (Service Transport, XML Messaging, Service Description and
Service Discovery layers) uses the well defined protocol in the Web Services protocol stack. This
standardization of protocol stack gives the business many advantages like wide range of
choices, reduction in the cost due to competition and increase in the quality.

Low Cost of communication: Web Services uses SOAP over HTTP protocol for the
communication, so you can use your existing low cost internet for implementing Web Services.
This solution is much less costly compared to proprietary solutions like EDI/B2B.

Support for Other communication means: Beside SOAP over HTTP, Web Services can also be
implemented on other reliable transport mechanisms. So, it gives flexibility use the
communication means of your requirement and choice. For example Web Services can also be
implemented using ftp protocol (Web services over FTP).

Loosely Coupled Applications: Web Services are self-describing software modules which
encapsulates discrete functionality. Web Services are accessible via standard Internet
communication protocols like XML and SOAP. These Web Services can be developed in any
technologies (like c++, Java, .NET, PHP, Perl etc.) and any application or Web Services can

Building Rich Internet Applications with Adobe Flex 3-5


_______________________________________________________________________
Lesson 3: Web Services
_____________________________________________________________________________

access these services. So, the Web Services are loosely coupled application and can be used by
applications developed in any technologies. For example, I have heard of people developing
Web Services using Java technologies and using the Web Services in VB or .NET applications.

Web Services Sharing: These days due to complexness of the business, organizations are using
different technologies like EAI, EDI, B2B, Portals etc. for distributing computing. Web Services
supports all these technologies, thus helping the business to use existing investments in other
technologies.

Web Services are Self Describing: Web Services are self describing applications, which reduces
the software development time. This helps the other business partners to quickly develop
application and start doing business. This helps business to save time and money by cutting
development time.

Automatic Discovery: Web Services automatic discovery mechanism helps the business to easy
find the Service Providers. This also helps your customer to find your services easily. With the
help of Web Services your business can also increase revenue by exposing their own Web
Services available to others.

Business Opportunity: Web Services has opened the door to new business opportunities by
making it easy to connect with partners.

3.2.4 What is Service-Oriented Architecture?


Service Oriented Architecture or SOA for short is a new architecture for the development of
loosely coupled distributed applications. In fact service-oriented architecture is collection of
many services in the network. These services communicate with each other and the
communications involves data exchange & even service coordination. Earlier SOA was based on
the DCOM or Object Request Brokers (ORBs). Nowadays SOA is based on the Web Services.
Broadly SOA can be classified into two terms: Services and Connections.

Services:
A service is a function or some processing logic or business processing that is well-defined, self-
contained, and does not depend on the context or state of other services. Example of Services
is Loan Processing Services, which can be self-contained unit for process the
Loan Applications. Other example may be Weather Services, which can be used to get the
weather information. Any application on the network can use the service of the Weather Service
to get the weather information.

Connections:
Connections means the link connecting these self-contained distributed services with each
other, it enable client to Services communications. In case of Web services SOAP over HTTP is
used to communicate the between services.
The following figure is a typical example of the service-oriented architecture. It shows how a
service consumer sends a service request to a service provider. After accepting the request,

Building Rich Internet Applications with Adobe Flex 3-6


_______________________________________________________________________
Lesson 3: Web Services
_____________________________________________________________________________

service provider sends a message to the service consumer. In this case a service provider can
also be a service consumer.

KEY POINTS:
Web Services can convert your applications into Web-applications.
Web Services can be used by other applications.
The basic Web Services platform is XML + HTTP.
Before you continue you should have a basic understanding of the following:
HTML
XML
The HTTP protocol is the most used Internet protocol.
XML provides a language which can be used between different platforms and programming
languages and still express complex messages and functions.

3.2.5 An Overview of Web Services


• Web services are application components
• Web services communicate using open protocols
• Web services are self-contained and self-describing
• Web services can be discovered using UDDI
• Web services can be used by other applications
• XML is the basis for Web services

Web services platform elements –

SOAP (Simple Object Access Protocol)


UDDI (Universal Description, Discovery and Integration)
WSDL (Web Services Description Language)

3.3 What Is an XML Web Service?


XML Web services are the fundamental building blocks in the move to distributed computing on
the Internet. Open standards and the focus on communication and collaboration among people
and applications have created an environment where XML Web services are becoming the
platform for application integration. Applications are constructed using multiple XML Web

Building Rich Internet Applications with Adobe Flex 3-7


_______________________________________________________________________
Lesson 3: Web Services
_____________________________________________________________________________

services from various sources that work together regardless of where they reside or how they
were implemented.

XML makes it possible for developers to expose valuable resources in a highly interoperable
fashion, where a resource is any type of application or data store used within an organization.
The XML Web services architecture defines a standard mechanism for making resources
available via XML messaging. Being able to access a resource by simply transmitting XML
messages over standard protocols like TCP, HTTP, or SMTP greatly lowers the bar for potential
consumers. The term "Web service" typically refers to the piece of code implementing the XML
interface to a resource, which may otherwise be difficult to access

This architecture makes it possible for any consumer with XML support to integrate with Web
service applications.
There are probably as many definitions of XML Web Service as there are companies building
them, but almost all definitions have these things in common:

XML Web Services expose useful functionality to Web users through a standard Web protocol.
In most cases, the protocol used is SOAP.
XML Web services provide a way to describe their interfaces in enough detail to allow a user to
build a client application to talk to them. This description is usually provided in an XML
document called a Web Services Description Language (WSDL) document.
XML Web services are registered so that potential users can find them easily. This is done with
Universal Discovery Description and Integration (UDDI).

3.3.1 SOAP
Soap is the communications protocol for XML Web services. When SOAP is described as a
communications protocol, most people think of DCOM or CORBA and start asking things like,
"How does SOAP do object activation?" or "What naming service does SOAP use?" While a

Building Rich Internet Applications with Adobe Flex 3-8


_______________________________________________________________________
Lesson 3: Web Services
_____________________________________________________________________________

SOAP implementation will probably include these things, the SOAP standard doesn't specify
them. SOAP is a specification that defines the XML format for messages.

There are other parts of the SOAP specification that describe how to represent program data as
XML and how to use SOAP to do Remote Procedure Calls.

Most current implementations of SOAP support RPC applications because programmers who are
used to doing COM or CORBA applications understand the RPC style. SOAP also supports
document style applications where the SOAP message is just a wrapper around an XML
document. Document-style SOAP applications are very flexible and many new XML Web services
take advantage of this flexibility to build services that would be difficult to implement using
RPC.

A major source of confusion when getting started with SOAP is the difference between the
SOAP specification and the many implementations of the SOAP specification. Most people who
use SOAP don't write SOAP messages directly but use a SOAP toolkit to create and parse the
SOAP messages. These toolkits generally translate function calls from some kind of language to
a SOAP message. For example, the Microsoft SOAP Toolkit 2.0 translates COM function calls to
SOAP and the Apache Toolkit translates JAVA function calls to SOAP.

One of the first questions newcomers to SOAP ask is how does SOAP deal with security. Early in
its development, SOAP was seen as an HTTP-based protocol so the assumption was made that
HTTP security would be adequate for SOAP. When SOAP expanded to become a more general-
purpose protocol running on top of a number of transports, security became a bigger issue. For
example, HTTP provides several ways to authenticate which user is making a SOAP call, but
how does that identity get propagated when the message is routed from HTTP to an SMTP
transport? SOAP was designed as a building-block protocol, so fortunately, there are already
specifications in the works to build on SOAP to provide additional security features for Web
services. The WS-Security specification defines a complete encryption system.

AN OVERVIEW OF SOAP

The basic Web services platform is XML plus HTTP.


SOAP stands for Simple Object Access Protocol
SOAP is a communication protocol
SOAP is for communication between applications
SOAP is a format for sending messages
SOAP is designed to communicate via Internet
SOAP is platform independent
SOAP is language independent
SOAP is based on XML
SOAP is simple and extensible
SOAP allows you to get around firewalls
SOAP will be developed as a W3C standard

Building Rich Internet Applications with Adobe Flex 3-9


_______________________________________________________________________
Lesson 3: Web Services
_____________________________________________________________________________

3.3.2 UDDI
Universal Discovery Description and Integration is the yellow pages of Web services.A UDDI
directory entry is an XML file that describes a business and the services it offers. There are
three parts to an entry in the UDDI directory. The "white pages" describe the company offering
the service: name, address, contacts, etc. The "yellow pages" include industrial categories
based on standard taxonomies.

The "green pages" describe the interface to the service in enough detail for someone to write
an application to use the Web service. The way services are defined is through a UDDI
document called a Type Model or tModel. In many cases, the tModel contains a WSDL file that
describes a SOAP interface to an XML Web service,.The UDDI directory also includes several
ways to search for the services you need to build your applications. For example, you can
search for providers of a service in a specified geographic location or for business of a specified
type. The UDDI directory will then supply information, contacts, links, and technical data to
allow you to evaluate which services meet your requirements.

OVERVIEW OF UDDI

• UDDI is a directory service where businesses can register and search for Web services.
• UDDI is a directory for storing information about web services
• UDDI is a directory of web service interfaces described by WSDL
• UDDI communicates via SOAP
• UDDI is built into the Microsoft .NET platform

3.3.3 WSDL
WSDL (often pronounced whiz-dull) stands for Web Services Description Language. For our
purposes, we can say that a WSDL file is an XML document that describes a set of SOAP
messages and how the messages are exchanged. In other words, WSDL is to SOAP what IDL is
to CORBA or COM. Since WSDL is XML, it is readable and editable but in most cases, it is
generated and consumed by software. Since WSDL is a machine-readable language (e.g., it's
just an XML file), tools and infrastructure can be easily built around it. Today developers can
use WSDL definitions to generate code that knows precisely how to interact with the Web
service it describes. The notation that a WSDL file uses to describe message formats is based
on the XML Schema standard which means it is both programming-language neutral and
standards-based which makes it suitable for describing XML Web services interfaces that are
accessible from a wide variety of platforms and programming languages. In addition to
describing message contents, WSDL defines where the service is available and what
communications protocol is used to talk to the service. This means that the WSDL file defines
everything required to write a program to work with an XML Web service
WSDL plays an important role in the overall Web services architecture since it describes the
complete contract for application communication (similar to the role of IDL in the DCOM
architecture). Although other techniques exist for describing Web services, the WS-I Basic

Building Rich Internet Applications with Adobe Flex 3-10


_______________________________________________________________________
Lesson 3: Web Services
_____________________________________________________________________________

Profile Version 1.0 mandates the use of WSDL and XML Schema (see Figure 4) for describing
Web services. This helps ensure interoperability at the service description layer.

OVERVIEW OF WSDL

WSDL is an XML-based language for describing Web services and how to access them.
WSDL stands for Web Services Description Language
WSDL is written in XML
WSDL is an XML document
WSDL is used to describe Web services
WSDL is also used to locate Web services

3.3.4 A Sample Application


<!-- WSDL definition structure -->
<definitions
name="MathService"
targetNamespace="http://example.org/math/"
xmlns="http://schemas.xmlsoap.org/wsdl/"
>
<!-- abstract definitions -->
<types> ...
<message> ...
<portType> ...

<!-- concrete definitions -->


<binding> ...
<service> ...
</definition>

A WSDL definition is an XML document with a root definition element from the
http://schemas.xmlsoap.org/wsdl/ namespace.. The definitions element may contain several
other elements including types, message, portType, binding, and service, all of which come
from the http://schemas.xmlsoap.org/wsdl/ namespace.
The first three elements (types, message, and portType) are all abstract definitions of the
Web service interface. These elements constitute the programmatic interface that you typically

Building Rich Internet Applications with Adobe Flex 3-11


_______________________________________________________________________
Lesson 3: Web Services
_____________________________________________________________________________

interface with in your code. The last two elements (binding and service) describe the
concrete details of how the abstract interface maps to messages on the wire.

WSDL Elements

Element Name Description

types A container for abstract type definitions defined using XML Schema

message A definition of an abstract message that may consist of multiple parts,


each part may be of a different type

portType An abstract set of operations supported by one or more endpoints


(commonly known as an interface); operations are defined by an
exchange of messages

binding A concrete protocol and data format specification for a particular portType

service A collection of related endpoints, where an endpoint is defined as a


combination of a binding and an address (URI)

3.4 REST Services


The acronym REST stands for Representational State Transfer; this means that each unique
URL is a representation of some object. You can get the contents of that object using an HTTP
GET, to delete it, you then might use a POST, PUT, or DELETE to modify the object.

3.4.1 Who is using REST?


All of Yahoo's web services use REST, including Flickr, del.icio.us API uses it, pubsub, bloglines,
technorati, and both eBay, and Amazon have web services for both REST and SOAP.

3.4.2 Motivation for REST


The motivation for REST was to capture the characteristics of the Web which made the Web
successful. Subsequently these characteristics are being used to guide the evolution of the
Web.

Building Rich Internet Applications with Adobe Flex 3-12


_______________________________________________________________________
Lesson 3: Web Services
_____________________________________________________________________________

3.4.3 REST - An Architectural Style, Not a


Standard
REST is not a standard. You will not see the W3C putting out a REST specification. You will not
see IBM or Microsoft or Sun selling a REST developer's toolkit. Why? Because REST is just an
architectural style. You can't bottle up that style. You can only understand it, and design your
Web services in that style. REST is not a standard, but it uses standards:
• HTTP
• URL
• XML/HTML/GIF/JPEG/etc (Resource Representations)
• text/xml, text/html, image/gif, image/jpeg, etc (MIME Types)

3.4.4 REST vs SOAP


The aforementioned companies that are using REST APIs haven't been around for very long,
and their APIs came out this year mostly. So REST is definitely the new trend in creating a web
service. The main advantages of REST web services are:
• Lightweight - not a lot of extra xml markup
• Human Readable Results
• Easy to build - no toolkits required
• SOAP also has some advantages:
• Easy to consume - sometimes
• Rigid - type checking, adheres to a contract
• Development tools

3.5 Annotation Structures


An annotation structure is an XML document (a structure document) that defines one particular
type of annotation. A Uniform Resource Identifier (URI) names every annotation structure. The
URI identifies the annotation type defined by the structure. The annotation structure is an
ordered collection of named fields, where each field defines one piece of data to be collected in
an annotation conforming to the structure. Fields are typed using standard XML Schema data
types; a field might be optional and can allow multiple values. Moreover, related fields might be
grouped together.

3.5.1 Annotation
An annotation is an XML document (an instance document) that contains the actual data of an
annotation. Every annotation has an associated annotation type, and the annotation is an
instantiation of the corresponding annotation structure. In other words, an annotation specifies
values for all of the fields defined by the annotation's structure. Within this API, annotations are
identified by globally unique identifiers (GUIDs). Many annotations can share the same
annotation type and structure.

Building Rich Internet Applications with Adobe Flex 3-13


_______________________________________________________________________
Lesson 3: Web Services
_____________________________________________________________________________

3.6 Application on WebServices

The following example will describe how to write, deploy and run a webservice using the
Editplus /netbeans IDE, JBOSS application server and the ANT tool.
There are three steps involved in writing a webservice.

STEP 1: Writing an Interface


package webservice1;
import javax.jws.WebService;
import javax.jws.WebMethod;
import java.rmi.Remote;
import java.rmi.RemoteException;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService(name="Calculator1")
@SOAPBinding(style=Style.RPC)
public interface Calculator extends Remote
{
@WebMethod int add(int x, int y);

@WebMethod int subtract(int x, int y);


}

STEP 2: Writing a class that implements the interface


package webservice1;
import javax.ejb.Stateless;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
@Stateless
@WebService(name="Calculator1",
endpointInterface="webservice1.Calculator")
public class CalculatorBean
{
public int add(int x, int y)
{
return x + y;
}

public int subtract(int x, int y)


{
return x - y;
}
}

STEP 3: Writing a client program


package webservice1;
import javax.xml.namespace.QName;

Building Rich Internet Applications with Adobe Flex 3-14


_______________________________________________________________________
Lesson 3: Web Services
_____________________________________________________________________________

import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;
import java.net.URL;
import java.io.File;
public class Client
{
public static void main(String[] args) throws Exception
{
URL url = new
URL("http://localhost:8080/tutorial/CalculatorBean?wsdl");
QName qname = new QName("http://webservice1/",
"CalculatorBeanService");
ServiceFactory factory = ServiceFactory.newInstance();
Service service = factory.createService(url, qname);
Calculator calculator = (Calculator)
service.getPort(Calculator.class);
System.out.println("1 + 1 = " + calculator.add(1, 1));
System.out.println("1 - 1 = " + calculator.subtract(1, 1));
}
}

The Webservice Folder should appear as under.

Building Rich Internet Applications with Adobe Flex 3-15


_______________________________________________________________________
Lesson 3: Web Services
_____________________________________________________________________________

Building Rich Internet Applications with Adobe Flex 3-16


_______________________________________________________________________
Lesson 3: Web Services
_____________________________________________________________________________

Building Rich Internet Applications with Adobe Flex 3-17


_______________________________________________________________________
Lesson 3: Web Services
_____________________________________________________________________________

Steps to compile a web service:

Building Rich Internet Applications with Adobe Flex 3-18


_______________________________________________________________________
Lesson 3: Web Services
_____________________________________________________________________________

Set the path of your webservice in the command prompt.

Cleaning the resourses by typing the command ant clean.

Run ant and start the Jboss application server.

Building Rich Internet Applications with Adobe Flex 3-19


_______________________________________________________________________
Lesson 3: Web Services
_____________________________________________________________________________

Building Rich Internet Applications with Adobe Flex 3-20


_______________________________________________________________________
Lesson 3: Web Services
_____________________________________________________________________________

3.7 Design Goals for WSE 3.0


WSE 3.0 is fundamentally a security product for Web services. When the WSE (1.0 and 2.0)
project was first conceived, its primary purpose was to show a practical and usable
implementation of the emerging WS-* security specifications, such as WS-Security, WS-Trust,
and WS-SecureConversation and as a result provide feedback into the standardization process.

Build Secure Web Services Easily: As well as an easy and intuitive API design, the objective
here was to abstract common best practices when securing end-to-end messages. From having
spoken to hundreds of existing WSE customers, there emerged five common scenarios for
message-level security

Simplified Development of Service-Oriented Systems Using the Web Service


Protocols and .NET Framework v2.0: Continue to provide an easy-to-use programming API
abstraction on targeted Web service specifications, introduce recent essential specifications
such as Message Transmission Optimization Mechanism (MTOM), take advantage of
improvements to the .NET Framework 2.0, and provide an integrated set of tools with Visual
Studio 2005.

Building Rich Internet Applications with Adobe Flex 3-21


_______________________________________________________________________
Lesson 3: Web Services
_____________________________________________________________________________

3.8 Summary

• Web Services is a set of standards that allow developers to implement distributed


applications - using radically different tools provided by many different vendors. The
web services model follows the publish, find and bind paradigm.

• SOA (Service oriented Architecture) is a new architecture for the development of loosely
coupled distributed applications. A service-oriented architecture is a collection of many
services in the network.

• XML Web services are becoming the platform for application integration. Applications are
constructed using multiple XML Web services from various sources that work together
regardless of where they reside or how they were implemented. Soap is the
communications protocol for XML Web services.

• Universal Discovery Description and Integration is the yellow pages of Web services.A
UDDI directory entry is an XML file that describes a business and the services it offers.

• A WSDL (Web Services Description Language) file is an XML document that describes a
set of SOAP messages and how the messages are exchanged. WSDL plays an important
role in the overall Web services architecture since it describes the complete contract for
application communication.

• REST (Representational State Transfer) is an architectural style that makes use of


standards such as HTTP, URL, XML/HTML/GIF/JPEG/etc (Resource Representations) and
text/xml, text/html, image/gif, image/jpeg, etc (MIME Types).

Building Rich Internet Applications with Adobe Flex 3-22


_______________________________________________________________________

You might also like