Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

What Is Webservice

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Mark-up is information added to a document that enhances its meaning in certain ways, in that it

identifies the parts and how they relate to each other. More specifically, a markup language is a set
of symbols that can be placed in the text of a document to demarcate and label the parts of that
document.

What is Webservice:

Webservices are client-server or consumer-provider applications that communicate (exchange data


or messages) in the network through HTTP protocol, using XML & other formats like JSON, Text, etc.

Webservice is for applications/systems running on different platforms written in different languages


 like English is for people on the planet.

Advantages:

Interoperability:

Webservice provides interoperability. Applications developed in different languages (e.g. A in Java, B


in .net) running on different platforms (A on Linux, B on Windows) can communicate with each
other.

Loose coupling. Webservices enable loose coupling. (A using webservice from B, can switch to C –
with minimal changes to code--)

This helps to integrate different products using web-services.

Extensibility: To integrate with other applications.

Mashup applications: An application can use more than one web-service (from different
applications) to create one piece of functionality.

e.g. Project report application can provide a web-service with Project status details. This can be
combined with webservice from Google Maps that shows actual status on ground. These two details
from the two webservices can be used to generate “actual status report and validity of project
report”

SOAP:

These use XML based SOAP protocol for messaging. (that uses HTTP POST)

SOAP can also use other protocols like SMTP, or directly with TCP.

RESTFul:

This is an architecture style (not protocol)

Supports multiple data formats: This can use not only XML, but any other format. (mostly used
formats are JSON, XML, Text)

This uses all http commands.

Webservice Standards

Each language & platform has standards for implementing the 2 types of web-services (SOAP &
Restful). (Also, these languages provide tools to implement the webservices)

Java uses below standards:


o JAX-WS (Java API for XML based web services) is the standard for SOAP based web service.
o JAX-RS (Java API for XML based Restful Services) is the standard for Restful web services.

SOA

 What is SOA:
o SOA is collection of architectural principles, Design  in such a way that an application is
composed of several S/W services with well defined interfaces and can communicate with
each in loosely coupled manner.
 Who:
o W3C & OASIS are standards bodies, who comes up with architectural principles for SOA.
o (W3C & OASIS are formed by orgs like Microsoft, Oracle, IBM etc)
o SOA also has two roles: Provider & Consumer.

What is Service:

o This is an implementation of business logic that operates independent of state of other


services.
o Services communicate with each other in a loosely coupled way, using a standard interface.
o --Hence, as long as interface between two services remains the same, a service can be
replaced by another implementation.
o WSDL file will have the interface contract for SOAP service.
o WADL file will have the contract for REST pattern.
o SOA uses XML as data format for exchanging messages between services. (XML has both
data & metadata)

 Advantages:
o Platform Independency: (as in web-service above)
o Focussed Developer roles: (focussed on specific Domain)
o Loosely Coupled: (i.e. Replacing a service with a different implementation, without changing
the code)
o Reusability of a service:
o Cost Reduction: (Due to reusability & reduction of complexity)
o Scalability:
o Availability: As we deploy the service on multiple instances, it is highly available (even if a
few instances go down)

Other technologies before web-services: RMI(Messaging), CORBA, DCOM.


CORBA – works only with Object Oriented Technologies.
JAVA-RMI  works only on Java Platform.
 SOA vs Web-Services
o SOA is architecture pattern that can make effective use of web-services.
o SOA can be implemented with technologies like CORBA, JAVA-RMI (but difficult to scale &
reuse  with same ease, platform independence & less complexity as with web-services)

XML & XML Schema Definition

XML

 What is XML:
o Extensible Mark-up Language: We can create our own mark-up for a specific
context/domain/purpose.
o HTML has predefined set of elements <b> & </b> (e.g. <b> …. </b>  These elements
indicates that text between these elements is to be in bold)
o In XML, we can define our own mark-up elements.
e.g. For e-commerce application, we can create mark-up element “Order-ML” that can
have nested elements “Order-ID, Line-Items, Shipping-Address” etc.
Applications can use this XML to  show the data to users, exchange this info with
others, manipulate the data.
o XML has both data & metadata, custom elements,

XML- Tree Structure:

<RootElement attributeKey1=”attribute value” attributeKeyN=”attribute value”

..body or content of the element

<childElement attributekey1=”attrib value” attribA=”some value”>

Body of child element.

</childElement>  Closing of child element

..

..
</RootElement>  closing of Root element.

(OR) element can be defined without child elements using  <element-name attribute=val … />

<  is the opening tag. /> is the closing tag.

 Why XML:
o This can be specific to the domain/business purpose.
o We have both Data & Metadata.
 XML Syntax:
o XML has well-formedness rules (so that XML can be understood & used across applications,
users).  This well-formedness is verified by XML parser.
XML is said to be well-formed, if it follows the below XML syntax rules:
(Refer to the XML tree structure above)
1. XML document is formed as “Tree of elements”  with only 1 root element per
document.
2. Start of an element is indicated by < elementName
3. Closing of an element is indicated by </ elementName>
 XML is case sensitive. Element names starts with letter or _, can’t start with letters
xml, ?, and barred symbols( in rule 9)
 XML elements can be extended to have more info. (This will not impact existing
applications using the XML)
But removing elements can break an application.
4. Content for the element is given between start and closing tag.
5. Elements in the content part of an element are its child elements.
6. Branches to the root are child elements.
7. Any element can have sub-elements (child elements).
8. Any element can have 1 or more attributes with respective values (as key value pairs).
Values should be enclosed in single/double quotes.
 Attributes can’t have  multiple values, tree structures, be expanded.
9. Body of element can’t have <, > ,“ , ‘, &
Hence, use below left-column to represent the barred symbols in the body of the
element.

&lt; < less than


&gt; > greater than
&amp; & ampersand
&apos; ' Apostrophe
&quot; " quotation mark
Note: Double quotes “ can be placed inside single quotes. But not viceversa.

10. <!-- This is a comment --> (Comment text should not have --)
11. White spaces are preserved in XML  i.e. Multiple white spaces are not reduced to 1
white-space. (unlike in HTML)
12. XML Prologue: <?xml version="1.0" encoding="UTF-8"?>
Prologue is optional. If given, it should be the 1st line of XML document.
It will not have closing tag.
o XML also has validation rules, which can be defined and verified using pre-defined XML
Schema (XSD)
 Purpose of XML: This is used mainly for three purposes in software applications 
1. As a Configuration File
This has configuration data (with elements defining the configuration elements)

e.g. Apache CXF is a web-service engine uses Spring configuration (XML file).

2. For Data Exchange.

e.g. SOAP & REST use XML file data exchange. (RPC can use XML data)

3. To save & manipulate data.

o Portal can use XML to save data and retrieve data.


o Portal can use XML-Style sheet to translate the XML to HTML and send the html to client
browser.

 XSD elements are used for contract definition.


 XML Namespaces: This is to uniquely qualify an XML element by allowing multiple elements to
be used with same name in an XML document.

XSD - XML Schema Definition:

o This is also an XML with namespace & elements defined by W3C as


http://www.w3.org/2001/XMLSchema.
o This is used for defining contract between XML provider and XML
consumer.
o This specifies  elements, attributes, Namespaces, order, Number of occurrences of
elements, Restrictions on data  for XML.
o XML is called valid XML document, if it follows XSD.
o XSD is also an XML file with .XSD extension.
e.g. Order.xsd specifies which elements can be there in Order.xml
o Elements that can be used in XSD are provided by W3C – World Wide Web Consortium.

 When to use XSD:


XSD is used to define the contract between two applications/systems that wanted to
communicate (i.e. exchange data/message) using XML.
o This allows each application to validate the XML data it sends and/or gets against XSD.
o There can be multiple XSDs for one XML file.

XML Name-Spaces:
 This is to uniquely qualify an XML element by allowing multiple elements to be used with same
name in an XML document.
 This allows usage of elements from different XSDs in a single XML document.
 Element names in a namespace should be unique. (so that element names can repeat across
namespaces)
o This is similar to Package (A package will have only 1 class or Interface with that name)
 Default Namespace is given by XMLNS=”URI”
 In XSD  we define the namespaces using a global unique identifier – URI- for that namespace.
(as it needs to be global unique id, we generally use URL)  else there can be conflict later.
o XML Parser will not look up at the contents of URI to get namespace details, but only use
it as unique identifier for the namespace.
 URL is the most common URI.
 URN-Unique Resource Name as another URI.

e.g. http://www.xyz.com/order
Instead of using the long URL for namespace, we use a prefix. (e.g. below)
XMLNS:amz = “http://www.xyz.com/order“
XMLNS is element name by W3C for XML-namespace in XSD. “amz“ is the prefix for
namespace.

(Namespace is defined in XML or XSD?)

Namespaces & prefix can be defined as attribute of root element or any nested element.

<?xml version="1.0" encoding="UTF-8"?>


<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.ws.com/Patients"
xmlns:tns="http://www.ws.com/Patients"
elementFormDefault="qualified">

<element name="patient" type="tns:patient_type"/>


<complexType name="patient_type"> !—This is defining the type patient_type
<sequence>
<element name ="id" type="int"/>
<element name ="name" type="string"/>
<element name ="age" type="int"/>
<element name ="dob" type="date"/>
<element name ="email" type="string"/>
<element name ="id" type="int"/>
</sequence>
</complexType>
</schema>

Elements can be defined in 2 ways 

1. <Element-nameA attribute1=”val1” ..> ….Child-elements(including text


body)…… </Element-nameA>
2. <Element-nameA attribute1=”val1” ../>  This is without child-elements
and text body.

 Explanation of above elements.


i. “TargetNamespace” indicates that any XML (with data) that complies/uses this XSD
should have the namespace (e.g. given in TargetNamespace as
"http://www.example.org/Patient”).
ii. xmlns:tns=http://www.example.org/Patient  This is for giving prefix
to TargetNamespace.
 Without this prefix, it is not possible to use conflicting names
from different namespaces. (e.g. from XMl-Schema Namespace &
targetNamespace)
iii. "http://www.w3.org/2001/XMLSchema" has all the elements (including
attributes, sub-elements) that can be used in schema definition file
(i.e. XSD)
iv. elementFormDefault: Qualified (indicates that all the elements of
XML complying with the XSD should be qualified with namespace
prefix). Unqualified  should not have prefix.

XML can be defined from template, or XSD file, or from DTD file.
<?xml version="1.0" encoding="UTF-8"?>
<tns:patient xmlns:tns="http://www.ws.com/Patients"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.ws.com/Patients XSDs/Patients.xsd ">
<tns:id>0</tns:id>
<tns:name>tns:name</tns:name>
<tns:age>0</tns:age>
<tns:dob>2001-01-01</tns:dob>
<tns:email>tns:email</tns:email>
<tns:id>0</tns:id>
</tns:patient>

 Explanation of above elements.


i. http://www.w3.org/2001/XMLSchema-instance uses (“a few attributes”) to
associate XML schemas with XML documents. (e.g. “SchemaLocation” is an attribute
from the namespace with prefix xsi i.e. namespace XMLSchema-instance)
ii. SchemaLocation(attribute) is the location of XSI (in the above e.g. it is
XSDs/Patients.xsd)
iii. XSDs/Patients.xsd  This is path of XSD file (relative to XML file)
(without this, XML will be validated with warnings)

Example of Restrictions:
<simpleType name="ID">
<restriction base="int">
<pattern value="[0-9]*"></pattern>
</restriction>
</simpleType>

 Sequence: used to list order of elements in order in which the elements are
defined between “sequence” tags.
 Choice: only 1 of the elements can be selected.
 All: all elements between “all” tags are to be used.

Controlling the # of elements


Below are the attributes of “element” to control the # of times that element can
occur.
o minOccurs  indicates minimum # of times an element can occur. (not less
than 0. 0 means optional)
o maxOccurs  indicates maximum # of times an element can occur. (can be
“unbounded”)
SOAP
 Advantages & Disadvantages of SOAP Webservices:

 Advantages:
o Platform Independence: This comes from http(which is transport independent) and
XML(data/structure independent)
o For providing & consuming service to & from legacy applications.
e.g. Airline reservation system(where user interface is tightly coupled
with business logic).  Now to allow online check-in, we can expose
business logic in legacy system as a webservice and make our application
talk to this webservice. (If we want to enable mobile application, we
can consume the same webservice)
o Webservices opens New Revenue & Profit Channels:
Instead of selling complete CRM application, Oracle exposed required
webservices and charged its customers only for the webservices consumed.
o Webservices can be exposed through firewalls.

 Disadvantages:
o Ambiguous standards due to different bodies like W3C, OASIS, etc.
o Performance impact to serialization & deserialization of SOAP messages.
As, every time XML has to be converted to & from language specific
objects.

 When to use SOAP Webservices:


 When Formal contract is required to describe the interface that the
webservice offer.
o WSDL is used for this purpose. (This describes the details of what the
webservice provides and how it can provide it).
Thus, it gives message format that is exchanged between webservice consumer &
provider.
 When there are many non-functional requirements.(like Security, Transaction
management), SOAP based standards are available out of the box & proven for
the purpose.
o SOAP Webservice engines like Apache-CXF, windows communication
foundation provide inbuild implementation for standards  that can be
used with minimum configuration and code.
 When requirements mandate “reliable asynchronous processing”, we can use
reliable messaging standards (that are available out of the box for SOAP
web-services)
 SOAP-Simple Object Access Protocol:
SOAP is a specification from W3C that gives the standard SOAP XML format.
Service 1st. Contract 1st.

You might also like